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 $";
19 #define exhash(item) sdbm_hash((item).dptr, (item).dsize)
24 static int seepair proto((char *, int, char *, int));
28 * +------------------------------+
29 * ino | n | keyoff | datoff | keyoff |
30 * +------------+--------+--------+
31 * | datoff | - - - ----> |
32 * +--------+---------------------+
34 * +--------------+---------------+
35 * | <---- - - - | data |
36 * +--------+-----+----+----------+
37 * | key | data | key |
38 * +--------+----------+----------+
40 * calculating the offsets for free area: if the number
41 * of entries (ino[0]) is zero, the offset to the END of
42 * the free area is the block size. Otherwise, it is the
43 * nth (ino[ino[0]]) entry's offset.
47 fitpair(char *pag, int need)
52 register short *ino = (short *) pag;
54 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
55 free = off - (n + 1) * sizeof(short);
56 need += 2 * sizeof(short);
58 debug(("free %d need %d\n", free, need));
64 putpair(char *pag, datum key, datum val)
68 register short *ino = (short *) pag;
70 off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
75 (void) memcpy(pag + off, key.dptr, key.dsize);
81 (void) memcpy(pag + off, val.dptr, val.dsize);
90 getpair(char *pag, datum key)
95 register short *ino = (short *) pag;
97 if ((n = ino[0]) == 0)
100 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
103 val.dptr = pag + ino[i + 1];
104 val.dsize = ino[i] - ino[i + 1];
110 duppair(char *pag, datum key)
112 register short *ino = (short *) pag;
113 return ino[0] > 0 && seepair(pag, ino[0], key.dptr, key.dsize) > 0;
118 getnkey(char *pag, int num)
122 register short *ino = (short *) pag;
125 if (ino[0] == 0 || num > ino[0])
128 off = (num > 1) ? ino[num - 1] : PBLKSIZ;
130 key.dptr = pag + ino[num];
131 key.dsize = off - ino[num];
137 delpair(char *pag, datum key)
141 register short *ino = (short *) pag;
143 if ((n = ino[0]) == 0)
146 if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
149 * found the key. if it is the last entry
150 * [i.e. i == n - 1] we just adjust the entry count.
151 * hard case: move all data down onto the deleted pair,
152 * shift offsets onto deleted offsets, and adjust them.
157 register char *dst = pag + (i == 1 ? PBLKSIZ : ino[i - 1]);
158 register char *src = pag + ino[i + 1];
159 register int zoo = dst - src;
161 debug(("free-up %d ", zoo));
163 * shift data/keys down
165 m = ino[i + 1] - ino[n];
167 #define MOVB *--dst = *--src
170 register int loop = (m + 8 - 1) >> 3;
172 switch (m & (8 - 1)) {
175 case 6: MOVB; case 5: MOVB;
176 case 4: MOVB; case 3: MOVB;
177 case 2: MOVB; case 1: MOVB;
185 memmove(dst, src, m);
192 * adjust offset index up
195 ino[i] = ino[i + 2] + zoo;
204 * search for the key in the page.
205 * return offset index in the range 0 < i < n.
206 * return 0 if not found.
209 seepair(char *pag, register int n, register char *key, register int siz)
212 register int off = PBLKSIZ;
213 register short *ino = (short *) pag;
215 for (i = 1; i < n; i += 2) {
216 if (siz == off - ino[i] &&
217 memEQ(key, pag + ino[i], siz))
225 splpage(char *pag, char *New, long int sbit)
231 register int off = PBLKSIZ;
233 register short *ino = (short *) cur;
235 (void) memcpy(cur, pag, PBLKSIZ);
236 (void) memset(pag, 0, PBLKSIZ);
237 (void) memset(New, 0, PBLKSIZ);
240 for (ino++; n > 0; ino += 2) {
241 key.dptr = cur + ino[0];
242 key.dsize = off - ino[0];
243 val.dptr = cur + ino[1];
244 val.dsize = ino[0] - ino[1];
246 * select the page pointer (by looking at sbit) and insert
248 (void) putpair((exhash(key) & sbit) ? New : pag, key, val);
254 debug(("%d split %d/%d\n", ((short *) cur)[0] / 2,
255 ((short *) New)[0] / 2,
256 ((short *) pag)[0] / 2));
261 * number of entries should be something
262 * reasonable, and all offsets in the index should be in order.
263 * this could be made more rigorous.
270 register short *ino = (short *) pag;
272 if ((n = ino[0]) < 0 || n > PBLKSIZ / sizeof(short))
277 for (ino++; n > 0; ino += 2) {
278 if (ino[0] > off || ino[1] > off ||