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 $";
24 # include <sys/file.h>
41 extern Malloc_t malloc proto((MEM_SIZE));
42 extern Free_t free proto((Malloc_t));
43 extern Off_t lseek(int, Off_t, int);
49 static int getdbit proto((DBM *, long));
50 static int setdbit proto((DBM *, long));
51 static int getpage proto((DBM *, long));
52 static datum getnext proto((DBM *));
53 static int makroom proto((DBM *, long, int));
58 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
59 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
60 #define ioerr(db) ((db)->flags |= DBM_IOERR)
62 #define OFF_PAG(off) (long) (off) * PBLKSIZ
63 #define OFF_DIR(off) (long) (off) * DBLKSIZ
65 static long masks[] = {
66 000000000000, 000000000001, 000000000003, 000000000007,
67 000000000017, 000000000037, 000000000077, 000000000177,
68 000000000377, 000000000777, 000000001777, 000000003777,
69 000000007777, 000000017777, 000000037777, 000000077777,
70 000000177777, 000000377777, 000000777777, 000001777777,
71 000003777777, 000007777777, 000017777777, 000037777777,
72 000077777777, 000177777777, 000377777777, 000777777777,
73 001777777777, 003777777777, 007777777777, 017777777777
77 sdbm_open(register char *file, register int flags, register int mode)
80 register char *dirname;
81 register char *pagname;
84 if (file == NULL || !*file)
85 return errno = EINVAL, (DBM *) NULL;
87 * need space for two seperate filenames
89 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
91 if ((dirname = (char *) malloc((unsigned) n)) == NULL)
92 return errno = ENOMEM, (DBM *) NULL;
94 * build the file names
96 dirname = strcat(strcpy(dirname, file), DIRFEXT);
97 pagname = strcpy(dirname + strlen(dirname) + 1, file);
98 pagname = strcat(pagname, PAGFEXT);
100 db = sdbm_prep(dirname, pagname, flags, mode);
101 free((char *) dirname);
106 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
111 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
112 return errno = ENOMEM, (DBM *) NULL;
119 * adjust user flags so that WRONLY becomes RDWR,
120 * as required by this package. Also set our internal
121 * flag for RDONLY if needed.
123 if (flags & O_WRONLY)
124 flags = (flags & ~O_WRONLY) | O_RDWR;
126 else if ((flags & 03) == O_RDONLY)
127 db->flags = DBM_RDONLY;
129 * open the files in sequence, and stat the dirfile.
130 * If we fail anywhere, undo everything, return NULL.
132 #if defined(OS2) || defined(MSDOS) || defined(WIN32)
135 if ((db->pagf = open(pagname, flags, mode)) > -1) {
136 if ((db->dirf = open(dirname, flags, mode)) > -1) {
138 * need the dirfile size to establish max bit number.
140 if (fstat(db->dirf, &dstat) == 0) {
142 * zero size: either a fresh database, or one with a single,
143 * unsplit data page: dirpage is all zeros.
145 db->dirbno = (!dstat.st_size) ? 0 : -1;
147 db->maxbno = dstat.st_size * BYTESIZ;
149 (void) memset(db->pagbuf, 0, PBLKSIZ);
150 (void) memset(db->dirbuf, 0, DBLKSIZ);
156 (void) close(db->dirf);
158 (void) close(db->pagf);
165 sdbm_close(register DBM *db)
170 (void) close(db->dirf);
171 (void) close(db->pagf);
177 sdbm_fetch(register DBM *db, datum key)
179 if (db == NULL || bad(key))
180 return errno = EINVAL, nullitem;
182 if (getpage(db, exhash(key)))
183 return getpair(db->pagbuf, key);
185 return ioerr(db), nullitem;
189 sdbm_delete(register DBM *db, datum key)
191 if (db == NULL || bad(key))
192 return errno = EINVAL, -1;
194 return errno = EPERM, -1;
196 if (getpage(db, exhash(key))) {
197 if (!delpair(db->pagbuf, key))
200 * update the page file
202 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
203 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
204 return ioerr(db), -1;
209 return ioerr(db), -1;
213 sdbm_store(register DBM *db, datum key, datum val, int flags)
218 if (db == NULL || bad(key))
219 return errno = EINVAL, -1;
221 return errno = EPERM, -1;
223 need = key.dsize + val.dsize;
225 * is the pair too big (or too small) for this database ??
227 if (need < 0 || need > PAIRMAX)
228 return errno = EINVAL, -1;
230 if (getpage(db, (hash = exhash(key)))) {
232 * if we need to replace, delete the key/data pair
233 * first. If it is not there, ignore.
235 if (flags == DBM_REPLACE)
236 (void) delpair(db->pagbuf, key);
238 else if (duppair(db->pagbuf, key))
242 * if we do not have enough room, we have to split.
244 if (!fitpair(db->pagbuf, need))
245 if (!makroom(db, hash, need))
246 return ioerr(db), -1;
248 * we have enough room or split is successful. insert the key,
249 * and update the page file.
251 (void) putpair(db->pagbuf, key, val);
253 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
254 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
255 return ioerr(db), -1;
262 return ioerr(db), -1;
266 * makroom - make room by splitting the overfull page
267 * this routine will attempt to make room for SPLTMAX times before
271 makroom(register DBM *db, long int hash, int need)
275 char *pag = db->pagbuf;
277 register int smax = SPLTMAX;
281 * split the current page
283 (void) splpage(pag, New, db->hmask + 1);
285 * address of the new page
287 newp = (hash & db->hmask) | (db->hmask + 1);
290 * write delay, read avoidence/cache shuffle:
291 * select the page for incoming pair: if key is to go to the new page,
292 * write out the previous one, and copy the new one over, thus making
293 * it the current page. If not, simply write the new page, and we are
294 * still looking at the page of interest. current page is not updated
295 * here, as sdbm_store will do so, after it inserts the incoming pair.
297 if (hash & (db->hmask + 1)) {
298 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
299 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
302 (void) memcpy(pag, New, PBLKSIZ);
304 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
305 || write(db->pagf, New, PBLKSIZ) < 0)
308 if (!setdbit(db, db->curbit))
311 * see if we have enough room now
313 if (fitpair(pag, need))
316 * try again... update curbit and hmask as getpage would have
317 * done. because of our update of the current page, we do not
318 * need to read in anything. BUT we have to write the current
319 * [deferred] page out, as the window of failure is too great.
321 db->curbit = 2 * db->curbit +
322 ((hash & (db->hmask + 1)) ? 2 : 1);
323 db->hmask |= db->hmask + 1;
325 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
326 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
331 * if we are here, this is real bad news. After SPLTMAX splits,
332 * we still cannot fit the key. say goodnight.
335 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
342 * the following two routines will break if
343 * deletions aren't taken into account. (ndbm bug)
346 sdbm_firstkey(register DBM *db)
349 return errno = EINVAL, nullitem;
353 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
354 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
355 return ioerr(db), nullitem;
364 sdbm_nextkey(register DBM *db)
367 return errno = EINVAL, nullitem;
372 * all important binary trie traversal
375 getpage(register DBM *db, register long int hash)
383 while (dbit < db->maxbno && getdbit(db, dbit))
384 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
386 debug(("dbit: %d...", dbit));
389 db->hmask = masks[hbit];
391 pagb = hash & db->hmask;
393 * see if the block we need is already in memory.
394 * note: this lookaside cache has about 10% hit rate.
396 if (pagb != db->pagbno) {
398 * note: here, we assume a "hole" is read as 0s.
399 * if not, must zero pagbuf first.
401 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
402 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
404 if (!chkpage(db->pagbuf))
408 debug(("pag read: %d\n", pagb));
414 getdbit(register DBM *db, register long int dbit)
422 if (dirb != db->dirbno) {
423 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
424 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
428 debug(("dir read: %d\n", dirb));
431 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
435 setdbit(register DBM *db, register long int dbit)
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 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
454 if (dbit >= db->maxbno)
455 db->maxbno += DBLKSIZ * BYTESIZ;
457 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
458 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
465 * getnext - get the next key in the page, and if done with
466 * the page, try the next page in sequence
469 getnext(register DBM *db)
475 key = getnkey(db->pagbuf, db->keyptr);
476 if (key.dptr != NULL)
479 * we either run out, or there is nothing on this page..
480 * try the next one... If we lost our position on the
481 * file, we will have to seek.
484 if (db->pagbno != db->blkptr++)
485 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
487 db->pagbno = db->blkptr;
488 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
490 if (!chkpage(db->pagbuf))
494 return ioerr(db), nullitem;