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((void *));
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.
138 if ((db->pagf = open(pagname, flags, mode)) > -1) {
139 if ((db->dirf = open(dirname, flags, mode)) > -1) {
141 * need the dirfile size to establish max bit number.
143 if (fstat(db->dirf, &dstat) == 0) {
145 * zero size: either a fresh database, or one with a single,
146 * unsplit data page: dirpage is all zeros.
148 db->dirbno = (!dstat.st_size) ? 0 : -1;
150 db->maxbno = dstat.st_size * BYTESIZ;
152 (void) memset(db->pagbuf, 0, PBLKSIZ);
153 (void) memset(db->dirbuf, 0, DBLKSIZ);
159 (void) close(db->dirf);
161 (void) close(db->pagf);
174 (void) close(db->dirf);
175 (void) close(db->pagf);
185 if (db == NULL || bad(key))
186 return errno = EINVAL, nullitem;
188 if (getpage(db, exhash(key)))
189 return getpair(db->pagbuf, key);
191 return ioerr(db), nullitem;
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(db, key, val, flags)
230 if (db == NULL || bad(key))
231 return errno = EINVAL, -1;
233 return errno = EPERM, -1;
235 need = key.dsize + val.dsize;
237 * is the pair too big (or too small) for this database ??
239 if (need < 0 || need > PAIRMAX)
240 return errno = EINVAL, -1;
242 if (getpage(db, (hash = exhash(key)))) {
244 * if we need to replace, delete the key/data pair
245 * first. If it is not there, ignore.
247 if (flags == DBM_REPLACE)
248 (void) delpair(db->pagbuf, key);
250 else if (duppair(db->pagbuf, key))
254 * if we do not have enough room, we have to split.
256 if (!fitpair(db->pagbuf, need))
257 if (!makroom(db, hash, need))
258 return ioerr(db), -1;
260 * we have enough room or split is successful. insert the key,
261 * and update the page file.
263 (void) putpair(db->pagbuf, key, val);
265 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
266 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
267 return ioerr(db), -1;
274 return ioerr(db), -1;
278 * makroom - make room by splitting the overfull page
279 * this routine will attempt to make room for SPLTMAX times before
283 makroom(db, hash, need)
290 char *pag = db->pagbuf;
292 register int smax = SPLTMAX;
296 * split the current page
298 (void) splpage(pag, new, db->hmask + 1);
300 * address of the new page
302 newp = (hash & db->hmask) | (db->hmask + 1);
305 * write delay, read avoidence/cache shuffle:
306 * select the page for incoming pair: if key is to go to the new page,
307 * write out the previous one, and copy the new one over, thus making
308 * it the current page. If not, simply write the new page, and we are
309 * still looking at the page of interest. current page is not updated
310 * here, as sdbm_store will do so, after it inserts the incoming pair.
312 if (hash & (db->hmask + 1)) {
313 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
314 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
317 (void) memcpy(pag, new, PBLKSIZ);
319 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
320 || write(db->pagf, new, PBLKSIZ) < 0)
323 if (!setdbit(db, db->curbit))
326 * see if we have enough room now
328 if (fitpair(pag, need))
331 * try again... update curbit and hmask as getpage would have
332 * done. because of our update of the current page, we do not
333 * need to read in anything. BUT we have to write the current
334 * [deferred] page out, as the window of failure is too great.
336 db->curbit = 2 * db->curbit +
337 ((hash & (db->hmask + 1)) ? 2 : 1);
338 db->hmask |= db->hmask + 1;
340 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
341 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
346 * if we are here, this is real bad news. After SPLTMAX splits,
347 * we still cannot fit the key. say goodnight.
350 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
357 * the following two routines will break if
358 * deletions aren't taken into account. (ndbm bug)
365 return errno = EINVAL, nullitem;
369 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
370 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
371 return ioerr(db), nullitem;
384 return errno = EINVAL, nullitem;
389 * all important binary trie traversal
402 while (dbit < db->maxbno && getdbit(db, dbit))
403 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
405 debug(("dbit: %d...", dbit));
408 db->hmask = masks[hbit];
410 pagb = hash & db->hmask;
412 * see if the block we need is already in memory.
413 * note: this lookaside cache has about 10% hit rate.
415 if (pagb != db->pagbno) {
417 * note: here, we assume a "hole" is read as 0s.
418 * if not, must zero pagbuf first.
420 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
421 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
423 if (!chkpage(db->pagbuf))
427 debug(("pag read: %d\n", pagb));
443 if (dirb != db->dirbno) {
444 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
445 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
449 debug(("dir read: %d\n", dirb));
452 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
466 if (dirb != db->dirbno) {
467 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
468 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
472 debug(("dir read: %d\n", dirb));
475 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
477 if (dbit >= db->maxbno)
478 db->maxbno += DBLKSIZ * BYTESIZ;
480 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
481 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
488 * getnext - get the next key in the page, and if done with
489 * the page, try the next page in sequence
499 key = getnkey(db->pagbuf, db->keyptr);
500 if (key.dptr != NULL)
503 * we either run out, or there is nothing on this page..
504 * try the next one... If we lost our position on the
505 * file, we will have to seek.
508 if (db->pagbno != db->blkptr++)
509 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
511 db->pagbno = db->blkptr;
512 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
514 if (!chkpage(db->pagbuf))
518 return ioerr(db), nullitem;