2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)hash_bigkey.c 8.3 (Berkeley) 5/31/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
44 * Big key/data handling for the hashing package.
59 #include <sys/param.h>
75 static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
76 static int collect_data(HTAB *, BUFHEAD *, int, int);
81 * You need to do an insert and the key/data pair is too big
88 __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
92 unsigned int 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);
123 space = FREESPACE(p);
125 move_bytes = MIN(space, val_size);
127 * If the data would fit exactly in the
128 * remaining space, we must overflow it to the
129 * next page; otherwise the invariant that the
130 * data must end on a page with FREESPACE
131 * non-zero would fail.
133 if (space == val_size && val_size == val->size)
135 off = OFFSET(p) - move_bytes;
136 memmove(cp + off, val_data, move_bytes);
137 val_data += move_bytes;
138 val_size -= move_bytes;
140 p[n - 2] = FULL_KEY_DATA;
141 FREESPACE(p) = FREESPACE(p) - move_bytes;
148 p = (u_int16_t *)bufp->page;
150 bufp->flags |= BUF_MOD;
153 /* Now move the data */
154 for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
155 space = FREESPACE(p) - BIGOVERHEAD) {
156 move_bytes = MIN(space, val_size);
158 * Here's the hack to make sure that if the data ends on the
159 * same page as the key ends, FREESPACE is at least one.
161 if (space == val_size && val_size == val->size)
163 off = OFFSET(p) - move_bytes;
164 memmove(cp + off, val_data, move_bytes);
165 val_size -= move_bytes;
166 val_data += move_bytes;
170 FREESPACE(p) = off - PAGE_META(n);
174 bufp = __add_ovflpage(hashp, bufp);
180 p[n] = FULL_KEY_DATA;
181 bufp->flags |= BUF_MOD;
187 * Called when bufp's page contains a partial key (index should be 1)
189 * All pages in the big key/data pair except bufp are freed. We cannot
190 * free bufp because the page pointing to it is lost and we can't get rid
198 __big_delete(HTAB *hashp, BUFHEAD *bufp)
200 BUFHEAD *last_bfp, *rbufp;
201 u_int16_t *bp, pageno;
206 bp = (u_int16_t *)bufp->page;
210 while (!key_done || (bp[2] != FULL_KEY_DATA)) {
211 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
215 * If there is freespace left on a FULL_KEY_DATA page, then
216 * the data is short and fits entirely on this page, and this
219 if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
221 pageno = bp[bp[0] - 1];
222 rbufp->flags |= BUF_MOD;
223 rbufp = __get_buf(hashp, pageno, rbufp, 0);
225 __free_ovflpage(hashp, last_bfp);
228 return (-1); /* Error. */
229 bp = (u_int16_t *)rbufp->page;
233 * If we get here then rbufp points to the last page of the big
234 * key/data pair. Bufp points to the first one -- it should now be
235 * empty pointing to the next page after this pair. Can't free it
236 * because we don't have the page pointing to it.
239 /* This is information from the last page of the pair. */
243 /* Now, bp is the first page of the pair. */
244 bp = (u_int16_t *)bufp->page;
246 /* There is an overflow page. */
249 bufp->ovfl = rbufp->ovfl;
251 /* This is the last page. */
255 FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
256 OFFSET(bp) = hashp->BSIZE;
258 bufp->flags |= BUF_MOD;
260 __free_ovflpage(hashp, rbufp);
261 if (last_bfp && last_bfp != rbufp)
262 __free_ovflpage(hashp, last_bfp);
270 * -1 = get next overflow page
271 * -2 means key not found and this is big key/data
275 __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
283 bp = (u_int16_t *)bufp->page;
288 for (bytes = hashp->BSIZE - bp[ndx];
289 bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
290 bytes = hashp->BSIZE - bp[ndx]) {
291 if (memcmp(p + bp[ndx], kkey, bytes))
295 bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
303 if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
304 #ifdef HASH_STATISTICS
313 * Given the buffer pointer of the first overflow page of a big pair,
314 * find the end of the big pair
316 * This will set bpp to the buffer header of the last page of the big pair.
317 * It will return the pageno of the overflow page following the last page
318 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
322 __find_last_page(HTAB *hashp, BUFHEAD **bpp)
325 u_int16_t *bp, pageno;
329 bp = (u_int16_t *)bufp->page;
334 * This is the last page if: the tag is FULL_KEY_DATA and
335 * either only 2 entries OVFLPAGE marker is explicit there
336 * is freespace on the page.
338 if (bp[2] == FULL_KEY_DATA &&
339 ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
343 bufp = __get_buf(hashp, pageno, bufp, 0);
345 return (0); /* Need to indicate an error! */
346 bp = (u_int16_t *)bufp->page;
357 * Return the data for the key/data pair that begins on this page at this
358 * index (index should always be 1).
361 __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current)
364 u_int16_t *bp, len, off, save_addr;
367 bp = (u_int16_t *)bufp->page;
368 while (bp[ndx + 1] == PARTIAL_KEY) {
369 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
372 bp = (u_int16_t *)bufp->page;
376 if (bp[ndx + 1] == FULL_KEY) {
377 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
380 bp = (u_int16_t *)bufp->page;
382 save_addr = save_p->addr;
386 if (!FREESPACE(bp)) {
388 * This is a hack. We can't distinguish between
389 * FULL_KEY_DATA that contains complete data or
390 * incomplete data, so we require that if the data
391 * is complete, there is at least 1 byte of free
397 save_addr = bufp->addr;
398 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
401 bp = (u_int16_t *)bufp->page;
403 /* The data is all on one page. */
406 val->data = (u_char *)tp + off;
407 val->size = bp[1] - off;
409 if (bp[0] == 2) { /* No more buckets in
415 hashp->cpage = __get_buf(hashp,
416 bp[bp[0] - 1], bufp, 0);
421 hashp->cpage->page)[0]) {
430 val->size = (size_t)collect_data(hashp, bufp, (int)len, set_current);
431 if (val->size == (size_t)-1)
433 if (save_p->addr != save_addr) {
434 /* We are pretty short on buffers. */
435 errno = EINVAL; /* OUT OF BUFFERS */
438 memmove(hashp->tmp_buf, (save_p->page) + off, len);
439 val->data = (u_char *)hashp->tmp_buf;
443 * Count how big the total datasize is by recursing through the pages. Then
444 * allocate a buffer and copy the data as you recurse up.
447 collect_data(HTAB *hashp, BUFHEAD *bufp, int len, int set)
457 mylen = hashp->BSIZE - bp[1];
458 save_addr = bufp->addr;
460 if (bp[2] == FULL_KEY_DATA) { /* End of Data */
461 totlen = len + mylen;
463 free(hashp->tmp_buf);
464 if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
468 if (bp[0] == 2) { /* No more buckets in chain */
473 __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
476 else if (!((u_int16_t *)hashp->cpage->page)[0]) {
483 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
484 if (!xbp || ((totlen =
485 collect_data(hashp, xbp, len + mylen, set)) < 1))
488 if (bufp->addr != save_addr) {
489 errno = EINVAL; /* Out of buffers. */
492 memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
497 * Fill in the key and data for this big pair.
500 __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set)
502 key->size = (size_t)collect_key(hashp, bufp, 0, val, set);
503 if (key->size == (size_t)-1)
505 key->data = (u_char *)hashp->tmp_key;
510 * Count how big the total key size is by recursing through the pages. Then
511 * collect the data, allocate a buffer and copy the key as you recurse up.
514 collect_key(HTAB *hashp, BUFHEAD *bufp, int len, DBT *val, int set)
519 u_int16_t *bp, save_addr;
523 mylen = hashp->BSIZE - bp[1];
525 save_addr = bufp->addr;
526 totlen = len + mylen;
527 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
528 if (hashp->tmp_key != NULL)
529 free(hashp->tmp_key);
530 if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
532 if (__big_return(hashp, bufp, 1, val, set))
535 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
536 if (!xbp || ((totlen =
537 collect_key(hashp, xbp, totlen, val, set)) < 1))
540 if (bufp->addr != save_addr) {
541 errno = EINVAL; /* MIS -- OUT OF BUFFERS */
544 memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
554 __big_split(HTAB *hashp,
555 BUFHEAD *op, /* Pointer to where to put keys that go in old bucket */
556 BUFHEAD *np, /* Pointer to new bucket page */
557 BUFHEAD *big_keyp, /* Pointer to first page containing the big key/data */
558 int addr, /* Address of big_keyp */
559 u_int32_t obucket, /* Old Bucket */
565 u_int16_t free_space, n, off, *tp;
569 /* Now figure out where the big key/data goes */
570 if (__big_keydata(hashp, big_keyp, &key, &val, 0))
572 change = (__call_hash(hashp, key.data, key.size) != obucket);
574 if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
576 __get_buf(hashp, ret->next_addr, big_keyp, 0)))
581 /* Now make one of np/op point to the big key/data pair */
583 assert(np->ovfl == NULL);
590 tmpp->flags |= BUF_MOD;
592 (void)fprintf(stderr,
593 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
594 (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
596 tmpp->ovfl = bp; /* one of op/np point to big_keyp */
597 tp = (u_int16_t *)tmpp->page;
599 assert(FREESPACE(tp) >= OVFLSIZE);
603 free_space = FREESPACE(tp);
604 tp[++n] = (u_int16_t)addr;
608 FREESPACE(tp) = free_space - OVFLSIZE;
611 * Finally, set the new and old return values. BIG_KEYP contains a
612 * pointer to the last page of the big key_data pair. Make sure that
613 * big_keyp has no following page (2 elements) or create an empty
620 tp = (u_int16_t *)big_keyp->page;
621 big_keyp->flags |= BUF_MOD;
624 * There may be either one or two offsets on this page. If
625 * there is one, then the overflow page is linked on normally
626 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
627 * the second offset and needs to get stuffed in after the
628 * next overflow page is added.
631 free_space = FREESPACE(tp);
634 FREESPACE(tp) = free_space + OVFLSIZE;
636 tmpp = __add_ovflpage(hashp, big_keyp);