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));
51 const datum nullitem = {0, 0};
56 static int getdbit proto((DBM *, long));
57 static int setdbit proto((DBM *, long));
58 static int getpage proto((DBM *, long));
59 static datum getnext proto((DBM *));
60 static int makroom proto((DBM *, long, int));
65 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
66 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
67 #define ioerr(db) ((db)->flags |= DBM_IOERR)
69 #define OFF_PAG(off) (long) (off) * PBLKSIZ
70 #define OFF_DIR(off) (long) (off) * DBLKSIZ
72 static const long masks[] = {
73 000000000000, 000000000001, 000000000003, 000000000007,
74 000000000017, 000000000037, 000000000077, 000000000177,
75 000000000377, 000000000777, 000000001777, 000000003777,
76 000000007777, 000000017777, 000000037777, 000000077777,
77 000000177777, 000000377777, 000000777777, 000001777777,
78 000003777777, 000007777777, 000017777777, 000037777777,
79 000077777777, 000177777777, 000377777777, 000777777777,
80 001777777777, 003777777777, 007777777777, 017777777777
84 sdbm_open(char *file, int flags, int mode)
90 const size_t dirfext_size = sizeof(DIRFEXT "");
91 const size_t pagfext_size = sizeof(PAGFEXT "");
93 if (file == NULL || !*file)
94 return errno = EINVAL, (DBM *) NULL;
96 * need space for two separate filenames
98 filelen = strlen(file);
100 if ((dirname = (char *) malloc(filelen + dirfext_size
101 + filelen + pagfext_size)) == NULL)
102 return errno = ENOMEM, (DBM *) NULL;
104 * build the file names
106 memcpy(dirname, file, filelen);
107 memcpy(dirname + filelen, DIRFEXT, dirfext_size);
108 pagname = dirname + filelen + dirfext_size;
109 memcpy(pagname, file, filelen);
110 memcpy(pagname + filelen, PAGFEXT, pagfext_size);
112 db = sdbm_prep(dirname, pagname, flags, mode);
113 free((char *) dirname);
118 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
123 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
124 return errno = ENOMEM, (DBM *) NULL;
131 * adjust user flags so that WRONLY becomes RDWR,
132 * as required by this package. Also set our internal
133 * flag for RDONLY if needed.
135 if (flags & O_WRONLY)
136 flags = (flags & ~O_WRONLY) | O_RDWR;
138 else if ((flags & 03) == O_RDONLY)
139 db->flags = DBM_RDONLY;
141 * open the files in sequence, and stat the dirfile.
142 * If we fail anywhere, undo everything, return NULL.
144 #if defined(OS2) || defined(MSDOS) || defined(WIN32) || defined(__CYGWIN__)
147 if ((db->pagf = open(pagname, flags, mode)) > -1) {
148 if ((db->dirf = open(dirname, flags, mode)) > -1) {
150 * need the dirfile size to establish max bit number.
152 if (fstat(db->dirf, &dstat) == 0) {
154 * zero size: either a fresh database, or one with a single,
155 * unsplit data page: dirpage is all zeros.
157 db->dirbno = (!dstat.st_size) ? 0 : -1;
159 db->maxbno = dstat.st_size * BYTESIZ;
161 (void) memset(db->pagbuf, 0, PBLKSIZ);
162 (void) memset(db->dirbuf, 0, DBLKSIZ);
168 (void) close(db->dirf);
170 (void) close(db->pagf);
182 (void) close(db->dirf);
183 (void) close(db->pagf);
189 sdbm_fetch(DBM *db, datum key)
191 if (db == NULL || bad(key))
192 return errno = EINVAL, nullitem;
194 if (getpage(db, exhash(key)))
195 return getpair(db->pagbuf, key);
197 return ioerr(db), nullitem;
201 sdbm_exists(DBM *db, datum key)
203 if (db == NULL || bad(key))
204 return errno = EINVAL, -1;
206 if (getpage(db, exhash(key)))
207 return exipair(db->pagbuf, key);
209 return ioerr(db), -1;
213 sdbm_delete(DBM *db, datum key)
215 if (db == NULL || bad(key))
216 return errno = EINVAL, -1;
218 return errno = EPERM, -1;
220 if (getpage(db, exhash(key))) {
221 if (!delpair(db->pagbuf, key))
224 * update the page file
226 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
227 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
228 return ioerr(db), -1;
233 return ioerr(db), -1;
237 sdbm_store(DBM *db, datum key, datum val, int flags)
242 if (db == NULL || bad(key))
243 return errno = EINVAL, -1;
245 return errno = EPERM, -1;
247 need = key.dsize + val.dsize;
249 * is the pair too big (or too small) for this database ??
251 if (need < 0 || need > PAIRMAX)
252 return errno = EINVAL, -1;
254 if (getpage(db, (hash = exhash(key)))) {
256 * if we need to replace, delete the key/data pair
257 * first. If it is not there, ignore.
259 if (flags == DBM_REPLACE)
260 (void) delpair(db->pagbuf, key);
262 else if (duppair(db->pagbuf, key))
266 * if we do not have enough room, we have to split.
268 if (!fitpair(db->pagbuf, need))
269 if (!makroom(db, hash, need))
270 return ioerr(db), -1;
272 * we have enough room or split is successful. insert the key,
273 * and update the page file.
275 (void) putpair(db->pagbuf, key, val);
277 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
278 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
279 return ioerr(db), -1;
286 return ioerr(db), -1;
290 * makroom - make room by splitting the overfull page
291 * this routine will attempt to make room for SPLTMAX times before
295 makroom(DBM *db, long int hash, int need)
299 #if defined(DOSISH) || defined(WIN32)
303 char *pag = db->pagbuf;
312 * split the current page
314 (void) splpage(pag, New, db->hmask + 1);
316 * address of the new page
318 newp = (hash & db->hmask) | (db->hmask + 1);
321 * write delay, read avoidance/cache shuffle:
322 * select the page for incoming pair: if key is to go to the new page,
323 * write out the previous one, and copy the new one over, thus making
324 * it the current page. If not, simply write the new page, and we are
325 * still looking at the page of interest. current page is not updated
326 * here, as sdbm_store will do so, after it inserts the incoming pair.
329 #if defined(DOSISH) || defined(WIN32)
331 * Fill hole with 0 if made it.
332 * (hole is NOT read as 0)
334 oldtail = lseek(db->pagf, 0L, SEEK_END);
335 memset(zer, 0, PBLKSIZ);
336 while (OFF_PAG(newp) > oldtail) {
337 if (lseek(db->pagf, 0L, SEEK_END) < 0 ||
338 write(db->pagf, zer, PBLKSIZ) < 0) {
345 if (hash & (db->hmask + 1)) {
346 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
347 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
350 (void) memcpy(pag, New, PBLKSIZ);
352 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
353 || write(db->pagf, New, PBLKSIZ) < 0)
356 if (!setdbit(db, db->curbit))
359 * see if we have enough room now
361 if (fitpair(pag, need))
364 * try again... update curbit and hmask as getpage would have
365 * done. because of our update of the current page, we do not
366 * need to read in anything. BUT we have to write the current
367 * [deferred] page out, as the window of failure is too great.
369 db->curbit = 2 * db->curbit +
370 ((hash & (db->hmask + 1)) ? 2 : 1);
371 db->hmask |= db->hmask + 1;
373 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
374 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
379 * if we are here, this is real bad news. After SPLTMAX splits,
380 * we still cannot fit the key. say goodnight.
383 rc = write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
384 /* PERL_UNUSED_VAR() or PERL_UNUSED_RESULT() would be
385 * useful here but that would mean pulling in perl.h */
393 * the following two routines will break if
394 * deletions aren't taken into account. (ndbm bug)
397 sdbm_firstkey(DBM *db)
400 return errno = EINVAL, nullitem;
404 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
405 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
406 return ioerr(db), nullitem;
415 sdbm_nextkey(DBM *db)
418 return errno = EINVAL, nullitem;
423 * all important binary trie traversal
426 getpage(DBM *db, long int hash)
434 while (dbit < db->maxbno && getdbit(db, dbit))
435 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
437 debug(("dbit: %d...", dbit));
440 db->hmask = masks[hbit];
442 pagb = hash & db->hmask;
444 * see if the block we need is already in memory.
445 * note: this lookaside cache has about 10% hit rate.
447 if (pagb != db->pagbno) {
449 * note: here, we assume a "hole" is read as 0s.
450 * if not, must zero pagbuf first.
452 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
453 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
455 if (!chkpage(db->pagbuf))
459 debug(("pag read: %d\n", pagb));
465 getdbit(DBM *db, long int dbit)
473 if (dirb != db->dirbno) {
475 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
476 || (got=read(db->dirf, db->dirbuf, DBLKSIZ)) < 0)
479 memset(db->dirbuf,0,DBLKSIZ);
482 debug(("dir read: %d\n", dirb));
485 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
489 setdbit(DBM *db, long int dbit)
497 if (dirb != db->dirbno) {
499 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
500 || (got=read(db->dirf, db->dirbuf, DBLKSIZ)) < 0)
503 memset(db->dirbuf,0,DBLKSIZ);
506 debug(("dir read: %d\n", dirb));
509 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
512 if (dbit >= db->maxbno)
513 db->maxbno += DBLKSIZ * BYTESIZ;
515 if (OFF_DIR((dirb+1))*BYTESIZ > db->maxbno)
516 db->maxbno=OFF_DIR((dirb+1))*BYTESIZ;
519 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
520 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
527 * getnext - get the next key in the page, and if done with
528 * the page, try the next page in sequence
537 key = getnkey(db->pagbuf, db->keyptr);
538 if (key.dptr != NULL)
541 * we either run out, or there is nothing on this page..
542 * try the next one... If we lost our position on the
543 * file, we will have to seek.
546 if (db->pagbno != db->blkptr++)
547 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
549 db->pagbno = db->blkptr;
550 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
552 if (!chkpage(db->pagbuf))
556 return ioerr(db), nullitem;