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.
20 # include <sys/file.h>
37 extern Malloc_t malloc proto((MEM_SIZE));
38 extern Free_t free proto((Malloc_t));
39 extern Off_t lseek(int, Off_t, int);
45 static int getdbit proto((DBM *, long));
46 static int setdbit proto((DBM *, long));
47 static int getpage proto((DBM *, long));
48 static datum getnext proto((DBM *));
49 static int makroom proto((DBM *, long, int));
54 #define bad(x) ((x).dptr == NULL || (x).dsize < 0)
55 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
56 #define ioerr(db) ((db)->flags |= DBM_IOERR)
58 #define OFF_PAG(off) (long) (off) * PBLKSIZ
59 #define OFF_DIR(off) (long) (off) * DBLKSIZ
61 static long masks[] = {
62 000000000000, 000000000001, 000000000003, 000000000007,
63 000000000017, 000000000037, 000000000077, 000000000177,
64 000000000377, 000000000777, 000000001777, 000000003777,
65 000000007777, 000000017777, 000000037777, 000000077777,
66 000000177777, 000000377777, 000000777777, 000001777777,
67 000003777777, 000007777777, 000017777777, 000037777777,
68 000077777777, 000177777777, 000377777777, 000777777777,
69 001777777777, 003777777777, 007777777777, 017777777777
73 sdbm_open(register char *file, register int flags, register int mode)
76 register char *dirname;
77 register char *pagname;
80 if (file == NULL || !*file)
81 return errno = EINVAL, (DBM *) NULL;
83 * need space for two seperate filenames
85 n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
87 if ((dirname = (char *) malloc((unsigned) n)) == NULL)
88 return errno = ENOMEM, (DBM *) NULL;
90 * build the file names
92 dirname = strcat(strcpy(dirname, file), DIRFEXT);
93 pagname = strcpy(dirname + strlen(dirname) + 1, file);
94 pagname = strcat(pagname, PAGFEXT);
96 db = sdbm_prep(dirname, pagname, flags, mode);
97 free((char *) dirname);
102 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
107 if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
108 return errno = ENOMEM, (DBM *) NULL;
115 * adjust user flags so that WRONLY becomes RDWR,
116 * as required by this package. Also set our internal
117 * flag for RDONLY if needed.
119 if (flags & O_WRONLY)
120 flags = (flags & ~O_WRONLY) | O_RDWR;
122 else if ((flags & 03) == O_RDONLY)
123 db->flags = DBM_RDONLY;
125 * open the files in sequence, and stat the dirfile.
126 * If we fail anywhere, undo everything, return NULL.
128 #if defined(OS2) || defined(MSDOS) || defined(WIN32)
131 if ((db->pagf = open(pagname, flags, mode)) > -1) {
132 if ((db->dirf = open(dirname, flags, mode)) > -1) {
134 * need the dirfile size to establish max bit number.
136 if (fstat(db->dirf, &dstat) == 0) {
138 * zero size: either a fresh database, or one with a single,
139 * unsplit data page: dirpage is all zeros.
141 db->dirbno = (!dstat.st_size) ? 0 : -1;
143 db->maxbno = dstat.st_size * BYTESIZ;
145 (void) memset(db->pagbuf, 0, PBLKSIZ);
146 (void) memset(db->dirbuf, 0, DBLKSIZ);
152 (void) close(db->dirf);
154 (void) close(db->pagf);
161 sdbm_close(register DBM *db)
166 (void) close(db->dirf);
167 (void) close(db->pagf);
173 sdbm_fetch(register DBM *db, datum key)
175 if (db == NULL || bad(key))
176 return errno = EINVAL, nullitem;
178 if (getpage(db, exhash(key)))
179 return getpair(db->pagbuf, key);
181 return ioerr(db), nullitem;
185 sdbm_delete(register DBM *db, datum key)
187 if (db == NULL || bad(key))
188 return errno = EINVAL, -1;
190 return errno = EPERM, -1;
192 if (getpage(db, exhash(key))) {
193 if (!delpair(db->pagbuf, key))
196 * update the page file
198 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
199 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
200 return ioerr(db), -1;
205 return ioerr(db), -1;
209 sdbm_store(register DBM *db, datum key, datum val, int flags)
214 if (db == NULL || bad(key))
215 return errno = EINVAL, -1;
217 return errno = EPERM, -1;
219 need = key.dsize + val.dsize;
221 * is the pair too big (or too small) for this database ??
223 if (need < 0 || need > PAIRMAX)
224 return errno = EINVAL, -1;
226 if (getpage(db, (hash = exhash(key)))) {
228 * if we need to replace, delete the key/data pair
229 * first. If it is not there, ignore.
231 if (flags == DBM_REPLACE)
232 (void) delpair(db->pagbuf, key);
234 else if (duppair(db->pagbuf, key))
238 * if we do not have enough room, we have to split.
240 if (!fitpair(db->pagbuf, need))
241 if (!makroom(db, hash, need))
242 return ioerr(db), -1;
244 * we have enough room or split is successful. insert the key,
245 * and update the page file.
247 (void) putpair(db->pagbuf, key, val);
249 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
250 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
251 return ioerr(db), -1;
258 return ioerr(db), -1;
262 * makroom - make room by splitting the overfull page
263 * this routine will attempt to make room for SPLTMAX times before
267 makroom(register DBM *db, long int hash, int need)
271 char *pag = db->pagbuf;
273 register int smax = SPLTMAX;
277 * split the current page
279 (void) splpage(pag, New, db->hmask + 1);
281 * address of the new page
283 newp = (hash & db->hmask) | (db->hmask + 1);
286 * write delay, read avoidence/cache shuffle:
287 * select the page for incoming pair: if key is to go to the new page,
288 * write out the previous one, and copy the new one over, thus making
289 * it the current page. If not, simply write the new page, and we are
290 * still looking at the page of interest. current page is not updated
291 * here, as sdbm_store will do so, after it inserts the incoming pair.
293 if (hash & (db->hmask + 1)) {
294 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
295 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
298 (void) memcpy(pag, New, PBLKSIZ);
300 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
301 || write(db->pagf, New, PBLKSIZ) < 0)
304 if (!setdbit(db, db->curbit))
307 * see if we have enough room now
309 if (fitpair(pag, need))
312 * try again... update curbit and hmask as getpage would have
313 * done. because of our update of the current page, we do not
314 * need to read in anything. BUT we have to write the current
315 * [deferred] page out, as the window of failure is too great.
317 db->curbit = 2 * db->curbit +
318 ((hash & (db->hmask + 1)) ? 2 : 1);
319 db->hmask |= db->hmask + 1;
321 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
322 || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
327 * if we are here, this is real bad news. After SPLTMAX splits,
328 * we still cannot fit the key. say goodnight.
331 (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
338 * the following two routines will break if
339 * deletions aren't taken into account. (ndbm bug)
342 sdbm_firstkey(register DBM *db)
345 return errno = EINVAL, nullitem;
349 if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
350 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
351 return ioerr(db), nullitem;
360 sdbm_nextkey(register DBM *db)
363 return errno = EINVAL, nullitem;
368 * all important binary trie traversal
371 getpage(register DBM *db, register long int hash)
379 while (dbit < db->maxbno && getdbit(db, dbit))
380 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
382 debug(("dbit: %d...", dbit));
385 db->hmask = masks[hbit];
387 pagb = hash & db->hmask;
389 * see if the block we need is already in memory.
390 * note: this lookaside cache has about 10% hit rate.
392 if (pagb != db->pagbno) {
394 * note: here, we assume a "hole" is read as 0s.
395 * if not, must zero pagbuf first.
397 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
398 || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
400 if (!chkpage(db->pagbuf))
404 debug(("pag read: %d\n", pagb));
410 getdbit(register DBM *db, register long int dbit)
418 if (dirb != db->dirbno) {
419 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
420 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
424 debug(("dir read: %d\n", dirb));
427 return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
431 setdbit(register DBM *db, register long int dbit)
439 if (dirb != db->dirbno) {
440 (void) memset(db->dirbuf, 0, DBLKSIZ);
441 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
442 || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
446 debug(("dir read: %d\n", dirb));
449 db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
451 if (dbit >= db->maxbno)
452 db->maxbno += DBLKSIZ * BYTESIZ;
454 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
455 || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
462 * getnext - get the next key in the page, and if done with
463 * the page, try the next page in sequence
466 getnext(register DBM *db)
472 key = getnkey(db->pagbuf, db->keyptr);
473 if (key.dptr != NULL)
476 * we either run out, or there is nothing on this page..
477 * try the next one... If we lost our position on the
478 * file, we will have to seek.
481 if (db->pagbno != db->blkptr++)
482 if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
484 db->pagbno = db->blkptr;
485 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
487 if (!chkpage(db->pagbuf))
491 return ioerr(db), nullitem;