2 * Sherlock Library -- SDBM emulator at top of GDBM
4 * (c) 1999 Martin Mares <mj@ucw.cz>
22 sdbm_open(struct sdbm_options *o)
24 struct sdbm *d = xmalloc(sizeof(struct sdbm));
25 d->db = gdbm_open(o->name,
26 (o->page_order ? (1 << o->page_order) : 0),
27 ((o->flags & SDBM_WRITE) ? ((o->flags & SDBM_CREAT) ? GDBM_WRCREAT : GDBM_WRITER) : GDBM_READER)
28 | ((o->flags & SDBM_SYNC) ? GDBM_SYNC : 0),
32 gdbm_setopt(d->db, GDBM_CACHESIZE, &o->cache_size, sizeof(o->cache_size));
33 d->prevkey.dptr = NULL;
38 sdbm_close(struct sdbm *d)
46 sdbm_put_user(byte *D, uns Dl, byte *val, uns *vallen)
60 sdbm_store(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
69 rc = gdbm_store(d->db, K, V, GDBM_INSERT);
70 return (rc < 0) ? rc : !rc;
74 sdbm_replace(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
80 return sdbm_delete(d, key, keylen);
85 rc = gdbm_store(d->db, K, V, GDBM_REPLACE);
86 return (rc < 0) ? rc : !rc;
90 sdbm_delete(struct sdbm *d, byte *key, uns keylen)
96 return !gdbm_delete(d->db, K);
100 sdbm_fetch(struct sdbm *d, byte *key, uns keylen, byte *val, uns *vallen)
108 return gdbm_exists(d->db, K);
109 V = gdbm_fetch(d->db, K);
112 rc = sdbm_put_user(V.dptr, V.dsize, val, vallen);
114 return rc ? SDBM_ERROR_TOO_LARGE : 1;
118 sdbm_rewind(struct sdbm *d)
122 free(d->prevkey.dptr);
123 d->prevkey.dptr = NULL;
128 sdbm_get_next(struct sdbm *d, byte *key, uns *keylen, byte *val, uns *vallen)
134 K = gdbm_nextkey(d->db, d->prevkey);
135 free(d->prevkey.dptr);
138 K = gdbm_firstkey(d->db);
142 if (sdbm_put_user(K.dptr, K.dsize, key, keylen))
143 return SDBM_ERROR_TOO_LARGE;
145 return sdbm_fetch(d, key, *keylen, val, vallen);
150 sdbm_sync(struct sdbm *d)