1*f14fb602SLionel Sambuc /* $NetBSD: hash_bigkey.c,v 1.24 2012/03/13 21:13:32 christos Exp $ */
22639ae9bSBen Gras
32639ae9bSBen Gras /*-
42639ae9bSBen Gras * Copyright (c) 1990, 1993, 1994
52639ae9bSBen Gras * The Regents of the University of California. All rights reserved.
62639ae9bSBen Gras *
72639ae9bSBen Gras * This code is derived from software contributed to Berkeley by
82639ae9bSBen Gras * Margo Seltzer.
92639ae9bSBen Gras *
102639ae9bSBen Gras * Redistribution and use in source and binary forms, with or without
112639ae9bSBen Gras * modification, are permitted provided that the following conditions
122639ae9bSBen Gras * are met:
132639ae9bSBen Gras * 1. Redistributions of source code must retain the above copyright
142639ae9bSBen Gras * notice, this list of conditions and the following disclaimer.
152639ae9bSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162639ae9bSBen Gras * notice, this list of conditions and the following disclaimer in the
172639ae9bSBen Gras * documentation and/or other materials provided with the distribution.
182639ae9bSBen Gras * 3. Neither the name of the University nor the names of its contributors
192639ae9bSBen Gras * may be used to endorse or promote products derived from this software
202639ae9bSBen Gras * without specific prior written permission.
212639ae9bSBen Gras *
222639ae9bSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232639ae9bSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242639ae9bSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252639ae9bSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262639ae9bSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272639ae9bSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282639ae9bSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292639ae9bSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302639ae9bSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312639ae9bSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322639ae9bSBen Gras * SUCH DAMAGE.
332639ae9bSBen Gras */
342639ae9bSBen Gras
352639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
362639ae9bSBen Gras #include "nbtool_config.h"
372639ae9bSBen Gras #endif
382639ae9bSBen Gras
392639ae9bSBen Gras #include <sys/cdefs.h>
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: hash_bigkey.c,v 1.24 2012/03/13 21:13:32 christos Exp $");
412639ae9bSBen Gras
422639ae9bSBen Gras /*
432639ae9bSBen Gras * PACKAGE: hash
442639ae9bSBen Gras * DESCRIPTION:
452639ae9bSBen Gras * Big key/data handling for the hashing package.
462639ae9bSBen Gras *
472639ae9bSBen Gras * ROUTINES:
482639ae9bSBen Gras * External
492639ae9bSBen Gras * __big_keydata
502639ae9bSBen Gras * __big_split
512639ae9bSBen Gras * __big_insert
522639ae9bSBen Gras * __big_return
532639ae9bSBen Gras * __big_delete
542639ae9bSBen Gras * __find_last_page
552639ae9bSBen Gras * Internal
562639ae9bSBen Gras * collect_key
572639ae9bSBen Gras * collect_data
582639ae9bSBen Gras */
592639ae9bSBen Gras
602639ae9bSBen Gras #include <sys/param.h>
612639ae9bSBen Gras
622639ae9bSBen Gras #include <errno.h>
632639ae9bSBen Gras #include <stdio.h>
642639ae9bSBen Gras #include <stdlib.h>
652639ae9bSBen Gras #include <string.h>
662639ae9bSBen Gras #include <assert.h>
672639ae9bSBen Gras
682639ae9bSBen Gras #include <db.h>
692639ae9bSBen Gras #include "hash.h"
702639ae9bSBen Gras #include "page.h"
712639ae9bSBen Gras #include "extern.h"
722639ae9bSBen Gras
732639ae9bSBen Gras static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
742639ae9bSBen Gras static int collect_data(HTAB *, BUFHEAD *, int, int);
752639ae9bSBen Gras
762639ae9bSBen Gras /*
772639ae9bSBen Gras * Big_insert
782639ae9bSBen Gras *
792639ae9bSBen Gras * You need to do an insert and the key/data pair is too big
802639ae9bSBen Gras *
812639ae9bSBen Gras * Returns:
822639ae9bSBen Gras * 0 ==> OK
832639ae9bSBen Gras *-1 ==> ERROR
842639ae9bSBen Gras */
852639ae9bSBen Gras int
__big_insert(HTAB * hashp,BUFHEAD * bufp,const DBT * key,const DBT * val)862639ae9bSBen Gras __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
872639ae9bSBen Gras {
882639ae9bSBen Gras uint16_t *p, n;
892639ae9bSBen Gras size_t key_size, val_size;
902639ae9bSBen Gras uint16_t space, move_bytes, off;
912639ae9bSBen Gras char *cp, *key_data, *val_data;
922639ae9bSBen Gras size_t temp;
932639ae9bSBen Gras
942639ae9bSBen Gras cp = bufp->page; /* Character pointer of p. */
952639ae9bSBen Gras p = (uint16_t *)(void *)cp;
962639ae9bSBen Gras
972639ae9bSBen Gras key_data = (char *)key->data;
982639ae9bSBen Gras _DBFIT(key->size, int);
992639ae9bSBen Gras key_size = key->size;
1002639ae9bSBen Gras val_data = (char *)val->data;
1012639ae9bSBen Gras _DBFIT(val->size, int);
1022639ae9bSBen Gras val_size = val->size;
1032639ae9bSBen Gras
1042639ae9bSBen Gras /* First move the Key */
1052639ae9bSBen Gras
1062639ae9bSBen Gras temp = FREESPACE(p) - BIGOVERHEAD;
1072639ae9bSBen Gras _DBFIT(temp, uint16_t);
1082639ae9bSBen Gras space = (uint16_t)temp;
1092639ae9bSBen Gras while (key_size) {
110*f14fb602SLionel Sambuc size_t kspace = MIN(space, key_size);
111*f14fb602SLionel Sambuc _DBFIT(kspace, uint16_t);
112*f14fb602SLionel Sambuc move_bytes = (uint16_t)kspace;
1132639ae9bSBen Gras off = OFFSET(p) - move_bytes;
1142639ae9bSBen Gras memmove(cp + off, key_data, (size_t)move_bytes);
1152639ae9bSBen Gras key_size -= move_bytes;
1162639ae9bSBen Gras key_data += move_bytes;
1172639ae9bSBen Gras n = p[0];
1182639ae9bSBen Gras p[++n] = off;
1192639ae9bSBen Gras p[0] = ++n;
1202639ae9bSBen Gras temp = off - PAGE_META(n);
1212639ae9bSBen Gras _DBFIT(temp, uint16_t);
1222639ae9bSBen Gras FREESPACE(p) = (uint16_t)temp;
1232639ae9bSBen Gras OFFSET(p) = off;
1242639ae9bSBen Gras p[n] = PARTIAL_KEY;
1252639ae9bSBen Gras bufp = __add_ovflpage(hashp, bufp);
1262639ae9bSBen Gras if (!bufp)
1272639ae9bSBen Gras return (-1);
1282639ae9bSBen Gras n = p[0];
1292639ae9bSBen Gras if (!key_size) {
1302639ae9bSBen Gras space = FREESPACE(p);
1312639ae9bSBen Gras if (space) {
132*f14fb602SLionel Sambuc size_t vspace = MIN(space, val_size);
133*f14fb602SLionel Sambuc _DBFIT(vspace, uint16_t);
134*f14fb602SLionel Sambuc move_bytes = (uint16_t)vspace;
1352639ae9bSBen Gras /*
1362639ae9bSBen Gras * If the data would fit exactly in the
1372639ae9bSBen Gras * remaining space, we must overflow it to the
1382639ae9bSBen Gras * next page; otherwise the invariant that the
1392639ae9bSBen Gras * data must end on a page with FREESPACE
1402639ae9bSBen Gras * non-zero would fail.
1412639ae9bSBen Gras */
1422639ae9bSBen Gras if (space == val_size && val_size == val->size)
1432639ae9bSBen Gras goto toolarge;
1442639ae9bSBen Gras off = OFFSET(p) - move_bytes;
1452639ae9bSBen Gras memmove(cp + off, val_data, (size_t)move_bytes);
1462639ae9bSBen Gras val_data += move_bytes;
1472639ae9bSBen Gras val_size -= move_bytes;
1482639ae9bSBen Gras p[n] = off;
1492639ae9bSBen Gras p[n - 2] = FULL_KEY_DATA;
1502639ae9bSBen Gras FREESPACE(p) = FREESPACE(p) - move_bytes;
1512639ae9bSBen Gras OFFSET(p) = off;
1522639ae9bSBen Gras } else {
1532639ae9bSBen Gras toolarge:
1542639ae9bSBen Gras p[n - 2] = FULL_KEY;
1552639ae9bSBen Gras }
1562639ae9bSBen Gras }
1572639ae9bSBen Gras p = (uint16_t *)(void *)bufp->page;
1582639ae9bSBen Gras cp = bufp->page;
1592639ae9bSBen Gras bufp->flags |= BUF_MOD;
1602639ae9bSBen Gras temp = FREESPACE(p) - BIGOVERHEAD;
1612639ae9bSBen Gras _DBFIT(temp, uint16_t);
1622639ae9bSBen Gras space = (uint16_t)temp;
1632639ae9bSBen Gras }
1642639ae9bSBen Gras
1652639ae9bSBen Gras /* Now move the data */
1662639ae9bSBen Gras temp = FREESPACE(p) - BIGOVERHEAD;
1672639ae9bSBen Gras _DBFIT(temp, uint16_t);
1682639ae9bSBen Gras space = (uint16_t)temp;
1692639ae9bSBen Gras while (val_size) {
170*f14fb602SLionel Sambuc size_t vspace = MIN(space, val_size);
171*f14fb602SLionel Sambuc _DBFIT(vspace, uint16_t);
172*f14fb602SLionel Sambuc move_bytes = (uint16_t)vspace;
1732639ae9bSBen Gras /*
1742639ae9bSBen Gras * Here's the hack to make sure that if the data ends on the
1752639ae9bSBen Gras * same page as the key ends, FREESPACE is at least one.
1762639ae9bSBen Gras */
1772639ae9bSBen Gras if (space == val_size && val_size == val->size)
1782639ae9bSBen Gras move_bytes--;
1792639ae9bSBen Gras off = OFFSET(p) - move_bytes;
1802639ae9bSBen Gras memmove(cp + off, val_data, (size_t)move_bytes);
1812639ae9bSBen Gras val_size -= move_bytes;
1822639ae9bSBen Gras val_data += move_bytes;
1832639ae9bSBen Gras n = p[0];
1842639ae9bSBen Gras p[++n] = off;
1852639ae9bSBen Gras p[0] = ++n;
1862639ae9bSBen Gras temp = off - PAGE_META(n);
1872639ae9bSBen Gras _DBFIT(temp, uint16_t);
1882639ae9bSBen Gras FREESPACE(p) = (uint16_t)temp;
1892639ae9bSBen Gras OFFSET(p) = off;
1902639ae9bSBen Gras if (val_size) {
1912639ae9bSBen Gras p[n] = FULL_KEY;
1922639ae9bSBen Gras bufp = __add_ovflpage(hashp, bufp);
1932639ae9bSBen Gras if (!bufp)
1942639ae9bSBen Gras return (-1);
1952639ae9bSBen Gras cp = bufp->page;
1962639ae9bSBen Gras p = (uint16_t *)(void *)cp;
1972639ae9bSBen Gras } else
1982639ae9bSBen Gras p[n] = FULL_KEY_DATA;
1992639ae9bSBen Gras bufp->flags |= BUF_MOD;
2002639ae9bSBen Gras temp = FREESPACE(p) - BIGOVERHEAD;
2012639ae9bSBen Gras _DBFIT(temp, uint16_t);
2022639ae9bSBen Gras space = (uint16_t)temp;
2032639ae9bSBen Gras }
2042639ae9bSBen Gras return (0);
2052639ae9bSBen Gras }
2062639ae9bSBen Gras
2072639ae9bSBen Gras /*
2082639ae9bSBen Gras * Called when bufp's page contains a partial key (index should be 1)
2092639ae9bSBen Gras *
2102639ae9bSBen Gras * All pages in the big key/data pair except bufp are freed. We cannot
2112639ae9bSBen Gras * free bufp because the page pointing to it is lost and we can't get rid
2122639ae9bSBen Gras * of its pointer.
2132639ae9bSBen Gras *
2142639ae9bSBen Gras * Returns:
2152639ae9bSBen Gras * 0 => OK
2162639ae9bSBen Gras *-1 => ERROR
2172639ae9bSBen Gras */
2182639ae9bSBen Gras int
__big_delete(HTAB * hashp,BUFHEAD * bufp)2192639ae9bSBen Gras __big_delete(HTAB *hashp, BUFHEAD *bufp)
2202639ae9bSBen Gras {
2212639ae9bSBen Gras BUFHEAD *last_bfp, *rbufp;
2222639ae9bSBen Gras uint16_t *bp, pageno;
2232639ae9bSBen Gras int key_done, n;
2242639ae9bSBen Gras size_t temp;
2252639ae9bSBen Gras
2262639ae9bSBen Gras rbufp = bufp;
2272639ae9bSBen Gras last_bfp = NULL;
2282639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
2292639ae9bSBen Gras pageno = 0;
2302639ae9bSBen Gras key_done = 0;
2312639ae9bSBen Gras
2322639ae9bSBen Gras while (!key_done || (bp[2] != FULL_KEY_DATA)) {
2332639ae9bSBen Gras if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
2342639ae9bSBen Gras key_done = 1;
2352639ae9bSBen Gras
2362639ae9bSBen Gras /*
2372639ae9bSBen Gras * If there is freespace left on a FULL_KEY_DATA page, then
2382639ae9bSBen Gras * the data is short and fits entirely on this page, and this
2392639ae9bSBen Gras * is the last page.
2402639ae9bSBen Gras */
2412639ae9bSBen Gras if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
2422639ae9bSBen Gras break;
2432639ae9bSBen Gras pageno = bp[bp[0] - 1];
2442639ae9bSBen Gras rbufp->flags |= BUF_MOD;
2452639ae9bSBen Gras rbufp = __get_buf(hashp, (uint32_t)pageno, rbufp, 0);
2462639ae9bSBen Gras if (last_bfp)
2472639ae9bSBen Gras __free_ovflpage(hashp, last_bfp);
2482639ae9bSBen Gras last_bfp = rbufp;
2492639ae9bSBen Gras if (!rbufp)
2502639ae9bSBen Gras return (-1); /* Error. */
2512639ae9bSBen Gras bp = (uint16_t *)(void *)rbufp->page;
2522639ae9bSBen Gras }
2532639ae9bSBen Gras
2542639ae9bSBen Gras /*
2552639ae9bSBen Gras * If we get here then rbufp points to the last page of the big
2562639ae9bSBen Gras * key/data pair. Bufp points to the first one -- it should now be
2572639ae9bSBen Gras * empty pointing to the next page after this pair. Can't free it
2582639ae9bSBen Gras * because we don't have the page pointing to it.
2592639ae9bSBen Gras */
2602639ae9bSBen Gras
2612639ae9bSBen Gras /* This is information from the last page of the pair. */
2622639ae9bSBen Gras n = bp[0];
2632639ae9bSBen Gras pageno = bp[n - 1];
2642639ae9bSBen Gras
2652639ae9bSBen Gras /* Now, bp is the first page of the pair. */
2662639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
2672639ae9bSBen Gras if (n > 2) {
2682639ae9bSBen Gras /* There is an overflow page. */
2692639ae9bSBen Gras bp[1] = pageno;
2702639ae9bSBen Gras bp[2] = OVFLPAGE;
2712639ae9bSBen Gras bufp->ovfl = rbufp->ovfl;
2722639ae9bSBen Gras } else
2732639ae9bSBen Gras /* This is the last page. */
2742639ae9bSBen Gras bufp->ovfl = NULL;
2752639ae9bSBen Gras n -= 2;
2762639ae9bSBen Gras bp[0] = n;
2772639ae9bSBen Gras temp = hashp->BSIZE - PAGE_META(n);
2782639ae9bSBen Gras _DBFIT(temp, uint16_t);
2792639ae9bSBen Gras FREESPACE(bp) = (uint16_t)temp;
2802639ae9bSBen Gras OFFSET(bp) = hashp->BSIZE;
2812639ae9bSBen Gras
2822639ae9bSBen Gras bufp->flags |= BUF_MOD;
2832639ae9bSBen Gras if (rbufp)
2842639ae9bSBen Gras __free_ovflpage(hashp, rbufp);
2852639ae9bSBen Gras if (last_bfp && last_bfp != rbufp)
2862639ae9bSBen Gras __free_ovflpage(hashp, last_bfp);
2872639ae9bSBen Gras
2882639ae9bSBen Gras hashp->NKEYS--;
2892639ae9bSBen Gras return (0);
2902639ae9bSBen Gras }
2912639ae9bSBen Gras /*
2922639ae9bSBen Gras * Returns:
2932639ae9bSBen Gras * 0 = key not found
2942639ae9bSBen Gras * -1 = get next overflow page
2952639ae9bSBen Gras * -2 means key not found and this is big key/data
2962639ae9bSBen Gras * -3 error
2972639ae9bSBen Gras */
2982639ae9bSBen Gras int
__find_bigpair(HTAB * hashp,BUFHEAD * bufp,int ndx,char * key,int size)2992639ae9bSBen Gras __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
3002639ae9bSBen Gras {
3012639ae9bSBen Gras uint16_t *bp;
3022639ae9bSBen Gras char *p;
3032639ae9bSBen Gras int ksize;
3042639ae9bSBen Gras uint16_t bytes;
3052639ae9bSBen Gras char *kkey;
3062639ae9bSBen Gras
3072639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
3082639ae9bSBen Gras p = bufp->page;
3092639ae9bSBen Gras ksize = size;
3102639ae9bSBen Gras kkey = key;
3112639ae9bSBen Gras
3122639ae9bSBen Gras for (bytes = hashp->BSIZE - bp[ndx];
3132639ae9bSBen Gras bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
3142639ae9bSBen Gras bytes = hashp->BSIZE - bp[ndx]) {
3152639ae9bSBen Gras if (memcmp(p + bp[ndx], kkey, (size_t)bytes))
3162639ae9bSBen Gras return (-2);
3172639ae9bSBen Gras kkey += bytes;
3182639ae9bSBen Gras ksize -= bytes;
3192639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)bp[ndx + 2], bufp, 0);
3202639ae9bSBen Gras if (!bufp)
3212639ae9bSBen Gras return (-3);
3222639ae9bSBen Gras p = bufp->page;
3232639ae9bSBen Gras bp = (uint16_t *)(void *)p;
3242639ae9bSBen Gras ndx = 1;
3252639ae9bSBen Gras }
3262639ae9bSBen Gras
3272639ae9bSBen Gras if (bytes != ksize || memcmp(p + bp[ndx], kkey, (size_t)bytes)) {
3282639ae9bSBen Gras #ifdef HASH_STATISTICS
3292639ae9bSBen Gras ++hash_collisions;
3302639ae9bSBen Gras #endif
3312639ae9bSBen Gras return (-2);
3322639ae9bSBen Gras } else
3332639ae9bSBen Gras return (ndx);
3342639ae9bSBen Gras }
3352639ae9bSBen Gras
3362639ae9bSBen Gras /*
3372639ae9bSBen Gras * Given the buffer pointer of the first overflow page of a big pair,
3382639ae9bSBen Gras * find the end of the big pair
3392639ae9bSBen Gras *
3402639ae9bSBen Gras * This will set bpp to the buffer header of the last page of the big pair.
3412639ae9bSBen Gras * It will return the pageno of the overflow page following the last page
3422639ae9bSBen Gras * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
3432639ae9bSBen Gras * bucket)
3442639ae9bSBen Gras */
3452639ae9bSBen Gras uint16_t
__find_last_page(HTAB * hashp,BUFHEAD ** bpp)3462639ae9bSBen Gras __find_last_page(HTAB *hashp, BUFHEAD **bpp)
3472639ae9bSBen Gras {
3482639ae9bSBen Gras BUFHEAD *bufp;
3492639ae9bSBen Gras uint16_t *bp, pageno;
3502639ae9bSBen Gras int n;
3512639ae9bSBen Gras
3522639ae9bSBen Gras bufp = *bpp;
3532639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
3542639ae9bSBen Gras for (;;) {
3552639ae9bSBen Gras n = bp[0];
3562639ae9bSBen Gras
3572639ae9bSBen Gras /*
3582639ae9bSBen Gras * This is the last page if: the tag is FULL_KEY_DATA and
3592639ae9bSBen Gras * either only 2 entries OVFLPAGE marker is explicit there
3602639ae9bSBen Gras * is freespace on the page.
3612639ae9bSBen Gras */
3622639ae9bSBen Gras if (bp[2] == FULL_KEY_DATA &&
3632639ae9bSBen Gras ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
3642639ae9bSBen Gras break;
3652639ae9bSBen Gras
3662639ae9bSBen Gras pageno = bp[n - 1];
3672639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)pageno, bufp, 0);
3682639ae9bSBen Gras if (!bufp)
3692639ae9bSBen Gras return (0); /* Need to indicate an error! */
3702639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
3712639ae9bSBen Gras }
3722639ae9bSBen Gras
3732639ae9bSBen Gras *bpp = bufp;
3742639ae9bSBen Gras if (bp[0] > 2)
3752639ae9bSBen Gras return (bp[3]);
3762639ae9bSBen Gras else
3772639ae9bSBen Gras return (0);
3782639ae9bSBen Gras }
3792639ae9bSBen Gras
3802639ae9bSBen Gras /*
3812639ae9bSBen Gras * Return the data for the key/data pair that begins on this page at this
3822639ae9bSBen Gras * index (index should always be 1).
3832639ae9bSBen Gras */
3842639ae9bSBen Gras int
__big_return(HTAB * hashp,BUFHEAD * bufp,int ndx,DBT * val,int set_current)3852639ae9bSBen Gras __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current)
3862639ae9bSBen Gras {
3872639ae9bSBen Gras BUFHEAD *save_p;
3882639ae9bSBen Gras uint16_t *bp, len, off, save_addr;
3892639ae9bSBen Gras char *tp;
3902639ae9bSBen Gras
3912639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
3922639ae9bSBen Gras while (bp[ndx + 1] == PARTIAL_KEY) {
3932639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0);
3942639ae9bSBen Gras if (!bufp)
3952639ae9bSBen Gras return (-1);
3962639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
3972639ae9bSBen Gras ndx = 1;
3982639ae9bSBen Gras }
3992639ae9bSBen Gras
4002639ae9bSBen Gras if (bp[ndx + 1] == FULL_KEY) {
4012639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0);
4022639ae9bSBen Gras if (!bufp)
4032639ae9bSBen Gras return (-1);
4042639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
4052639ae9bSBen Gras save_p = bufp;
4062639ae9bSBen Gras save_addr = save_p->addr;
4072639ae9bSBen Gras off = bp[1];
4082639ae9bSBen Gras len = 0;
4092639ae9bSBen Gras } else
4102639ae9bSBen Gras if (!FREESPACE(bp)) {
4112639ae9bSBen Gras /*
4122639ae9bSBen Gras * This is a hack. We can't distinguish between
4132639ae9bSBen Gras * FULL_KEY_DATA that contains complete data or
4142639ae9bSBen Gras * incomplete data, so we require that if the data
4152639ae9bSBen Gras * is complete, there is at least 1 byte of free
4162639ae9bSBen Gras * space left.
4172639ae9bSBen Gras */
4182639ae9bSBen Gras off = bp[bp[0]];
4192639ae9bSBen Gras len = bp[1] - off;
4202639ae9bSBen Gras save_p = bufp;
4212639ae9bSBen Gras save_addr = bufp->addr;
4222639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp,
4232639ae9bSBen Gras 0);
4242639ae9bSBen Gras if (!bufp)
4252639ae9bSBen Gras return (-1);
4262639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
4272639ae9bSBen Gras } else {
4282639ae9bSBen Gras /* The data is all on one page. */
4292639ae9bSBen Gras tp = (char *)(void *)bp;
4302639ae9bSBen Gras off = bp[bp[0]];
4312639ae9bSBen Gras val->data = (uint8_t *)tp + off;
4322639ae9bSBen Gras val->size = bp[1] - off;
4332639ae9bSBen Gras if (set_current) {
4342639ae9bSBen Gras if (bp[0] == 2) { /* No more buckets in
4352639ae9bSBen Gras * chain */
4362639ae9bSBen Gras hashp->cpage = NULL;
4372639ae9bSBen Gras hashp->cbucket++;
4382639ae9bSBen Gras hashp->cndx = 1;
4392639ae9bSBen Gras } else {
4402639ae9bSBen Gras hashp->cpage = __get_buf(hashp,
4412639ae9bSBen Gras (uint32_t)bp[bp[0] - 1], bufp, 0);
4422639ae9bSBen Gras if (!hashp->cpage)
4432639ae9bSBen Gras return (-1);
4442639ae9bSBen Gras hashp->cndx = 1;
4452639ae9bSBen Gras if (!((uint16_t *)(void *)
4462639ae9bSBen Gras hashp->cpage->page)[0]) {
4472639ae9bSBen Gras hashp->cbucket++;
4482639ae9bSBen Gras hashp->cpage = NULL;
4492639ae9bSBen Gras }
4502639ae9bSBen Gras }
4512639ae9bSBen Gras }
4522639ae9bSBen Gras return (0);
4532639ae9bSBen Gras }
4542639ae9bSBen Gras
4552639ae9bSBen Gras val->size = collect_data(hashp, bufp, (int)len, set_current);
4562639ae9bSBen Gras if (val->size == (size_t)-1)
4572639ae9bSBen Gras return (-1);
4582639ae9bSBen Gras if (save_p->addr != save_addr) {
4592639ae9bSBen Gras /* We are pretty short on buffers. */
4602639ae9bSBen Gras errno = EINVAL; /* OUT OF BUFFERS */
4612639ae9bSBen Gras return (-1);
4622639ae9bSBen Gras }
4632639ae9bSBen Gras memmove(hashp->tmp_buf, (save_p->page) + off, (size_t)len);
4642639ae9bSBen Gras val->data = (uint8_t *)hashp->tmp_buf;
4652639ae9bSBen Gras return (0);
4662639ae9bSBen Gras }
4672639ae9bSBen Gras /*
4682639ae9bSBen Gras * Count how big the total datasize is by recursing through the pages. Then
4692639ae9bSBen Gras * allocate a buffer and copy the data as you recurse up.
4702639ae9bSBen Gras */
4712639ae9bSBen Gras static int
collect_data(HTAB * hashp,BUFHEAD * bufp,int len,int set)4722639ae9bSBen Gras collect_data(HTAB *hashp, BUFHEAD *bufp, int len, int set)
4732639ae9bSBen Gras {
4742639ae9bSBen Gras uint16_t *bp;
4752639ae9bSBen Gras char *p;
4762639ae9bSBen Gras BUFHEAD *xbp;
4772639ae9bSBen Gras uint16_t save_addr;
4782639ae9bSBen Gras int mylen, totlen;
4792639ae9bSBen Gras
4802639ae9bSBen Gras p = bufp->page;
4812639ae9bSBen Gras bp = (uint16_t *)(void *)p;
4822639ae9bSBen Gras mylen = hashp->BSIZE - bp[1];
4832639ae9bSBen Gras save_addr = bufp->addr;
4842639ae9bSBen Gras
4852639ae9bSBen Gras if (bp[2] == FULL_KEY_DATA) { /* End of Data */
4862639ae9bSBen Gras totlen = len + mylen;
4872639ae9bSBen Gras if (hashp->tmp_buf)
4882639ae9bSBen Gras free(hashp->tmp_buf);
4892639ae9bSBen Gras if ((hashp->tmp_buf = calloc(1, (size_t)totlen)) == NULL)
4902639ae9bSBen Gras return (-1);
4912639ae9bSBen Gras if (set) {
4922639ae9bSBen Gras hashp->cndx = 1;
4932639ae9bSBen Gras if (bp[0] == 2) { /* No more buckets in chain */
4942639ae9bSBen Gras hashp->cpage = NULL;
4952639ae9bSBen Gras hashp->cbucket++;
4962639ae9bSBen Gras } else {
4972639ae9bSBen Gras hashp->cpage =
4982639ae9bSBen Gras __get_buf(hashp, (uint32_t)bp[bp[0] - 1],
4992639ae9bSBen Gras bufp, 0);
5002639ae9bSBen Gras if (!hashp->cpage)
5012639ae9bSBen Gras return (-1);
5022639ae9bSBen Gras else if (!((uint16_t *)(void *)hashp->cpage->page)[0]) {
5032639ae9bSBen Gras hashp->cbucket++;
5042639ae9bSBen Gras hashp->cpage = NULL;
5052639ae9bSBen Gras }
5062639ae9bSBen Gras }
5072639ae9bSBen Gras }
5082639ae9bSBen Gras } else {
5092639ae9bSBen Gras xbp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0);
5102639ae9bSBen Gras if (!xbp || ((totlen =
5112639ae9bSBen Gras collect_data(hashp, xbp, len + mylen, set)) < 1))
5122639ae9bSBen Gras return (-1);
5132639ae9bSBen Gras }
5142639ae9bSBen Gras if (bufp->addr != save_addr) {
5152639ae9bSBen Gras errno = EINVAL; /* Out of buffers. */
5162639ae9bSBen Gras return (-1);
5172639ae9bSBen Gras }
5182639ae9bSBen Gras memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], (size_t)mylen);
5192639ae9bSBen Gras return (totlen);
5202639ae9bSBen Gras }
5212639ae9bSBen Gras
5222639ae9bSBen Gras /*
5232639ae9bSBen Gras * Fill in the key and data for this big pair.
5242639ae9bSBen Gras */
5252639ae9bSBen Gras int
__big_keydata(HTAB * hashp,BUFHEAD * bufp,DBT * key,DBT * val,int set)5262639ae9bSBen Gras __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set)
5272639ae9bSBen Gras {
5282639ae9bSBen Gras key->size = collect_key(hashp, bufp, 0, val, set);
5292639ae9bSBen Gras if (key->size == (size_t)-1)
5302639ae9bSBen Gras return (-1);
5312639ae9bSBen Gras key->data = (uint8_t *)hashp->tmp_key;
5322639ae9bSBen Gras return (0);
5332639ae9bSBen Gras }
5342639ae9bSBen Gras
5352639ae9bSBen Gras /*
5362639ae9bSBen Gras * Count how big the total key size is by recursing through the pages. Then
5372639ae9bSBen Gras * collect the data, allocate a buffer and copy the key as you recurse up.
5382639ae9bSBen Gras */
5392639ae9bSBen Gras static int
collect_key(HTAB * hashp,BUFHEAD * bufp,int len,DBT * val,int set)5402639ae9bSBen Gras collect_key(HTAB *hashp, BUFHEAD *bufp, int len, DBT *val, int set)
5412639ae9bSBen Gras {
5422639ae9bSBen Gras BUFHEAD *xbp;
5432639ae9bSBen Gras char *p;
5442639ae9bSBen Gras int mylen, totlen;
5452639ae9bSBen Gras uint16_t *bp, save_addr;
5462639ae9bSBen Gras
5472639ae9bSBen Gras p = bufp->page;
5482639ae9bSBen Gras bp = (uint16_t *)(void *)p;
5492639ae9bSBen Gras mylen = hashp->BSIZE - bp[1];
5502639ae9bSBen Gras
5512639ae9bSBen Gras save_addr = bufp->addr;
5522639ae9bSBen Gras totlen = len + mylen;
5532639ae9bSBen Gras if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
5542639ae9bSBen Gras if (hashp->tmp_key != NULL)
5552639ae9bSBen Gras free(hashp->tmp_key);
5562639ae9bSBen Gras if ((hashp->tmp_key = calloc(1, (size_t)totlen)) == NULL)
5572639ae9bSBen Gras return (-1);
5582639ae9bSBen Gras if (__big_return(hashp, bufp, 1, val, set))
5592639ae9bSBen Gras return (-1);
5602639ae9bSBen Gras } else {
5612639ae9bSBen Gras xbp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp, 0);
5622639ae9bSBen Gras if (!xbp || ((totlen =
5632639ae9bSBen Gras collect_key(hashp, xbp, totlen, val, set)) < 1))
5642639ae9bSBen Gras return (-1);
5652639ae9bSBen Gras }
5662639ae9bSBen Gras if (bufp->addr != save_addr) {
5672639ae9bSBen Gras errno = EINVAL; /* MIS -- OUT OF BUFFERS */
5682639ae9bSBen Gras return (-1);
5692639ae9bSBen Gras }
5702639ae9bSBen Gras memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], (size_t)mylen);
5712639ae9bSBen Gras return (totlen);
5722639ae9bSBen Gras }
5732639ae9bSBen Gras
5742639ae9bSBen Gras /*
5752639ae9bSBen Gras * Returns:
5762639ae9bSBen Gras * 0 => OK
5772639ae9bSBen Gras * -1 => error
5782639ae9bSBen Gras */
5792639ae9bSBen Gras int
__big_split(HTAB * hashp,BUFHEAD * op,BUFHEAD * np,BUFHEAD * big_keyp,int addr,uint32_t obucket,SPLIT_RETURN * ret)5802639ae9bSBen Gras __big_split(
5812639ae9bSBen Gras HTAB *hashp,
5822639ae9bSBen Gras BUFHEAD *op, /* Pointer to where to put keys that go in old bucket */
5832639ae9bSBen Gras BUFHEAD *np, /* Pointer to new bucket page */
5842639ae9bSBen Gras /* Pointer to first page containing the big key/data */
5852639ae9bSBen Gras BUFHEAD *big_keyp,
5862639ae9bSBen Gras int addr, /* Address of big_keyp */
5872639ae9bSBen Gras uint32_t obucket,/* Old Bucket */
5882639ae9bSBen Gras SPLIT_RETURN *ret
5892639ae9bSBen Gras )
5902639ae9bSBen Gras {
5912639ae9bSBen Gras BUFHEAD *tmpp;
5922639ae9bSBen Gras uint16_t *tp;
5932639ae9bSBen Gras BUFHEAD *bp;
5942639ae9bSBen Gras DBT key, val;
5952639ae9bSBen Gras uint32_t change;
5962639ae9bSBen Gras uint16_t free_space, n, off;
5972639ae9bSBen Gras size_t temp;
5982639ae9bSBen Gras
5992639ae9bSBen Gras bp = big_keyp;
6002639ae9bSBen Gras
6012639ae9bSBen Gras /* Now figure out where the big key/data goes */
6022639ae9bSBen Gras if (__big_keydata(hashp, big_keyp, &key, &val, 0))
6032639ae9bSBen Gras return (-1);
6042639ae9bSBen Gras change = (__call_hash(hashp, key.data, (int)key.size) != obucket);
6052639ae9bSBen Gras
6062639ae9bSBen Gras if ((ret->next_addr = __find_last_page(hashp, &big_keyp)) != 0) {
6072639ae9bSBen Gras if (!(ret->nextp =
6082639ae9bSBen Gras __get_buf(hashp, (uint32_t)ret->next_addr, big_keyp, 0)))
6092639ae9bSBen Gras return (-1);
6102639ae9bSBen Gras } else
6112639ae9bSBen Gras ret->nextp = NULL;
6122639ae9bSBen Gras
6132639ae9bSBen Gras /* Now make one of np/op point to the big key/data pair */
6142639ae9bSBen Gras _DIAGASSERT(np->ovfl == NULL);
6152639ae9bSBen Gras if (change)
6162639ae9bSBen Gras tmpp = np;
6172639ae9bSBen Gras else
6182639ae9bSBen Gras tmpp = op;
6192639ae9bSBen Gras
6202639ae9bSBen Gras tmpp->flags |= BUF_MOD;
6212639ae9bSBen Gras #ifdef DEBUG1
6222639ae9bSBen Gras (void)fprintf(stderr,
6232639ae9bSBen Gras "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
6242639ae9bSBen Gras (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
6252639ae9bSBen Gras #endif
6262639ae9bSBen Gras tmpp->ovfl = bp; /* one of op/np point to big_keyp */
6272639ae9bSBen Gras tp = (uint16_t *)(void *)tmpp->page;
6282639ae9bSBen Gras _DIAGASSERT(FREESPACE(tp) >= OVFLSIZE);
6292639ae9bSBen Gras n = tp[0];
6302639ae9bSBen Gras off = OFFSET(tp);
6312639ae9bSBen Gras free_space = FREESPACE(tp);
6322639ae9bSBen Gras tp[++n] = (uint16_t)addr;
6332639ae9bSBen Gras tp[++n] = OVFLPAGE;
6342639ae9bSBen Gras tp[0] = n;
6352639ae9bSBen Gras OFFSET(tp) = off;
6362639ae9bSBen Gras temp = free_space - OVFLSIZE;
6372639ae9bSBen Gras _DBFIT(temp, uint16_t);
6382639ae9bSBen Gras FREESPACE(tp) = (uint16_t)temp;
6392639ae9bSBen Gras
6402639ae9bSBen Gras /*
6412639ae9bSBen Gras * Finally, set the new and old return values. BIG_KEYP contains a
6422639ae9bSBen Gras * pointer to the last page of the big key_data pair. Make sure that
6432639ae9bSBen Gras * big_keyp has no following page (2 elements) or create an empty
6442639ae9bSBen Gras * following page.
6452639ae9bSBen Gras */
6462639ae9bSBen Gras
6472639ae9bSBen Gras ret->newp = np;
6482639ae9bSBen Gras ret->oldp = op;
6492639ae9bSBen Gras
6502639ae9bSBen Gras tp = (uint16_t *)(void *)big_keyp->page;
6512639ae9bSBen Gras big_keyp->flags |= BUF_MOD;
6522639ae9bSBen Gras if (tp[0] > 2) {
6532639ae9bSBen Gras /*
6542639ae9bSBen Gras * There may be either one or two offsets on this page. If
6552639ae9bSBen Gras * there is one, then the overflow page is linked on normally
6562639ae9bSBen Gras * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
6572639ae9bSBen Gras * the second offset and needs to get stuffed in after the
6582639ae9bSBen Gras * next overflow page is added.
6592639ae9bSBen Gras */
6602639ae9bSBen Gras n = tp[4];
6612639ae9bSBen Gras free_space = FREESPACE(tp);
6622639ae9bSBen Gras off = OFFSET(tp);
6632639ae9bSBen Gras tp[0] -= 2;
6642639ae9bSBen Gras temp = free_space + OVFLSIZE;
6652639ae9bSBen Gras _DBFIT(temp, uint16_t);
6662639ae9bSBen Gras FREESPACE(tp) = (uint16_t)temp;
6672639ae9bSBen Gras OFFSET(tp) = off;
6682639ae9bSBen Gras tmpp = __add_ovflpage(hashp, big_keyp);
6692639ae9bSBen Gras if (!tmpp)
6702639ae9bSBen Gras return (-1);
6712639ae9bSBen Gras tp[4] = n;
6722639ae9bSBen Gras } else
6732639ae9bSBen Gras tmpp = big_keyp;
6742639ae9bSBen Gras
6752639ae9bSBen Gras if (change)
6762639ae9bSBen Gras ret->newp = tmpp;
6772639ae9bSBen Gras else
6782639ae9bSBen Gras ret->oldp = tmpp;
6792639ae9bSBen Gras return (0);
6802639ae9bSBen Gras }
681