2 * sdbm - ndbm work-alike hashed database library
3 * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
4 * author: oz@nexus.yorku.ca
5 * status: public domain.
20 # include <sys/file.h>
37 extern Malloc_t malloc proto((MEM_SIZE));
38 extern Free_t free proto((Malloc_t));
39 extern Off_t lseek(int, Off_t, int);
45 static int getdbit proto((DBM *, long));
46 static int setdbit proto((DBM *, long));
47 static int getpage proto((DBM *, long));
48 static datum getnext proto((DBM *));
49 static int makroom proto((DBM *, long, int));
54 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
55 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
56 #define ioerr(db) ((db)->flags |= DBM_IOERR)
58 #define OFF_PAG(off) (long) (off) * PBLKSIZ
59 #define OFF_DIR(off) (long) (off) * DBLKSIZ
61 static long masks[] = {
62 000000000000, 000000000001, 000000000003, 000000000007,
63 000000000017, 000000000037, 000000000077, 000000000177,
64 000000000377, 000000000777, 000000001777, 000000003777,
65 000000007777, 000000017777, 000000037777, 000000077777,
66 000000177777, 000000377777, 000000777777, 000001777777,
67 000003777777, 000007777777, 000017777777, 000037777777,
68 000077777777, 000177777777, 000377777777, 000777777777,
69 001777777777, 003777777777, 007777777777, 017777777777
73 sdbm_open(register char *file, register int flags, register int mode)
76 register char *dirname;
77 register char *pagname;
80 if (file == NULL || !*file)
81 return errno = EINVAL, (DBM *) NULL;
83 * need space for two seperate filenames
85 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
87 if ((dirname = (char *) malloc((unsigned) n)) == NULL)
88 return errno = ENOMEM, (DBM *) NULL;
90 * build the file names
92 dirname = strcat(strcpy(dirname, file), DIRFEXT);
93 pagname = strcpy(dirname + strlen(dirname) + 1, file);
94 pagname = strcat(pagname, PAGFEXT);
96 db = sdbm_prep(dirname, pagname, flags, mode);
97 free((char *) dirname);
102 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
107 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
108 return errno = ENOMEM, (DBM *) NULL;
115 * adjust user flags so that WRONLY becomes RDWR,
116 * as required by this package. Also set our internal
117 * flag for RDONLY if needed.
119 if (flags & O_WRONLY)
120 flags = (flags & ~O_WRONLY) | O_RDWR;
122 else if ((flags & 03) == O_RDONLY)
123 db->flags = DBM_RDONLY;
125 * open the files in sequence, and stat the dirfile.
126 * If we fail anywhere, undo everything, return NULL.
128 #if defined(OS2) || defined(MSDOS) || defined(WIN32)
131 if ((db->pagf = open(pagname, flags, mode)) > -1) {
132 if ((db->dirf = open(dirname, flags, mode)) > -1) {
134 * need the dirfile size to establish max bit number.
136 if (fstat(db->dirf, &dstat) == 0) {
138 * zero size: either a fresh database, or one with a single,
139 * unsplit data page: dirpage is all zeros.
141 db->dirbno = (!dstat.st_size) ? 0 : -1;
143 db->maxbno = dstat.st_size * BYTESIZ;
145 (void) memset(db->pagbuf, 0, PBLKSIZ);
146 (void) memset(db->dirbuf, 0, DBLKSIZ);
152 (void) close(db->dirf);
154 (void) close(db->pagf);
161 sdbm_close(register DBM *db)
166 (void) close(db->dirf);
167 (void) close(db->pagf);
173 sdbm_fetch(register DBM *db, datum key)
175 if (db == NULL || bad(key))
176 return errno = EINVAL, nullitem;
178 if (getpage(db, exhash(key)))
179 return getpair(db->pagbuf, key);
181 return ioerr(db), nullitem;
185 sdbm_exists(register DBM *db, datum key)
187 if (db == NULL || bad(key))
188 return errno = EINVAL, -1;
190 if (getpage(db, exhash(key)))
191 return exipair(db->pagbuf, key);
193 return ioerr(db), -1;
197 sdbm_delete(register DBM *db, datum key)
199 if (db == NULL || bad(key))
200 return errno = EINVAL, -1;
202 return errno = EPERM, -1;
204 if (getpage(db, exhash(key))) {
205 if (!delpair(db->pagbuf, key))
208 * update the page file
210 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
211 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
212 return ioerr(db), -1;
217 return ioerr(db), -1;
221 sdbm_store(register DBM *db, datum key, datum val, int flags)
226 if (db == NULL || bad(key))
227 return errno = EINVAL, -1;
229 return errno = EPERM, -1;
231 need = key.dsize + val.dsize;
233 * is the pair too big (or too small) for this database ??
235 if (need < 0 || need > PAIRMAX)
236 return errno = EINVAL, -1;
238 if (getpage(db, (hash = exhash(key)))) {
240 * if we need to replace, delete the key/data pair
241 * first. If it is not there, ignore.
243 if (flags == DBM_REPLACE)
244 (void) delpair(db->pagbuf, key);
246 else if (duppair(db->pagbuf, key))
250 * if we do not have enough room, we have to split.
252 if (!fitpair(db->pagbuf, need))
253 if (!makroom(db, hash, need))
254 return ioerr(db), -1;
256 * we have enough room or split is successful. insert the key,
257 * and update the page file.
259 (void) putpair(db->pagbuf, key, val);
261 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
262 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
263 return ioerr(db), -1;
270 return ioerr(db), -1;
274 * makroom - make room by splitting the overfull page
275 * this routine will attempt to make room for SPLTMAX times before
279 makroom(register DBM *db, long int hash, int need)
283 char *pag = db->pagbuf;
285 register int smax = SPLTMAX;
289 * split the current page
291 (void) splpage(pag, New, db->hmask + 1);
293 * address of the new page
295 newp = (hash & db->hmask) | (db->hmask + 1);
298 * write delay, read avoidence/cache shuffle:
299 * select the page for incoming pair: if key is to go to the new page,
300 * write out the previous one, and copy the new one over, thus making
301 * it the current page. If not, simply write the new page, and we are
302 * still looking at the page of interest. current page is not updated
303 * here, as sdbm_store will do so, after it inserts the incoming pair.
305 if (hash & (db->hmask + 1)) {
306 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
307 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
310 (void) memcpy(pag, New, PBLKSIZ);
312 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
313 || write(db->pagf, New, PBLKSIZ) < 0)
316 if (!setdbit(db, db->curbit))
319 * see if we have enough room now
321 if (fitpair(pag, need))
324 * try again... update curbit and hmask as getpage would have
325 * done. because of our update of the current page, we do not
326 * need to read in anything. BUT we have to write the current
327 * [deferred] page out, as the window of failure is too great.
329 db->curbit = 2 * db->curbit +
330 ((hash & (db->hmask + 1)) ? 2 : 1);
331 db->hmask |= db->hmask + 1;
333 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
334 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
339 * if we are here, this is real bad news. After SPLTMAX splits,
340 * we still cannot fit the key. say goodnight.
343 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
350 * the following two routines will break if
351 * deletions aren't taken into account. (ndbm bug)
354 sdbm_firstkey(register DBM *db)
357 return errno = EINVAL, nullitem;
361 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
362 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
363 return ioerr(db), nullitem;
372 sdbm_nextkey(register DBM *db)
375 return errno = EINVAL, nullitem;
380 * all important binary trie traversal
383 getpage(register DBM *db, register long int hash)
391 while (dbit < db->maxbno && getdbit(db, dbit))
392 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
394 debug(("dbit: %d...", dbit));
397 db->hmask = masks[hbit];
399 pagb = hash & db->hmask;
401 * see if the block we need is already in memory.
402 * note: this lookaside cache has about 10% hit rate.
404 if (pagb != db->pagbno) {
406 * note: here, we assume a "hole" is read as 0s.
407 * if not, must zero pagbuf first.
409 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
410 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
412 if (!chkpage(db->pagbuf))
416 debug(("pag read: %d\n", pagb));
422 getdbit(register DBM *db, register long int dbit)
430 if (dirb != db->dirbno) {
431 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
432 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
436 debug(("dir read: %d\n", dirb));
439 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
443 setdbit(register DBM *db, register long int dbit)
451 if (dirb != db->dirbno) {
452 (void) memset(db->dirbuf, 0, DBLKSIZ);
453 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
454 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
458 debug(("dir read: %d\n", dirb));
461 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
463 if (dbit >= db->maxbno)
464 db->maxbno += DBLKSIZ * BYTESIZ;
466 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
467 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
474 * getnext - get the next key in the page, and if done with
475 * the page, try the next page in sequence
478 getnext(register DBM *db)
484 key = getnkey(db->pagbuf, db->keyptr);
485 if (key.dptr != NULL)
488 * we either run out, or there is nothing on this page..
489 * try the next one... If we lost our position on the
490 * file, we will have to seek.
493 if (db->pagbno != db->blkptr++)
494 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
496 db->pagbno = db->blkptr;
497 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
499 if (!chkpage(db->pagbuf))
503 return ioerr(db), nullitem;