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.
23 # include <sys/file.h>
38 #include <errno.h> /* See notes in perl.h about avoiding
44 extern Malloc_t malloc proto((MEM_SIZE));
45 extern Free_t free proto((Malloc_t));
54 static int getdbit proto((DBM *, long));
55 static int setdbit proto((DBM *, long));
56 static int getpage proto((DBM *, long));
57 static datum getnext proto((DBM *));
58 static int makroom proto((DBM *, long, int));
63 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
64 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
65 #define ioerr(db) ((db)->flags |= DBM_IOERR)
67 #define OFF_PAG(off) (long) (off) * PBLKSIZ
68 #define OFF_DIR(off) (long) (off) * DBLKSIZ
70 static const long masks[] = {
71 000000000000, 000000000001, 000000000003, 000000000007,
72 000000000017, 000000000037, 000000000077, 000000000177,
73 000000000377, 000000000777, 000000001777, 000000003777,
74 000000007777, 000000017777, 000000037777, 000000077777,
75 000000177777, 000000377777, 000000777777, 000001777777,
76 000003777777, 000007777777, 000017777777, 000037777777,
77 000077777777, 000177777777, 000377777777, 000777777777,
78 001777777777, 003777777777, 007777777777, 017777777777
82 sdbm_open(char *file, int flags, int mode)
88 const size_t dirfext_size = sizeof(DIRFEXT "");
89 const size_t pagfext_size = sizeof(PAGFEXT "");
91 if (file == NULL || !*file)
92 return errno = EINVAL, (DBM *) NULL;
94 * need space for two separate filenames
96 filelen = strlen(file);
98 if ((dirname = (char *) malloc(filelen + dirfext_size
99 + filelen + pagfext_size)) == NULL)
100 return errno = ENOMEM, (DBM *) NULL;
102 * build the file names
104 memcpy(dirname, file, filelen);
105 memcpy(dirname + filelen, DIRFEXT, dirfext_size);
106 pagname = dirname + filelen + dirfext_size;
107 memcpy(pagname, file, filelen);
108 memcpy(pagname + filelen, PAGFEXT, pagfext_size);
110 db = sdbm_prep(dirname, pagname, flags, mode);
111 free((char *) dirname);
116 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
121 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
122 return errno = ENOMEM, (DBM *) NULL;
129 * adjust user flags so that WRONLY becomes RDWR,
130 * as required by this package. Also set our internal
131 * flag for RDONLY if needed.
133 if (flags & O_WRONLY)
134 flags = (flags & ~O_WRONLY) | O_RDWR;
136 else if ((flags & 03) == O_RDONLY)
137 db->flags = DBM_RDONLY;
139 * open the files in sequence, and stat the dirfile.
140 * If we fail anywhere, undo everything, return NULL.
142 #if defined(OS2) || defined(MSDOS) || defined(WIN32) || defined(__CYGWIN__)
145 if ((db->pagf = open(pagname, flags, mode)) > -1) {
146 if ((db->dirf = open(dirname, flags, mode)) > -1) {
148 * need the dirfile size to establish max bit number.
150 if (fstat(db->dirf, &dstat) == 0) {
152 * zero size: either a fresh database, or one with a single,
153 * unsplit data page: dirpage is all zeros.
155 db->dirbno = (!dstat.st_size) ? 0 : -1;
157 db->maxbno = dstat.st_size * BYTESIZ;
159 (void) memset(db->pagbuf, 0, PBLKSIZ);
160 (void) memset(db->dirbuf, 0, DBLKSIZ);
166 (void) close(db->dirf);
168 (void) close(db->pagf);
180 (void) close(db->dirf);
181 (void) close(db->pagf);
187 sdbm_fetch(DBM *db, datum key)
189 if (db == NULL || bad(key))
190 return errno = EINVAL, nullitem;
192 if (getpage(db, exhash(key)))
193 return getpair(db->pagbuf, key);
195 return ioerr(db), nullitem;
199 sdbm_exists(DBM *db, datum key)
201 if (db == NULL || bad(key))
202 return errno = EINVAL, -1;
204 if (getpage(db, exhash(key)))
205 return exipair(db->pagbuf, key);
207 return ioerr(db), -1;
211 sdbm_delete(DBM *db, datum key)
213 if (db == NULL || bad(key))
214 return errno = EINVAL, -1;
216 return errno = EPERM, -1;
218 if (getpage(db, exhash(key))) {
219 if (!delpair(db->pagbuf, key))
222 * update the page file
224 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
225 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
226 return ioerr(db), -1;
231 return ioerr(db), -1;
235 sdbm_store(DBM *db, datum key, datum val, int flags)
240 if (db == NULL || bad(key))
241 return errno = EINVAL, -1;
243 return errno = EPERM, -1;
245 need = key.dsize + val.dsize;
247 * is the pair too big (or too small) for this database ??
249 if (need < 0 || need > PAIRMAX)
250 return errno = EINVAL, -1;
252 if (getpage(db, (hash = exhash(key)))) {
254 * if we need to replace, delete the key/data pair
255 * first. If it is not there, ignore.
257 if (flags == DBM_REPLACE)
258 (void) delpair(db->pagbuf, key);
260 else if (duppair(db->pagbuf, key))
264 * if we do not have enough room, we have to split.
266 if (!fitpair(db->pagbuf, need))
267 if (!makroom(db, hash, need))
268 return ioerr(db), -1;
270 * we have enough room or split is successful. insert the key,
271 * and update the page file.
273 (void) putpair(db->pagbuf, key, val);
275 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
276 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
277 return ioerr(db), -1;
284 return ioerr(db), -1;
288 * makroom - make room by splitting the overfull page
289 * this routine will attempt to make room for SPLTMAX times before
293 makroom(DBM *db, long int hash, int need)
297 #if defined(DOSISH) || defined(WIN32)
301 char *pag = db->pagbuf;
310 * split the current page
312 (void) splpage(pag, New, db->hmask + 1);
314 * address of the new page
316 newp = (hash & db->hmask) | (db->hmask + 1);
319 * write delay, read avoidance/cache shuffle:
320 * select the page for incoming pair: if key is to go to the new page,
321 * write out the previous one, and copy the new one over, thus making
322 * it the current page. If not, simply write the new page, and we are
323 * still looking at the page of interest. current page is not updated
324 * here, as sdbm_store will do so, after it inserts the incoming pair.
327 #if defined(DOSISH) || defined(WIN32)
329 * Fill hole with 0 if made it.
330 * (hole is NOT read as 0)
332 oldtail = lseek(db->pagf, 0L, SEEK_END);
333 memset(zer, 0, PBLKSIZ);
334 while (OFF_PAG(newp) > oldtail) {
335 if (lseek(db->pagf, 0L, SEEK_END) < 0 ||
336 write(db->pagf, zer, PBLKSIZ) < 0) {
343 if (hash & (db->hmask + 1)) {
344 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
345 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
348 (void) memcpy(pag, New, PBLKSIZ);
350 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
351 || write(db->pagf, New, PBLKSIZ) < 0)
354 if (!setdbit(db, db->curbit))
357 * see if we have enough room now
359 if (fitpair(pag, need))
362 * try again... update curbit and hmask as getpage would have
363 * done. because of our update of the current page, we do not
364 * need to read in anything. BUT we have to write the current
365 * [deferred] page out, as the window of failure is too great.
367 db->curbit = 2 * db->curbit +
368 ((hash & (db->hmask + 1)) ? 2 : 1);
369 db->hmask |= db->hmask + 1;
371 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
372 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
377 * if we are here, this is real bad news. After SPLTMAX splits,
378 * we still cannot fit the key. say goodnight.
381 rc = write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
382 /* PERL_UNUSED_VAR() or PERL_UNUSED_RESULT() would be
383 * useful here but that would mean pulling in perl.h */
391 * the following two routines will break if
392 * deletions aren't taken into account. (ndbm bug)
395 sdbm_firstkey(DBM *db)
398 return errno = EINVAL, nullitem;
402 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
403 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
404 return ioerr(db), nullitem;
413 sdbm_nextkey(DBM *db)
416 return errno = EINVAL, nullitem;
421 * all important binary trie traversal
424 getpage(DBM *db, long int hash)
432 while (dbit < db->maxbno && getdbit(db, dbit))
433 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
435 debug(("dbit: %d...", dbit));
438 db->hmask = masks[hbit];
440 pagb = hash & db->hmask;
442 * see if the block we need is already in memory.
443 * note: this lookaside cache has about 10% hit rate.
445 if (pagb != db->pagbno) {
447 * note: here, we assume a "hole" is read as 0s.
448 * if not, must zero pagbuf first.
450 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
451 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
453 if (!chkpage(db->pagbuf))
457 debug(("pag read: %d\n", pagb));
463 getdbit(DBM *db, long int dbit)
471 if (dirb != db->dirbno) {
473 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
474 || (got=read(db->dirf, db->dirbuf, DBLKSIZ)) < 0)
477 memset(db->dirbuf,0,DBLKSIZ);
480 debug(("dir read: %d\n", dirb));
483 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
487 setdbit(DBM *db, long int dbit)
495 if (dirb != db->dirbno) {
497 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
498 || (got=read(db->dirf, db->dirbuf, DBLKSIZ)) < 0)
501 memset(db->dirbuf,0,DBLKSIZ);
504 debug(("dir read: %d\n", dirb));
507 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
510 if (dbit >= db->maxbno)
511 db->maxbno += DBLKSIZ * BYTESIZ;
513 if (OFF_DIR((dirb+1))*BYTESIZ > db->maxbno)
514 db->maxbno=OFF_DIR((dirb+1))*BYTESIZ;
517 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
518 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
525 * getnext - get the next key in the page, and if done with
526 * the page, try the next page in sequence
535 key = getnkey(db->pagbuf, db->keyptr);
536 if (key.dptr != NULL)
539 * we either run out, or there is nothing on this page..
540 * try the next one... If we lost our position on the
541 * file, we will have to seek.
544 if (db->pagbno != db->blkptr++)
545 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
547 db->pagbno = db->blkptr;
548 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
550 if (!chkpage(db->pagbuf))
554 return ioerr(db), nullitem;