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.
16 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
21 static int seepair proto((char *, int, char *, int));
25 * +------------------------------+
26 * ino | n | keyoff | datoff | keyoff |
27 * +------------+--------+--------+
28 * | datoff | - - - ----> |
29 * +--------+---------------------+
31 * +--------------+---------------+
32 * | <---- - - - | data |
33 * +--------+-----+----+----------+
34 * | key | data | key |
35 * +--------+----------+----------+
37 * calculating the offsets for free area: if the number
38 * of entries (ino[0]) is zero, the offset to the END of
39 * the free area is the block size. Otherwise, it is the
40 * nth (ino[ino[0]]) entry's offset.
44 fitpair(char *pag, int need)
49 register short *ino = (short *) pag;
51 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
52 free = off - (n + 1) * sizeof(short);
53 need += 2 * sizeof(short);
55 debug(("free %d need %d\n", free, need));
61 putpair(char *pag, datum key, datum val)
65 register short *ino = (short *) pag;
67 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
72 (void) memcpy(pag + off, key.dptr, key.dsize);
78 (void) memcpy(pag + off, val.dptr, val.dsize);
87 getpair(char *pag, datum key)
92 register short *ino = (short *) pag;
94 if ((n = ino[0]) == 0)
97 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
100 val.dptr = pag + ino[i + 1];
101 val.dsize = ino[i] - ino[i + 1];
107 duppair(char *pag, datum key)
109 register short *ino = (short *) pag;
110 return ino[0] > 0 && seepair(pag, ino[0], key.dptr, key.dsize) > 0;
115 getnkey(char *pag, int num)
119 register short *ino = (short *) pag;
122 if (ino[0] == 0 || num > ino[0])
125 off = (num > 1) ? ino[num - 1] : PBLKSIZ;
127 key.dptr = pag + ino[num];
128 key.dsize = off - ino[num];
134 delpair(char *pag, datum key)
138 register short *ino = (short *) pag;
140 if ((n = ino[0]) == 0)
143 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
146 * found the key. if it is the last entry
147 * [i.e. i == n - 1] we just adjust the entry count.
148 * hard case: move all data down onto the deleted pair,
149 * shift offsets onto deleted offsets, and adjust them.
154 register char *dst = pag + (i == 1 ? PBLKSIZ : ino[i - 1]);
155 register char *src = pag + ino[i + 1];
156 register int zoo = dst - src;
158 debug(("free-up %d ", zoo));
160 * shift data/keys down
162 m = ino[i + 1] - ino[n];
164 #define MOVB *--dst = *--src
167 register int loop = (m + 8 - 1) >> 3;
169 switch (m & (8 - 1)) {
172 case 6: MOVB; case 5: MOVB;
173 case 4: MOVB; case 3: MOVB;
174 case 2: MOVB; case 1: MOVB;
182 memmove(dst, src, m);
189 * adjust offset index up
192 ino[i] = ino[i + 2] + zoo;
201 * search for the key in the page.
202 * return offset index in the range 0 < i < n.
203 * return 0 if not found.
206 seepair(char *pag, register int n, register char *key, register int siz)
209 register int off = PBLKSIZ;
210 register short *ino = (short *) pag;
212 for (i = 1; i < n; i += 2) {
213 if (siz == off - ino[i] &&
214 memEQ(key, pag + ino[i], siz))
222 splpage(char *pag, char *New, long int sbit)
228 register int off = PBLKSIZ;
230 register short *ino = (short *) cur;
232 (void) memcpy(cur, pag, PBLKSIZ);
233 (void) memset(pag, 0, PBLKSIZ);
234 (void) memset(New, 0, PBLKSIZ);
237 for (ino++; n > 0; ino += 2) {
238 key.dptr = cur + ino[0];
239 key.dsize = off - ino[0];
240 val.dptr = cur + ino[1];
241 val.dsize = ino[0] - ino[1];
243 * select the page pointer (by looking at sbit) and insert
245 (void) putpair((exhash(key) & sbit) ? New : pag, key, val);
251 debug(("%d split %d/%d\n", ((short *) cur)[0] / 2,
252 ((short *) New)[0] / 2,
253 ((short *) pag)[0] / 2));
258 * number of entries should be something
259 * reasonable, and all offsets in the index should be in order.
260 * this could be made more rigorous.
267 register short *ino = (short *) pag;
269 if ((n = ino[0]) < 0 || n > PBLKSIZ / sizeof(short))
274 for (ino++; n > 0; ino += 2) {
275 if (ino[0] > off || ino[1] > off ||