2 * UCW Library -- Fast Database Management Routines
4 * (c) 1999--2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 * This library uses the standard algorithm for external hashing (page directory
12 * mapping topmost K bits of hash value to page address, directory splits and
13 * so on). Peculiarities of this implementation (aka design decisions):
15 * o We allow both fixed and variable length keys and values (this includes
16 * zero size values for cases you want to represent only a set of keys).
17 * o We assume that key_size + val_size < page_size.
18 * o We never shrink the directory nor free empty pages. (The reason is that
19 * if the database was once large, it's likely it will again become large soon.)
20 * o The only pages which can be freed are those of the directory (during
21 * directory split), so we keep only a simple 32-entry free block list
22 * and we assume it's sorted.
23 * o All pointers are always given in pages from start of the file.
24 * This gives us page_size*2^32 limit for file size which should be enough.
29 #include "lib/pagecache.h"
31 #include "lib/db_internal.h"
39 #define GET_PAGE(d,x) pgc_get((d)->cache, (d)->fd, ((sh_off_t)(x)) << (d)->page_order)
40 #define GET_ZERO_PAGE(d,x) pgc_get_zero((d)->cache, (d)->fd, ((sh_off_t)(x)) << (d)->page_order)
41 #define READ_PAGE(d,x) pgc_read((d)->cache, (d)->fd, ((sh_off_t)(x)) << (d)->page_order)
42 #define READ_DIR(d,off) pgc_read((d)->cache, (d)->fd, (((sh_off_t)(d)->root->dir_start) << (d)->page_order) + (off))
45 sdbm_open(struct sdbm_options *o)
48 struct sdbm_root root, *r;
49 uns cache_size = o->cache_size ? o->cache_size : 16;
51 d = xmalloc_zero(sizeof(struct sdbm));
53 d->fd = sh_open(o->name, ((d->flags & SDBM_WRITE) ? O_RDWR : O_RDONLY), 0666);
54 if (d->fd >= 0) /* Already exists, let's check it */
56 if (read(d->fd, &root, sizeof(root)) != sizeof(root))
58 if (root.magic != SDBM_MAGIC || root.version != SDBM_VERSION)
60 d->file_size = sh_seek(d->fd, 0, SEEK_END) >> root.page_order;
61 d->page_order = root.page_order;
62 d->page_size = 1 << root.page_order;
63 d->cache = pgc_open(d->page_size, cache_size);
64 d->root_page = pgc_read(d->cache, d->fd, 0);
65 d->root = (void *) d->root_page->data;
67 else if ((d->flags & SDBM_CREAT) && (d->fd = sh_open(o->name, O_RDWR | O_CREAT, 0666)) >= 0)
70 uns page_order = o->page_order;
73 d->page_size = 1 << page_order;
74 d->cache = pgc_open(d->page_size, cache_size);
75 d->root_page = GET_ZERO_PAGE(d, 0);
76 r = d->root = (void *) d->root_page->data; /* Build root page */
77 r->magic = SDBM_MAGIC;
78 r->version = SDBM_VERSION;
79 r->page_order = d->page_order = page_order;
80 r->key_size = o->key_size;
81 r->val_size = o->val_size;
85 q = GET_ZERO_PAGE(d, 1); /* Build page directory */
86 GET32(q->data, 0) = 2;
88 q = GET_ZERO_PAGE(d, 2); /* Build single data page */
93 d->dir_size = 1 << d->root->dir_order;
94 d->dir_shift = 32 - d->root->dir_order;
95 d->page_mask = d->page_size - 1;
96 d->key_size = d->root->key_size;
97 d->val_size = d->root->val_size;
106 sdbm_close(struct sdbm *d)
109 pgc_put(d->cache, d->root_page);
118 sdbm_alloc_pages(struct sdbm *d, uns number)
120 uns where = d->file_size;
121 if (where + number < where) /* Wrap around? */
122 die("SDB: Database file too large, giving up");
123 d->file_size += number;
128 sdbm_alloc_page(struct sdbm *d)
132 if (!d->root->free_pool[0].count)
133 return sdbm_alloc_pages(d, 1);
134 pos = d->root->free_pool[0].first;
135 d->root->free_pool[0].first++;
136 if (!--d->root->free_pool[0].count)
138 memmove(d->root->free_pool, d->root->free_pool+1, (SDBM_NUM_FREE_PAGE_POOLS-1) * sizeof(d->root->free_pool[0]));
139 d->root->free_pool[SDBM_NUM_FREE_PAGE_POOLS-1].count = 0;
141 pgc_mark_dirty(d->cache, d->root_page);
146 sdbm_free_pages(struct sdbm *d, uns start, uns number)
150 while (d->root->free_pool[i].count)
152 ASSERT(i < SDBM_NUM_FREE_PAGE_POOLS);
153 d->root->free_pool[i].first = start;
154 d->root->free_pool[i].count = number;
155 pgc_mark_dirty(d->cache, d->root_page);
159 sdbm_hash(byte *key, uns keylen)
162 * This used to be the same hash function as GDBM uses,
163 * but it turned out that it tends to give the same results
164 * on similar keys. Damn it.
166 u32 value = 0x238F13AF * keylen;
168 value = 37*value + *key++;
169 return (1103515243 * value + 12345);
173 sdbm_get_entry(struct sdbm *d, byte *pos, byte **key, uns *keylen, byte **val, uns *vallen)
177 if (d->key_size >= 0)
178 *keylen = d->key_size;
181 *keylen = (p[0] << 8) | p[1];
186 if (d->val_size >= 0)
187 *vallen = d->val_size;
190 *vallen = (p[0] << 8) | p[1];
199 sdbm_entry_len(struct sdbm *d, uns keylen, uns vallen)
201 uns len = keylen + vallen;
210 sdbm_store_entry(struct sdbm *d, byte *pos, byte *key, uns keylen, byte *val, uns vallen)
214 *pos++ = keylen >> 8;
217 memmove(pos, key, keylen);
221 *pos++ = vallen >> 8;
224 memmove(pos, val, vallen);
228 sdbm_page_rank(struct sdbm *d, uns dirpos)
233 uns pm = d->page_mask;
235 b = READ_DIR(d, dirpos & ~pm);
236 pg = GET32(b->data, dirpos & pm);
238 while ((l & pm) && GET32(b->data, (l - 4) & pm) == pg)
241 /* We heavily depend on unused directory entries being zero */
242 while ((r & pm) && GET32(b->data, r & pm) == pg)
244 pgc_put(d->cache, b);
246 if (!(l & pm) && !(r & pm))
248 /* Note that if it spans page boundary, it must contain an integer number of pages */
251 b = READ_DIR(d, (l - 4) & ~pm);
252 x = GET32(b->data, 0);
253 pgc_put(d->cache, b);
258 while (r < 4*d->dir_size)
260 b = READ_DIR(d, r & ~pm);
261 x = GET32(b->data, 0);
262 pgc_put(d->cache, b);
272 sdbm_expand_directory(struct sdbm *d)
278 if (d->root->dir_order >= 31)
279 die("SDB: Database directory too large, giving up");
281 if (4*d->dir_size < d->page_size)
283 /* It still fits within single page */
285 dir = (u32 *) b->data;
286 for(i=d->dir_size-1; i>=0; i--)
287 dir[2*i] = dir[2*i+1] = dir[i];
288 pgc_mark_dirty(d->cache, b);
289 pgc_put(d->cache, b);
293 uns old_dir = d->root->dir_start;
294 uns old_dir_pages = 1 << (d->root->dir_order + 2 - d->page_order);
296 new_dir = d->root->dir_start = sdbm_alloc_pages(d, 2*old_dir_pages);
297 ent = 1 << (d->page_order - 3);
298 for(page=0; page < old_dir_pages; page++)
300 b = READ_PAGE(d, old_dir + page);
301 dir = (u32 *) b->data;
302 c = GET_PAGE(d, new_dir + 2*page);
305 t[2*i] = t[2*i+1] = dir[i];
306 pgc_put(d->cache, c);
307 c = GET_PAGE(d, new_dir + 2*page + 1);
310 t[2*i] = t[2*i+1] = dir[ent+i];
311 pgc_put(d->cache, c);
312 pgc_put(d->cache, b);
314 if (!(d->flags & SDBM_FAST))
317 * Unless in super-fast mode, fill old directory pages with zeroes.
318 * This slows us down a bit, but allows database reconstruction after
319 * the free list is lost.
321 for(page=0; page < old_dir_pages; page++)
323 b = GET_ZERO_PAGE(d, old_dir + page);
324 pgc_put(d->cache, b);
327 sdbm_free_pages(d, old_dir, old_dir_pages);
330 d->root->dir_order++;
331 d->dir_size = 1 << d->root->dir_order;
332 d->dir_shift = 32 - d->root->dir_order;
333 pgc_mark_dirty(d->cache, d->root_page);
334 if (!(d->flags & SDBM_FAST))
339 sdbm_split_data(struct sdbm *d, struct sdbm_bucket *s, struct sdbm_bucket *d0, struct sdbm_bucket *d1, uns sigbit)
342 byte *dp[2] = { d0->data, d1->data };
346 while (sp < s->data + s->used)
348 sz = sdbm_get_entry(d, sp, &K, &Kl, &D, &Dl);
350 i = (sdbm_hash(K, Kl) & (1 << sigbit)) ? 1 : 0;
351 sdbm_store_entry(d, dp[i], K, Kl, D, Dl);
354 d0->used = dp[0] - d0->data;
355 d1->used = dp[1] - d1->data;
359 sdbm_split_dir(struct sdbm *d, uns dirpos, uns count, uns pos)
367 b = READ_DIR(d, dirpos & ~d->page_mask);
368 i = d->page_size - (dirpos & d->page_mask);
374 GET32(b->data, dirpos & d->page_mask) = pos;
378 pgc_mark_dirty(d->cache, b);
379 pgc_put(d->cache, b);
384 sdbm_dirpos(struct sdbm *d, uns hash)
386 if (d->dir_shift != 32) /* avoid shifting by 32 bits */
387 return (hash >> d->dir_shift) << 2; /* offset in the directory */
393 sdbm_split_page(struct sdbm *d, struct page *b, u32 hash)
396 uns i, rank, sigbit, rank_log, dirpos, newpg;
398 dirpos = sdbm_dirpos(d, hash);
399 rank = sdbm_page_rank(d, dirpos); /* rank = # of pointers to this page */
402 sdbm_expand_directory(d);
406 rank_log = 1; /* rank_log = log2(rank) */
407 while ((1U << rank_log) < rank)
409 sigbit = d->dir_shift + rank_log - 1; /* sigbit = bit we split on */
411 newpg = sdbm_alloc_page(d);
412 p[1] = GET_PAGE(d, newpg);
413 sdbm_split_data(d, (void *) b->data, (void *) p[0]->data, (void *) p[1]->data, sigbit);
414 sdbm_split_dir(d, (dirpos & ~(4*rank - 1))+2*rank, rank/2, newpg);
415 pgc_mark_dirty(d->cache, p[0]);
416 i = (hash & (1 << sigbit)) ? 1 : 0;
417 pgc_put(d->cache, p[!i]);
422 sdbm_put_user(byte *D, uns Dl, byte *val, uns *vallen)
436 sdbm_access(struct sdbm *d, byte *key, uns keylen, byte *val, uns *vallen, uns mode) /* 0=read, 1=store, 2=replace */
439 u32 hash, h, pos, size;
440 struct sdbm_bucket *b;
444 if ((d->key_size >= 0 && keylen != (uns) d->key_size) || keylen > 65535)
445 return SDBM_ERROR_BAD_KEY_SIZE;
446 if (val && ((d->val_size >= 0 && *vallen != (uns) d->val_size) || *vallen >= 65535) && mode)
447 return SDBM_ERROR_BAD_VAL_SIZE;
448 if (!mode && !(d->flags & SDBM_WRITE))
449 return SDBM_ERROR_READ_ONLY;
450 hash = sdbm_hash(key, keylen);
451 h = sdbm_dirpos(d, hash);
452 p = READ_DIR(d, h & ~d->page_mask);
453 pos = GET32(p->data, h & d->page_mask);
454 pgc_put(d->cache, p);
455 q = READ_PAGE(d, pos);
456 b = (void *) q->data;
463 s = sdbm_get_entry(d, c, &K, &Kl, &D, &Dl);
464 if (Kl == keylen && !memcmp(K, key, Kl))
469 case 0: /* fetch: found */
470 rc = sdbm_put_user(D, Dl, val, vallen);
471 pgc_put(d->cache, q);
472 return rc ? SDBM_ERROR_TOO_LARGE : 1;
473 case 1: /* store: already present */
474 pgc_put(d->cache, q);
476 default: /* replace: delete the old one */
477 memmove(c, c+s, e-(c+s));
484 if (!mode || !val) /* fetch or delete: no success */
486 pgc_put(d->cache, q);
493 size = sdbm_entry_len(d, keylen, *vallen);
494 while (b->used + size > d->page_size - sizeof(struct sdbm_bucket))
496 /* Page overflow, need to split */
497 if (size >= d->page_size - sizeof(struct sdbm_bucket))
499 pgc_put(d->cache, q);
500 return SDBM_ERROR_GIANT;
502 q = sdbm_split_page(d, q, hash);
503 b = (void *) q->data;
505 sdbm_store_entry(d, b->data + b->used, key, keylen, val, *vallen);
508 pgc_mark_dirty(d->cache, q);
509 pgc_put(d->cache, q);
510 if (d->flags & SDBM_SYNC)
516 sdbm_store(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
518 return sdbm_access(d, key, keylen, val, &vallen, 1);
522 sdbm_replace(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
524 return sdbm_access(d, key, keylen, val, &vallen, 2);
528 sdbm_delete(struct sdbm *d, byte *key, uns keylen)
530 return sdbm_access(d, key, keylen, NULL, NULL, 2);
534 sdbm_fetch(struct sdbm *d, byte *key, uns keylen, byte *val, uns *vallen)
536 return sdbm_access(d, key, keylen, val, vallen, 0);
540 sdbm_rewind(struct sdbm *d)
544 d->find_free_list = 0;
548 sdbm_get_next(struct sdbm *d, byte *key, uns *keylen, byte *val, uns *vallen)
550 uns page = d->find_page;
551 uns pos = d->find_pos;
555 struct sdbm_bucket *b;
561 if (page >= d->file_size)
563 if (page == d->root->dir_start)
564 page += (4*d->dir_size + d->page_size - 1) >> d->page_order;
565 else if (page == d->root->free_pool[d->find_free_list].first)
566 page += d->root->free_pool[d->find_free_list++].count;
571 p = READ_PAGE(d, page);
572 b = (void *) p->data;
573 if (pos - 4 >= b->used)
577 pgc_put(d->cache, p);
580 c = sdbm_get_entry(d, p->data + pos, &K, &Kl, &V, &Vl);
582 d->find_pos = pos + c;
583 c = sdbm_put_user(K, Kl, key, keylen) ||
584 sdbm_put_user(V, Vl, val, vallen);
585 pgc_put(d->cache, p);
586 return c ? SDBM_ERROR_TOO_LARGE : 1;
594 sdbm_sync(struct sdbm *d)
597 if (d->flags & SDBM_FSYNC)