xref: /freebsd/lib/libc/db/btree/bt_split.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ef5d438eSPaul Traina  * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Mike Olson.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
355f301949SBen Laurie #include <sys/param.h>
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #include <limits.h>
3858f0484fSRodney W. Grimes #include <stdio.h>
3958f0484fSRodney W. Grimes #include <stdlib.h>
4058f0484fSRodney W. Grimes #include <string.h>
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <db.h>
4358f0484fSRodney W. Grimes #include "btree.h"
4458f0484fSRodney W. Grimes 
45c05ac53bSDavid E. O'Brien static int	 bt_broot(BTREE *, PAGE *, PAGE *, PAGE *);
4615164b99SDavid E. O'Brien static PAGE	*bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
47c05ac53bSDavid E. O'Brien static int	 bt_preserve(BTREE *, pgno_t);
4815164b99SDavid E. O'Brien static PAGE	*bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t);
4915164b99SDavid E. O'Brien static PAGE	*bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
50c05ac53bSDavid E. O'Brien static int	 bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *);
51c05ac53bSDavid E. O'Brien static recno_t	 rec_total(PAGE *);
5258f0484fSRodney W. Grimes 
5358f0484fSRodney W. Grimes #ifdef STATISTICS
5458f0484fSRodney W. Grimes u_long	bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
5558f0484fSRodney W. Grimes #endif
5658f0484fSRodney W. Grimes 
5758f0484fSRodney W. Grimes /*
5858f0484fSRodney W. Grimes  * __BT_SPLIT -- Split the tree.
5958f0484fSRodney W. Grimes  *
6058f0484fSRodney W. Grimes  * Parameters:
6158f0484fSRodney W. Grimes  *	t:	tree
6258f0484fSRodney W. Grimes  *	sp:	page to split
6358f0484fSRodney W. Grimes  *	key:	key to insert
6458f0484fSRodney W. Grimes  *	data:	data to insert
6558f0484fSRodney W. Grimes  *	flags:	BIGKEY/BIGDATA flags
6658f0484fSRodney W. Grimes  *	ilen:	insert length
6758f0484fSRodney W. Grimes  *	skip:	index to leave open
6858f0484fSRodney W. Grimes  *
6958f0484fSRodney W. Grimes  * Returns:
7058f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
7158f0484fSRodney W. Grimes  */
7258f0484fSRodney W. Grimes int
__bt_split(BTREE * t,PAGE * sp,const DBT * key,const DBT * data,int flags,size_t ilen,u_int32_t argskip)730ac22237SXin LI __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags,
740ac22237SXin LI     size_t ilen, u_int32_t argskip)
7558f0484fSRodney W. Grimes {
7658f0484fSRodney W. Grimes 	BINTERNAL *bi;
7758f0484fSRodney W. Grimes 	BLEAF *bl, *tbl;
7858f0484fSRodney W. Grimes 	DBT a, b;
7958f0484fSRodney W. Grimes 	EPGNO *parent;
8058f0484fSRodney W. Grimes 	PAGE *h, *l, *r, *lchild, *rchild;
8158f0484fSRodney W. Grimes 	indx_t nxtindex;
82ef5d438eSPaul Traina 	u_int16_t skip;
83ef5d438eSPaul Traina 	u_int32_t n, nbytes, nksize;
8458f0484fSRodney W. Grimes 	int parentsplit;
8558f0484fSRodney W. Grimes 	char *dest;
8658f0484fSRodney W. Grimes 
8758f0484fSRodney W. Grimes 	/*
8858f0484fSRodney W. Grimes 	 * Split the page into two pages, l and r.  The split routines return
8958f0484fSRodney W. Grimes 	 * a pointer to the page into which the key should be inserted and with
9058f0484fSRodney W. Grimes 	 * skip set to the offset which should be used.  Additionally, l and r
9158f0484fSRodney W. Grimes 	 * are pinned.
9258f0484fSRodney W. Grimes 	 */
93ef5d438eSPaul Traina 	skip = argskip;
9458f0484fSRodney W. Grimes 	h = sp->pgno == P_ROOT ?
9558f0484fSRodney W. Grimes 	    bt_root(t, sp, &l, &r, &skip, ilen) :
9658f0484fSRodney W. Grimes 	    bt_page(t, sp, &l, &r, &skip, ilen);
9758f0484fSRodney W. Grimes 	if (h == NULL)
9858f0484fSRodney W. Grimes 		return (RET_ERROR);
9958f0484fSRodney W. Grimes 
10058f0484fSRodney W. Grimes 	/*
10158f0484fSRodney W. Grimes 	 * Insert the new key/data pair into the leaf page.  (Key inserts
10258f0484fSRodney W. Grimes 	 * always cause a leaf page to split first.)
10358f0484fSRodney W. Grimes 	 */
10458f0484fSRodney W. Grimes 	h->linp[skip] = h->upper -= ilen;
10558f0484fSRodney W. Grimes 	dest = (char *)h + h->upper;
106ef5d438eSPaul Traina 	if (F_ISSET(t, R_RECNO))
10758f0484fSRodney W. Grimes 		WR_RLEAF(dest, data, flags)
10858f0484fSRodney W. Grimes 	else
10958f0484fSRodney W. Grimes 		WR_BLEAF(dest, key, data, flags)
11058f0484fSRodney W. Grimes 
11158f0484fSRodney W. Grimes 	/* If the root page was split, make it look right. */
11258f0484fSRodney W. Grimes 	if (sp->pgno == P_ROOT &&
113ef5d438eSPaul Traina 	    (F_ISSET(t, R_RECNO) ?
11458f0484fSRodney W. Grimes 	    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
11558f0484fSRodney W. Grimes 		goto err2;
11658f0484fSRodney W. Grimes 
11758f0484fSRodney W. Grimes 	/*
11858f0484fSRodney W. Grimes 	 * Now we walk the parent page stack -- a LIFO stack of the pages that
11958f0484fSRodney W. Grimes 	 * were traversed when we searched for the page that split.  Each stack
12058f0484fSRodney W. Grimes 	 * entry is a page number and a page index offset.  The offset is for
12158f0484fSRodney W. Grimes 	 * the page traversed on the search.  We've just split a page, so we
12258f0484fSRodney W. Grimes 	 * have to insert a new key into the parent page.
12358f0484fSRodney W. Grimes 	 *
12458f0484fSRodney W. Grimes 	 * If the insert into the parent page causes it to split, may have to
12558f0484fSRodney W. Grimes 	 * continue splitting all the way up the tree.  We stop if the root
12658f0484fSRodney W. Grimes 	 * splits or the page inserted into didn't have to split to hold the
12758f0484fSRodney W. Grimes 	 * new key.  Some algorithms replace the key for the old page as well
12858f0484fSRodney W. Grimes 	 * as the new page.  We don't, as there's no reason to believe that the
12958f0484fSRodney W. Grimes 	 * first key on the old page is any better than the key we have, and,
13058f0484fSRodney W. Grimes 	 * in the case of a key being placed at index 0 causing the split, the
13158f0484fSRodney W. Grimes 	 * key is unavailable.
13258f0484fSRodney W. Grimes 	 *
13358f0484fSRodney W. Grimes 	 * There are a maximum of 5 pages pinned at any time.  We keep the left
13458f0484fSRodney W. Grimes 	 * and right pages pinned while working on the parent.   The 5 are the
13558f0484fSRodney W. Grimes 	 * two children, left parent and right parent (when the parent splits)
13658f0484fSRodney W. Grimes 	 * and the root page or the overflow key page when calling bt_preserve.
13758f0484fSRodney W. Grimes 	 * This code must make sure that all pins are released other than the
13858f0484fSRodney W. Grimes 	 * root page or overflow page which is unlocked elsewhere.
13958f0484fSRodney W. Grimes 	 */
14058f0484fSRodney W. Grimes 	while ((parent = BT_POP(t)) != NULL) {
14158f0484fSRodney W. Grimes 		lchild = l;
14258f0484fSRodney W. Grimes 		rchild = r;
14358f0484fSRodney W. Grimes 
14458f0484fSRodney W. Grimes 		/* Get the parent page. */
14558f0484fSRodney W. Grimes 		if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
14658f0484fSRodney W. Grimes 			goto err2;
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes 		/*
14958f0484fSRodney W. Grimes 		 * The new key goes ONE AFTER the index, because the split
15058f0484fSRodney W. Grimes 		 * was to the right.
15158f0484fSRodney W. Grimes 		 */
15258f0484fSRodney W. Grimes 		skip = parent->index + 1;
15358f0484fSRodney W. Grimes 
15458f0484fSRodney W. Grimes 		/*
15558f0484fSRodney W. Grimes 		 * Calculate the space needed on the parent page.
15658f0484fSRodney W. Grimes 		 *
15758f0484fSRodney W. Grimes 		 * Prefix trees: space hack when inserting into BINTERNAL
15858f0484fSRodney W. Grimes 		 * pages.  Retain only what's needed to distinguish between
15958f0484fSRodney W. Grimes 		 * the new entry and the LAST entry on the page to its left.
16058f0484fSRodney W. Grimes 		 * If the keys compare equal, retain the entire key.  Note,
16158f0484fSRodney W. Grimes 		 * we don't touch overflow keys, and the entire key must be
16258f0484fSRodney W. Grimes 		 * retained for the next-to-left most key on the leftmost
16358f0484fSRodney W. Grimes 		 * page of each level, or the search will fail.  Applicable
16458f0484fSRodney W. Grimes 		 * ONLY to internal pages that have leaf pages as children.
16558f0484fSRodney W. Grimes 		 * Further reduction of the key between pairs of internal
16658f0484fSRodney W. Grimes 		 * pages loses too much information.
16758f0484fSRodney W. Grimes 		 */
16858f0484fSRodney W. Grimes 		switch (rchild->flags & P_TYPE) {
16958f0484fSRodney W. Grimes 		case P_BINTERNAL:
17058f0484fSRodney W. Grimes 			bi = GETBINTERNAL(rchild, 0);
17158f0484fSRodney W. Grimes 			nbytes = NBINTERNAL(bi->ksize);
17258f0484fSRodney W. Grimes 			break;
17358f0484fSRodney W. Grimes 		case P_BLEAF:
17458f0484fSRodney W. Grimes 			bl = GETBLEAF(rchild, 0);
17558f0484fSRodney W. Grimes 			nbytes = NBINTERNAL(bl->ksize);
17658f0484fSRodney W. Grimes 			if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
17758f0484fSRodney W. Grimes 			    (h->prevpg != P_INVALID || skip > 1)) {
17858f0484fSRodney W. Grimes 				tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
17958f0484fSRodney W. Grimes 				a.size = tbl->ksize;
18058f0484fSRodney W. Grimes 				a.data = tbl->bytes;
18158f0484fSRodney W. Grimes 				b.size = bl->ksize;
18258f0484fSRodney W. Grimes 				b.data = bl->bytes;
18358f0484fSRodney W. Grimes 				nksize = t->bt_pfx(&a, &b);
18458f0484fSRodney W. Grimes 				n = NBINTERNAL(nksize);
18558f0484fSRodney W. Grimes 				if (n < nbytes) {
18658f0484fSRodney W. Grimes #ifdef STATISTICS
18758f0484fSRodney W. Grimes 					bt_pfxsaved += nbytes - n;
18858f0484fSRodney W. Grimes #endif
18958f0484fSRodney W. Grimes 					nbytes = n;
19058f0484fSRodney W. Grimes 				} else
19158f0484fSRodney W. Grimes 					nksize = 0;
19258f0484fSRodney W. Grimes 			} else
19358f0484fSRodney W. Grimes 				nksize = 0;
19458f0484fSRodney W. Grimes 			break;
19558f0484fSRodney W. Grimes 		case P_RINTERNAL:
19658f0484fSRodney W. Grimes 		case P_RLEAF:
19758f0484fSRodney W. Grimes 			nbytes = NRINTERNAL;
19858f0484fSRodney W. Grimes 			break;
19958f0484fSRodney W. Grimes 		default:
20058f0484fSRodney W. Grimes 			abort();
20158f0484fSRodney W. Grimes 		}
20258f0484fSRodney W. Grimes 
20358f0484fSRodney W. Grimes 		/* Split the parent page if necessary or shift the indices. */
204f60486b3SXin LI 		if ((u_int32_t)(h->upper - h->lower) < nbytes + sizeof(indx_t)) {
20558f0484fSRodney W. Grimes 			sp = h;
20658f0484fSRodney W. Grimes 			h = h->pgno == P_ROOT ?
20758f0484fSRodney W. Grimes 			    bt_root(t, h, &l, &r, &skip, nbytes) :
20858f0484fSRodney W. Grimes 			    bt_page(t, h, &l, &r, &skip, nbytes);
20958f0484fSRodney W. Grimes 			if (h == NULL)
21058f0484fSRodney W. Grimes 				goto err1;
21158f0484fSRodney W. Grimes 			parentsplit = 1;
21258f0484fSRodney W. Grimes 		} else {
21358f0484fSRodney W. Grimes 			if (skip < (nxtindex = NEXTINDEX(h)))
21458f0484fSRodney W. Grimes 				memmove(h->linp + skip + 1, h->linp + skip,
21558f0484fSRodney W. Grimes 				    (nxtindex - skip) * sizeof(indx_t));
21658f0484fSRodney W. Grimes 			h->lower += sizeof(indx_t);
21758f0484fSRodney W. Grimes 			parentsplit = 0;
21858f0484fSRodney W. Grimes 		}
21958f0484fSRodney W. Grimes 
22058f0484fSRodney W. Grimes 		/* Insert the key into the parent page. */
22158f0484fSRodney W. Grimes 		switch (rchild->flags & P_TYPE) {
22258f0484fSRodney W. Grimes 		case P_BINTERNAL:
22358f0484fSRodney W. Grimes 			h->linp[skip] = h->upper -= nbytes;
22458f0484fSRodney W. Grimes 			dest = (char *)h + h->linp[skip];
22558f0484fSRodney W. Grimes 			memmove(dest, bi, nbytes);
22658f0484fSRodney W. Grimes 			((BINTERNAL *)dest)->pgno = rchild->pgno;
22758f0484fSRodney W. Grimes 			break;
22858f0484fSRodney W. Grimes 		case P_BLEAF:
22958f0484fSRodney W. Grimes 			h->linp[skip] = h->upper -= nbytes;
23058f0484fSRodney W. Grimes 			dest = (char *)h + h->linp[skip];
23158f0484fSRodney W. Grimes 			WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
23258f0484fSRodney W. Grimes 			    rchild->pgno, bl->flags & P_BIGKEY);
23358f0484fSRodney W. Grimes 			memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
2343579f3d9SPedro F. Giffuni 			if (bl->flags & P_BIGKEY) {
2353579f3d9SPedro F. Giffuni 				pgno_t pgno;
2363579f3d9SPedro F. Giffuni 				memcpy(&pgno, bl->bytes, sizeof(pgno));
2373579f3d9SPedro F. Giffuni 				if (bt_preserve(t, pgno) == RET_ERROR)
23858f0484fSRodney W. Grimes 					goto err1;
2393579f3d9SPedro F. Giffuni 			}
24058f0484fSRodney W. Grimes 			break;
24158f0484fSRodney W. Grimes 		case P_RINTERNAL:
24258f0484fSRodney W. Grimes 			/*
24358f0484fSRodney W. Grimes 			 * Update the left page count.  If split
24458f0484fSRodney W. Grimes 			 * added at index 0, fix the correct page.
24558f0484fSRodney W. Grimes 			 */
24658f0484fSRodney W. Grimes 			if (skip > 0)
24758f0484fSRodney W. Grimes 				dest = (char *)h + h->linp[skip - 1];
24858f0484fSRodney W. Grimes 			else
24958f0484fSRodney W. Grimes 				dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
25058f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->nrecs = rec_total(lchild);
25158f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->pgno = lchild->pgno;
25258f0484fSRodney W. Grimes 
25358f0484fSRodney W. Grimes 			/* Update the right page count. */
25458f0484fSRodney W. Grimes 			h->linp[skip] = h->upper -= nbytes;
25558f0484fSRodney W. Grimes 			dest = (char *)h + h->linp[skip];
25658f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->nrecs = rec_total(rchild);
25758f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->pgno = rchild->pgno;
25858f0484fSRodney W. Grimes 			break;
25958f0484fSRodney W. Grimes 		case P_RLEAF:
26058f0484fSRodney W. Grimes 			/*
26158f0484fSRodney W. Grimes 			 * Update the left page count.  If split
26258f0484fSRodney W. Grimes 			 * added at index 0, fix the correct page.
26358f0484fSRodney W. Grimes 			 */
26458f0484fSRodney W. Grimes 			if (skip > 0)
26558f0484fSRodney W. Grimes 				dest = (char *)h + h->linp[skip - 1];
26658f0484fSRodney W. Grimes 			else
26758f0484fSRodney W. Grimes 				dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
26858f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild);
26958f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->pgno = lchild->pgno;
27058f0484fSRodney W. Grimes 
27158f0484fSRodney W. Grimes 			/* Update the right page count. */
27258f0484fSRodney W. Grimes 			h->linp[skip] = h->upper -= nbytes;
27358f0484fSRodney W. Grimes 			dest = (char *)h + h->linp[skip];
27458f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild);
27558f0484fSRodney W. Grimes 			((RINTERNAL *)dest)->pgno = rchild->pgno;
27658f0484fSRodney W. Grimes 			break;
27758f0484fSRodney W. Grimes 		default:
27858f0484fSRodney W. Grimes 			abort();
27958f0484fSRodney W. Grimes 		}
28058f0484fSRodney W. Grimes 
28158f0484fSRodney W. Grimes 		/* Unpin the held pages. */
28258f0484fSRodney W. Grimes 		if (!parentsplit) {
28358f0484fSRodney W. Grimes 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
28458f0484fSRodney W. Grimes 			break;
28558f0484fSRodney W. Grimes 		}
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes 		/* If the root page was split, make it look right. */
28858f0484fSRodney W. Grimes 		if (sp->pgno == P_ROOT &&
289ef5d438eSPaul Traina 		    (F_ISSET(t, R_RECNO) ?
29058f0484fSRodney W. Grimes 		    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
29158f0484fSRodney W. Grimes 			goto err1;
29258f0484fSRodney W. Grimes 
29358f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
29458f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
29558f0484fSRodney W. Grimes 	}
29658f0484fSRodney W. Grimes 
29758f0484fSRodney W. Grimes 	/* Unpin the held pages. */
29858f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, l, MPOOL_DIRTY);
29958f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, r, MPOOL_DIRTY);
30058f0484fSRodney W. Grimes 
30158f0484fSRodney W. Grimes 	/* Clear any pages left on the stack. */
30258f0484fSRodney W. Grimes 	return (RET_SUCCESS);
30358f0484fSRodney W. Grimes 
30458f0484fSRodney W. Grimes 	/*
30558f0484fSRodney W. Grimes 	 * If something fails in the above loop we were already walking back
30658f0484fSRodney W. Grimes 	 * up the tree and the tree is now inconsistent.  Nothing much we can
30758f0484fSRodney W. Grimes 	 * do about it but release any memory we're holding.
30858f0484fSRodney W. Grimes 	 */
30958f0484fSRodney W. Grimes err1:	mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
31058f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
31158f0484fSRodney W. Grimes 
31258f0484fSRodney W. Grimes err2:	mpool_put(t->bt_mp, l, 0);
31358f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, r, 0);
31458f0484fSRodney W. Grimes 	__dbpanic(t->bt_dbp);
31558f0484fSRodney W. Grimes 	return (RET_ERROR);
31658f0484fSRodney W. Grimes }
31758f0484fSRodney W. Grimes 
31858f0484fSRodney W. Grimes /*
31958f0484fSRodney W. Grimes  * BT_PAGE -- Split a non-root page of a btree.
32058f0484fSRodney W. Grimes  *
32158f0484fSRodney W. Grimes  * Parameters:
32258f0484fSRodney W. Grimes  *	t:	tree
32358f0484fSRodney W. Grimes  *	h:	root page
32458f0484fSRodney W. Grimes  *	lp:	pointer to left page pointer
32558f0484fSRodney W. Grimes  *	rp:	pointer to right page pointer
32658f0484fSRodney W. Grimes  *	skip:	pointer to index to leave open
32758f0484fSRodney W. Grimes  *	ilen:	insert length
32858f0484fSRodney W. Grimes  *
32958f0484fSRodney W. Grimes  * Returns:
33058f0484fSRodney W. Grimes  *	Pointer to page in which to insert or NULL on error.
33158f0484fSRodney W. Grimes  */
33258f0484fSRodney W. Grimes static PAGE *
bt_page(BTREE * t,PAGE * h,PAGE ** lp,PAGE ** rp,indx_t * skip,size_t ilen)3330ac22237SXin LI bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
33458f0484fSRodney W. Grimes {
33558f0484fSRodney W. Grimes 	PAGE *l, *r, *tp;
33658f0484fSRodney W. Grimes 	pgno_t npg;
33758f0484fSRodney W. Grimes 
33858f0484fSRodney W. Grimes #ifdef STATISTICS
33958f0484fSRodney W. Grimes 	++bt_split;
34058f0484fSRodney W. Grimes #endif
34158f0484fSRodney W. Grimes 	/* Put the new right page for the split into place. */
34258f0484fSRodney W. Grimes 	if ((r = __bt_new(t, &npg)) == NULL)
34358f0484fSRodney W. Grimes 		return (NULL);
34458f0484fSRodney W. Grimes 	r->pgno = npg;
34558f0484fSRodney W. Grimes 	r->lower = BTDATAOFF;
34658f0484fSRodney W. Grimes 	r->upper = t->bt_psize;
34758f0484fSRodney W. Grimes 	r->nextpg = h->nextpg;
34858f0484fSRodney W. Grimes 	r->prevpg = h->pgno;
34958f0484fSRodney W. Grimes 	r->flags = h->flags & P_TYPE;
35058f0484fSRodney W. Grimes 
35158f0484fSRodney W. Grimes 	/*
35258f0484fSRodney W. Grimes 	 * If we're splitting the last page on a level because we're appending
35358f0484fSRodney W. Grimes 	 * a key to it (skip is NEXTINDEX()), it's likely that the data is
35458f0484fSRodney W. Grimes 	 * sorted.  Adding an empty page on the side of the level is less work
35558f0484fSRodney W. Grimes 	 * and can push the fill factor much higher than normal.  If we're
35658f0484fSRodney W. Grimes 	 * wrong it's no big deal, we'll just do the split the right way next
35758f0484fSRodney W. Grimes 	 * time.  It may look like it's equally easy to do a similar hack for
35858f0484fSRodney W. Grimes 	 * reverse sorted data, that is, split the tree left, but it's not.
35958f0484fSRodney W. Grimes 	 * Don't even try.
36058f0484fSRodney W. Grimes 	 */
36158f0484fSRodney W. Grimes 	if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
36258f0484fSRodney W. Grimes #ifdef STATISTICS
36358f0484fSRodney W. Grimes 		++bt_sortsplit;
36458f0484fSRodney W. Grimes #endif
36558f0484fSRodney W. Grimes 		h->nextpg = r->pgno;
36658f0484fSRodney W. Grimes 		r->lower = BTDATAOFF + sizeof(indx_t);
36758f0484fSRodney W. Grimes 		*skip = 0;
36858f0484fSRodney W. Grimes 		*lp = h;
36958f0484fSRodney W. Grimes 		*rp = r;
37058f0484fSRodney W. Grimes 		return (r);
37158f0484fSRodney W. Grimes 	}
37258f0484fSRodney W. Grimes 
37358f0484fSRodney W. Grimes 	/* Put the new left page for the split into place. */
374c9f30aaaSXin LI 	if ((l = (PAGE *)calloc(1, t->bt_psize)) == NULL) {
37558f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, r, 0);
37658f0484fSRodney W. Grimes 		return (NULL);
37758f0484fSRodney W. Grimes 	}
37858f0484fSRodney W. Grimes 	l->pgno = h->pgno;
37958f0484fSRodney W. Grimes 	l->nextpg = r->pgno;
38058f0484fSRodney W. Grimes 	l->prevpg = h->prevpg;
38158f0484fSRodney W. Grimes 	l->lower = BTDATAOFF;
38258f0484fSRodney W. Grimes 	l->upper = t->bt_psize;
38358f0484fSRodney W. Grimes 	l->flags = h->flags & P_TYPE;
38458f0484fSRodney W. Grimes 
38558f0484fSRodney W. Grimes 	/* Fix up the previous pointer of the page after the split page. */
38658f0484fSRodney W. Grimes 	if (h->nextpg != P_INVALID) {
38758f0484fSRodney W. Grimes 		if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
38858f0484fSRodney W. Grimes 			free(l);
38958f0484fSRodney W. Grimes 			/* XXX mpool_free(t->bt_mp, r->pgno); */
39058f0484fSRodney W. Grimes 			return (NULL);
39158f0484fSRodney W. Grimes 		}
39258f0484fSRodney W. Grimes 		tp->prevpg = r->pgno;
393ef5d438eSPaul Traina 		mpool_put(t->bt_mp, tp, MPOOL_DIRTY);
39458f0484fSRodney W. Grimes 	}
39558f0484fSRodney W. Grimes 
39658f0484fSRodney W. Grimes 	/*
39758f0484fSRodney W. Grimes 	 * Split right.  The key/data pairs aren't sorted in the btree page so
39858f0484fSRodney W. Grimes 	 * it's simpler to copy the data from the split page onto two new pages
39958f0484fSRodney W. Grimes 	 * instead of copying half the data to the right page and compacting
40058f0484fSRodney W. Grimes 	 * the left page in place.  Since the left page can't change, we have
40158f0484fSRodney W. Grimes 	 * to swap the original and the allocated left page after the split.
40258f0484fSRodney W. Grimes 	 */
40358f0484fSRodney W. Grimes 	tp = bt_psplit(t, h, l, r, skip, ilen);
40458f0484fSRodney W. Grimes 
40558f0484fSRodney W. Grimes 	/* Move the new left page onto the old left page. */
40658f0484fSRodney W. Grimes 	memmove(h, l, t->bt_psize);
40758f0484fSRodney W. Grimes 	if (tp == l)
40858f0484fSRodney W. Grimes 		tp = h;
40958f0484fSRodney W. Grimes 	free(l);
41058f0484fSRodney W. Grimes 
41158f0484fSRodney W. Grimes 	*lp = h;
41258f0484fSRodney W. Grimes 	*rp = r;
41358f0484fSRodney W. Grimes 	return (tp);
41458f0484fSRodney W. Grimes }
41558f0484fSRodney W. Grimes 
41658f0484fSRodney W. Grimes /*
41758f0484fSRodney W. Grimes  * BT_ROOT -- Split the root page of a btree.
41858f0484fSRodney W. Grimes  *
41958f0484fSRodney W. Grimes  * Parameters:
42058f0484fSRodney W. Grimes  *	t:	tree
42158f0484fSRodney W. Grimes  *	h:	root page
42258f0484fSRodney W. Grimes  *	lp:	pointer to left page pointer
42358f0484fSRodney W. Grimes  *	rp:	pointer to right page pointer
42458f0484fSRodney W. Grimes  *	skip:	pointer to index to leave open
42558f0484fSRodney W. Grimes  *	ilen:	insert length
42658f0484fSRodney W. Grimes  *
42758f0484fSRodney W. Grimes  * Returns:
42858f0484fSRodney W. Grimes  *	Pointer to page in which to insert or NULL on error.
42958f0484fSRodney W. Grimes  */
43058f0484fSRodney W. Grimes static PAGE *
bt_root(BTREE * t,PAGE * h,PAGE ** lp,PAGE ** rp,indx_t * skip,size_t ilen)4310ac22237SXin LI bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
43258f0484fSRodney W. Grimes {
43358f0484fSRodney W. Grimes 	PAGE *l, *r, *tp;
43458f0484fSRodney W. Grimes 	pgno_t lnpg, rnpg;
43558f0484fSRodney W. Grimes 
43658f0484fSRodney W. Grimes #ifdef STATISTICS
43758f0484fSRodney W. Grimes 	++bt_split;
43858f0484fSRodney W. Grimes 	++bt_rootsplit;
43958f0484fSRodney W. Grimes #endif
44058f0484fSRodney W. Grimes 	/* Put the new left and right pages for the split into place. */
44158f0484fSRodney W. Grimes 	if ((l = __bt_new(t, &lnpg)) == NULL ||
44258f0484fSRodney W. Grimes 	    (r = __bt_new(t, &rnpg)) == NULL)
44358f0484fSRodney W. Grimes 		return (NULL);
44458f0484fSRodney W. Grimes 	l->pgno = lnpg;
44558f0484fSRodney W. Grimes 	r->pgno = rnpg;
44658f0484fSRodney W. Grimes 	l->nextpg = r->pgno;
44758f0484fSRodney W. Grimes 	r->prevpg = l->pgno;
44858f0484fSRodney W. Grimes 	l->prevpg = r->nextpg = P_INVALID;
44958f0484fSRodney W. Grimes 	l->lower = r->lower = BTDATAOFF;
45058f0484fSRodney W. Grimes 	l->upper = r->upper = t->bt_psize;
45158f0484fSRodney W. Grimes 	l->flags = r->flags = h->flags & P_TYPE;
45258f0484fSRodney W. Grimes 
45358f0484fSRodney W. Grimes 	/* Split the root page. */
45458f0484fSRodney W. Grimes 	tp = bt_psplit(t, h, l, r, skip, ilen);
45558f0484fSRodney W. Grimes 
45658f0484fSRodney W. Grimes 	*lp = l;
45758f0484fSRodney W. Grimes 	*rp = r;
45858f0484fSRodney W. Grimes 	return (tp);
45958f0484fSRodney W. Grimes }
46058f0484fSRodney W. Grimes 
46158f0484fSRodney W. Grimes /*
46258f0484fSRodney W. Grimes  * BT_RROOT -- Fix up the recno root page after it has been split.
46358f0484fSRodney W. Grimes  *
46458f0484fSRodney W. Grimes  * Parameters:
46558f0484fSRodney W. Grimes  *	t:	tree
46658f0484fSRodney W. Grimes  *	h:	root page
46758f0484fSRodney W. Grimes  *	l:	left page
46858f0484fSRodney W. Grimes  *	r:	right page
46958f0484fSRodney W. Grimes  *
47058f0484fSRodney W. Grimes  * Returns:
47158f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
47258f0484fSRodney W. Grimes  */
47358f0484fSRodney W. Grimes static int
bt_rroot(BTREE * t,PAGE * h,PAGE * l,PAGE * r)4740ac22237SXin LI bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
47558f0484fSRodney W. Grimes {
47658f0484fSRodney W. Grimes 	char *dest;
47758f0484fSRodney W. Grimes 
47858f0484fSRodney W. Grimes 	/* Insert the left and right keys, set the header information. */
47958f0484fSRodney W. Grimes 	h->linp[0] = h->upper = t->bt_psize - NRINTERNAL;
48058f0484fSRodney W. Grimes 	dest = (char *)h + h->upper;
48158f0484fSRodney W. Grimes 	WR_RINTERNAL(dest,
48258f0484fSRodney W. Grimes 	    l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
48358f0484fSRodney W. Grimes 
4845f301949SBen Laurie 	__PAST_END(h->linp, 1) = h->upper -= NRINTERNAL;
48558f0484fSRodney W. Grimes 	dest = (char *)h + h->upper;
48658f0484fSRodney W. Grimes 	WR_RINTERNAL(dest,
48758f0484fSRodney W. Grimes 	    r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
48858f0484fSRodney W. Grimes 
48958f0484fSRodney W. Grimes 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
49058f0484fSRodney W. Grimes 
49158f0484fSRodney W. Grimes 	/* Unpin the root page, set to recno internal page. */
49258f0484fSRodney W. Grimes 	h->flags &= ~P_TYPE;
49358f0484fSRodney W. Grimes 	h->flags |= P_RINTERNAL;
49458f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
49558f0484fSRodney W. Grimes 
49658f0484fSRodney W. Grimes 	return (RET_SUCCESS);
49758f0484fSRodney W. Grimes }
49858f0484fSRodney W. Grimes 
49958f0484fSRodney W. Grimes /*
50058f0484fSRodney W. Grimes  * BT_BROOT -- Fix up the btree root page after it has been split.
50158f0484fSRodney W. Grimes  *
50258f0484fSRodney W. Grimes  * Parameters:
50358f0484fSRodney W. Grimes  *	t:	tree
50458f0484fSRodney W. Grimes  *	h:	root page
50558f0484fSRodney W. Grimes  *	l:	left page
50658f0484fSRodney W. Grimes  *	r:	right page
50758f0484fSRodney W. Grimes  *
50858f0484fSRodney W. Grimes  * Returns:
50958f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
51058f0484fSRodney W. Grimes  */
51158f0484fSRodney W. Grimes static int
bt_broot(BTREE * t,PAGE * h,PAGE * l,PAGE * r)5120ac22237SXin LI bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
51358f0484fSRodney W. Grimes {
51458f0484fSRodney W. Grimes 	BINTERNAL *bi;
51558f0484fSRodney W. Grimes 	BLEAF *bl;
516ef5d438eSPaul Traina 	u_int32_t nbytes;
51758f0484fSRodney W. Grimes 	char *dest;
51858f0484fSRodney W. Grimes 
51958f0484fSRodney W. Grimes 	/*
52058f0484fSRodney W. Grimes 	 * If the root page was a leaf page, change it into an internal page.
52158f0484fSRodney W. Grimes 	 * We copy the key we split on (but not the key's data, in the case of
52258f0484fSRodney W. Grimes 	 * a leaf page) to the new root page.
52358f0484fSRodney W. Grimes 	 *
52458f0484fSRodney W. Grimes 	 * The btree comparison code guarantees that the left-most key on any
52558f0484fSRodney W. Grimes 	 * level of the tree is never used, so it doesn't need to be filled in.
52658f0484fSRodney W. Grimes 	 */
52758f0484fSRodney W. Grimes 	nbytes = NBINTERNAL(0);
52858f0484fSRodney W. Grimes 	h->linp[0] = h->upper = t->bt_psize - nbytes;
52958f0484fSRodney W. Grimes 	dest = (char *)h + h->upper;
53058f0484fSRodney W. Grimes 	WR_BINTERNAL(dest, 0, l->pgno, 0);
53158f0484fSRodney W. Grimes 
53258f0484fSRodney W. Grimes 	switch (h->flags & P_TYPE) {
53358f0484fSRodney W. Grimes 	case P_BLEAF:
53458f0484fSRodney W. Grimes 		bl = GETBLEAF(r, 0);
53558f0484fSRodney W. Grimes 		nbytes = NBINTERNAL(bl->ksize);
5365f301949SBen Laurie 		__PAST_END(h->linp, 1) = h->upper -= nbytes;
53758f0484fSRodney W. Grimes 		dest = (char *)h + h->upper;
53858f0484fSRodney W. Grimes 		WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
53958f0484fSRodney W. Grimes 		memmove(dest, bl->bytes, bl->ksize);
54058f0484fSRodney W. Grimes 
54158f0484fSRodney W. Grimes 		/*
54258f0484fSRodney W. Grimes 		 * If the key is on an overflow page, mark the overflow chain
54358f0484fSRodney W. Grimes 		 * so it isn't deleted when the leaf copy of the key is deleted.
54458f0484fSRodney W. Grimes 		 */
5453579f3d9SPedro F. Giffuni 	if (bl->flags & P_BIGKEY) {
5463579f3d9SPedro F. Giffuni 			pgno_t pgno;
5473579f3d9SPedro F. Giffuni 			memcpy(&pgno, bl->bytes, sizeof(pgno));
5483579f3d9SPedro F. Giffuni 			if (bt_preserve(t, pgno) == RET_ERROR)
54958f0484fSRodney W. Grimes 				return (RET_ERROR);
5503579f3d9SPedro F. Giffuni 		}
55158f0484fSRodney W. Grimes 		break;
55258f0484fSRodney W. Grimes 	case P_BINTERNAL:
55358f0484fSRodney W. Grimes 		bi = GETBINTERNAL(r, 0);
55458f0484fSRodney W. Grimes 		nbytes = NBINTERNAL(bi->ksize);
5555f301949SBen Laurie 		__PAST_END(h->linp, 1) = h->upper -= nbytes;
55658f0484fSRodney W. Grimes 		dest = (char *)h + h->upper;
55758f0484fSRodney W. Grimes 		memmove(dest, bi, nbytes);
55858f0484fSRodney W. Grimes 		((BINTERNAL *)dest)->pgno = r->pgno;
55958f0484fSRodney W. Grimes 		break;
56058f0484fSRodney W. Grimes 	default:
56158f0484fSRodney W. Grimes 		abort();
56258f0484fSRodney W. Grimes 	}
56358f0484fSRodney W. Grimes 
56458f0484fSRodney W. Grimes 	/* There are two keys on the page. */
56558f0484fSRodney W. Grimes 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
56658f0484fSRodney W. Grimes 
56758f0484fSRodney W. Grimes 	/* Unpin the root page, set to btree internal page. */
56858f0484fSRodney W. Grimes 	h->flags &= ~P_TYPE;
56958f0484fSRodney W. Grimes 	h->flags |= P_BINTERNAL;
57058f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
57158f0484fSRodney W. Grimes 
57258f0484fSRodney W. Grimes 	return (RET_SUCCESS);
57358f0484fSRodney W. Grimes }
57458f0484fSRodney W. Grimes 
57558f0484fSRodney W. Grimes /*
57658f0484fSRodney W. Grimes  * BT_PSPLIT -- Do the real work of splitting the page.
57758f0484fSRodney W. Grimes  *
57858f0484fSRodney W. Grimes  * Parameters:
57958f0484fSRodney W. Grimes  *	t:	tree
58058f0484fSRodney W. Grimes  *	h:	page to be split
58158f0484fSRodney W. Grimes  *	l:	page to put lower half of data
58258f0484fSRodney W. Grimes  *	r:	page to put upper half of data
58358f0484fSRodney W. Grimes  *	pskip:	pointer to index to leave open
58458f0484fSRodney W. Grimes  *	ilen:	insert length
58558f0484fSRodney W. Grimes  *
58658f0484fSRodney W. Grimes  * Returns:
58758f0484fSRodney W. Grimes  *	Pointer to page in which to insert.
58858f0484fSRodney W. Grimes  */
58958f0484fSRodney W. Grimes static PAGE *
bt_psplit(BTREE * t,PAGE * h,PAGE * l,PAGE * r,indx_t * pskip,size_t ilen)5900ac22237SXin LI bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
59158f0484fSRodney W. Grimes {
59258f0484fSRodney W. Grimes 	BINTERNAL *bi;
59358f0484fSRodney W. Grimes 	BLEAF *bl;
594ef5d438eSPaul Traina 	CURSOR *c;
59558f0484fSRodney W. Grimes 	RLEAF *rl;
59658f0484fSRodney W. Grimes 	PAGE *rval;
59758f0484fSRodney W. Grimes 	void *src;
59858f0484fSRodney W. Grimes 	indx_t full, half, nxt, off, skip, top, used;
599ef5d438eSPaul Traina 	u_int32_t nbytes;
60058f0484fSRodney W. Grimes 	int bigkeycnt, isbigkey;
60158f0484fSRodney W. Grimes 
60258f0484fSRodney W. Grimes 	/*
60358f0484fSRodney W. Grimes 	 * Split the data to the left and right pages.  Leave the skip index
60458f0484fSRodney W. Grimes 	 * open.  Additionally, make some effort not to split on an overflow
60558f0484fSRodney W. Grimes 	 * key.  This makes internal page processing faster and can save
60658f0484fSRodney W. Grimes 	 * space as overflow keys used by internal pages are never deleted.
60758f0484fSRodney W. Grimes 	 */
60858f0484fSRodney W. Grimes 	bigkeycnt = 0;
60958f0484fSRodney W. Grimes 	skip = *pskip;
61058f0484fSRodney W. Grimes 	full = t->bt_psize - BTDATAOFF;
61158f0484fSRodney W. Grimes 	half = full / 2;
61258f0484fSRodney W. Grimes 	used = 0;
61358f0484fSRodney W. Grimes 	for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
61458f0484fSRodney W. Grimes 		if (skip == off) {
61558f0484fSRodney W. Grimes 			nbytes = ilen;
61658f0484fSRodney W. Grimes 			isbigkey = 0;		/* XXX: not really known. */
61758f0484fSRodney W. Grimes 		} else
61858f0484fSRodney W. Grimes 			switch (h->flags & P_TYPE) {
61958f0484fSRodney W. Grimes 			case P_BINTERNAL:
62058f0484fSRodney W. Grimes 				src = bi = GETBINTERNAL(h, nxt);
62158f0484fSRodney W. Grimes 				nbytes = NBINTERNAL(bi->ksize);
62258f0484fSRodney W. Grimes 				isbigkey = bi->flags & P_BIGKEY;
62358f0484fSRodney W. Grimes 				break;
62458f0484fSRodney W. Grimes 			case P_BLEAF:
62558f0484fSRodney W. Grimes 				src = bl = GETBLEAF(h, nxt);
62658f0484fSRodney W. Grimes 				nbytes = NBLEAF(bl);
62758f0484fSRodney W. Grimes 				isbigkey = bl->flags & P_BIGKEY;
62858f0484fSRodney W. Grimes 				break;
62958f0484fSRodney W. Grimes 			case P_RINTERNAL:
63058f0484fSRodney W. Grimes 				src = GETRINTERNAL(h, nxt);
63158f0484fSRodney W. Grimes 				nbytes = NRINTERNAL;
63258f0484fSRodney W. Grimes 				isbigkey = 0;
63358f0484fSRodney W. Grimes 				break;
63458f0484fSRodney W. Grimes 			case P_RLEAF:
63558f0484fSRodney W. Grimes 				src = rl = GETRLEAF(h, nxt);
63658f0484fSRodney W. Grimes 				nbytes = NRLEAF(rl);
63758f0484fSRodney W. Grimes 				isbigkey = 0;
63858f0484fSRodney W. Grimes 				break;
63958f0484fSRodney W. Grimes 			default:
64058f0484fSRodney W. Grimes 				abort();
64158f0484fSRodney W. Grimes 			}
64258f0484fSRodney W. Grimes 
64358f0484fSRodney W. Grimes 		/*
64458f0484fSRodney W. Grimes 		 * If the key/data pairs are substantial fractions of the max
64558f0484fSRodney W. Grimes 		 * possible size for the page, it's possible to get situations
64658f0484fSRodney W. Grimes 		 * where we decide to try and copy too much onto the left page.
64758f0484fSRodney W. Grimes 		 * Make sure that doesn't happen.
64858f0484fSRodney W. Grimes 		 */
649fc35a184SXin LI 		if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) ||
650fc35a184SXin LI 		    nxt == top - 1) {
65158f0484fSRodney W. Grimes 			--off;
65258f0484fSRodney W. Grimes 			break;
65358f0484fSRodney W. Grimes 		}
65458f0484fSRodney W. Grimes 
65558f0484fSRodney W. Grimes 		/* Copy the key/data pair, if not the skipped index. */
65658f0484fSRodney W. Grimes 		if (skip != off) {
65758f0484fSRodney W. Grimes 			++nxt;
65858f0484fSRodney W. Grimes 
65958f0484fSRodney W. Grimes 			l->linp[off] = l->upper -= nbytes;
66058f0484fSRodney W. Grimes 			memmove((char *)l + l->upper, src, nbytes);
66158f0484fSRodney W. Grimes 		}
66258f0484fSRodney W. Grimes 
6637d0cc08eSGuido van Rooij 		used += nbytes + sizeof(indx_t);
66458f0484fSRodney W. Grimes 		if (used >= half) {
66558f0484fSRodney W. Grimes 			if (!isbigkey || bigkeycnt == 3)
66658f0484fSRodney W. Grimes 				break;
66758f0484fSRodney W. Grimes 			else
66858f0484fSRodney W. Grimes 				++bigkeycnt;
66958f0484fSRodney W. Grimes 		}
67058f0484fSRodney W. Grimes 	}
67158f0484fSRodney W. Grimes 
67258f0484fSRodney W. Grimes 	/*
67358f0484fSRodney W. Grimes 	 * Off is the last offset that's valid for the left page.
67458f0484fSRodney W. Grimes 	 * Nxt is the first offset to be placed on the right page.
67558f0484fSRodney W. Grimes 	 */
67658f0484fSRodney W. Grimes 	l->lower += (off + 1) * sizeof(indx_t);
67758f0484fSRodney W. Grimes 
67858f0484fSRodney W. Grimes 	/*
67958f0484fSRodney W. Grimes 	 * If splitting the page that the cursor was on, the cursor has to be
68058f0484fSRodney W. Grimes 	 * adjusted to point to the same record as before the split.  If the
68158f0484fSRodney W. Grimes 	 * cursor is at or past the skipped slot, the cursor is incremented by
68258f0484fSRodney W. Grimes 	 * one.  If the cursor is on the right page, it is decremented by the
68358f0484fSRodney W. Grimes 	 * number of records split to the left page.
68458f0484fSRodney W. Grimes 	 */
685ef5d438eSPaul Traina 	c = &t->bt_cursor;
686ef5d438eSPaul Traina 	if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) {
687ef5d438eSPaul Traina 		if (c->pg.index >= skip)
688ef5d438eSPaul Traina 			++c->pg.index;
689ef5d438eSPaul Traina 		if (c->pg.index < nxt)			/* Left page. */
690ef5d438eSPaul Traina 			c->pg.pgno = l->pgno;
69158f0484fSRodney W. Grimes 		else {					/* Right page. */
692ef5d438eSPaul Traina 			c->pg.pgno = r->pgno;
693ef5d438eSPaul Traina 			c->pg.index -= nxt;
69458f0484fSRodney W. Grimes 		}
69558f0484fSRodney W. Grimes 	}
69658f0484fSRodney W. Grimes 
69758f0484fSRodney W. Grimes 	/*
69858f0484fSRodney W. Grimes 	 * If the skipped index was on the left page, just return that page.
69958f0484fSRodney W. Grimes 	 * Otherwise, adjust the skip index to reflect the new position on
70058f0484fSRodney W. Grimes 	 * the right page.
70158f0484fSRodney W. Grimes 	 */
70258f0484fSRodney W. Grimes 	if (skip <= off) {
7030c169133SJun Kuriyama 		skip = MAX_PAGE_OFFSET;
70458f0484fSRodney W. Grimes 		rval = l;
70558f0484fSRodney W. Grimes 	} else {
70658f0484fSRodney W. Grimes 		rval = r;
70758f0484fSRodney W. Grimes 		*pskip -= nxt;
70858f0484fSRodney W. Grimes 	}
70958f0484fSRodney W. Grimes 
71058f0484fSRodney W. Grimes 	for (off = 0; nxt < top; ++off) {
71158f0484fSRodney W. Grimes 		if (skip == nxt) {
71258f0484fSRodney W. Grimes 			++off;
7130c169133SJun Kuriyama 			skip = MAX_PAGE_OFFSET;
71458f0484fSRodney W. Grimes 		}
71558f0484fSRodney W. Grimes 		switch (h->flags & P_TYPE) {
71658f0484fSRodney W. Grimes 		case P_BINTERNAL:
71758f0484fSRodney W. Grimes 			src = bi = GETBINTERNAL(h, nxt);
71858f0484fSRodney W. Grimes 			nbytes = NBINTERNAL(bi->ksize);
71958f0484fSRodney W. Grimes 			break;
72058f0484fSRodney W. Grimes 		case P_BLEAF:
72158f0484fSRodney W. Grimes 			src = bl = GETBLEAF(h, nxt);
72258f0484fSRodney W. Grimes 			nbytes = NBLEAF(bl);
72358f0484fSRodney W. Grimes 			break;
72458f0484fSRodney W. Grimes 		case P_RINTERNAL:
72558f0484fSRodney W. Grimes 			src = GETRINTERNAL(h, nxt);
72658f0484fSRodney W. Grimes 			nbytes = NRINTERNAL;
72758f0484fSRodney W. Grimes 			break;
72858f0484fSRodney W. Grimes 		case P_RLEAF:
72958f0484fSRodney W. Grimes 			src = rl = GETRLEAF(h, nxt);
73058f0484fSRodney W. Grimes 			nbytes = NRLEAF(rl);
73158f0484fSRodney W. Grimes 			break;
73258f0484fSRodney W. Grimes 		default:
73358f0484fSRodney W. Grimes 			abort();
73458f0484fSRodney W. Grimes 		}
73558f0484fSRodney W. Grimes 		++nxt;
73658f0484fSRodney W. Grimes 		r->linp[off] = r->upper -= nbytes;
73758f0484fSRodney W. Grimes 		memmove((char *)r + r->upper, src, nbytes);
73858f0484fSRodney W. Grimes 	}
73958f0484fSRodney W. Grimes 	r->lower += off * sizeof(indx_t);
74058f0484fSRodney W. Grimes 
74158f0484fSRodney W. Grimes 	/* If the key is being appended to the page, adjust the index. */
74258f0484fSRodney W. Grimes 	if (skip == top)
74358f0484fSRodney W. Grimes 		r->lower += sizeof(indx_t);
74458f0484fSRodney W. Grimes 
74558f0484fSRodney W. Grimes 	return (rval);
74658f0484fSRodney W. Grimes }
74758f0484fSRodney W. Grimes 
74858f0484fSRodney W. Grimes /*
74958f0484fSRodney W. Grimes  * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
75058f0484fSRodney W. Grimes  *
75158f0484fSRodney W. Grimes  * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
75258f0484fSRodney W. Grimes  * record that references them gets deleted.  Chains pointed to by internal
75358f0484fSRodney W. Grimes  * pages never get deleted.  This routine marks a chain as pointed to by an
75458f0484fSRodney W. Grimes  * internal page.
75558f0484fSRodney W. Grimes  *
75658f0484fSRodney W. Grimes  * Parameters:
75758f0484fSRodney W. Grimes  *	t:	tree
75858f0484fSRodney W. Grimes  *	pg:	page number of first page in the chain.
75958f0484fSRodney W. Grimes  *
76058f0484fSRodney W. Grimes  * Returns:
76158f0484fSRodney W. Grimes  *	RET_SUCCESS, RET_ERROR.
76258f0484fSRodney W. Grimes  */
76358f0484fSRodney W. Grimes static int
bt_preserve(BTREE * t,pgno_t pg)7640ac22237SXin LI bt_preserve(BTREE *t, pgno_t pg)
76558f0484fSRodney W. Grimes {
76658f0484fSRodney W. Grimes 	PAGE *h;
76758f0484fSRodney W. Grimes 
76858f0484fSRodney W. Grimes 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
76958f0484fSRodney W. Grimes 		return (RET_ERROR);
77058f0484fSRodney W. Grimes 	h->flags |= P_PRESERVE;
77158f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
77258f0484fSRodney W. Grimes 	return (RET_SUCCESS);
77358f0484fSRodney W. Grimes }
77458f0484fSRodney W. Grimes 
77558f0484fSRodney W. Grimes /*
77658f0484fSRodney W. Grimes  * REC_TOTAL -- Return the number of recno entries below a page.
77758f0484fSRodney W. Grimes  *
77858f0484fSRodney W. Grimes  * Parameters:
77958f0484fSRodney W. Grimes  *	h:	page
78058f0484fSRodney W. Grimes  *
78158f0484fSRodney W. Grimes  * Returns:
78258f0484fSRodney W. Grimes  *	The number of recno entries below a page.
78358f0484fSRodney W. Grimes  *
78458f0484fSRodney W. Grimes  * XXX
78558f0484fSRodney W. Grimes  * These values could be set by the bt_psplit routine.  The problem is that the
78658f0484fSRodney W. Grimes  * entry has to be popped off of the stack etc. or the values have to be passed
78758f0484fSRodney W. Grimes  * all the way back to bt_split/bt_rroot and it's not very clean.
78858f0484fSRodney W. Grimes  */
78958f0484fSRodney W. Grimes static recno_t
rec_total(PAGE * h)7900ac22237SXin LI rec_total(PAGE *h)
79158f0484fSRodney W. Grimes {
79258f0484fSRodney W. Grimes 	recno_t recs;
79358f0484fSRodney W. Grimes 	indx_t nxt, top;
79458f0484fSRodney W. Grimes 
79558f0484fSRodney W. Grimes 	for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
79658f0484fSRodney W. Grimes 		recs += GETRINTERNAL(h, nxt)->nrecs;
79758f0484fSRodney W. Grimes 	return (recs);
79858f0484fSRodney W. Grimes }
799