2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)hash_bigkey.c 8.3 (Berkeley) 5/31/94
42 * Big key/data handling for the hashing package.
57 #include <sys/param.h>
73 static int collect_key __P((HTAB *, BUFHEAD *, int, DBT *, int));
74 static int collect_data __P((HTAB *, BUFHEAD *, int, int));
79 * You need to do an insert and the key/data pair is too big
86 __big_insert(hashp, bufp, key, val)
91 register u_int16_t *p;
92 int key_size, n, val_size;
93 u_int16_t space, move_bytes, off;
94 char *cp, *key_data, *val_data;
96 cp = bufp->page; /* Character pointer of p. */
99 key_data = (char *)key->data;
100 key_size = key->size;
101 val_data = (char *)val->data;
102 val_size = val->size;
104 /* First move the Key */
105 for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
106 space = FREESPACE(p) - BIGOVERHEAD) {
107 move_bytes = MIN(space, key_size);
108 off = OFFSET(p) - move_bytes;
109 memmove(cp + off, key_data, move_bytes);
110 key_size -= move_bytes;
111 key_data += move_bytes;
115 FREESPACE(p) = off - PAGE_META(n);
118 bufp = __add_ovflpage(hashp, bufp);
124 move_bytes = MIN(FREESPACE(p), val_size);
125 off = OFFSET(p) - move_bytes;
127 memmove(cp + off, val_data, move_bytes);
128 val_data += move_bytes;
129 val_size -= move_bytes;
130 p[n - 2] = FULL_KEY_DATA;
131 FREESPACE(p) = FREESPACE(p) - move_bytes;
135 p = (u_int16_t *)bufp->page;
137 bufp->flags |= BUF_MOD;
140 /* Now move the data */
141 for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
142 space = FREESPACE(p) - BIGOVERHEAD) {
143 move_bytes = MIN(space, val_size);
145 * Here's the hack to make sure that if the data ends on the
146 * same page as the key ends, FREESPACE is at least one.
148 if (space == val_size && val_size == val->size)
150 off = OFFSET(p) - move_bytes;
151 memmove(cp + off, val_data, move_bytes);
152 val_size -= move_bytes;
153 val_data += move_bytes;
157 FREESPACE(p) = off - PAGE_META(n);
161 bufp = __add_ovflpage(hashp, bufp);
167 p[n] = FULL_KEY_DATA;
168 bufp->flags |= BUF_MOD;
174 * Called when bufp's page contains a partial key (index should be 1)
176 * All pages in the big key/data pair except bufp are freed. We cannot
177 * free bufp because the page pointing to it is lost and we can't get rid
185 __big_delete(hashp, bufp)
189 register BUFHEAD *last_bfp, *rbufp;
190 u_int16_t *bp, pageno;
195 bp = (u_int16_t *)bufp->page;
199 while (!key_done || (bp[2] != FULL_KEY_DATA)) {
200 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
204 * If there is freespace left on a FULL_KEY_DATA page, then
205 * the data is short and fits entirely on this page, and this
208 if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
210 pageno = bp[bp[0] - 1];
211 rbufp->flags |= BUF_MOD;
212 rbufp = __get_buf(hashp, pageno, rbufp, 0);
214 __free_ovflpage(hashp, last_bfp);
217 return (-1); /* Error. */
218 bp = (u_int16_t *)rbufp->page;
222 * If we get here then rbufp points to the last page of the big
223 * key/data pair. Bufp points to the first one -- it should now be
224 * empty pointing to the next page after this pair. Can't free it
225 * because we don't have the page pointing to it.
228 /* This is information from the last page of the pair. */
232 /* Now, bp is the first page of the pair. */
233 bp = (u_int16_t *)bufp->page;
235 /* There is an overflow page. */
238 bufp->ovfl = rbufp->ovfl;
240 /* This is the last page. */
244 FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
245 OFFSET(bp) = hashp->BSIZE - 1;
247 bufp->flags |= BUF_MOD;
249 __free_ovflpage(hashp, rbufp);
250 if (last_bfp != rbufp)
251 __free_ovflpage(hashp, last_bfp);
259 * -1 = get next overflow page
260 * -2 means key not found and this is big key/data
264 __find_bigpair(hashp, bufp, ndx, key, size)
271 register u_int16_t *bp;
277 bp = (u_int16_t *)bufp->page;
282 for (bytes = hashp->BSIZE - bp[ndx];
283 bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
284 bytes = hashp->BSIZE - bp[ndx]) {
285 if (memcmp(p + bp[ndx], kkey, bytes))
289 bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
297 if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
298 #ifdef HASH_STATISTICS
307 * Given the buffer pointer of the first overflow page of a big pair,
308 * find the end of the big pair
310 * This will set bpp to the buffer header of the last page of the big pair.
311 * It will return the pageno of the overflow page following the last page
312 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
316 __find_last_page(hashp, bpp)
321 u_int16_t *bp, pageno;
325 bp = (u_int16_t *)bufp->page;
330 * This is the last page if: the tag is FULL_KEY_DATA and
331 * either only 2 entries OVFLPAGE marker is explicit there
332 * is freespace on the page.
334 if (bp[2] == FULL_KEY_DATA &&
335 ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
339 bufp = __get_buf(hashp, pageno, bufp, 0);
341 return (0); /* Need to indicate an error! */
342 bp = (u_int16_t *)bufp->page;
353 * Return the data for the key/data pair that begins on this page at this
354 * index (index should always be 1).
357 __big_return(hashp, bufp, ndx, val, set_current)
365 u_int16_t *bp, len, off, save_addr;
368 bp = (u_int16_t *)bufp->page;
369 while (bp[ndx + 1] == PARTIAL_KEY) {
370 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
373 bp = (u_int16_t *)bufp->page;
377 if (bp[ndx + 1] == FULL_KEY) {
378 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
381 bp = (u_int16_t *)bufp->page;
383 save_addr = save_p->addr;
387 if (!FREESPACE(bp)) {
389 * This is a hack. We can't distinguish between
390 * FULL_KEY_DATA that contains complete data or
391 * incomplete data, so we require that if the data
392 * is complete, there is at least 1 byte of free
398 save_addr = bufp->addr;
399 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
402 bp = (u_int16_t *)bufp->page;
404 /* The data is all on one page. */
407 val->data = (u_char *)tp + off;
408 val->size = bp[1] - off;
410 if (bp[0] == 2) { /* No more buckets in
416 hashp->cpage = __get_buf(hashp,
417 bp[bp[0] - 1], bufp, 0);
422 hashp->cpage->page)[0]) {
431 val->size = collect_data(hashp, bufp, (int)len, set_current);
434 if (save_p->addr != save_addr) {
435 /* We are pretty short on buffers. */
436 errno = EINVAL; /* OUT OF BUFFERS */
439 memmove(hashp->tmp_buf, (save_p->page) + off, len);
440 val->data = (u_char *)hashp->tmp_buf;
444 * Count how big the total datasize is by recursing through the pages. Then
445 * allocate a buffer and copy the data as you recurse up.
448 collect_data(hashp, bufp, len, set)
453 register u_int16_t *bp;
461 mylen = hashp->BSIZE - bp[1];
462 save_addr = bufp->addr;
464 if (bp[2] == FULL_KEY_DATA) { /* End of Data */
465 totlen = len + mylen;
467 free(hashp->tmp_buf);
468 if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
472 if (bp[0] == 2) { /* No more buckets in chain */
477 __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
480 else if (!((u_int16_t *)hashp->cpage->page)[0]) {
487 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
488 if (!xbp || ((totlen =
489 collect_data(hashp, xbp, len + mylen, set)) < 1))
492 if (bufp->addr != save_addr) {
493 errno = EINVAL; /* Out of buffers. */
496 memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
501 * Fill in the key and data for this big pair.
504 __big_keydata(hashp, bufp, key, val, set)
510 key->size = collect_key(hashp, bufp, 0, val, set);
513 key->data = (u_char *)hashp->tmp_key;
518 * Count how big the total key size is by recursing through the pages. Then
519 * collect the data, allocate a buffer and copy the key as you recurse up.
522 collect_key(hashp, bufp, len, val, set)
532 u_int16_t *bp, save_addr;
536 mylen = hashp->BSIZE - bp[1];
538 save_addr = bufp->addr;
539 totlen = len + mylen;
540 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
541 if (hashp->tmp_key != NULL)
542 free(hashp->tmp_key);
543 if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
545 if (__big_return(hashp, bufp, 1, val, set))
548 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
549 if (!xbp || ((totlen =
550 collect_key(hashp, xbp, totlen, val, set)) < 1))
553 if (bufp->addr != save_addr) {
554 errno = EINVAL; /* MIS -- OUT OF BUFFERS */
557 memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
567 __big_split(hashp, op, np, big_keyp, addr, obucket, ret)
569 BUFHEAD *op; /* Pointer to where to put keys that go in old bucket */
570 BUFHEAD *np; /* Pointer to new bucket page */
571 /* Pointer to first page containing the big key/data */
573 int addr; /* Address of big_keyp */
574 u_int32_t obucket;/* Old Bucket */
577 register BUFHEAD *tmpp;
578 register u_int16_t *tp;
582 u_int16_t free_space, n, off;
586 /* Now figure out where the big key/data goes */
587 if (__big_keydata(hashp, big_keyp, &key, &val, 0))
589 change = (__call_hash(hashp, key.data, key.size) != obucket);
591 if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
593 __get_buf(hashp, ret->next_addr, big_keyp, 0)))
598 /* Now make one of np/op point to the big key/data pair */
600 assert(np->ovfl == NULL);
607 tmpp->flags |= BUF_MOD;
609 (void)fprintf(stderr,
610 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
611 (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
613 tmpp->ovfl = bp; /* one of op/np point to big_keyp */
614 tp = (u_int16_t *)tmpp->page;
616 assert(FREESPACE(tp) >= OVFLSIZE);
620 free_space = FREESPACE(tp);
621 tp[++n] = (u_int16_t)addr;
625 FREESPACE(tp) = free_space - OVFLSIZE;
628 * Finally, set the new and old return values. BIG_KEYP contains a
629 * pointer to the last page of the big key_data pair. Make sure that
630 * big_keyp has no following page (2 elements) or create an empty
637 tp = (u_int16_t *)big_keyp->page;
638 big_keyp->flags |= BUF_MOD;
641 * There may be either one or two offsets on this page. If
642 * there is one, then the overflow page is linked on normally
643 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
644 * the second offset and needs to get stuffed in after the
645 * next overflow page is added.
648 free_space = FREESPACE(tp);
651 FREESPACE(tp) = free_space + OVFLSIZE;
653 tmpp = __add_ovflpage(hashp, big_keyp);