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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)hash_bigkey.c 8.3 (Berkeley) 5/31/94
33 * $DragonFly: src/lib/libc/db/hash/hash_bigkey.c,v 1.10 2006/08/03 16:40:46 swildner Exp $
39 * Big key/data handling for the hashing package.
54 #include <sys/param.h>
70 static int collect_key (HTAB *, BUFHEAD *, int, DBT *, int);
71 static int collect_data (HTAB *, BUFHEAD *, int, int);
76 * You need to do an insert and the key/data pair is too big
83 __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
86 int key_size, n, val_size;
87 u_int16_t space, move_bytes, off;
88 char *cp, *key_data, *val_data;
90 cp = bufp->page; /* Character pointer of p. */
93 key_data = (char *)key->data;
95 val_data = (char *)val->data;
98 /* First move the Key */
99 for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
100 space = FREESPACE(p) - BIGOVERHEAD) {
101 move_bytes = MIN(space, key_size);
102 off = OFFSET(p) - move_bytes;
103 memmove(cp + off, key_data, move_bytes);
104 key_size -= move_bytes;
105 key_data += move_bytes;
109 FREESPACE(p) = off - PAGE_META(n);
112 bufp = __add_ovflpage(hashp, bufp);
118 move_bytes = MIN(FREESPACE(p), val_size);
119 off = OFFSET(p) - move_bytes;
121 memmove(cp + off, val_data, move_bytes);
122 val_data += move_bytes;
123 val_size -= move_bytes;
124 p[n - 2] = FULL_KEY_DATA;
125 FREESPACE(p) = FREESPACE(p) - move_bytes;
130 p = (u_int16_t *)bufp->page;
132 bufp->flags |= BUF_MOD;
135 /* Now move the data */
136 for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
137 space = FREESPACE(p) - BIGOVERHEAD) {
138 move_bytes = MIN(space, val_size);
140 * Here's the hack to make sure that if the data ends on the
141 * same page as the key ends, FREESPACE is at least one.
143 if (space == val_size && val_size == val->size)
145 off = OFFSET(p) - move_bytes;
146 memmove(cp + off, val_data, move_bytes);
147 val_size -= move_bytes;
148 val_data += move_bytes;
152 FREESPACE(p) = off - PAGE_META(n);
156 bufp = __add_ovflpage(hashp, bufp);
162 p[n] = FULL_KEY_DATA;
163 bufp->flags |= BUF_MOD;
169 * Called when bufp's page contains a partial key (index should be 1)
171 * All pages in the big key/data pair except bufp are freed. We cannot
172 * free bufp because the page pointing to it is lost and we can't get rid
180 __big_delete(HTAB *hashp, BUFHEAD *bufp)
182 BUFHEAD *last_bfp, *rbufp;
183 u_int16_t *bp, pageno;
188 bp = (u_int16_t *)bufp->page;
192 while (!key_done || (bp[2] != FULL_KEY_DATA)) {
193 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
197 * If there is freespace left on a FULL_KEY_DATA page, then
198 * the data is short and fits entirely on this page, and this
201 if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
203 pageno = bp[bp[0] - 1];
204 rbufp->flags |= BUF_MOD;
205 rbufp = __get_buf(hashp, pageno, rbufp, 0);
207 __free_ovflpage(hashp, last_bfp);
210 return (-1); /* Error. */
211 bp = (u_int16_t *)rbufp->page;
215 * If we get here then rbufp points to the last page of the big
216 * key/data pair. Bufp points to the first one -- it should now be
217 * empty pointing to the next page after this pair. Can't free it
218 * because we don't have the page pointing to it.
221 /* This is information from the last page of the pair. */
225 /* Now, bp is the first page of the pair. */
226 bp = (u_int16_t *)bufp->page;
228 /* There is an overflow page. */
231 bufp->ovfl = rbufp->ovfl;
233 /* This is the last page. */
237 FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
238 OFFSET(bp) = hashp->BSIZE - 1;
240 bufp->flags |= BUF_MOD;
242 __free_ovflpage(hashp, rbufp);
243 if (last_bfp != rbufp)
244 __free_ovflpage(hashp, last_bfp);
252 * -1 = get next overflow page
253 * -2 means key not found and this is big key/data
257 __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, const char *key, int size)
265 bp = (u_int16_t *)bufp->page;
270 for (bytes = hashp->BSIZE - bp[ndx];
271 bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
272 bytes = hashp->BSIZE - bp[ndx]) {
273 if (memcmp(p + bp[ndx], kkey, bytes))
277 bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
285 if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
286 #ifdef HASH_STATISTICS
295 * Given the buffer pointer of the first overflow page of a big pair,
296 * find the end of the big pair
298 * This will set bpp to the buffer header of the last page of the big pair.
299 * It will return the pageno of the overflow page following the last page
300 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
304 __find_last_page(HTAB *hashp, BUFHEAD **bpp)
307 u_int16_t *bp, pageno;
311 bp = (u_int16_t *)bufp->page;
316 * This is the last page if: the tag is FULL_KEY_DATA and
317 * either only 2 entries OVFLPAGE marker is explicit there
318 * is freespace on the page.
320 if (bp[2] == FULL_KEY_DATA &&
321 ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
325 bufp = __get_buf(hashp, pageno, bufp, 0);
327 return (0); /* Need to indicate an error! */
328 bp = (u_int16_t *)bufp->page;
339 * Return the data for the key/data pair that begins on this page at this
340 * index (index should always be 1).
343 __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current)
346 u_int16_t *bp, len, off, save_addr;
349 bp = (u_int16_t *)bufp->page;
350 while (bp[ndx + 1] == PARTIAL_KEY) {
351 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
354 bp = (u_int16_t *)bufp->page;
358 if (bp[ndx + 1] == FULL_KEY) {
359 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
362 bp = (u_int16_t *)bufp->page;
364 save_addr = save_p->addr;
368 if (!FREESPACE(bp)) {
370 * This is a hack. We can't distinguish between
371 * FULL_KEY_DATA that contains complete data or
372 * incomplete data, so we require that if the data
373 * is complete, there is at least 1 byte of free
379 save_addr = bufp->addr;
380 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
383 bp = (u_int16_t *)bufp->page;
385 /* The data is all on one page. */
388 val->data = (u_char *)tp + off;
389 val->size = bp[1] - off;
391 if (bp[0] == 2) { /* No more buckets in
397 hashp->cpage = __get_buf(hashp,
398 bp[bp[0] - 1], bufp, 0);
403 hashp->cpage->page)[0]) {
412 val->size = collect_data(hashp, bufp, (int)len, set_current);
415 if (save_p->addr != save_addr) {
416 /* We are pretty short on buffers. */
417 errno = EINVAL; /* OUT OF BUFFERS */
420 memmove(hashp->tmp_buf, (save_p->page) + off, len);
421 val->data = (u_char *)hashp->tmp_buf;
425 * Count how big the total datasize is by recursing through the pages. Then
426 * allocate a buffer and copy the data as you recurse up.
429 collect_data(HTAB *hashp, BUFHEAD *bufp, int len, int set)
439 mylen = hashp->BSIZE - bp[1];
440 save_addr = bufp->addr;
442 if (bp[2] == FULL_KEY_DATA) { /* End of Data */
443 totlen = len + mylen;
445 free(hashp->tmp_buf);
446 if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
450 if (bp[0] == 2) { /* No more buckets in chain */
455 __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
458 else if (!((u_int16_t *)hashp->cpage->page)[0]) {
465 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
466 if (!xbp || ((totlen =
467 collect_data(hashp, xbp, len + mylen, set)) < 1))
470 if (bufp->addr != save_addr) {
471 errno = EINVAL; /* Out of buffers. */
474 memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
479 * Fill in the key and data for this big pair.
482 __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set)
484 key->size = collect_key(hashp, bufp, 0, val, set);
487 key->data = (u_char *)hashp->tmp_key;
492 * Count how big the total key size is by recursing through the pages. Then
493 * collect the data, allocate a buffer and copy the key as you recurse up.
496 collect_key(HTAB *hashp, BUFHEAD *bufp, int len, DBT *val, int set)
501 u_int16_t *bp, save_addr;
505 mylen = hashp->BSIZE - bp[1];
507 save_addr = bufp->addr;
508 totlen = len + mylen;
509 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
510 if (hashp->tmp_key != NULL)
511 free(hashp->tmp_key);
512 if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
514 if (__big_return(hashp, bufp, 1, val, set))
517 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
518 if (!xbp || ((totlen =
519 collect_key(hashp, xbp, totlen, val, set)) < 1))
522 if (bufp->addr != save_addr) {
523 errno = EINVAL; /* MIS -- OUT OF BUFFERS */
526 memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
532 * op: Pointer to where to put keys that go in old bucket
533 * np: Pointer to new bucket page
534 * big_keyp: Pointer to first page containing the big key/data
535 * addr: Address of big_keyp
536 * obucket: Old Bucket
542 __big_split(HTAB *hashp, BUFHEAD *op, BUFHEAD *np, BUFHEAD *big_keyp,
543 int addr, u_int32_t obucket, SPLIT_RETURN *ret)
550 u_int16_t free_space, n, off;
554 /* Now figure out where the big key/data goes */
555 if (__big_keydata(hashp, big_keyp, &key, &val, 0))
557 change = (__call_hash(hashp, key.data, key.size) != obucket);
559 if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
561 __get_buf(hashp, ret->next_addr, big_keyp, 0)))
566 /* Now make one of np/op point to the big key/data pair */
568 assert(np->ovfl == NULL);
575 tmpp->flags |= BUF_MOD;
578 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
579 (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
581 tmpp->ovfl = bp; /* one of op/np point to big_keyp */
582 tp = (u_int16_t *)tmpp->page;
584 assert(FREESPACE(tp) >= OVFLSIZE);
588 free_space = FREESPACE(tp);
589 tp[++n] = (u_int16_t)addr;
593 FREESPACE(tp) = free_space - OVFLSIZE;
596 * Finally, set the new and old return values. BIG_KEYP contains a
597 * pointer to the last page of the big key_data pair. Make sure that
598 * big_keyp has no following page (2 elements) or create an empty
605 tp = (u_int16_t *)big_keyp->page;
606 big_keyp->flags |= BUF_MOD;
609 * There may be either one or two offsets on this page. If
610 * there is one, then the overflow page is linked on normally
611 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
612 * the second offset and needs to get stuffed in after the
613 * next overflow page is added.
616 free_space = FREESPACE(tp);
619 FREESPACE(tp) = free_space + OVFLSIZE;
621 tmpp = __add_ovflpage(hashp, big_keyp);