hash_page.c revision 1.1 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1990, 1993
3 1.1 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Margo Seltzer.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 cgd static char sccsid[] = "@(#)hash_page.c 8.1 (Berkeley) 6/6/93";
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * PACKAGE: hashing
43 1.1 cgd *
44 1.1 cgd * DESCRIPTION:
45 1.1 cgd * Page manipulation for hashing package.
46 1.1 cgd *
47 1.1 cgd * ROUTINES:
48 1.1 cgd *
49 1.1 cgd * External
50 1.1 cgd * __get_page
51 1.1 cgd * __add_ovflpage
52 1.1 cgd * Internal
53 1.1 cgd * overflow_page
54 1.1 cgd * open_temp
55 1.1 cgd */
56 1.1 cgd
57 1.1 cgd #include <sys/types.h>
58 1.1 cgd
59 1.1 cgd #include <errno.h>
60 1.1 cgd #include <fcntl.h>
61 1.1 cgd #include <signal.h>
62 1.1 cgd #include <stdio.h>
63 1.1 cgd #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.1 cgd #include <unistd.h>
66 1.1 cgd #ifdef DEBUG
67 1.1 cgd #include <assert.h>
68 1.1 cgd #endif
69 1.1 cgd
70 1.1 cgd #include <db.h>
71 1.1 cgd #include "hash.h"
72 1.1 cgd #include "page.h"
73 1.1 cgd #include "extern.h"
74 1.1 cgd
75 1.1 cgd static u_long *fetch_bitmap __P((HTAB *, int));
76 1.1 cgd static u_long first_free __P((u_long));
77 1.1 cgd static int open_temp __P((HTAB *));
78 1.1 cgd static u_short overflow_page __P((HTAB *));
79 1.1 cgd static void putpair __P((char *, const DBT *, const DBT *));
80 1.1 cgd static void squeeze_key __P((u_short *, const DBT *, const DBT *));
81 1.1 cgd static int ugly_split
82 1.1 cgd __P((HTAB *, u_int, BUFHEAD *, BUFHEAD *, int, int));
83 1.1 cgd
84 1.1 cgd #define PAGE_INIT(P) { \
85 1.1 cgd ((u_short *)(P))[0] = 0; \
86 1.1 cgd ((u_short *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_short); \
87 1.1 cgd ((u_short *)(P))[2] = hashp->BSIZE; \
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd /*
91 1.1 cgd * This is called AFTER we have verified that there is room on the page for
92 1.1 cgd * the pair (PAIRFITS has returned true) so we go right ahead and start moving
93 1.1 cgd * stuff on.
94 1.1 cgd */
95 1.1 cgd static void
96 1.1 cgd putpair(p, key, val)
97 1.1 cgd char *p;
98 1.1 cgd const DBT *key, *val;
99 1.1 cgd {
100 1.1 cgd register u_short *bp, n, off;
101 1.1 cgd
102 1.1 cgd bp = (u_short *)p;
103 1.1 cgd
104 1.1 cgd /* Enter the key first. */
105 1.1 cgd n = bp[0];
106 1.1 cgd
107 1.1 cgd off = OFFSET(bp) - key->size;
108 1.1 cgd memmove(p + off, key->data, key->size);
109 1.1 cgd bp[++n] = off;
110 1.1 cgd
111 1.1 cgd /* Now the data. */
112 1.1 cgd off -= val->size;
113 1.1 cgd memmove(p + off, val->data, val->size);
114 1.1 cgd bp[++n] = off;
115 1.1 cgd
116 1.1 cgd /* Adjust page info. */
117 1.1 cgd bp[0] = n;
118 1.1 cgd bp[n + 1] = off - ((n + 3) * sizeof(u_short));
119 1.1 cgd bp[n + 2] = off;
120 1.1 cgd }
121 1.1 cgd
122 1.1 cgd /*
123 1.1 cgd * Returns:
124 1.1 cgd * 0 OK
125 1.1 cgd * -1 error
126 1.1 cgd */
127 1.1 cgd extern int
128 1.1 cgd __delpair(hashp, bufp, ndx)
129 1.1 cgd HTAB *hashp;
130 1.1 cgd BUFHEAD *bufp;
131 1.1 cgd register int ndx;
132 1.1 cgd {
133 1.1 cgd register u_short *bp, newoff;
134 1.1 cgd register int n;
135 1.1 cgd u_short pairlen;
136 1.1 cgd
137 1.1 cgd bp = (u_short *)bufp->page;
138 1.1 cgd n = bp[0];
139 1.1 cgd
140 1.1 cgd if (bp[ndx + 1] < REAL_KEY)
141 1.1 cgd return (__big_delete(hashp, bufp));
142 1.1 cgd if (ndx != 1)
143 1.1 cgd newoff = bp[ndx - 1];
144 1.1 cgd else
145 1.1 cgd newoff = hashp->BSIZE;
146 1.1 cgd pairlen = newoff - bp[ndx + 1];
147 1.1 cgd
148 1.1 cgd if (ndx != (n - 1)) {
149 1.1 cgd /* Hard Case -- need to shuffle keys */
150 1.1 cgd register int i;
151 1.1 cgd register char *src = bufp->page + (int)OFFSET(bp);
152 1.1 cgd register char *dst = src + (int)pairlen;
153 1.1 cgd memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
154 1.1 cgd
155 1.1 cgd /* Now adjust the pointers */
156 1.1 cgd for (i = ndx + 2; i <= n; i += 2) {
157 1.1 cgd if (bp[i + 1] == OVFLPAGE) {
158 1.1 cgd bp[i - 2] = bp[i];
159 1.1 cgd bp[i - 1] = bp[i + 1];
160 1.1 cgd } else {
161 1.1 cgd bp[i - 2] = bp[i] + pairlen;
162 1.1 cgd bp[i - 1] = bp[i + 1] + pairlen;
163 1.1 cgd }
164 1.1 cgd }
165 1.1 cgd }
166 1.1 cgd /* Finally adjust the page data */
167 1.1 cgd bp[n] = OFFSET(bp) + pairlen;
168 1.1 cgd bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_short);
169 1.1 cgd bp[0] = n - 2;
170 1.1 cgd hashp->NKEYS--;
171 1.1 cgd
172 1.1 cgd bufp->flags |= BUF_MOD;
173 1.1 cgd return (0);
174 1.1 cgd }
175 1.1 cgd /*
176 1.1 cgd * Returns:
177 1.1 cgd * 0 ==> OK
178 1.1 cgd * -1 ==> Error
179 1.1 cgd */
180 1.1 cgd extern int
181 1.1 cgd __split_page(hashp, obucket, nbucket)
182 1.1 cgd HTAB *hashp;
183 1.1 cgd u_int obucket, nbucket;
184 1.1 cgd {
185 1.1 cgd register BUFHEAD *new_bufp, *old_bufp;
186 1.1 cgd register u_short *ino;
187 1.1 cgd register char *np;
188 1.1 cgd DBT key, val;
189 1.1 cgd int n, ndx, retval;
190 1.1 cgd u_short copyto, diff, off, moved;
191 1.1 cgd char *op;
192 1.1 cgd
193 1.1 cgd copyto = (u_short)hashp->BSIZE;
194 1.1 cgd off = (u_short)hashp->BSIZE;
195 1.1 cgd old_bufp = __get_buf(hashp, obucket, NULL, 0);
196 1.1 cgd if (old_bufp == NULL)
197 1.1 cgd return (-1);
198 1.1 cgd new_bufp = __get_buf(hashp, nbucket, NULL, 0);
199 1.1 cgd if (new_bufp == NULL)
200 1.1 cgd return (-1);
201 1.1 cgd
202 1.1 cgd old_bufp->flags |= (BUF_MOD | BUF_PIN);
203 1.1 cgd new_bufp->flags |= (BUF_MOD | BUF_PIN);
204 1.1 cgd
205 1.1 cgd ino = (u_short *)(op = old_bufp->page);
206 1.1 cgd np = new_bufp->page;
207 1.1 cgd
208 1.1 cgd moved = 0;
209 1.1 cgd
210 1.1 cgd for (n = 1, ndx = 1; n < ino[0]; n += 2) {
211 1.1 cgd if (ino[n + 1] < REAL_KEY) {
212 1.1 cgd retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
213 1.1 cgd (int)copyto, (int)moved);
214 1.1 cgd old_bufp->flags &= ~BUF_PIN;
215 1.1 cgd new_bufp->flags &= ~BUF_PIN;
216 1.1 cgd return (retval);
217 1.1 cgd
218 1.1 cgd }
219 1.1 cgd key.data = (u_char *)op + ino[n];
220 1.1 cgd key.size = off - ino[n];
221 1.1 cgd
222 1.1 cgd if (__call_hash(hashp, key.data, key.size) == obucket) {
223 1.1 cgd /* Don't switch page */
224 1.1 cgd diff = copyto - off;
225 1.1 cgd if (diff) {
226 1.1 cgd copyto = ino[n + 1] + diff;
227 1.1 cgd memmove(op + copyto, op + ino[n + 1],
228 1.1 cgd off - ino[n + 1]);
229 1.1 cgd ino[ndx] = copyto + ino[n] - ino[n + 1];
230 1.1 cgd ino[ndx + 1] = copyto;
231 1.1 cgd } else
232 1.1 cgd copyto = ino[n + 1];
233 1.1 cgd ndx += 2;
234 1.1 cgd } else {
235 1.1 cgd /* Switch page */
236 1.1 cgd val.data = (u_char *)op + ino[n + 1];
237 1.1 cgd val.size = ino[n] - ino[n + 1];
238 1.1 cgd putpair(np, &key, &val);
239 1.1 cgd moved += 2;
240 1.1 cgd }
241 1.1 cgd
242 1.1 cgd off = ino[n + 1];
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd /* Now clean up the page */
246 1.1 cgd ino[0] -= moved;
247 1.1 cgd FREESPACE(ino) = copyto - sizeof(u_short) * (ino[0] + 3);
248 1.1 cgd OFFSET(ino) = copyto;
249 1.1 cgd
250 1.1 cgd #ifdef DEBUG3
251 1.1 cgd (void)fprintf(stderr, "split %d/%d\n",
252 1.1 cgd ((u_short *)np)[0] / 2,
253 1.1 cgd ((u_short *)op)[0] / 2);
254 1.1 cgd #endif
255 1.1 cgd /* unpin both pages */
256 1.1 cgd old_bufp->flags &= ~BUF_PIN;
257 1.1 cgd new_bufp->flags &= ~BUF_PIN;
258 1.1 cgd return (0);
259 1.1 cgd }
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * Called when we encounter an overflow or big key/data page during split
263 1.1 cgd * handling. This is special cased since we have to begin checking whether
264 1.1 cgd * the key/data pairs fit on their respective pages and because we may need
265 1.1 cgd * overflow pages for both the old and new pages.
266 1.1 cgd *
267 1.1 cgd * The first page might be a page with regular key/data pairs in which case
268 1.1 cgd * we have a regular overflow condition and just need to go on to the next
269 1.1 cgd * page or it might be a big key/data pair in which case we need to fix the
270 1.1 cgd * big key/data pair.
271 1.1 cgd *
272 1.1 cgd * Returns:
273 1.1 cgd * 0 ==> success
274 1.1 cgd * -1 ==> failure
275 1.1 cgd */
276 1.1 cgd static int
277 1.1 cgd ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
278 1.1 cgd HTAB *hashp;
279 1.1 cgd u_int obucket; /* Same as __split_page. */
280 1.1 cgd BUFHEAD *old_bufp, *new_bufp;
281 1.1 cgd int copyto; /* First byte on page which contains key/data values. */
282 1.1 cgd int moved; /* Number of pairs moved to new page. */
283 1.1 cgd {
284 1.1 cgd register BUFHEAD *bufp; /* Buffer header for ino */
285 1.1 cgd register u_short *ino; /* Page keys come off of */
286 1.1 cgd register u_short *np; /* New page */
287 1.1 cgd register u_short *op; /* Page keys go on to if they aren't moving */
288 1.1 cgd
289 1.1 cgd BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
290 1.1 cgd DBT key, val;
291 1.1 cgd SPLIT_RETURN ret;
292 1.1 cgd u_short n, off, ov_addr, scopyto;
293 1.1 cgd char *cino; /* Character value of ino */
294 1.1 cgd
295 1.1 cgd bufp = old_bufp;
296 1.1 cgd ino = (u_short *)old_bufp->page;
297 1.1 cgd np = (u_short *)new_bufp->page;
298 1.1 cgd op = (u_short *)old_bufp->page;
299 1.1 cgd last_bfp = NULL;
300 1.1 cgd scopyto = (u_short)copyto; /* ANSI */
301 1.1 cgd
302 1.1 cgd n = ino[0] - 1;
303 1.1 cgd while (n < ino[0]) {
304 1.1 cgd if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
305 1.1 cgd /*
306 1.1 cgd * Ov_addr gets set before reaching this point; there's
307 1.1 cgd * always an overflow page before a big key/data page.
308 1.1 cgd */
309 1.1 cgd if (__big_split(hashp, old_bufp,
310 1.1 cgd new_bufp, bufp, ov_addr, obucket, &ret))
311 1.1 cgd return (-1);
312 1.1 cgd old_bufp = ret.oldp;
313 1.1 cgd if (!old_bufp)
314 1.1 cgd return (-1);
315 1.1 cgd op = (u_short *)old_bufp->page;
316 1.1 cgd new_bufp = ret.newp;
317 1.1 cgd if (!new_bufp)
318 1.1 cgd return (-1);
319 1.1 cgd np = (u_short *)new_bufp->page;
320 1.1 cgd bufp = ret.nextp;
321 1.1 cgd if (!bufp)
322 1.1 cgd return (0);
323 1.1 cgd cino = (char *)bufp->page;
324 1.1 cgd ino = (u_short *)cino;
325 1.1 cgd last_bfp = ret.nextp;
326 1.1 cgd } else if (ino[n + 1] == OVFLPAGE) {
327 1.1 cgd ov_addr = ino[n];
328 1.1 cgd /*
329 1.1 cgd * Fix up the old page -- the extra 2 are the fields
330 1.1 cgd * which contained the overflow information.
331 1.1 cgd */
332 1.1 cgd ino[0] -= (moved + 2);
333 1.1 cgd FREESPACE(ino) =
334 1.1 cgd scopyto - sizeof(u_short) * (ino[0] + 3);
335 1.1 cgd OFFSET(ino) = scopyto;
336 1.1 cgd
337 1.1 cgd bufp = __get_buf(hashp, ov_addr, bufp, 0);
338 1.1 cgd if (!bufp)
339 1.1 cgd return (-1);
340 1.1 cgd
341 1.1 cgd ino = (u_short *)bufp->page;
342 1.1 cgd n = 1;
343 1.1 cgd scopyto = hashp->BSIZE;
344 1.1 cgd moved = 0;
345 1.1 cgd
346 1.1 cgd if (last_bfp)
347 1.1 cgd __free_ovflpage(hashp, last_bfp);
348 1.1 cgd last_bfp = bufp;
349 1.1 cgd }
350 1.1 cgd /* Move regular sized pairs of there are any */
351 1.1 cgd off = hashp->BSIZE;
352 1.1 cgd for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
353 1.1 cgd cino = (char *)ino;
354 1.1 cgd key.data = (u_char *)cino + ino[n];
355 1.1 cgd key.size = off - ino[n];
356 1.1 cgd val.data = (u_char *)cino + ino[n + 1];
357 1.1 cgd val.size = ino[n] - ino[n + 1];
358 1.1 cgd off = ino[n + 1];
359 1.1 cgd
360 1.1 cgd if (__call_hash(hashp, key.data, key.size) == obucket) {
361 1.1 cgd /* Keep on old page */
362 1.1 cgd if (PAIRFITS(op, (&key), (&val)))
363 1.1 cgd putpair((char *)op, &key, &val);
364 1.1 cgd else {
365 1.1 cgd old_bufp =
366 1.1 cgd __add_ovflpage(hashp, old_bufp);
367 1.1 cgd if (!old_bufp)
368 1.1 cgd return (-1);
369 1.1 cgd op = (u_short *)old_bufp->page;
370 1.1 cgd putpair((char *)op, &key, &val);
371 1.1 cgd }
372 1.1 cgd old_bufp->flags |= BUF_MOD;
373 1.1 cgd } else {
374 1.1 cgd /* Move to new page */
375 1.1 cgd if (PAIRFITS(np, (&key), (&val)))
376 1.1 cgd putpair((char *)np, &key, &val);
377 1.1 cgd else {
378 1.1 cgd new_bufp =
379 1.1 cgd __add_ovflpage(hashp, new_bufp);
380 1.1 cgd if (!new_bufp)
381 1.1 cgd return (-1);
382 1.1 cgd np = (u_short *)new_bufp->page;
383 1.1 cgd putpair((char *)np, &key, &val);
384 1.1 cgd }
385 1.1 cgd new_bufp->flags |= BUF_MOD;
386 1.1 cgd }
387 1.1 cgd }
388 1.1 cgd }
389 1.1 cgd if (last_bfp)
390 1.1 cgd __free_ovflpage(hashp, last_bfp);
391 1.1 cgd return (0);
392 1.1 cgd }
393 1.1 cgd
394 1.1 cgd /*
395 1.1 cgd * Add the given pair to the page
396 1.1 cgd *
397 1.1 cgd * Returns:
398 1.1 cgd * 0 ==> OK
399 1.1 cgd * 1 ==> failure
400 1.1 cgd */
401 1.1 cgd extern int
402 1.1 cgd __addel(hashp, bufp, key, val)
403 1.1 cgd HTAB *hashp;
404 1.1 cgd BUFHEAD *bufp;
405 1.1 cgd const DBT *key, *val;
406 1.1 cgd {
407 1.1 cgd register u_short *bp, *sop;
408 1.1 cgd int do_expand;
409 1.1 cgd
410 1.1 cgd bp = (u_short *)bufp->page;
411 1.1 cgd do_expand = 0;
412 1.1 cgd while (bp[0] && (bp[bp[0]] < REAL_KEY))
413 1.1 cgd /* Exception case */
414 1.1 cgd if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
415 1.1 cgd /* This is a big-keydata pair */
416 1.1 cgd bufp = __add_ovflpage(hashp, bufp);
417 1.1 cgd if (!bufp)
418 1.1 cgd return (-1);
419 1.1 cgd bp = (u_short *)bufp->page;
420 1.1 cgd } else
421 1.1 cgd /* Try to squeeze key on this page */
422 1.1 cgd if (FREESPACE(bp) > PAIRSIZE(key, val)) {
423 1.1 cgd squeeze_key(bp, key, val);
424 1.1 cgd return (0);
425 1.1 cgd } else {
426 1.1 cgd bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
427 1.1 cgd if (!bufp)
428 1.1 cgd return (-1);
429 1.1 cgd bp = (u_short *)bufp->page;
430 1.1 cgd }
431 1.1 cgd
432 1.1 cgd if (PAIRFITS(bp, key, val))
433 1.1 cgd putpair(bufp->page, key, val);
434 1.1 cgd else {
435 1.1 cgd do_expand = 1;
436 1.1 cgd bufp = __add_ovflpage(hashp, bufp);
437 1.1 cgd if (!bufp)
438 1.1 cgd return (-1);
439 1.1 cgd sop = (u_short *)bufp->page;
440 1.1 cgd
441 1.1 cgd if (PAIRFITS(sop, key, val))
442 1.1 cgd putpair((char *)sop, key, val);
443 1.1 cgd else
444 1.1 cgd if (__big_insert(hashp, bufp, key, val))
445 1.1 cgd return (-1);
446 1.1 cgd }
447 1.1 cgd bufp->flags |= BUF_MOD;
448 1.1 cgd /*
449 1.1 cgd * If the average number of keys per bucket exceeds the fill factor,
450 1.1 cgd * expand the table.
451 1.1 cgd */
452 1.1 cgd hashp->NKEYS++;
453 1.1 cgd if (do_expand ||
454 1.1 cgd (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
455 1.1 cgd return (__expand_table(hashp));
456 1.1 cgd return (0);
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd /*
460 1.1 cgd *
461 1.1 cgd * Returns:
462 1.1 cgd * pointer on success
463 1.1 cgd * NULL on error
464 1.1 cgd */
465 1.1 cgd extern BUFHEAD *
466 1.1 cgd __add_ovflpage(hashp, bufp)
467 1.1 cgd HTAB *hashp;
468 1.1 cgd BUFHEAD *bufp;
469 1.1 cgd {
470 1.1 cgd register u_short *sp;
471 1.1 cgd u_short ndx, ovfl_num;
472 1.1 cgd #ifdef DEBUG1
473 1.1 cgd int tmp1, tmp2;
474 1.1 cgd #endif
475 1.1 cgd sp = (u_short *)bufp->page;
476 1.1 cgd
477 1.1 cgd /* Check if we are dynamically determining the fill factor */
478 1.1 cgd if (hashp->FFACTOR == DEF_FFACTOR) {
479 1.1 cgd hashp->FFACTOR = sp[0] >> 1;
480 1.1 cgd if (hashp->FFACTOR < MIN_FFACTOR)
481 1.1 cgd hashp->FFACTOR = MIN_FFACTOR;
482 1.1 cgd }
483 1.1 cgd bufp->flags |= BUF_MOD;
484 1.1 cgd ovfl_num = overflow_page(hashp);
485 1.1 cgd #ifdef DEBUG1
486 1.1 cgd tmp1 = bufp->addr;
487 1.1 cgd tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
488 1.1 cgd #endif
489 1.1 cgd if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
490 1.1 cgd return (NULL);
491 1.1 cgd bufp->ovfl->flags |= BUF_MOD;
492 1.1 cgd #ifdef DEBUG1
493 1.1 cgd (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
494 1.1 cgd tmp1, tmp2, bufp->ovfl->addr);
495 1.1 cgd #endif
496 1.1 cgd ndx = sp[0];
497 1.1 cgd /*
498 1.1 cgd * Since a pair is allocated on a page only if there's room to add
499 1.1 cgd * an overflow page, we know that the OVFL information will fit on
500 1.1 cgd * the page.
501 1.1 cgd */
502 1.1 cgd sp[ndx + 4] = OFFSET(sp);
503 1.1 cgd sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
504 1.1 cgd sp[ndx + 1] = ovfl_num;
505 1.1 cgd sp[ndx + 2] = OVFLPAGE;
506 1.1 cgd sp[0] = ndx + 2;
507 1.1 cgd #ifdef HASH_STATISTICS
508 1.1 cgd hash_overflows++;
509 1.1 cgd #endif
510 1.1 cgd return (bufp->ovfl);
511 1.1 cgd }
512 1.1 cgd
513 1.1 cgd /*
514 1.1 cgd * Returns:
515 1.1 cgd * 0 indicates SUCCESS
516 1.1 cgd * -1 indicates FAILURE
517 1.1 cgd */
518 1.1 cgd extern int
519 1.1 cgd __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
520 1.1 cgd HTAB *hashp;
521 1.1 cgd char *p;
522 1.1 cgd u_int bucket;
523 1.1 cgd int is_bucket, is_disk, is_bitmap;
524 1.1 cgd {
525 1.1 cgd register int fd, page, size;
526 1.1 cgd int rsize;
527 1.1 cgd u_short *bp;
528 1.1 cgd
529 1.1 cgd fd = hashp->fp;
530 1.1 cgd size = hashp->BSIZE;
531 1.1 cgd
532 1.1 cgd if ((fd == -1) || !is_disk) {
533 1.1 cgd PAGE_INIT(p);
534 1.1 cgd return (0);
535 1.1 cgd }
536 1.1 cgd if (is_bucket)
537 1.1 cgd page = BUCKET_TO_PAGE(bucket);
538 1.1 cgd else
539 1.1 cgd page = OADDR_TO_PAGE(bucket);
540 1.1 cgd if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
541 1.1 cgd ((rsize = read(fd, p, size)) == -1))
542 1.1 cgd return (-1);
543 1.1 cgd bp = (u_short *)p;
544 1.1 cgd if (!rsize)
545 1.1 cgd bp[0] = 0; /* We hit the EOF, so initialize a new page */
546 1.1 cgd else
547 1.1 cgd if (rsize != size) {
548 1.1 cgd errno = EFTYPE;
549 1.1 cgd return (-1);
550 1.1 cgd }
551 1.1 cgd if (!is_bitmap && !bp[0]) {
552 1.1 cgd PAGE_INIT(p);
553 1.1 cgd } else
554 1.1 cgd if (hashp->LORDER != BYTE_ORDER) {
555 1.1 cgd register int i, max;
556 1.1 cgd
557 1.1 cgd if (is_bitmap) {
558 1.1 cgd max = hashp->BSIZE >> 2; /* divide by 4 */
559 1.1 cgd for (i = 0; i < max; i++)
560 1.1 cgd BLSWAP(((long *)p)[i]);
561 1.1 cgd } else {
562 1.1 cgd BSSWAP(bp[0]);
563 1.1 cgd max = bp[0] + 2;
564 1.1 cgd for (i = 1; i <= max; i++)
565 1.1 cgd BSSWAP(bp[i]);
566 1.1 cgd }
567 1.1 cgd }
568 1.1 cgd return (0);
569 1.1 cgd }
570 1.1 cgd
571 1.1 cgd /*
572 1.1 cgd * Write page p to disk
573 1.1 cgd *
574 1.1 cgd * Returns:
575 1.1 cgd * 0 ==> OK
576 1.1 cgd * -1 ==>failure
577 1.1 cgd */
578 1.1 cgd extern int
579 1.1 cgd __put_page(hashp, p, bucket, is_bucket, is_bitmap)
580 1.1 cgd HTAB *hashp;
581 1.1 cgd char *p;
582 1.1 cgd u_int bucket;
583 1.1 cgd int is_bucket, is_bitmap;
584 1.1 cgd {
585 1.1 cgd register int fd, page, size;
586 1.1 cgd int wsize;
587 1.1 cgd
588 1.1 cgd size = hashp->BSIZE;
589 1.1 cgd if ((hashp->fp == -1) && open_temp(hashp))
590 1.1 cgd return (-1);
591 1.1 cgd fd = hashp->fp;
592 1.1 cgd
593 1.1 cgd if (hashp->LORDER != BYTE_ORDER) {
594 1.1 cgd register int i;
595 1.1 cgd register int max;
596 1.1 cgd
597 1.1 cgd if (is_bitmap) {
598 1.1 cgd max = hashp->BSIZE >> 2; /* divide by 4 */
599 1.1 cgd for (i = 0; i < max; i++)
600 1.1 cgd BLSWAP(((long *)p)[i]);
601 1.1 cgd } else {
602 1.1 cgd max = ((u_short *)p)[0] + 2;
603 1.1 cgd for (i = 0; i <= max; i++)
604 1.1 cgd BSSWAP(((u_short *)p)[i]);
605 1.1 cgd }
606 1.1 cgd }
607 1.1 cgd if (is_bucket)
608 1.1 cgd page = BUCKET_TO_PAGE(bucket);
609 1.1 cgd else
610 1.1 cgd page = OADDR_TO_PAGE(bucket);
611 1.1 cgd if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
612 1.1 cgd ((wsize = write(fd, p, size)) == -1))
613 1.1 cgd /* Errno is set */
614 1.1 cgd return (-1);
615 1.1 cgd if (wsize != size) {
616 1.1 cgd errno = EFTYPE;
617 1.1 cgd return (-1);
618 1.1 cgd }
619 1.1 cgd return (0);
620 1.1 cgd }
621 1.1 cgd
622 1.1 cgd #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
623 1.1 cgd /*
624 1.1 cgd * Initialize a new bitmap page. Bitmap pages are left in memory
625 1.1 cgd * once they are read in.
626 1.1 cgd */
627 1.1 cgd extern int
628 1.1 cgd __init_bitmap(hashp, pnum, nbits, ndx)
629 1.1 cgd HTAB *hashp;
630 1.1 cgd int pnum, nbits, ndx;
631 1.1 cgd {
632 1.1 cgd u_long *ip;
633 1.1 cgd int clearbytes, clearints;
634 1.1 cgd
635 1.1 cgd if (!(ip = malloc(hashp->BSIZE)))
636 1.1 cgd return (1);
637 1.1 cgd hashp->nmaps++;
638 1.1 cgd clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
639 1.1 cgd clearbytes = clearints << INT_TO_BYTE;
640 1.1 cgd (void)memset((char *)ip, 0, clearbytes);
641 1.1 cgd (void)memset(((char *)ip) + clearbytes, 0xFF,
642 1.1 cgd hashp->BSIZE - clearbytes);
643 1.1 cgd ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
644 1.1 cgd SETBIT(ip, 0);
645 1.1 cgd hashp->BITMAPS[ndx] = (u_short)pnum;
646 1.1 cgd hashp->mapp[ndx] = ip;
647 1.1 cgd return (0);
648 1.1 cgd }
649 1.1 cgd
650 1.1 cgd static u_long
651 1.1 cgd first_free(map)
652 1.1 cgd u_long map;
653 1.1 cgd {
654 1.1 cgd register u_long i, mask;
655 1.1 cgd
656 1.1 cgd mask = 0x1;
657 1.1 cgd for (i = 0; i < BITS_PER_MAP; i++) {
658 1.1 cgd if (!(mask & map))
659 1.1 cgd return (i);
660 1.1 cgd mask = mask << 1;
661 1.1 cgd }
662 1.1 cgd return (i);
663 1.1 cgd }
664 1.1 cgd
665 1.1 cgd static u_short
666 1.1 cgd overflow_page(hashp)
667 1.1 cgd HTAB *hashp;
668 1.1 cgd {
669 1.1 cgd register u_long *freep;
670 1.1 cgd register int max_free, offset, splitnum;
671 1.1 cgd u_short addr;
672 1.1 cgd int bit, first_page, free_bit, free_page, i, in_use_bits, j;
673 1.1 cgd #ifdef DEBUG2
674 1.1 cgd int tmp1, tmp2;
675 1.1 cgd #endif
676 1.1 cgd splitnum = hashp->OVFL_POINT;
677 1.1 cgd max_free = hashp->SPARES[splitnum];
678 1.1 cgd
679 1.1 cgd free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
680 1.1 cgd free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
681 1.1 cgd
682 1.1 cgd /* Look through all the free maps to find the first free block */
683 1.1 cgd first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
684 1.1 cgd for ( i = first_page; i <= free_page; i++ ) {
685 1.1 cgd if (!(freep = (u_long *)hashp->mapp[i]) &&
686 1.1 cgd !(freep = fetch_bitmap(hashp, i)))
687 1.1 cgd return (NULL);
688 1.1 cgd if (i == free_page)
689 1.1 cgd in_use_bits = free_bit;
690 1.1 cgd else
691 1.1 cgd in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
692 1.1 cgd
693 1.1 cgd if (i == first_page) {
694 1.1 cgd bit = hashp->LAST_FREED &
695 1.1 cgd ((hashp->BSIZE << BYTE_SHIFT) - 1);
696 1.1 cgd j = bit / BITS_PER_MAP;
697 1.1 cgd bit = bit & ~(BITS_PER_MAP - 1);
698 1.1 cgd } else {
699 1.1 cgd bit = 0;
700 1.1 cgd j = 0;
701 1.1 cgd }
702 1.1 cgd for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
703 1.1 cgd if (freep[j] != ALL_SET)
704 1.1 cgd goto found;
705 1.1 cgd }
706 1.1 cgd
707 1.1 cgd /* No Free Page Found */
708 1.1 cgd hashp->LAST_FREED = hashp->SPARES[splitnum];
709 1.1 cgd hashp->SPARES[splitnum]++;
710 1.1 cgd offset = hashp->SPARES[splitnum] -
711 1.1 cgd (splitnum ? hashp->SPARES[splitnum - 1] : 0);
712 1.1 cgd
713 1.1 cgd #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
714 1.1 cgd if (offset > SPLITMASK) {
715 1.1 cgd if (++splitnum >= NCACHED) {
716 1.1 cgd (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
717 1.1 cgd return (NULL);
718 1.1 cgd }
719 1.1 cgd hashp->OVFL_POINT = splitnum;
720 1.1 cgd hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
721 1.1 cgd hashp->SPARES[splitnum-1]--;
722 1.1 cgd offset = 1;
723 1.1 cgd }
724 1.1 cgd
725 1.1 cgd /* Check if we need to allocate a new bitmap page */
726 1.1 cgd if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
727 1.1 cgd free_page++;
728 1.1 cgd if (free_page >= NCACHED) {
729 1.1 cgd (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
730 1.1 cgd return (NULL);
731 1.1 cgd }
732 1.1 cgd /*
733 1.1 cgd * This is tricky. The 1 indicates that you want the new page
734 1.1 cgd * allocated with 1 clear bit. Actually, you are going to
735 1.1 cgd * allocate 2 pages from this map. The first is going to be
736 1.1 cgd * the map page, the second is the overflow page we were
737 1.1 cgd * looking for. The init_bitmap routine automatically, sets
738 1.1 cgd * the first bit of itself to indicate that the bitmap itself
739 1.1 cgd * is in use. We would explicitly set the second bit, but
740 1.1 cgd * don't have to if we tell init_bitmap not to leave it clear
741 1.1 cgd * in the first place.
742 1.1 cgd */
743 1.1 cgd if (__init_bitmap(hashp, (int)OADDR_OF(splitnum, offset),
744 1.1 cgd 1, free_page))
745 1.1 cgd return (NULL);
746 1.1 cgd hashp->SPARES[splitnum]++;
747 1.1 cgd #ifdef DEBUG2
748 1.1 cgd free_bit = 2;
749 1.1 cgd #endif
750 1.1 cgd offset++;
751 1.1 cgd if (offset > SPLITMASK) {
752 1.1 cgd if (++splitnum >= NCACHED) {
753 1.1 cgd (void)write(STDERR_FILENO, OVMSG,
754 1.1 cgd sizeof(OVMSG) - 1);
755 1.1 cgd return (NULL);
756 1.1 cgd }
757 1.1 cgd hashp->OVFL_POINT = splitnum;
758 1.1 cgd hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
759 1.1 cgd hashp->SPARES[splitnum-1]--;
760 1.1 cgd offset = 0;
761 1.1 cgd }
762 1.1 cgd } else {
763 1.1 cgd /*
764 1.1 cgd * Free_bit addresses the last used bit. Bump it to address
765 1.1 cgd * the first available bit.
766 1.1 cgd */
767 1.1 cgd free_bit++;
768 1.1 cgd SETBIT(freep, free_bit);
769 1.1 cgd }
770 1.1 cgd
771 1.1 cgd /* Calculate address of the new overflow page */
772 1.1 cgd addr = OADDR_OF(splitnum, offset);
773 1.1 cgd #ifdef DEBUG2
774 1.1 cgd (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
775 1.1 cgd addr, free_bit, free_page);
776 1.1 cgd #endif
777 1.1 cgd return (addr);
778 1.1 cgd
779 1.1 cgd found:
780 1.1 cgd bit = bit + first_free(freep[j]);
781 1.1 cgd SETBIT(freep, bit);
782 1.1 cgd #ifdef DEBUG2
783 1.1 cgd tmp1 = bit;
784 1.1 cgd tmp2 = i;
785 1.1 cgd #endif
786 1.1 cgd /*
787 1.1 cgd * Bits are addressed starting with 0, but overflow pages are addressed
788 1.1 cgd * beginning at 1. Bit is a bit addressnumber, so we need to increment
789 1.1 cgd * it to convert it to a page number.
790 1.1 cgd */
791 1.1 cgd bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
792 1.1 cgd if (bit >= hashp->LAST_FREED)
793 1.1 cgd hashp->LAST_FREED = bit - 1;
794 1.1 cgd
795 1.1 cgd /* Calculate the split number for this page */
796 1.1 cgd for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
797 1.1 cgd offset = (i ? bit - hashp->SPARES[i - 1] : bit);
798 1.1 cgd if (offset >= SPLITMASK)
799 1.1 cgd return (NULL); /* Out of overflow pages */
800 1.1 cgd addr = OADDR_OF(i, offset);
801 1.1 cgd #ifdef DEBUG2
802 1.1 cgd (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
803 1.1 cgd addr, tmp1, tmp2);
804 1.1 cgd #endif
805 1.1 cgd
806 1.1 cgd /* Allocate and return the overflow page */
807 1.1 cgd return (addr);
808 1.1 cgd }
809 1.1 cgd
810 1.1 cgd /*
811 1.1 cgd * Mark this overflow page as free.
812 1.1 cgd */
813 1.1 cgd extern void
814 1.1 cgd __free_ovflpage(hashp, obufp)
815 1.1 cgd HTAB *hashp;
816 1.1 cgd BUFHEAD *obufp;
817 1.1 cgd {
818 1.1 cgd register u_short addr;
819 1.1 cgd u_long *freep;
820 1.1 cgd int bit_address, free_page, free_bit;
821 1.1 cgd u_short ndx;
822 1.1 cgd
823 1.1 cgd addr = obufp->addr;
824 1.1 cgd #ifdef DEBUG1
825 1.1 cgd (void)fprintf(stderr, "Freeing %d\n", addr);
826 1.1 cgd #endif
827 1.1 cgd ndx = (((u_short)addr) >> SPLITSHIFT);
828 1.1 cgd bit_address =
829 1.1 cgd (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
830 1.1 cgd if (bit_address < hashp->LAST_FREED)
831 1.1 cgd hashp->LAST_FREED = bit_address;
832 1.1 cgd free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
833 1.1 cgd free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
834 1.1 cgd
835 1.1 cgd if (!(freep = hashp->mapp[free_page]))
836 1.1 cgd freep = fetch_bitmap(hashp, free_page);
837 1.1 cgd #ifdef DEBUG
838 1.1 cgd /*
839 1.1 cgd * This had better never happen. It means we tried to read a bitmap
840 1.1 cgd * that has already had overflow pages allocated off it, and we
841 1.1 cgd * failed to read it from the file.
842 1.1 cgd */
843 1.1 cgd if (!freep)
844 1.1 cgd assert(0);
845 1.1 cgd #endif
846 1.1 cgd CLRBIT(freep, free_bit);
847 1.1 cgd #ifdef DEBUG2
848 1.1 cgd (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
849 1.1 cgd obufp->addr, free_bit, free_page);
850 1.1 cgd #endif
851 1.1 cgd __reclaim_buf(hashp, obufp);
852 1.1 cgd }
853 1.1 cgd
854 1.1 cgd /*
855 1.1 cgd * Returns:
856 1.1 cgd * 0 success
857 1.1 cgd * -1 failure
858 1.1 cgd */
859 1.1 cgd static int
860 1.1 cgd open_temp(hashp)
861 1.1 cgd HTAB *hashp;
862 1.1 cgd {
863 1.1 cgd sigset_t set, oset;
864 1.1 cgd static char namestr[] = "_hashXXXXXX";
865 1.1 cgd
866 1.1 cgd /* Block signals; make sure file goes away at process exit. */
867 1.1 cgd (void)sigfillset(&set);
868 1.1 cgd (void)sigprocmask(SIG_BLOCK, &set, &oset);
869 1.1 cgd if ((hashp->fp = mkstemp(namestr)) != -1) {
870 1.1 cgd (void)unlink(namestr);
871 1.1 cgd (void)fcntl(hashp->fp, F_SETFD, 1);
872 1.1 cgd }
873 1.1 cgd (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
874 1.1 cgd return (hashp->fp != -1 ? 0 : -1);
875 1.1 cgd }
876 1.1 cgd
877 1.1 cgd /*
878 1.1 cgd * We have to know that the key will fit, but the last entry on the page is
879 1.1 cgd * an overflow pair, so we need to shift things.
880 1.1 cgd */
881 1.1 cgd static void
882 1.1 cgd squeeze_key(sp, key, val)
883 1.1 cgd u_short *sp;
884 1.1 cgd const DBT *key, *val;
885 1.1 cgd {
886 1.1 cgd register char *p;
887 1.1 cgd u_short free_space, n, off, pageno;
888 1.1 cgd
889 1.1 cgd p = (char *)sp;
890 1.1 cgd n = sp[0];
891 1.1 cgd free_space = FREESPACE(sp);
892 1.1 cgd off = OFFSET(sp);
893 1.1 cgd
894 1.1 cgd pageno = sp[n - 1];
895 1.1 cgd off -= key->size;
896 1.1 cgd sp[n - 1] = off;
897 1.1 cgd memmove(p + off, key->data, key->size);
898 1.1 cgd off -= val->size;
899 1.1 cgd sp[n] = off;
900 1.1 cgd memmove(p + off, val->data, val->size);
901 1.1 cgd sp[0] = n + 2;
902 1.1 cgd sp[n + 1] = pageno;
903 1.1 cgd sp[n + 2] = OVFLPAGE;
904 1.1 cgd FREESPACE(sp) = free_space - PAIRSIZE(key, val);
905 1.1 cgd OFFSET(sp) = off;
906 1.1 cgd }
907 1.1 cgd
908 1.1 cgd static u_long *
909 1.1 cgd fetch_bitmap(hashp, ndx)
910 1.1 cgd HTAB *hashp;
911 1.1 cgd int ndx;
912 1.1 cgd {
913 1.1 cgd if (ndx >= hashp->nmaps ||
914 1.1 cgd !(hashp->mapp[ndx] = malloc(hashp->BSIZE)) ||
915 1.1 cgd __get_page(hashp, (char *)hashp->mapp[ndx],
916 1.1 cgd hashp->BITMAPS[ndx], 0, 1, 1))
917 1.1 cgd return (NULL);
918 1.1 cgd return (hashp->mapp[ndx]);
919 1.1 cgd }
920 1.1 cgd
921 1.1 cgd #ifdef DEBUG4
922 1.1 cgd int
923 1.1 cgd print_chain(addr)
924 1.1 cgd int addr;
925 1.1 cgd {
926 1.1 cgd BUFHEAD *bufp;
927 1.1 cgd short *bp, oaddr;
928 1.1 cgd
929 1.1 cgd (void)fprintf(stderr, "%d ", addr);
930 1.1 cgd bufp = __get_buf(hashp, addr, NULL, 0);
931 1.1 cgd bp = (short *)bufp->page;
932 1.1 cgd while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
933 1.1 cgd ((bp[0] > 2) && bp[2] < REAL_KEY))) {
934 1.1 cgd oaddr = bp[bp[0] - 1];
935 1.1 cgd (void)fprintf(stderr, "%d ", (int)oaddr);
936 1.1 cgd bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
937 1.1 cgd bp = (short *)bufp->page;
938 1.1 cgd }
939 1.1 cgd (void)fprintf(stderr, "\n");
940 1.1 cgd }
941 1.1 cgd #endif
942