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));
42 extern Off_t lseek(int, off_t, int);
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(register char *file, register int flags, register int mode)
81 register char *dirname;
82 register char *pagname;
85 if (file == NULL || !*file)
86 return errno = EINVAL, (DBM *) NULL;
88 * need space for two seperate filenames
90 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
92 if ((dirname = (char *) malloc((unsigned) n)) == NULL)
93 return errno = ENOMEM, (DBM *) NULL;
95 * build the file names
97 dirname = strcat(strcpy(dirname, file), DIRFEXT);
98 pagname = strcpy(dirname + strlen(dirname) + 1, file);
99 pagname = strcat(pagname, PAGFEXT);
101 db = sdbm_prep(dirname, pagname, flags, mode);
102 free((char *) dirname);
107 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
112 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
113 return errno = ENOMEM, (DBM *) NULL;
120 * adjust user flags so that WRONLY becomes RDWR,
121 * as required by this package. Also set our internal
122 * flag for RDONLY if needed.
124 if (flags & O_WRONLY)
125 flags = (flags & ~O_WRONLY) | O_RDWR;
127 else if ((flags & 03) == O_RDONLY)
128 db->flags = DBM_RDONLY;
130 * open the files in sequence, and stat the dirfile.
131 * If we fail anywhere, undo everything, return NULL.
133 #if defined(OS2) || defined(MSDOS) || defined(WIN32)
136 if ((db->pagf = open(pagname, flags, mode)) > -1) {
137 if ((db->dirf = open(dirname, flags, mode)) > -1) {
139 * need the dirfile size to establish max bit number.
141 if (fstat(db->dirf, &dstat) == 0) {
143 * zero size: either a fresh database, or one with a single,
144 * unsplit data page: dirpage is all zeros.
146 db->dirbno = (!dstat.st_size) ? 0 : -1;
148 db->maxbno = dstat.st_size * BYTESIZ;
150 (void) memset(db->pagbuf, 0, PBLKSIZ);
151 (void) memset(db->dirbuf, 0, DBLKSIZ);
157 (void) close(db->dirf);
159 (void) close(db->pagf);
166 sdbm_close(register DBM *db)
171 (void) close(db->dirf);
172 (void) close(db->pagf);
178 sdbm_fetch(register DBM *db, datum key)
180 if (db == NULL || bad(key))
181 return errno = EINVAL, nullitem;
183 if (getpage(db, exhash(key)))
184 return getpair(db->pagbuf, key);
186 return ioerr(db), nullitem;
190 sdbm_delete(register DBM *db, datum key)
192 if (db == NULL || bad(key))
193 return errno = EINVAL, -1;
195 return errno = EPERM, -1;
197 if (getpage(db, exhash(key))) {
198 if (!delpair(db->pagbuf, key))
201 * update the page file
203 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
204 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
205 return ioerr(db), -1;
210 return ioerr(db), -1;
214 sdbm_store(register DBM *db, datum key, datum val, int flags)
219 if (db == NULL || bad(key))
220 return errno = EINVAL, -1;
222 return errno = EPERM, -1;
224 need = key.dsize + val.dsize;
226 * is the pair too big (or too small) for this database ??
228 if (need < 0 || need > PAIRMAX)
229 return errno = EINVAL, -1;
231 if (getpage(db, (hash = exhash(key)))) {
233 * if we need to replace, delete the key/data pair
234 * first. If it is not there, ignore.
236 if (flags == DBM_REPLACE)
237 (void) delpair(db->pagbuf, key);
239 else if (duppair(db->pagbuf, key))
243 * if we do not have enough room, we have to split.
245 if (!fitpair(db->pagbuf, need))
246 if (!makroom(db, hash, need))
247 return ioerr(db), -1;
249 * we have enough room or split is successful. insert the key,
250 * and update the page file.
252 (void) putpair(db->pagbuf, key, val);
254 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
255 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
256 return ioerr(db), -1;
263 return ioerr(db), -1;
267 * makroom - make room by splitting the overfull page
268 * this routine will attempt to make room for SPLTMAX times before
272 makroom(register DBM *db, long int hash, int need)
276 char *pag = db->pagbuf;
278 register int smax = SPLTMAX;
282 * split the current page
284 (void) splpage(pag, New, db->hmask + 1);
286 * address of the new page
288 newp = (hash & db->hmask) | (db->hmask + 1);
291 * write delay, read avoidence/cache shuffle:
292 * select the page for incoming pair: if key is to go to the new page,
293 * write out the previous one, and copy the new one over, thus making
294 * it the current page. If not, simply write the new page, and we are
295 * still looking at the page of interest. current page is not updated
296 * here, as sdbm_store will do so, after it inserts the incoming pair.
298 if (hash & (db->hmask + 1)) {
299 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
300 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
303 (void) memcpy(pag, New, PBLKSIZ);
305 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
306 || write(db->pagf, New, PBLKSIZ) < 0)
309 if (!setdbit(db, db->curbit))
312 * see if we have enough room now
314 if (fitpair(pag, need))
317 * try again... update curbit and hmask as getpage would have
318 * done. because of our update of the current page, we do not
319 * need to read in anything. BUT we have to write the current
320 * [deferred] page out, as the window of failure is too great.
322 db->curbit = 2 * db->curbit +
323 ((hash & (db->hmask + 1)) ? 2 : 1);
324 db->hmask |= db->hmask + 1;
326 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
327 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
332 * if we are here, this is real bad news. After SPLTMAX splits,
333 * we still cannot fit the key. say goodnight.
336 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
343 * the following two routines will break if
344 * deletions aren't taken into account. (ndbm bug)
347 sdbm_firstkey(register DBM *db)
350 return errno = EINVAL, nullitem;
354 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
355 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
356 return ioerr(db), nullitem;
365 sdbm_nextkey(register DBM *db)
368 return errno = EINVAL, nullitem;
373 * all important binary trie traversal
376 getpage(register DBM *db, register long int hash)
384 while (dbit < db->maxbno && getdbit(db, dbit))
385 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
387 debug(("dbit: %d...", dbit));
390 db->hmask = masks[hbit];
392 pagb = hash & db->hmask;
394 * see if the block we need is already in memory.
395 * note: this lookaside cache has about 10% hit rate.
397 if (pagb != db->pagbno) {
399 * note: here, we assume a "hole" is read as 0s.
400 * if not, must zero pagbuf first.
402 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
403 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
405 if (!chkpage(db->pagbuf))
409 debug(("pag read: %d\n", pagb));
415 getdbit(register DBM *db, register long int dbit)
423 if (dirb != db->dirbno) {
424 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
425 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
429 debug(("dir read: %d\n", dirb));
432 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
436 setdbit(register DBM *db, register long int dbit)
444 if (dirb != db->dirbno) {
445 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
446 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
450 debug(("dir read: %d\n", dirb));
453 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
455 if (dbit >= db->maxbno)
456 db->maxbno += DBLKSIZ * BYTESIZ;
458 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
459 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
466 * getnext - get the next key in the page, and if done with
467 * the page, try the next page in sequence
470 getnext(register DBM *db)
476 key = getnkey(db->pagbuf, db->keyptr);
477 if (key.dptr != NULL)
480 * we either run out, or there is nothing on this page..
481 * try the next one... If we lost our position on the
482 * file, we will have to seek.
485 if (db->pagbno != db->blkptr++)
486 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
488 db->pagbno = db->blkptr;
489 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
491 if (!chkpage(db->pagbuf))
495 return ioerr(db), nullitem;