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.
11 static char rcsid[] = "$Id: sdbm.c,v 1.16 90/12/13 13:01:31 oz Exp $";
23 # include <sys/file.h>
39 extern Malloc_t malloc proto((MEM_SIZE));
40 extern Free_t free proto((Malloc_t));
46 static int getdbit proto((DBM *, long));
47 static int setdbit proto((DBM *, long));
48 static int getpage proto((DBM *, long));
49 static datum getnext proto((DBM *));
50 static int makroom proto((DBM *, long, int));
55 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
56 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
57 #define ioerr(db) ((db)->flags |= DBM_IOERR)
59 #define OFF_PAG(off) (long) (off) * PBLKSIZ
60 #define OFF_DIR(off) (long) (off) * DBLKSIZ
62 static long masks[] = {
63 000000000000, 000000000001, 000000000003, 000000000007,
64 000000000017, 000000000037, 000000000077, 000000000177,
65 000000000377, 000000000777, 000000001777, 000000003777,
66 000000007777, 000000017777, 000000037777, 000000077777,
67 000000177777, 000000377777, 000000777777, 000001777777,
68 000003777777, 000007777777, 000017777777, 000037777777,
69 000077777777, 000177777777, 000377777777, 000777777777,
70 001777777777, 003777777777, 007777777777, 017777777777
73 datum nullitem = {NULL, 0};
76 sdbm_open(file, flags, mode)
82 register char *dirname;
83 register char *pagname;
86 if (file == NULL || !*file)
87 return errno = EINVAL, (DBM *) NULL;
89 * need space for two seperate filenames
91 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
93 if ((dirname = malloc((unsigned) n)) == NULL)
94 return errno = ENOMEM, (DBM *) NULL;
96 * build the file names
98 dirname = strcat(strcpy(dirname, file), DIRFEXT);
99 pagname = strcpy(dirname + strlen(dirname) + 1, file);
100 pagname = strcat(pagname, PAGFEXT);
102 db = sdbm_prep(dirname, pagname, flags, mode);
103 free((char *) dirname);
108 sdbm_prep(dirname, pagname, flags, mode)
117 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
118 return errno = ENOMEM, (DBM *) NULL;
125 * adjust user flags so that WRONLY becomes RDWR,
126 * as required by this package. Also set our internal
127 * flag for RDONLY if needed.
129 if (flags & O_WRONLY)
130 flags = (flags & ~O_WRONLY) | O_RDWR;
132 else if ((flags & 03) == O_RDONLY)
133 db->flags = DBM_RDONLY;
135 * open the files in sequence, and stat the dirfile.
136 * If we fail anywhere, undo everything, return NULL.
141 if ((db->pagf = open(pagname, flags, mode)) > -1) {
142 if ((db->dirf = open(dirname, flags, mode)) > -1) {
144 * need the dirfile size to establish max bit number.
146 if (fstat(db->dirf, &dstat) == 0) {
148 * zero size: either a fresh database, or one with a single,
149 * unsplit data page: dirpage is all zeros.
151 db->dirbno = (!dstat.st_size) ? 0 : -1;
153 db->maxbno = dstat.st_size * BYTESIZ;
155 (void) memset(db->pagbuf, 0, PBLKSIZ);
156 (void) memset(db->dirbuf, 0, DBLKSIZ);
162 (void) close(db->dirf);
164 (void) close(db->pagf);
177 (void) close(db->dirf);
178 (void) close(db->pagf);
188 if (db == NULL || bad(key))
189 return errno = EINVAL, nullitem;
191 if (getpage(db, exhash(key)))
192 return getpair(db->pagbuf, key);
194 return ioerr(db), nullitem;
202 if (db == NULL || bad(key))
203 return errno = EINVAL, -1;
205 return errno = EPERM, -1;
207 if (getpage(db, exhash(key))) {
208 if (!delpair(db->pagbuf, key))
211 * update the page file
213 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
214 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
215 return ioerr(db), -1;
220 return ioerr(db), -1;
224 sdbm_store(db, key, val, flags)
233 if (db == NULL || bad(key))
234 return errno = EINVAL, -1;
236 return errno = EPERM, -1;
238 need = key.dsize + val.dsize;
240 * is the pair too big (or too small) for this database ??
242 if (need < 0 || need > PAIRMAX)
243 return errno = EINVAL, -1;
245 if (getpage(db, (hash = exhash(key)))) {
247 * if we need to replace, delete the key/data pair
248 * first. If it is not there, ignore.
250 if (flags == DBM_REPLACE)
251 (void) delpair(db->pagbuf, key);
253 else if (duppair(db->pagbuf, key))
257 * if we do not have enough room, we have to split.
259 if (!fitpair(db->pagbuf, need))
260 if (!makroom(db, hash, need))
261 return ioerr(db), -1;
263 * we have enough room or split is successful. insert the key,
264 * and update the page file.
266 (void) putpair(db->pagbuf, key, val);
268 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
269 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
270 return ioerr(db), -1;
277 return ioerr(db), -1;
281 * makroom - make room by splitting the overfull page
282 * this routine will attempt to make room for SPLTMAX times before
286 makroom(db, hash, need)
293 char *pag = db->pagbuf;
295 register int smax = SPLTMAX;
299 * split the current page
301 (void) splpage(pag, new, db->hmask + 1);
303 * address of the new page
305 newp = (hash & db->hmask) | (db->hmask + 1);
308 * write delay, read avoidence/cache shuffle:
309 * select the page for incoming pair: if key is to go to the new page,
310 * write out the previous one, and copy the new one over, thus making
311 * it the current page. If not, simply write the new page, and we are
312 * still looking at the page of interest. current page is not updated
313 * here, as sdbm_store will do so, after it inserts the incoming pair.
315 if (hash & (db->hmask + 1)) {
316 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
317 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
320 (void) memcpy(pag, new, PBLKSIZ);
322 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
323 || write(db->pagf, new, PBLKSIZ) < 0)
326 if (!setdbit(db, db->curbit))
329 * see if we have enough room now
331 if (fitpair(pag, need))
334 * try again... update curbit and hmask as getpage would have
335 * done. because of our update of the current page, we do not
336 * need to read in anything. BUT we have to write the current
337 * [deferred] page out, as the window of failure is too great.
339 db->curbit = 2 * db->curbit +
340 ((hash & (db->hmask + 1)) ? 2 : 1);
341 db->hmask |= db->hmask + 1;
343 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
344 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
349 * if we are here, this is real bad news. After SPLTMAX splits,
350 * we still cannot fit the key. say goodnight.
353 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
360 * the following two routines will break if
361 * deletions aren't taken into account. (ndbm bug)
368 return errno = EINVAL, nullitem;
372 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
373 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
374 return ioerr(db), nullitem;
387 return errno = EINVAL, nullitem;
392 * all important binary trie traversal
405 while (dbit < db->maxbno && getdbit(db, dbit))
406 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
408 debug(("dbit: %d...", dbit));
411 db->hmask = masks[hbit];
413 pagb = hash & db->hmask;
415 * see if the block we need is already in memory.
416 * note: this lookaside cache has about 10% hit rate.
418 if (pagb != db->pagbno) {
420 * note: here, we assume a "hole" is read as 0s.
421 * if not, must zero pagbuf first.
423 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
424 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
426 if (!chkpage(db->pagbuf))
430 debug(("pag read: %d\n", pagb));
446 if (dirb != db->dirbno) {
447 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
448 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
452 debug(("dir read: %d\n", dirb));
455 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
469 if (dirb != db->dirbno) {
470 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
471 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
475 debug(("dir read: %d\n", dirb));
478 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
480 if (dbit >= db->maxbno)
481 db->maxbno += DBLKSIZ * BYTESIZ;
483 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
484 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
491 * getnext - get the next key in the page, and if done with
492 * the page, try the next page in sequence
502 key = getnkey(db->pagbuf, db->keyptr);
503 if (key.dptr != NULL)
506 * we either run out, or there is nothing on this page..
507 * try the next one... If we lost our position on the
508 * file, we will have to seek.
511 if (db->pagbno != db->blkptr++)
512 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
514 db->pagbno = db->blkptr;
515 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
517 if (!chkpage(db->pagbuf))
521 return ioerr(db), nullitem;