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.
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
76 sdbm_open(register char *file, register int flags, register int mode)
79 register char *dirname;
80 register char *pagname;
83 if (file == NULL || !*file)
84 return errno = EINVAL, (DBM *) NULL;
86 * need space for two seperate filenames
88 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
90 if ((dirname = (char *) malloc((unsigned) n)) == NULL)
91 return errno = ENOMEM, (DBM *) NULL;
93 * build the file names
95 dirname = strcat(strcpy(dirname, file), DIRFEXT);
96 pagname = strcpy(dirname + strlen(dirname) + 1, file);
97 pagname = strcat(pagname, PAGFEXT);
99 db = sdbm_prep(dirname, pagname, flags, mode);
100 free((char *) dirname);
105 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
110 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
111 return errno = ENOMEM, (DBM *) NULL;
118 * adjust user flags so that WRONLY becomes RDWR,
119 * as required by this package. Also set our internal
120 * flag for RDONLY if needed.
122 if (flags & O_WRONLY)
123 flags = (flags & ~O_WRONLY) | O_RDWR;
125 else if ((flags & 03) == O_RDONLY)
126 db->flags = DBM_RDONLY;
128 * open the files in sequence, and stat the dirfile.
129 * If we fail anywhere, undo everything, return NULL.
131 #if defined(OS2) || defined(MSDOS) || defined(WIN32) || defined(CYGWIN)
134 if ((db->pagf = open(pagname, flags, mode)) > -1) {
135 if ((db->dirf = open(dirname, flags, mode)) > -1) {
137 * need the dirfile size to establish max bit number.
139 if (fstat(db->dirf, &dstat) == 0) {
141 * zero size: either a fresh database, or one with a single,
142 * unsplit data page: dirpage is all zeros.
144 db->dirbno = (!dstat.st_size) ? 0 : -1;
146 db->maxbno = dstat.st_size * BYTESIZ;
148 (void) memset(db->pagbuf, 0, PBLKSIZ);
149 (void) memset(db->dirbuf, 0, DBLKSIZ);
155 (void) close(db->dirf);
157 (void) close(db->pagf);
164 sdbm_close(register DBM *db)
169 (void) close(db->dirf);
170 (void) close(db->pagf);
176 sdbm_fetch(register DBM *db, datum key)
178 if (db == NULL || bad(key))
179 return errno = EINVAL, nullitem;
181 if (getpage(db, exhash(key)))
182 return getpair(db->pagbuf, key);
184 return ioerr(db), nullitem;
188 sdbm_exists(register DBM *db, datum key)
190 if (db == NULL || bad(key))
191 return errno = EINVAL, -1;
193 if (getpage(db, exhash(key)))
194 return exipair(db->pagbuf, key);
196 return ioerr(db), -1;
200 sdbm_delete(register DBM *db, datum key)
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(register DBM *db, datum key, datum val, int flags)
229 if (db == NULL || bad(key))
230 return errno = EINVAL, -1;
232 return errno = EPERM, -1;
234 need = key.dsize + val.dsize;
236 * is the pair too big (or too small) for this database ??
238 if (need < 0 || need > PAIRMAX)
239 return errno = EINVAL, -1;
241 if (getpage(db, (hash = exhash(key)))) {
243 * if we need to replace, delete the key/data pair
244 * first. If it is not there, ignore.
246 if (flags == DBM_REPLACE)
247 (void) delpair(db->pagbuf, key);
249 else if (duppair(db->pagbuf, key))
253 * if we do not have enough room, we have to split.
255 if (!fitpair(db->pagbuf, need))
256 if (!makroom(db, hash, need))
257 return ioerr(db), -1;
259 * we have enough room or split is successful. insert the key,
260 * and update the page file.
262 (void) putpair(db->pagbuf, key, val);
264 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
265 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
266 return ioerr(db), -1;
273 return ioerr(db), -1;
277 * makroom - make room by splitting the overfull page
278 * this routine will attempt to make room for SPLTMAX times before
282 makroom(register DBM *db, long int hash, int need)
286 char *pag = db->pagbuf;
288 register int smax = SPLTMAX;
292 * split the current page
294 (void) splpage(pag, New, db->hmask + 1);
296 * address of the new page
298 newp = (hash & db->hmask) | (db->hmask + 1);
301 * write delay, read avoidence/cache shuffle:
302 * select the page for incoming pair: if key is to go to the new page,
303 * write out the previous one, and copy the new one over, thus making
304 * it the current page. If not, simply write the new page, and we are
305 * still looking at the page of interest. current page is not updated
306 * here, as sdbm_store will do so, after it inserts the incoming pair.
308 if (hash & (db->hmask + 1)) {
309 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
310 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
313 (void) memcpy(pag, New, PBLKSIZ);
315 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
316 || write(db->pagf, New, PBLKSIZ) < 0)
319 if (!setdbit(db, db->curbit))
322 * see if we have enough room now
324 if (fitpair(pag, need))
327 * try again... update curbit and hmask as getpage would have
328 * done. because of our update of the current page, we do not
329 * need to read in anything. BUT we have to write the current
330 * [deferred] page out, as the window of failure is too great.
332 db->curbit = 2 * db->curbit +
333 ((hash & (db->hmask + 1)) ? 2 : 1);
334 db->hmask |= db->hmask + 1;
336 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
337 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
342 * if we are here, this is real bad news. After SPLTMAX splits,
343 * we still cannot fit the key. say goodnight.
346 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
353 * the following two routines will break if
354 * deletions aren't taken into account. (ndbm bug)
357 sdbm_firstkey(register DBM *db)
360 return errno = EINVAL, nullitem;
364 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
365 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
366 return ioerr(db), nullitem;
375 sdbm_nextkey(register DBM *db)
378 return errno = EINVAL, nullitem;
383 * all important binary trie traversal
386 getpage(register DBM *db, register long int hash)
394 while (dbit < db->maxbno && getdbit(db, dbit))
395 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
397 debug(("dbit: %d...", dbit));
400 db->hmask = masks[hbit];
402 pagb = hash & db->hmask;
404 * see if the block we need is already in memory.
405 * note: this lookaside cache has about 10% hit rate.
407 if (pagb != db->pagbno) {
409 * note: here, we assume a "hole" is read as 0s.
410 * if not, must zero pagbuf first.
412 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
413 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
415 if (!chkpage(db->pagbuf))
419 debug(("pag read: %d\n", pagb));
425 getdbit(register DBM *db, register long int dbit)
433 if (dirb != db->dirbno) {
434 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
435 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
439 debug(("dir read: %d\n", dirb));
442 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
446 setdbit(register DBM *db, register long int dbit)
454 if (dirb != db->dirbno) {
455 (void) memset(db->dirbuf, 0, DBLKSIZ);
456 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
457 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
461 debug(("dir read: %d\n", dirb));
464 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
466 if (dbit >= db->maxbno)
467 db->maxbno += DBLKSIZ * BYTESIZ;
469 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
470 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
477 * getnext - get the next key in the page, and if done with
478 * the page, try the next page in sequence
481 getnext(register DBM *db)
487 key = getnkey(db->pagbuf, db->keyptr);
488 if (key.dptr != NULL)
491 * we either run out, or there is nothing on this page..
492 * try the next one... If we lost our position on the
493 * file, we will have to seek.
496 if (db->pagbno != db->blkptr++)
497 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
499 db->pagbno = db->blkptr;
500 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
502 if (!chkpage(db->pagbuf))
506 return ioerr(db), nullitem;