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>
40 extern Malloc_t malloc proto((MEM_SIZE));
41 extern Free_t free proto((Malloc_t));
48 static int getdbit proto((DBM *, long));
49 static int setdbit proto((DBM *, long));
50 static int getpage proto((DBM *, long));
51 static datum getnext proto((DBM *));
52 static int makroom proto((DBM *, long, int));
57 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
58 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
59 #define ioerr(db) ((db)->flags |= DBM_IOERR)
61 #define OFF_PAG(off) (long) (off) * PBLKSIZ
62 #define OFF_DIR(off) (long) (off) * DBLKSIZ
64 static long masks[] = {
65 000000000000, 000000000001, 000000000003, 000000000007,
66 000000000017, 000000000037, 000000000077, 000000000177,
67 000000000377, 000000000777, 000000001777, 000000003777,
68 000000007777, 000000017777, 000000037777, 000000077777,
69 000000177777, 000000377777, 000000777777, 000001777777,
70 000003777777, 000007777777, 000017777777, 000037777777,
71 000077777777, 000177777777, 000377777777, 000777777777,
72 001777777777, 003777777777, 007777777777, 017777777777
75 datum nullitem = {NULL, 0};
78 sdbm_open(file, flags, mode)
84 register char *dirname;
85 register char *pagname;
88 if (file == NULL || !*file)
89 return errno = EINVAL, (DBM *) NULL;
91 * need space for two seperate filenames
93 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
95 if ((dirname = malloc((unsigned) n)) == NULL)
96 return errno = ENOMEM, (DBM *) NULL;
98 * build the file names
100 dirname = strcat(strcpy(dirname, file), DIRFEXT);
101 pagname = strcpy(dirname + strlen(dirname) + 1, file);
102 pagname = strcat(pagname, PAGFEXT);
104 db = sdbm_prep(dirname, pagname, flags, mode);
105 free((char *) dirname);
110 sdbm_prep(dirname, pagname, flags, mode)
119 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
120 return errno = ENOMEM, (DBM *) NULL;
127 * adjust user flags so that WRONLY becomes RDWR,
128 * as required by this package. Also set our internal
129 * flag for RDONLY if needed.
131 if (flags & O_WRONLY)
132 flags = (flags & ~O_WRONLY) | O_RDWR;
134 else if ((flags & 03) == O_RDONLY)
135 db->flags = DBM_RDONLY;
137 * open the files in sequence, and stat the dirfile.
138 * If we fail anywhere, undo everything, return NULL.
140 #if defined(OS2) || defined(MSDOS) || defined(WIN32)
143 if ((db->pagf = open(pagname, flags, mode)) > -1) {
144 if ((db->dirf = open(dirname, flags, mode)) > -1) {
146 * need the dirfile size to establish max bit number.
148 if (fstat(db->dirf, &dstat) == 0) {
150 * zero size: either a fresh database, or one with a single,
151 * unsplit data page: dirpage is all zeros.
153 db->dirbno = (!dstat.st_size) ? 0 : -1;
155 db->maxbno = dstat.st_size * BYTESIZ;
157 (void) memset(db->pagbuf, 0, PBLKSIZ);
158 (void) memset(db->dirbuf, 0, DBLKSIZ);
164 (void) close(db->dirf);
166 (void) close(db->pagf);
179 (void) close(db->dirf);
180 (void) close(db->pagf);
190 if (db == NULL || bad(key))
191 return errno = EINVAL, nullitem;
193 if (getpage(db, exhash(key)))
194 return getpair(db->pagbuf, key);
196 return ioerr(db), nullitem;
204 if (db == NULL || bad(key))
205 return errno = EINVAL, -1;
207 return errno = EPERM, -1;
209 if (getpage(db, exhash(key))) {
210 if (!delpair(db->pagbuf, key))
213 * update the page file
215 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
216 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
217 return ioerr(db), -1;
222 return ioerr(db), -1;
226 sdbm_store(db, key, val, flags)
235 if (db == NULL || bad(key))
236 return errno = EINVAL, -1;
238 return errno = EPERM, -1;
240 need = key.dsize + val.dsize;
242 * is the pair too big (or too small) for this database ??
244 if (need < 0 || need > PAIRMAX)
245 return errno = EINVAL, -1;
247 if (getpage(db, (hash = exhash(key)))) {
249 * if we need to replace, delete the key/data pair
250 * first. If it is not there, ignore.
252 if (flags == DBM_REPLACE)
253 (void) delpair(db->pagbuf, key);
255 else if (duppair(db->pagbuf, key))
259 * if we do not have enough room, we have to split.
261 if (!fitpair(db->pagbuf, need))
262 if (!makroom(db, hash, need))
263 return ioerr(db), -1;
265 * we have enough room or split is successful. insert the key,
266 * and update the page file.
268 (void) putpair(db->pagbuf, key, val);
270 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
271 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
272 return ioerr(db), -1;
279 return ioerr(db), -1;
283 * makroom - make room by splitting the overfull page
284 * this routine will attempt to make room for SPLTMAX times before
288 makroom(db, hash, need)
295 char *pag = db->pagbuf;
297 register int smax = SPLTMAX;
301 * split the current page
303 (void) splpage(pag, new, db->hmask + 1);
305 * address of the new page
307 newp = (hash & db->hmask) | (db->hmask + 1);
310 * write delay, read avoidence/cache shuffle:
311 * select the page for incoming pair: if key is to go to the new page,
312 * write out the previous one, and copy the new one over, thus making
313 * it the current page. If not, simply write the new page, and we are
314 * still looking at the page of interest. current page is not updated
315 * here, as sdbm_store will do so, after it inserts the incoming pair.
317 if (hash & (db->hmask + 1)) {
318 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
319 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
322 (void) memcpy(pag, new, PBLKSIZ);
324 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
325 || write(db->pagf, new, PBLKSIZ) < 0)
328 if (!setdbit(db, db->curbit))
331 * see if we have enough room now
333 if (fitpair(pag, need))
336 * try again... update curbit and hmask as getpage would have
337 * done. because of our update of the current page, we do not
338 * need to read in anything. BUT we have to write the current
339 * [deferred] page out, as the window of failure is too great.
341 db->curbit = 2 * db->curbit +
342 ((hash & (db->hmask + 1)) ? 2 : 1);
343 db->hmask |= db->hmask + 1;
345 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
346 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
351 * if we are here, this is real bad news. After SPLTMAX splits,
352 * we still cannot fit the key. say goodnight.
355 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
362 * the following two routines will break if
363 * deletions aren't taken into account. (ndbm bug)
370 return errno = EINVAL, nullitem;
374 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
375 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
376 return ioerr(db), nullitem;
389 return errno = EINVAL, nullitem;
394 * all important binary trie traversal
407 while (dbit < db->maxbno && getdbit(db, dbit))
408 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
410 debug(("dbit: %d...", dbit));
413 db->hmask = masks[hbit];
415 pagb = hash & db->hmask;
417 * see if the block we need is already in memory.
418 * note: this lookaside cache has about 10% hit rate.
420 if (pagb != db->pagbno) {
422 * note: here, we assume a "hole" is read as 0s.
423 * if not, must zero pagbuf first.
425 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
426 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
428 if (!chkpage(db->pagbuf))
432 debug(("pag read: %d\n", pagb));
448 if (dirb != db->dirbno) {
449 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
450 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
454 debug(("dir read: %d\n", dirb));
457 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
471 if (dirb != db->dirbno) {
472 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
473 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
477 debug(("dir read: %d\n", dirb));
480 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
482 if (dbit >= db->maxbno)
483 db->maxbno += DBLKSIZ * BYTESIZ;
485 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
486 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
493 * getnext - get the next key in the page, and if done with
494 * the page, try the next page in sequence
504 key = getnkey(db->pagbuf, db->keyptr);
505 if (key.dptr != NULL)
508 * we either run out, or there is nothing on this page..
509 * try the next one... If we lost our position on the
510 * file, we will have to seek.
513 if (db->pagbno != db->blkptr++)
514 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
516 db->pagbno = db->blkptr;
517 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
519 if (!chkpage(db->pagbuf))
523 return ioerr(db), nullitem;