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.
11 static char rcsid[] = "$Id: pair.c,v 1.10 90/12/13 13:00:35 oz Exp $";
20 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
25 static int seepair proto((char *, int, char *, int));
29 * +------------------------------+
30 * ino | n | keyoff | datoff | keyoff |
31 * +------------+--------+--------+
32 * | datoff | - - - ----> |
33 * +--------+---------------------+
35 * +--------------+---------------+
36 * | <---- - - - | data |
37 * +--------+-----+----+----------+
38 * | key | data | key |
39 * +--------+----------+----------+
41 * calculating the offsets for free area: if the number
42 * of entries (ino[0]) is zero, the offset to the END of
43 * the free area is the block size. Otherwise, it is the
44 * nth (ino[ino[0]]) entry's offset.
48 fitpair(char *pag, int need)
53 register short *ino = (short *) pag;
55 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
56 free = off - (n + 1) * sizeof(short);
57 need += 2 * sizeof(short);
59 debug(("free %d need %d\n", free, need));
65 putpair(char *pag, datum key, datum val)
69 register short *ino = (short *) pag;
71 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
76 (void) memcpy(pag + off, key.dptr, key.dsize);
82 (void) memcpy(pag + off, val.dptr, val.dsize);
91 getpair(char *pag, datum key)
96 register short *ino = (short *) pag;
98 if ((n = ino[0]) == 0)
101 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
104 val.dptr = pag + ino[i + 1];
105 val.dsize = ino[i] - ino[i + 1];
111 duppair(char *pag, datum key)
113 register short *ino = (short *) pag;
114 return ino[0] > 0 && seepair(pag, ino[0], key.dptr, key.dsize) > 0;
119 getnkey(char *pag, int num)
123 register short *ino = (short *) pag;
126 if (ino[0] == 0 || num > ino[0])
129 off = (num > 1) ? ino[num - 1] : PBLKSIZ;
131 key.dptr = pag + ino[num];
132 key.dsize = off - ino[num];
138 delpair(char *pag, datum key)
142 register short *ino = (short *) pag;
144 if ((n = ino[0]) == 0)
147 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
150 * found the key. if it is the last entry
151 * [i.e. i == n - 1] we just adjust the entry count.
152 * hard case: move all data down onto the deleted pair,
153 * shift offsets onto deleted offsets, and adjust them.
158 register char *dst = pag + (i == 1 ? PBLKSIZ : ino[i - 1]);
159 register char *src = pag + ino[i + 1];
160 register int zoo = dst - src;
162 debug(("free-up %d ", zoo));
164 * shift data/keys down
166 m = ino[i + 1] - ino[n];
168 #define MOVB *--dst = *--src
171 register int loop = (m + 8 - 1) >> 3;
173 switch (m & (8 - 1)) {
176 case 6: MOVB; case 5: MOVB;
177 case 4: MOVB; case 3: MOVB;
178 case 2: MOVB; case 1: MOVB;
186 memmove(dst, src, m);
193 * adjust offset index up
196 ino[i] = ino[i + 2] + zoo;
205 * search for the key in the page.
206 * return offset index in the range 0 < i < n.
207 * return 0 if not found.
210 seepair(char *pag, register int n, register char *key, register int siz)
213 register int off = PBLKSIZ;
214 register short *ino = (short *) pag;
216 for (i = 1; i < n; i += 2) {
217 if (siz == off - ino[i] &&
218 memEQ(key, pag + ino[i], siz))
226 splpage(char *pag, char *New, long int sbit)
232 register int off = PBLKSIZ;
234 register short *ino = (short *) cur;
236 (void) memcpy(cur, pag, PBLKSIZ);
237 (void) memset(pag, 0, PBLKSIZ);
238 (void) memset(New, 0, PBLKSIZ);
241 for (ino++; n > 0; ino += 2) {
242 key.dptr = cur + ino[0];
243 key.dsize = off - ino[0];
244 val.dptr = cur + ino[1];
245 val.dsize = ino[0] - ino[1];
247 * select the page pointer (by looking at sbit) and insert
249 (void) putpair((exhash(key) & sbit) ? New : pag, key, val);
255 debug(("%d split %d/%d\n", ((short *) cur)[0] / 2,
256 ((short *) New)[0] / 2,
257 ((short *) pag)[0] / 2));
262 * number of entries should be something
263 * reasonable, and all offsets in the index should be in order.
264 * this could be made more rigorous.
271 register short *ino = (short *) pag;
273 if ((n = ino[0]) < 0 || n > PBLKSIZ / sizeof(short))
278 for (ino++; n > 0; ino += 2) {
279 if (ino[0] > off || ino[1] > off ||