2 * Sherlock Library -- Fast Database Management Routines
4 * (c) 1999--2001 Martin Mares <mj@ucw.cz>
8 * This library uses the standard algorithm for external hashing (page directory
9 * mapping topmost K bits of hash value to page address, directory splits and
10 * so on). Peculiarities of this implementation (aka design decisions):
12 * o We allow both fixed and variable length keys and values (this includes
13 * zero size values for cases you want to represent only a set of keys).
14 * o We assume that key_size + val_size < page_size.
15 * o We never shrink the directory nor free empty pages. (The reason is that
16 * if the database was once large, it's likely it will again become large soon.)
17 * o The only pages which can be freed are those of the directory (during
18 * directory split), so we keep only a simple 32-entry free block list
19 * and we assume it's sorted.
20 * o All pointers are always given in pages from start of the file.
21 * This gives us page_size*2^32 limit for file size which should be enough.
26 #include "lib/pagecache.h"
28 #include "lib/db_internal.h"
36 #define GET_PAGE(d,x) pgc_get((d)->cache, (d)->fd, ((sh_off_t)(x)) << (d)->page_order)
37 #define GET_ZERO_PAGE(d,x) pgc_get_zero((d)->cache, (d)->fd, ((sh_off_t)(x)) << (d)->page_order)
38 #define READ_PAGE(d,x) pgc_read((d)->cache, (d)->fd, ((sh_off_t)(x)) << (d)->page_order)
39 #define READ_DIR(d,off) pgc_read((d)->cache, (d)->fd, (((sh_off_t)(d)->root->dir_start) << (d)->page_order) + (off))
42 sdbm_open(struct sdbm_options *o)
45 struct sdbm_root root, *r;
46 uns cache_size = o->cache_size ? o->cache_size : 16;
48 d = xmalloc_zero(sizeof(struct sdbm));
50 d->fd = sh_open(o->name, ((d->flags & SDBM_WRITE) ? O_RDWR : O_RDONLY), 0666);
51 if (d->fd >= 0) /* Already exists, let's check it */
53 if (read(d->fd, &root, sizeof(root)) != sizeof(root))
55 if (root.magic != SDBM_MAGIC || root.version != SDBM_VERSION)
57 d->file_size = sh_seek(d->fd, 0, SEEK_END) >> root.page_order;
58 d->page_order = root.page_order;
59 d->page_size = 1 << root.page_order;
60 d->cache = pgc_open(d->page_size, cache_size);
61 d->root_page = pgc_read(d->cache, d->fd, 0);
62 d->root = (void *) d->root_page->data;
64 else if ((d->flags & SDBM_CREAT) && (d->fd = sh_open(o->name, O_RDWR | O_CREAT, 0666)) >= 0)
67 uns page_order = o->page_order;
70 d->page_size = 1 << page_order;
71 d->cache = pgc_open(d->page_size, cache_size);
72 d->root_page = GET_ZERO_PAGE(d, 0);
73 r = d->root = (void *) d->root_page->data; /* Build root page */
74 r->magic = SDBM_MAGIC;
75 r->version = SDBM_VERSION;
76 r->page_order = d->page_order = page_order;
77 r->key_size = o->key_size;
78 r->val_size = o->val_size;
82 q = GET_ZERO_PAGE(d, 1); /* Build page directory */
83 GET32(q->data, 0) = 2;
85 q = GET_ZERO_PAGE(d, 2); /* Build single data page */
90 d->dir_size = 1 << d->root->dir_order;
91 d->dir_shift = 32 - d->root->dir_order;
92 d->page_mask = d->page_size - 1;
93 d->key_size = d->root->key_size;
94 d->val_size = d->root->val_size;
103 sdbm_close(struct sdbm *d)
106 pgc_put(d->cache, d->root_page);
115 sdbm_alloc_pages(struct sdbm *d, uns number)
117 uns where = d->file_size;
118 if (where + number < where) /* Wrap around? */
119 die("SDB: Database file too large, giving up");
120 d->file_size += number;
125 sdbm_alloc_page(struct sdbm *d)
129 if (!d->root->free_pool[0].count)
130 return sdbm_alloc_pages(d, 1);
131 pos = d->root->free_pool[0].first;
132 d->root->free_pool[0].first++;
133 if (!--d->root->free_pool[0].count)
135 memmove(d->root->free_pool, d->root->free_pool+1, (SDBM_NUM_FREE_PAGE_POOLS-1) * sizeof(d->root->free_pool[0]));
136 d->root->free_pool[SDBM_NUM_FREE_PAGE_POOLS-1].count = 0;
138 pgc_mark_dirty(d->cache, d->root_page);
143 sdbm_free_pages(struct sdbm *d, uns start, uns number)
147 while (d->root->free_pool[i].count)
149 ASSERT(i < SDBM_NUM_FREE_PAGE_POOLS);
150 d->root->free_pool[i].first = start;
151 d->root->free_pool[i].count = number;
152 pgc_mark_dirty(d->cache, d->root_page);
156 sdbm_hash(byte *key, uns keylen)
159 * This used to be the same hash function as GDBM uses,
160 * but it turned out that it tends to give the same results
161 * on similar keys. Damn it.
163 u32 value = 0x238F13AF * keylen;
165 value = 37*value + *key++;
166 return (1103515243 * value + 12345);
170 sdbm_get_entry(struct sdbm *d, byte *pos, byte **key, uns *keylen, byte **val, uns *vallen)
174 if (d->key_size >= 0)
175 *keylen = d->key_size;
178 *keylen = (p[0] << 8) | p[1];
183 if (d->val_size >= 0)
184 *vallen = d->val_size;
187 *vallen = (p[0] << 8) | p[1];
196 sdbm_entry_len(struct sdbm *d, uns keylen, uns vallen)
198 uns len = keylen + vallen;
207 sdbm_store_entry(struct sdbm *d, byte *pos, byte *key, uns keylen, byte *val, uns vallen)
211 *pos++ = keylen >> 8;
214 memmove(pos, key, keylen);
218 *pos++ = vallen >> 8;
221 memmove(pos, val, vallen);
225 sdbm_page_rank(struct sdbm *d, uns dirpos)
230 uns pm = d->page_mask;
232 b = READ_DIR(d, dirpos & ~pm);
233 pg = GET32(b->data, dirpos & pm);
235 while ((l & pm) && GET32(b->data, (l - 4) & pm) == pg)
238 /* We heavily depend on unused directory entries being zero */
239 while ((r & pm) && GET32(b->data, r & pm) == pg)
241 pgc_put(d->cache, b);
243 if (!(l & pm) && !(r & pm))
245 /* Note that if it spans page boundary, it must contain an integer number of pages */
248 b = READ_DIR(d, (l - 4) & ~pm);
249 x = GET32(b->data, 0);
250 pgc_put(d->cache, b);
255 while (r < 4*d->dir_size)
257 b = READ_DIR(d, r & ~pm);
258 x = GET32(b->data, 0);
259 pgc_put(d->cache, b);
269 sdbm_expand_directory(struct sdbm *d)
275 if (d->root->dir_order >= 31)
276 die("SDB: Database directory too large, giving up");
278 if (4*d->dir_size < d->page_size)
280 /* It still fits within single page */
282 dir = (u32 *) b->data;
283 for(i=d->dir_size-1; i>=0; i--)
284 dir[2*i] = dir[2*i+1] = dir[i];
285 pgc_mark_dirty(d->cache, b);
286 pgc_put(d->cache, b);
290 uns old_dir = d->root->dir_start;
291 uns old_dir_pages = 1 << (d->root->dir_order + 2 - d->page_order);
293 new_dir = d->root->dir_start = sdbm_alloc_pages(d, 2*old_dir_pages);
294 ent = 1 << (d->page_order - 3);
295 for(page=0; page < old_dir_pages; page++)
297 b = READ_PAGE(d, old_dir + page);
298 dir = (u32 *) b->data;
299 c = GET_PAGE(d, new_dir + 2*page);
302 t[2*i] = t[2*i+1] = dir[i];
303 pgc_put(d->cache, c);
304 c = GET_PAGE(d, new_dir + 2*page + 1);
307 t[2*i] = t[2*i+1] = dir[ent+i];
308 pgc_put(d->cache, c);
309 pgc_put(d->cache, b);
311 if (!(d->flags & SDBM_FAST))
314 * Unless in super-fast mode, fill old directory pages with zeroes.
315 * This slows us down a bit, but allows database reconstruction after
316 * the free list is lost.
318 for(page=0; page < old_dir_pages; page++)
320 b = GET_ZERO_PAGE(d, old_dir + page);
321 pgc_put(d->cache, b);
324 sdbm_free_pages(d, old_dir, old_dir_pages);
327 d->root->dir_order++;
328 d->dir_size = 1 << d->root->dir_order;
329 d->dir_shift = 32 - d->root->dir_order;
330 pgc_mark_dirty(d->cache, d->root_page);
331 if (!(d->flags & SDBM_FAST))
336 sdbm_split_data(struct sdbm *d, struct sdbm_bucket *s, struct sdbm_bucket *d0, struct sdbm_bucket *d1, uns sigbit)
339 byte *dp[2] = { d0->data, d1->data };
343 while (sp < s->data + s->used)
345 sz = sdbm_get_entry(d, sp, &K, &Kl, &D, &Dl);
347 i = (sdbm_hash(K, Kl) & (1 << sigbit)) ? 1 : 0;
348 sdbm_store_entry(d, dp[i], K, Kl, D, Dl);
351 d0->used = dp[0] - d0->data;
352 d1->used = dp[1] - d1->data;
356 sdbm_split_dir(struct sdbm *d, uns dirpos, uns count, uns pos)
364 b = READ_DIR(d, dirpos & ~d->page_mask);
365 i = d->page_size - (dirpos & d->page_mask);
371 GET32(b->data, dirpos & d->page_mask) = pos;
375 pgc_mark_dirty(d->cache, b);
376 pgc_put(d->cache, b);
381 sdbm_dirpos(struct sdbm *d, uns hash)
383 if (d->dir_shift != 32) /* avoid shifting by 32 bits */
384 return (hash >> d->dir_shift) << 2; /* offset in the directory */
390 sdbm_split_page(struct sdbm *d, struct page *b, u32 hash)
393 uns i, rank, sigbit, rank_log, dirpos, newpg;
395 dirpos = sdbm_dirpos(d, hash);
396 rank = sdbm_page_rank(d, dirpos); /* rank = # of pointers to this page */
399 sdbm_expand_directory(d);
403 rank_log = 1; /* rank_log = log2(rank) */
404 while ((1U << rank_log) < rank)
406 sigbit = d->dir_shift + rank_log - 1; /* sigbit = bit we split on */
408 newpg = sdbm_alloc_page(d);
409 p[1] = GET_PAGE(d, newpg);
410 sdbm_split_data(d, (void *) b->data, (void *) p[0]->data, (void *) p[1]->data, sigbit);
411 sdbm_split_dir(d, (dirpos & ~(4*rank - 1))+2*rank, rank/2, newpg);
412 pgc_mark_dirty(d->cache, p[0]);
413 i = (hash & (1 << sigbit)) ? 1 : 0;
414 pgc_put(d->cache, p[!i]);
419 sdbm_put_user(byte *D, uns Dl, byte *val, uns *vallen)
433 sdbm_access(struct sdbm *d, byte *key, uns keylen, byte *val, uns *vallen, uns mode) /* 0=read, 1=store, 2=replace */
436 u32 hash, h, pos, size;
437 struct sdbm_bucket *b;
441 if ((d->key_size >= 0 && keylen != (uns) d->key_size) || keylen > 65535)
442 return SDBM_ERROR_BAD_KEY_SIZE;
443 if (val && ((d->val_size >= 0 && *vallen != (uns) d->val_size) || *vallen >= 65535) && mode)
444 return SDBM_ERROR_BAD_VAL_SIZE;
445 if (!mode && !(d->flags & SDBM_WRITE))
446 return SDBM_ERROR_READ_ONLY;
447 hash = sdbm_hash(key, keylen);
448 h = sdbm_dirpos(d, hash);
449 p = READ_DIR(d, h & ~d->page_mask);
450 pos = GET32(p->data, h & d->page_mask);
451 pgc_put(d->cache, p);
452 q = READ_PAGE(d, pos);
453 b = (void *) q->data;
460 s = sdbm_get_entry(d, c, &K, &Kl, &D, &Dl);
461 if (Kl == keylen && !memcmp(K, key, Kl))
466 case 0: /* fetch: found */
467 rc = sdbm_put_user(D, Dl, val, vallen);
468 pgc_put(d->cache, q);
469 return rc ? SDBM_ERROR_TOO_LARGE : 1;
470 case 1: /* store: already present */
471 pgc_put(d->cache, q);
473 default: /* replace: delete the old one */
474 memmove(c, c+s, e-(c+s));
481 if (!mode || !val) /* fetch or delete: no success */
483 pgc_put(d->cache, q);
490 size = sdbm_entry_len(d, keylen, *vallen);
491 while (b->used + size > d->page_size - sizeof(struct sdbm_bucket))
493 /* Page overflow, need to split */
494 if (size >= d->page_size - sizeof(struct sdbm_bucket))
496 pgc_put(d->cache, q);
497 return SDBM_ERROR_GIANT;
499 q = sdbm_split_page(d, q, hash);
500 b = (void *) q->data;
502 sdbm_store_entry(d, b->data + b->used, key, keylen, val, *vallen);
505 pgc_mark_dirty(d->cache, q);
506 pgc_put(d->cache, q);
507 if (d->flags & SDBM_SYNC)
513 sdbm_store(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
515 return sdbm_access(d, key, keylen, val, &vallen, 1);
519 sdbm_replace(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
521 return sdbm_access(d, key, keylen, val, &vallen, 2);
525 sdbm_delete(struct sdbm *d, byte *key, uns keylen)
527 return sdbm_access(d, key, keylen, NULL, NULL, 2);
531 sdbm_fetch(struct sdbm *d, byte *key, uns keylen, byte *val, uns *vallen)
533 return sdbm_access(d, key, keylen, val, vallen, 0);
537 sdbm_rewind(struct sdbm *d)
541 d->find_free_list = 0;
545 sdbm_get_next(struct sdbm *d, byte *key, uns *keylen, byte *val, uns *vallen)
547 uns page = d->find_page;
548 uns pos = d->find_pos;
552 struct sdbm_bucket *b;
558 if (page >= d->file_size)
560 if (page == d->root->dir_start)
561 page += (4*d->dir_size + d->page_size - 1) >> d->page_order;
562 else if (page == d->root->free_pool[d->find_free_list].first)
563 page += d->root->free_pool[d->find_free_list++].count;
568 p = READ_PAGE(d, page);
569 b = (void *) p->data;
570 if (pos - 4 >= b->used)
574 pgc_put(d->cache, p);
577 c = sdbm_get_entry(d, p->data + pos, &K, &Kl, &V, &Vl);
579 d->find_pos = pos + c;
580 c = sdbm_put_user(K, Kl, key, keylen) ||
581 sdbm_put_user(V, Vl, val, vallen);
582 pgc_put(d->cache, p);
583 return c ? SDBM_ERROR_TOO_LARGE : 1;
591 sdbm_sync(struct sdbm *d)
594 if (d->flags & SDBM_FSYNC)