Blame nss/lib/dbm/src/h_bigkey.c

Packit 40b132
/*-
Packit 40b132
 * Copyright (c) 1990, 1993, 1994
Packit 40b132
 *	The Regents of the University of California.  All rights reserved.
Packit 40b132
 *
Packit 40b132
 * This code is derived from software contributed to Berkeley by
Packit 40b132
 * Margo Seltzer.
Packit 40b132
 *
Packit 40b132
 * Redistribution and use in source and binary forms, with or without
Packit 40b132
 * modification, are permitted provided that the following conditions
Packit 40b132
 * are met:
Packit 40b132
 * 1. Redistributions of source code must retain the above copyright
Packit 40b132
 *    notice, this list of conditions and the following disclaimer.
Packit 40b132
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 40b132
 *    notice, this list of conditions and the following disclaimer in the
Packit 40b132
 *    documentation and/or other materials provided with the distribution.
Packit 40b132
 * 3. ***REMOVED*** - see 
Packit 40b132
 *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
Packit 40b132
 * 4. Neither the name of the University nor the names of its contributors
Packit 40b132
 *    may be used to endorse or promote products derived from this software
Packit 40b132
 *    without specific prior written permission.
Packit 40b132
 *
Packit 40b132
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 40b132
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 40b132
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 40b132
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 40b132
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 40b132
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 40b132
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 40b132
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 40b132
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 40b132
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 40b132
 * SUCH DAMAGE.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#if defined(LIBC_SCCS) && !defined(lint)
Packit 40b132
static char sccsid[] = "@(#)hash_bigkey.c	8.3 (Berkeley) 5/31/94";
Packit 40b132
#endif /* LIBC_SCCS and not lint */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * PACKAGE: hash
Packit 40b132
 * DESCRIPTION:
Packit 40b132
 *	Big key/data handling for the hashing package.
Packit 40b132
 *
Packit 40b132
 * ROUTINES:
Packit 40b132
 * External
Packit 40b132
 *	__big_keydata
Packit 40b132
 *	__big_split
Packit 40b132
 *	__big_insert
Packit 40b132
 *	__big_return
Packit 40b132
 *	__big_delete
Packit 40b132
 *	__find_last_page
Packit 40b132
 * Internal
Packit 40b132
 *	collect_key
Packit 40b132
 *	collect_data
Packit 40b132
 */
Packit 40b132
Packit 40b132
#if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
Packit 40b132
#include <sys/param.h>
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#include <errno.h>
Packit 40b132
#include <stdio.h>
Packit 40b132
#include <stdlib.h>
Packit 40b132
#include <string.h>
Packit 40b132
Packit 40b132
#ifdef DEBUG
Packit 40b132
#include <assert.h>
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#include "mcom_db.h"
Packit 40b132
#include "hash.h"
Packit 40b132
#include "page.h"
Packit 40b132
/* #include "extern.h" */
Packit 40b132
Packit 40b132
static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
Packit 40b132
static int collect_data(HTAB *, BUFHEAD *, int, int);
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Big_insert
Packit 40b132
 *
Packit 40b132
 * You need to do an insert and the key/data pair is too big
Packit 40b132
 *
Packit 40b132
 * Returns:
Packit 40b132
 * 0 ==> OK
Packit 40b132
 *-1 ==> ERROR
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
__big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
Packit 40b132
{
Packit 40b132
	register uint16 *p;
Packit 40b132
	uint key_size, n, val_size;
Packit 40b132
	uint16 space, move_bytes, off;
Packit 40b132
	char *cp, *key_data, *val_data;
Packit 40b132
Packit 40b132
	cp = bufp->page;		/* Character pointer of p. */
Packit 40b132
	p = (uint16 *)cp;
Packit 40b132
Packit 40b132
	key_data = (char *)key->data;
Packit 40b132
	key_size = key->size;
Packit 40b132
	val_data = (char *)val->data;
Packit 40b132
	val_size = val->size;
Packit 40b132
Packit 40b132
	/* First move the Key */
Packit 40b132
	for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
Packit 40b132
	    space = FREESPACE(p) - BIGOVERHEAD) {
Packit 40b132
		move_bytes = PR_MIN(space, key_size);
Packit 40b132
		off = OFFSET(p) - move_bytes;
Packit 40b132
		memmove(cp + off, key_data, move_bytes);
Packit 40b132
		key_size -= move_bytes;
Packit 40b132
		key_data += move_bytes;
Packit 40b132
		n = p[0];
Packit 40b132
		p[++n] = off;
Packit 40b132
		p[0] = ++n;
Packit 40b132
		FREESPACE(p) = off - PAGE_META(n);
Packit 40b132
		OFFSET(p) = off;
Packit 40b132
		p[n] = PARTIAL_KEY;
Packit 40b132
		bufp = __add_ovflpage(hashp, bufp);
Packit 40b132
		if (!bufp)
Packit 40b132
			return (-1);
Packit 40b132
		n = p[0];
Packit 40b132
		if (!key_size) {
Packit 40b132
			if (FREESPACE(p)) {
Packit 40b132
				move_bytes = PR_MIN(FREESPACE(p), val_size);
Packit 40b132
				off = OFFSET(p) - move_bytes;
Packit 40b132
				p[n] = off;
Packit 40b132
				memmove(cp + off, val_data, move_bytes);
Packit 40b132
				val_data += move_bytes;
Packit 40b132
				val_size -= move_bytes;
Packit 40b132
				p[n - 2] = FULL_KEY_DATA;
Packit 40b132
				FREESPACE(p) = FREESPACE(p) - move_bytes;
Packit 40b132
				OFFSET(p) = off;
Packit 40b132
			} else
Packit 40b132
				p[n - 2] = FULL_KEY;
Packit 40b132
		}
Packit 40b132
		p = (uint16 *)bufp->page;
Packit 40b132
		cp = bufp->page;
Packit 40b132
		bufp->flags |= BUF_MOD;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/* Now move the data */
Packit 40b132
	for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
Packit 40b132
	    space = FREESPACE(p) - BIGOVERHEAD) {
Packit 40b132
		move_bytes = PR_MIN(space, val_size);
Packit 40b132
		/*
Packit 40b132
		 * Here's the hack to make sure that if the data ends on the
Packit 40b132
		 * same page as the key ends, FREESPACE is at least one.
Packit 40b132
		 */
Packit 40b132
		if (space == val_size && val_size == val->size)
Packit 40b132
			move_bytes--;
Packit 40b132
		off = OFFSET(p) - move_bytes;
Packit 40b132
		memmove(cp + off, val_data, move_bytes);
Packit 40b132
		val_size -= move_bytes;
Packit 40b132
		val_data += move_bytes;
Packit 40b132
		n = p[0];
Packit 40b132
		p[++n] = off;
Packit 40b132
		p[0] = ++n;
Packit 40b132
		FREESPACE(p) = off - PAGE_META(n);
Packit 40b132
		OFFSET(p) = off;
Packit 40b132
		if (val_size) {
Packit 40b132
			p[n] = FULL_KEY;
Packit 40b132
			bufp = __add_ovflpage(hashp, bufp);
Packit 40b132
			if (!bufp)
Packit 40b132
				return (-1);
Packit 40b132
			cp = bufp->page;
Packit 40b132
			p = (uint16 *)cp;
Packit 40b132
		} else
Packit 40b132
			p[n] = FULL_KEY_DATA;
Packit 40b132
		bufp->flags |= BUF_MOD;
Packit 40b132
	}
Packit 40b132
	return (0);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Called when bufp's page  contains a partial key (index should be 1)
Packit 40b132
 *
Packit 40b132
 * All pages in the big key/data pair except bufp are freed.  We cannot
Packit 40b132
 * free bufp because the page pointing to it is lost and we can't get rid
Packit 40b132
 * of its pointer.
Packit 40b132
 *
Packit 40b132
 * Returns:
Packit 40b132
 * 0 => OK
Packit 40b132
 *-1 => ERROR
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
__big_delete(HTAB *hashp, BUFHEAD *bufp)
Packit 40b132
{
Packit 40b132
	register BUFHEAD *last_bfp, *rbufp;
Packit 40b132
	uint16 *bp, pageno;
Packit 40b132
	int key_done, n;
Packit 40b132
Packit 40b132
	rbufp = bufp;
Packit 40b132
	last_bfp = NULL;
Packit 40b132
	bp = (uint16 *)bufp->page;
Packit 40b132
	pageno = 0;
Packit 40b132
	key_done = 0;
Packit 40b132
Packit 40b132
	while (!key_done || (bp[2] != FULL_KEY_DATA)) {
Packit 40b132
		if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
Packit 40b132
			key_done = 1;
Packit 40b132
Packit 40b132
		/*
Packit 40b132
		 * If there is freespace left on a FULL_KEY_DATA page, then
Packit 40b132
		 * the data is short and fits entirely on this page, and this
Packit 40b132
		 * is the last page.
Packit 40b132
		 */
Packit 40b132
		if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
Packit 40b132
			break;
Packit 40b132
		pageno = bp[bp[0] - 1];
Packit 40b132
		rbufp->flags |= BUF_MOD;
Packit 40b132
		rbufp = __get_buf(hashp, pageno, rbufp, 0);
Packit 40b132
		if (last_bfp)
Packit 40b132
			__free_ovflpage(hashp, last_bfp);
Packit 40b132
		last_bfp = rbufp;
Packit 40b132
		if (!rbufp)
Packit 40b132
			return (-1);		/* Error. */
Packit 40b132
		bp = (uint16 *)rbufp->page;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * If we get here then rbufp points to the last page of the big
Packit 40b132
	 * key/data pair.  Bufp points to the first one -- it should now be
Packit 40b132
	 * empty pointing to the next page after this pair.  Can't free it
Packit 40b132
	 * because we don't have the page pointing to it.
Packit 40b132
	 */
Packit 40b132
Packit 40b132
	/* This is information from the last page of the pair. */
Packit 40b132
	n = bp[0];
Packit 40b132
	pageno = bp[n - 1];
Packit 40b132
Packit 40b132
	/* Now, bp is the first page of the pair. */
Packit 40b132
	bp = (uint16 *)bufp->page;
Packit 40b132
	if (n > 2) {
Packit 40b132
		/* There is an overflow page. */
Packit 40b132
		bp[1] = pageno;
Packit 40b132
		bp[2] = OVFLPAGE;
Packit 40b132
		bufp->ovfl = rbufp->ovfl;
Packit 40b132
	} else
Packit 40b132
		/* This is the last page. */
Packit 40b132
		bufp->ovfl = NULL;
Packit 40b132
	n -= 2;
Packit 40b132
	bp[0] = n;
Packit 40b132
	FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
Packit 40b132
	OFFSET(bp) = hashp->BSIZE - 1;
Packit 40b132
Packit 40b132
	bufp->flags |= BUF_MOD;
Packit 40b132
	if (rbufp)
Packit 40b132
		__free_ovflpage(hashp, rbufp);
Packit 40b132
	if (last_bfp != rbufp)
Packit 40b132
		__free_ovflpage(hashp, last_bfp);
Packit 40b132
Packit 40b132
	hashp->NKEYS--;
Packit 40b132
	return (0);
Packit 40b132
}
Packit 40b132
/*
Packit 40b132
 * Returns:
Packit 40b132
 *  0 = key not found
Packit 40b132
 * -1 = get next overflow page
Packit 40b132
 * -2 means key not found and this is big key/data
Packit 40b132
 * -3 error
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
__find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
Packit 40b132
{
Packit 40b132
	register uint16 *bp;
Packit 40b132
	register char *p;
Packit 40b132
	int ksize;
Packit 40b132
	uint16 bytes;
Packit 40b132
	char *kkey;
Packit 40b132
Packit 40b132
	bp = (uint16 *)bufp->page;
Packit 40b132
	p = bufp->page;
Packit 40b132
	ksize = size;
Packit 40b132
	kkey = key;
Packit 40b132
Packit 40b132
	for (bytes = hashp->BSIZE - bp[ndx];
Packit 40b132
	    bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
Packit 40b132
	    bytes = hashp->BSIZE - bp[ndx]) {
Packit 40b132
		if (memcmp(p + bp[ndx], kkey, bytes))
Packit 40b132
			return (-2);
Packit 40b132
		kkey += bytes;
Packit 40b132
		ksize -= bytes;
Packit 40b132
		bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
Packit 40b132
		if (!bufp)
Packit 40b132
			return (-3);
Packit 40b132
		p = bufp->page;
Packit 40b132
		bp = (uint16 *)p;
Packit 40b132
		ndx = 1;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
Packit 40b132
#ifdef HASH_STATISTICS
Packit 40b132
		++hash_collisions;
Packit 40b132
#endif
Packit 40b132
		return (-2);
Packit 40b132
	} else
Packit 40b132
		return (ndx);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Given the buffer pointer of the first overflow page of a big pair,
Packit 40b132
 * find the end of the big pair
Packit 40b132
 *
Packit 40b132
 * This will set bpp to the buffer header of the last page of the big pair.
Packit 40b132
 * It will return the pageno of the overflow page following the last page
Packit 40b132
 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
Packit 40b132
 * bucket)
Packit 40b132
 */
Packit 40b132
extern uint16
Packit 40b132
__find_last_page(HTAB *hashp, BUFHEAD **bpp)
Packit 40b132
{
Packit 40b132
	BUFHEAD *bufp;
Packit 40b132
	uint16 *bp, pageno;
Packit 40b132
	uint n;
Packit 40b132
Packit 40b132
	bufp = *bpp;
Packit 40b132
	bp = (uint16 *)bufp->page;
Packit 40b132
	for (;;) {
Packit 40b132
		n = bp[0];
Packit 40b132
Packit 40b132
		/*
Packit 40b132
		 * This is the last page if: the tag is FULL_KEY_DATA and
Packit 40b132
		 * either only 2 entries OVFLPAGE marker is explicit there
Packit 40b132
		 * is freespace on the page.
Packit 40b132
		 */
Packit 40b132
		if (bp[2] == FULL_KEY_DATA &&
Packit 40b132
		    ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
Packit 40b132
			break;
Packit 40b132
Packit 40b132
		/* LJM bound the size of n to reasonable limits
Packit 40b132
		 */
Packit 40b132
		if(n > hashp->BSIZE/sizeof(uint16))
Packit 40b132
			return(0);
Packit 40b132
Packit 40b132
		pageno = bp[n - 1];
Packit 40b132
		bufp = __get_buf(hashp, pageno, bufp, 0);
Packit 40b132
		if (!bufp)
Packit 40b132
			return (0);	/* Need to indicate an error! */
Packit 40b132
		bp = (uint16 *)bufp->page;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	*bpp = bufp;
Packit 40b132
	if (bp[0] > 2)
Packit 40b132
		return (bp[3]);
Packit 40b132
	else
Packit 40b132
		return (0);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Return the data for the key/data pair that begins on this page at this
Packit 40b132
 * index (index should always be 1).
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
__big_return(
Packit 40b132
	HTAB *hashp,
Packit 40b132
	BUFHEAD *bufp,
Packit 40b132
	int ndx,
Packit 40b132
	DBT *val,
Packit 40b132
	int set_current)
Packit 40b132
{
Packit 40b132
	BUFHEAD *save_p;
Packit 40b132
	uint16 *bp, len, off, save_addr;
Packit 40b132
	char *tp;
Packit 40b132
	int save_flags;
Packit 40b132
Packit 40b132
	bp = (uint16 *)bufp->page;
Packit 40b132
	while (bp[ndx + 1] == PARTIAL_KEY) {
Packit 40b132
		bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
Packit 40b132
		if (!bufp)
Packit 40b132
			return (-1);
Packit 40b132
		bp = (uint16 *)bufp->page;
Packit 40b132
		ndx = 1;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	if (bp[ndx + 1] == FULL_KEY) {
Packit 40b132
		bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
Packit 40b132
		if (!bufp)
Packit 40b132
			return (-1);
Packit 40b132
		bp = (uint16 *)bufp->page;
Packit 40b132
		save_p = bufp;
Packit 40b132
		save_addr = save_p->addr;
Packit 40b132
		off = bp[1];
Packit 40b132
		len = 0;
Packit 40b132
	} else
Packit 40b132
		if (!FREESPACE(bp)) {
Packit 40b132
			/*
Packit 40b132
			 * This is a hack.  We can't distinguish between
Packit 40b132
			 * FULL_KEY_DATA that contains complete data or
Packit 40b132
			 * incomplete data, so we require that if the data
Packit 40b132
			 * is complete, there is at least 1 byte of free
Packit 40b132
			 * space left.
Packit 40b132
			 */
Packit 40b132
			off = bp[bp[0]];
Packit 40b132
			len = bp[1] - off;
Packit 40b132
			save_p = bufp;
Packit 40b132
			save_addr = bufp->addr;
Packit 40b132
			bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
Packit 40b132
			if (!bufp)
Packit 40b132
				return (-1);
Packit 40b132
			bp = (uint16 *)bufp->page;
Packit 40b132
		} else {
Packit 40b132
			/* The data is all on one page. */
Packit 40b132
			tp = (char *)bp;
Packit 40b132
			off = bp[bp[0]];
Packit 40b132
			val->data = (uint8 *)tp + off;
Packit 40b132
			val->size = bp[1] - off;
Packit 40b132
			if (set_current) {
Packit 40b132
				if (bp[0] == 2) {	/* No more buckets in
Packit 40b132
							 * chain */
Packit 40b132
					hashp->cpage = NULL;
Packit 40b132
					hashp->cbucket++;
Packit 40b132
					hashp->cndx = 1;
Packit 40b132
				} else {
Packit 40b132
					hashp->cpage = __get_buf(hashp,
Packit 40b132
					    bp[bp[0] - 1], bufp, 0);
Packit 40b132
					if (!hashp->cpage)
Packit 40b132
						return (-1);
Packit 40b132
					hashp->cndx = 1;
Packit 40b132
					if (!((uint16 *)
Packit 40b132
					    hashp->cpage->page)[0]) {
Packit 40b132
						hashp->cbucket++;
Packit 40b132
						hashp->cpage = NULL;
Packit 40b132
					}
Packit 40b132
				}
Packit 40b132
			}
Packit 40b132
			return (0);
Packit 40b132
		}
Packit 40b132
Packit 40b132
	/* pin our saved buf so that we don't lose if 
Packit 40b132
	 * we run out of buffers */
Packit 40b132
 	save_flags = save_p->flags;
Packit 40b132
	save_p->flags |= BUF_PIN;
Packit 40b132
	val->size = collect_data(hashp, bufp, (int)len, set_current);
Packit 40b132
	save_p->flags = save_flags;
Packit 40b132
	if (val->size == (size_t)-1)
Packit 40b132
		return (-1);
Packit 40b132
	if (save_p->addr != save_addr) {
Packit 40b132
		/* We are pretty short on buffers. */
Packit 40b132
		errno = EINVAL;			/* OUT OF BUFFERS */
Packit 40b132
		return (-1);
Packit 40b132
	}
Packit 40b132
	memmove(hashp->tmp_buf, (save_p->page) + off, len);
Packit 40b132
	val->data = (uint8 *)hashp->tmp_buf;
Packit 40b132
	return (0);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Count how big the total datasize is by looping through the pages.  Then
Packit 40b132
 * allocate a buffer and copy the data in the second loop. NOTE: Our caller
Packit 40b132
 * may already have a bp which it is holding onto. The caller is
Packit 40b132
 * responsible for copying that bp into our temp buffer. 'len' is how much
Packit 40b132
 * space to reserve for that buffer.
Packit 40b132
 */
Packit 40b132
static int
Packit 40b132
collect_data(
Packit 40b132
	HTAB *hashp,
Packit 40b132
	BUFHEAD *bufp,
Packit 40b132
	int len, int set)
Packit 40b132
{
Packit 40b132
	register uint16 *bp;
Packit 40b132
	BUFHEAD *save_bufp;
Packit 40b132
	int save_flags;
Packit 40b132
	int mylen, totlen;
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * save the input buf head because we need to walk the list twice.
Packit 40b132
	 * pin it to make sure it doesn't leave the buffer pool. 
Packit 40b132
	 * This has the effect of growing the buffer pool if necessary.
Packit 40b132
	 */
Packit 40b132
	save_bufp = bufp;
Packit 40b132
	save_flags = save_bufp->flags;
Packit 40b132
	save_bufp->flags |= BUF_PIN;
Packit 40b132
Packit 40b132
	/* read the length of the buffer */
Packit 40b132
	for (totlen = len; bufp ; bufp = __get_buf(hashp, bp[bp[0]-1], bufp, 0)) {
Packit 40b132
		bp = (uint16 *)bufp->page;
Packit 40b132
		mylen = hashp->BSIZE - bp[1];
Packit 40b132
Packit 40b132
		/* if mylen ever goes negative it means that the
Packit 40b132
		 * page is screwed up.
Packit 40b132
		 */
Packit 40b132
		if (mylen < 0) {
Packit 40b132
			save_bufp->flags = save_flags;
Packit 40b132
			return (-1);
Packit 40b132
 		}
Packit 40b132
		totlen += mylen;
Packit 40b132
		if (bp[2] == FULL_KEY_DATA) {		/* End of Data */
Packit 40b132
			break;
Packit 40b132
		}
Packit 40b132
	}
Packit 40b132
Packit 40b132
 	if (!bufp) {
Packit 40b132
		save_bufp->flags = save_flags;
Packit 40b132
		return (-1);
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/* allocate a temp buf */
Packit 40b132
	if (hashp->tmp_buf)
Packit 40b132
		free(hashp->tmp_buf);
Packit 40b132
	if ((hashp->tmp_buf = (char *)malloc((size_t)totlen)) == NULL) {
Packit 40b132
		save_bufp->flags = save_flags;
Packit 40b132
		return (-1);
Packit 40b132
 	}
Packit 40b132
Packit 40b132
	/* copy the buffers back into temp buf */
Packit 40b132
	for (bufp = save_bufp; bufp ;
Packit 40b132
				bufp = __get_buf(hashp, bp[bp[0]-1], bufp, 0)) {
Packit 40b132
		bp = (uint16 *)bufp->page;
Packit 40b132
		mylen = hashp->BSIZE - bp[1];
Packit 40b132
		memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], (size_t)mylen);
Packit 40b132
		len += mylen;
Packit 40b132
		if (bp[2] == FULL_KEY_DATA) {
Packit 40b132
			break;
Packit 40b132
		}
Packit 40b132
	}
Packit 40b132
Packit 40b132
	/* 'clear' the pin flags */
Packit 40b132
	save_bufp->flags = save_flags;
Packit 40b132
Packit 40b132
	/* update the database cursor */
Packit 40b132
	if (set) {
Packit 40b132
		hashp->cndx = 1;
Packit 40b132
		if (bp[0] == 2) {	/* No more buckets in chain */
Packit 40b132
			hashp->cpage = NULL;
Packit 40b132
			hashp->cbucket++;
Packit 40b132
		} else {
Packit 40b132
			hashp->cpage = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
Packit 40b132
			if (!hashp->cpage)
Packit 40b132
				return (-1);
Packit 40b132
			else if (!((uint16 *)hashp->cpage->page)[0]) {
Packit 40b132
				hashp->cbucket++;
Packit 40b132
				hashp->cpage = NULL;
Packit 40b132
			}
Packit 40b132
		}
Packit 40b132
	}
Packit 40b132
	return (totlen);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Fill in the key and data for this big pair.
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
__big_keydata(
Packit 40b132
	HTAB *hashp, 
Packit 40b132
	BUFHEAD *bufp, 
Packit 40b132
	DBT *key, DBT *val,
Packit 40b132
	int set)
Packit 40b132
{
Packit 40b132
	key->size = collect_key(hashp, bufp, 0, val, set);
Packit 40b132
	if (key->size == (size_t)-1)
Packit 40b132
		return (-1);
Packit 40b132
	key->data = (uint8 *)hashp->tmp_key;
Packit 40b132
	return (0);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Count how big the total key size is by recursing through the pages.  Then
Packit 40b132
 * collect the data, allocate a buffer and copy the key as you recurse up.
Packit 40b132
 */
Packit 40b132
static int
Packit 40b132
collect_key(
Packit 40b132
	HTAB *hashp,
Packit 40b132
	BUFHEAD *bufp,
Packit 40b132
	int len,
Packit 40b132
	DBT *val,
Packit 40b132
	int set)
Packit 40b132
{
Packit 40b132
	BUFHEAD *xbp;
Packit 40b132
	char *p;
Packit 40b132
	int mylen, totlen;
Packit 40b132
	uint16 *bp, save_addr;
Packit 40b132
Packit 40b132
	p = bufp->page;
Packit 40b132
	bp = (uint16 *)p;
Packit 40b132
	mylen = hashp->BSIZE - bp[1];
Packit 40b132
Packit 40b132
	save_addr = bufp->addr;
Packit 40b132
	totlen = len + mylen;
Packit 40b132
	if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) {    /* End of Key. */
Packit 40b132
		if (hashp->tmp_key != NULL)
Packit 40b132
			free(hashp->tmp_key);
Packit 40b132
		if ((hashp->tmp_key = (char *)malloc((size_t)totlen)) == NULL)
Packit 40b132
			return (-1);
Packit 40b132
		if (__big_return(hashp, bufp, 1, val, set))
Packit 40b132
			return (-1);
Packit 40b132
	} else {
Packit 40b132
		xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
Packit 40b132
		if (!xbp || ((totlen =
Packit 40b132
		    collect_key(hashp, xbp, totlen, val, set)) < 1))
Packit 40b132
			return (-1);
Packit 40b132
	}
Packit 40b132
	if (bufp->addr != save_addr) {
Packit 40b132
		errno = EINVAL;		/* MIS -- OUT OF BUFFERS */
Packit 40b132
		return (-1);
Packit 40b132
	}
Packit 40b132
	memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], (size_t)mylen);
Packit 40b132
	return (totlen);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Returns:
Packit 40b132
 *  0 => OK
Packit 40b132
 * -1 => error
Packit 40b132
 */
Packit 40b132
extern int
Packit 40b132
__big_split(
Packit 40b132
	HTAB *hashp,
Packit 40b132
	BUFHEAD *op,	/* Pointer to where to put keys that go in old bucket */
Packit 40b132
	BUFHEAD *np,	/* Pointer to new bucket page */
Packit 40b132
			/* Pointer to first page containing the big key/data */
Packit 40b132
	BUFHEAD *big_keyp,
Packit 40b132
	uint32 addr,	/* Address of big_keyp */
Packit 40b132
	uint32   obucket,/* Old Bucket */
Packit 40b132
	SPLIT_RETURN *ret)
Packit 40b132
{
Packit 40b132
	register BUFHEAD *tmpp;
Packit 40b132
	register uint16 *tp;
Packit 40b132
	BUFHEAD *bp;
Packit 40b132
	DBT key, val;
Packit 40b132
	uint32 change;
Packit 40b132
	uint16 free_space, n, off;
Packit 40b132
Packit 40b132
	bp = big_keyp;
Packit 40b132
Packit 40b132
	/* Now figure out where the big key/data goes */
Packit 40b132
	if (__big_keydata(hashp, big_keyp, &key, &val, 0))
Packit 40b132
		return (-1);
Packit 40b132
	change = (__call_hash(hashp,(char*) key.data, key.size) != obucket);
Packit 40b132
Packit 40b132
	if ((ret->next_addr = __find_last_page(hashp, &big_keyp))) {
Packit 40b132
		if (!(ret->nextp =
Packit 40b132
		    __get_buf(hashp, ret->next_addr, big_keyp, 0)))
Packit 40b132
			return (-1);;
Packit 40b132
	} else
Packit 40b132
		ret->nextp = NULL;
Packit 40b132
Packit 40b132
	/* Now make one of np/op point to the big key/data pair */
Packit 40b132
#ifdef DEBUG
Packit 40b132
	assert(np->ovfl == NULL);
Packit 40b132
#endif
Packit 40b132
	if (change)
Packit 40b132
		tmpp = np;
Packit 40b132
	else
Packit 40b132
		tmpp = op;
Packit 40b132
Packit 40b132
	tmpp->flags |= BUF_MOD;
Packit 40b132
#ifdef DEBUG1
Packit 40b132
	(void)fprintf(stderr,
Packit 40b132
	    "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
Packit 40b132
	    (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
Packit 40b132
#endif
Packit 40b132
	tmpp->ovfl = bp;	/* one of op/np point to big_keyp */
Packit 40b132
	tp = (uint16 *)tmpp->page;
Packit 40b132
Packit 40b132
Packit 40b132
#if 0  /* this get's tripped on database corrupted error */
Packit 40b132
	assert(FREESPACE(tp) >= OVFLSIZE);
Packit 40b132
#endif
Packit 40b132
	if(FREESPACE(tp) < OVFLSIZE)
Packit 40b132
		return(DATABASE_CORRUPTED_ERROR);
Packit 40b132
Packit 40b132
	n = tp[0];
Packit 40b132
	off = OFFSET(tp);
Packit 40b132
	free_space = FREESPACE(tp);
Packit 40b132
	tp[++n] = (uint16)addr;
Packit 40b132
	tp[++n] = OVFLPAGE;
Packit 40b132
	tp[0] = n;
Packit 40b132
	OFFSET(tp) = off;
Packit 40b132
	FREESPACE(tp) = free_space - OVFLSIZE;
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * Finally, set the new and old return values. BIG_KEYP contains a
Packit 40b132
	 * pointer to the last page of the big key_data pair. Make sure that
Packit 40b132
	 * big_keyp has no following page (2 elements) or create an empty
Packit 40b132
	 * following page.
Packit 40b132
	 */
Packit 40b132
Packit 40b132
	ret->newp = np;
Packit 40b132
	ret->oldp = op;
Packit 40b132
Packit 40b132
	tp = (uint16 *)big_keyp->page;
Packit 40b132
	big_keyp->flags |= BUF_MOD;
Packit 40b132
	if (tp[0] > 2) {
Packit 40b132
		/*
Packit 40b132
		 * There may be either one or two offsets on this page.  If
Packit 40b132
		 * there is one, then the overflow page is linked on normally
Packit 40b132
		 * and tp[4] is OVFLPAGE.  If there are two, tp[4] contains
Packit 40b132
		 * the second offset and needs to get stuffed in after the
Packit 40b132
		 * next overflow page is added.
Packit 40b132
		 */
Packit 40b132
		n = tp[4];
Packit 40b132
		free_space = FREESPACE(tp);
Packit 40b132
		off = OFFSET(tp);
Packit 40b132
		tp[0] -= 2;
Packit 40b132
		FREESPACE(tp) = free_space + OVFLSIZE;
Packit 40b132
		OFFSET(tp) = off;
Packit 40b132
		tmpp = __add_ovflpage(hashp, big_keyp);
Packit 40b132
		if (!tmpp)
Packit 40b132
			return (-1);
Packit 40b132
		tp[4] = n;
Packit 40b132
	} else
Packit 40b132
		tmpp = big_keyp;
Packit 40b132
Packit 40b132
	if (change)
Packit 40b132
		ret->newp = tmpp;
Packit 40b132
	else
Packit 40b132
		ret->oldp = tmpp;
Packit 40b132
	return (0);
Packit 40b132
}