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.
13 # define EXTCONST extern const
21 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
26 static int seepair proto((char *, int, char *, int));
30 * +------------------------------+
31 * ino | n | keyoff | datoff | keyoff |
32 * +------------+--------+--------+
33 * | datoff | - - - ----> |
34 * +--------+---------------------+
36 * +--------------+---------------+
37 * | <---- - - - | data |
38 * +--------+-----+----+----------+
39 * | key | data | key |
40 * +--------+----------+----------+
42 * calculating the offsets for free area: if the number
43 * of entries (ino[0]) is zero, the offset to the END of
44 * the free area is the block size. Otherwise, it is the
45 * nth (ino[ino[0]]) entry's offset.
49 fitpair(char *pag, int need)
54 register short *ino = (short *) pag;
56 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
57 free = off - (n + 1) * sizeof(short);
58 need += 2 * sizeof(short);
60 debug(("free %d need %d\n", free, need));
66 putpair(char *pag, datum key, datum val)
70 register short *ino = (short *) pag;
72 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
77 (void) memcpy(pag + off, key.dptr, key.dsize);
83 (void) memcpy(pag + off, val.dptr, val.dsize);
92 getpair(char *pag, datum key)
97 register short *ino = (short *) pag;
99 if ((n = ino[0]) == 0)
102 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
105 val.dptr = pag + ino[i + 1];
106 val.dsize = ino[i] - ino[i + 1];
111 exipair(char *pag, datum key)
113 register short *ino = (short *) pag;
118 return (seepair(pag, ino[0], key.dptr, key.dsize) != 0);
123 duppair(char *pag, datum key)
125 register short *ino = (short *) pag;
126 return ino[0] > 0 && seepair(pag, ino[0], key.dptr, key.dsize) > 0;
131 getnkey(char *pag, int num)
135 register short *ino = (short *) pag;
138 if (ino[0] == 0 || num > ino[0])
141 off = (num > 1) ? ino[num - 1] : PBLKSIZ;
143 key.dptr = pag + ino[num];
144 key.dsize = off - ino[num];
150 delpair(char *pag, datum key)
154 register short *ino = (short *) pag;
156 if ((n = ino[0]) == 0)
159 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
162 * found the key. if it is the last entry
163 * [i.e. i == n - 1] we just adjust the entry count.
164 * hard case: move all data down onto the deleted pair,
165 * shift offsets onto deleted offsets, and adjust them.
170 register char *dst = pag + (i == 1 ? PBLKSIZ : ino[i - 1]);
171 register char *src = pag + ino[i + 1];
172 register int zoo = dst - src;
174 debug(("free-up %d ", zoo));
176 * shift data/keys down
178 m = ino[i + 1] - ino[n];
180 #define MOVB *--dst = *--src
183 register int loop = (m + 8 - 1) >> 3;
185 switch (m & (8 - 1)) {
188 case 6: MOVB; case 5: MOVB;
189 case 4: MOVB; case 3: MOVB;
190 case 2: MOVB; case 1: MOVB;
198 memmove(dst, src, m);
205 * adjust offset index up
208 ino[i] = ino[i + 2] + zoo;
217 * search for the key in the page.
218 * return offset index in the range 0 < i < n.
219 * return 0 if not found.
222 seepair(char *pag, register int n, register char *key, register int siz)
225 register int off = PBLKSIZ;
226 register short *ino = (short *) pag;
228 for (i = 1; i < n; i += 2) {
229 if (siz == off - ino[i] &&
230 memEQ(key, pag + ino[i], siz))
238 splpage(char *pag, char *New, long int sbit)
244 register int off = PBLKSIZ;
246 register short *ino = (short *) cur;
248 (void) memcpy(cur, pag, PBLKSIZ);
249 (void) memset(pag, 0, PBLKSIZ);
250 (void) memset(New, 0, PBLKSIZ);
253 for (ino++; n > 0; ino += 2) {
254 key.dptr = cur + ino[0];
255 key.dsize = off - ino[0];
256 val.dptr = cur + ino[1];
257 val.dsize = ino[0] - ino[1];
259 * select the page pointer (by looking at sbit) and insert
261 (void) putpair((exhash(key) & sbit) ? New : pag, key, val);
267 debug(("%d split %d/%d\n", ((short *) cur)[0] / 2,
268 ((short *) New)[0] / 2,
269 ((short *) pag)[0] / 2));
274 * number of entries should be something
275 * reasonable, and all offsets in the index should be in order.
276 * this could be made more rigorous.
283 register short *ino = (short *) pag;
285 if ((n = ino[0]) < 0 || n > PBLKSIZ / sizeof(short))
290 for (ino++; n > 0; ino += 2) {
291 if (ino[0] > off || ino[1] > off ||