Home | History | Annotate | Line # | Download | only in btree
bt_split.c revision 1.16
      1 /*	$NetBSD: bt_split.c,v 1.16 2008/09/10 17:52:35 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Mike Olson.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __RCSID("$NetBSD: bt_split.c,v 1.16 2008/09/10 17:52:35 joerg Exp $");
     37 
     38 #include "namespace.h"
     39 #include <sys/types.h>
     40 
     41 #include <assert.h>
     42 #include <limits.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include <db.h>
     48 #include "btree.h"
     49 
     50 static int	 bt_broot(BTREE *, PAGE *, PAGE *, PAGE *);
     51 static PAGE	*bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
     52 static int	 bt_preserve(BTREE *, pgno_t);
     53 static PAGE	*bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t);
     54 static PAGE	*bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t);
     55 static int	 bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *);
     56 static recno_t	 rec_total(PAGE *);
     57 
     58 #ifdef STATISTICS
     59 unsigned long	bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
     60 #endif
     61 
     62 /*
     63  * __BT_SPLIT -- Split the tree.
     64  *
     65  * Parameters:
     66  *	t:	tree
     67  *	sp:	page to split
     68  *	key:	key to insert
     69  *	data:	data to insert
     70  *	flags:	BIGKEY/BIGDATA flags
     71  *	ilen:	insert length
     72  *	skip:	index to leave open
     73  *
     74  * Returns:
     75  *	RET_ERROR, RET_SUCCESS
     76  */
     77 int
     78 __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags,
     79     size_t ilen, uint32_t argskip)
     80 {
     81 	BINTERNAL *bi = NULL;	/* pacify gcc */
     82 	BLEAF *bl = NULL, *tbl;	/* pacify gcc */
     83 	DBT a, b;
     84 	EPGNO *parent;
     85 	PAGE *h, *l, *r, *lchild, *rchild;
     86 	indx_t nxtindex;
     87 	uint16_t skip;
     88 	uint32_t n, nbytes, nksize = 0; /* pacify gcc */
     89 	int parentsplit;
     90 	char *dest;
     91 
     92 	/*
     93 	 * Split the page into two pages, l and r.  The split routines return
     94 	 * a pointer to the page into which the key should be inserted and with
     95 	 * skip set to the offset which should be used.  Additionally, l and r
     96 	 * are pinned.
     97 	 */
     98 	skip = argskip;
     99 	h = sp->pgno == P_ROOT ?
    100 	    bt_root(t, sp, &l, &r, &skip, ilen) :
    101 	    bt_page(t, sp, &l, &r, &skip, ilen);
    102 	if (h == NULL)
    103 		return (RET_ERROR);
    104 
    105 	/*
    106 	 * Insert the new key/data pair into the leaf page.  (Key inserts
    107 	 * always cause a leaf page to split first.)
    108 	 */
    109 	_DBFIT(ilen, indx_t);
    110 	h->upper -= (indx_t)ilen;
    111 	h->linp[skip] = h->upper;
    112 	dest = (char *)(void *)h + h->upper;
    113 	if (F_ISSET(t, R_RECNO))
    114 		WR_RLEAF(dest, data, flags);
    115 	else
    116 		WR_BLEAF(dest, key, data, flags);
    117 
    118 	/* If the root page was split, make it look right. */
    119 	if (sp->pgno == P_ROOT &&
    120 	    (F_ISSET(t, R_RECNO) ?
    121 	    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
    122 		goto err2;
    123 
    124 	/*
    125 	 * Now we walk the parent page stack -- a LIFO stack of the pages that
    126 	 * were traversed when we searched for the page that split.  Each stack
    127 	 * entry is a page number and a page index offset.  The offset is for
    128 	 * the page traversed on the search.  We've just split a page, so we
    129 	 * have to insert a new key into the parent page.
    130 	 *
    131 	 * If the insert into the parent page causes it to split, may have to
    132 	 * continue splitting all the way up the tree.  We stop if the root
    133 	 * splits or the page inserted into didn't have to split to hold the
    134 	 * new key.  Some algorithms replace the key for the old page as well
    135 	 * as the new page.  We don't, as there's no reason to believe that the
    136 	 * first key on the old page is any better than the key we have, and,
    137 	 * in the case of a key being placed at index 0 causing the split, the
    138 	 * key is unavailable.
    139 	 *
    140 	 * There are a maximum of 5 pages pinned at any time.  We keep the left
    141 	 * and right pages pinned while working on the parent.   The 5 are the
    142 	 * two children, left parent and right parent (when the parent splits)
    143 	 * and the root page or the overflow key page when calling bt_preserve.
    144 	 * This code must make sure that all pins are released other than the
    145 	 * root page or overflow page which is unlocked elsewhere.
    146 	 */
    147 	while ((parent = BT_POP(t)) != NULL) {
    148 		lchild = l;
    149 		rchild = r;
    150 
    151 		/* Get the parent page. */
    152 		if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
    153 			goto err2;
    154 
    155 	 	/*
    156 		 * The new key goes ONE AFTER the index, because the split
    157 		 * was to the right.
    158 		 */
    159 		skip = parent->index + 1;
    160 
    161 		/*
    162 		 * Calculate the space needed on the parent page.
    163 		 *
    164 		 * Prefix trees: space hack when inserting into BINTERNAL
    165 		 * pages.  Retain only what's needed to distinguish between
    166 		 * the new entry and the LAST entry on the page to its left.
    167 		 * If the keys compare equal, retain the entire key.  Note,
    168 		 * we don't touch overflow keys, and the entire key must be
    169 		 * retained for the next-to-left most key on the leftmost
    170 		 * page of each level, or the search will fail.  Applicable
    171 		 * ONLY to internal pages that have leaf pages as children.
    172 		 * Further reduction of the key between pairs of internal
    173 		 * pages loses too much information.
    174 		 */
    175 		switch (rchild->flags & P_TYPE) {
    176 		case P_BINTERNAL:
    177 			bi = GETBINTERNAL(rchild, 0);
    178 			nbytes = NBINTERNAL(bi->ksize);
    179 			break;
    180 		case P_BLEAF:
    181 			bl = GETBLEAF(rchild, 0);
    182 			nbytes = NBINTERNAL(bl->ksize);
    183 			if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
    184 			    (h->prevpg != P_INVALID || skip > 1)) {
    185 				size_t temp;
    186 				tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
    187 				a.size = tbl->ksize;
    188 				a.data = tbl->bytes;
    189 				b.size = bl->ksize;
    190 				b.data = bl->bytes;
    191 				temp = t->bt_pfx(&a, &b);
    192 				_DBFIT(temp, uint32_t);
    193 				nksize = (uint32_t)temp;
    194 				n = NBINTERNAL(nksize);
    195 				if (n < nbytes) {
    196 #ifdef STATISTICS
    197 					bt_pfxsaved += nbytes - n;
    198 #endif
    199 					nbytes = n;
    200 				} else
    201 					nksize = 0;
    202 			} else
    203 				nksize = 0;
    204 			break;
    205 		case P_RINTERNAL:
    206 		case P_RLEAF:
    207 			nbytes = NRINTERNAL;
    208 			break;
    209 		default:
    210 			abort();
    211 		}
    212 
    213 		/* Split the parent page if necessary or shift the indices. */
    214 		if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
    215 			sp = h;
    216 			h = h->pgno == P_ROOT ?
    217 			    bt_root(t, h, &l, &r, &skip, nbytes) :
    218 			    bt_page(t, h, &l, &r, &skip, nbytes);
    219 			if (h == NULL)
    220 				goto err1;
    221 			parentsplit = 1;
    222 		} else {
    223 			if (skip < (nxtindex = NEXTINDEX(h)))
    224 				memmove(h->linp + skip + 1, h->linp + skip,
    225 				    (nxtindex - skip) * sizeof(indx_t));
    226 			h->lower += sizeof(indx_t);
    227 			parentsplit = 0;
    228 		}
    229 
    230 		/* Insert the key into the parent page. */
    231 		switch (rchild->flags & P_TYPE) {
    232 		case P_BINTERNAL:
    233 			h->linp[skip] = h->upper -= nbytes;
    234 			dest = (char *)(void *)h + h->linp[skip];
    235 			memmove(dest, bi, nbytes);
    236 			((BINTERNAL *)(void *)dest)->pgno = rchild->pgno;
    237 			break;
    238 		case P_BLEAF:
    239 			h->linp[skip] = h->upper -= nbytes;
    240 			dest = (char *)(void *)h + h->linp[skip];
    241 			WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
    242 			    rchild->pgno, bl->flags & P_BIGKEY);
    243 			memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
    244 			if (bl->flags & P_BIGKEY &&
    245 			    bt_preserve(t, *(pgno_t *)(void *)bl->bytes) ==
    246 			    RET_ERROR)
    247 				goto err1;
    248 			break;
    249 		case P_RINTERNAL:
    250 			/*
    251 			 * Update the left page count.  If split
    252 			 * added at index 0, fix the correct page.
    253 			 */
    254 			if (skip > 0)
    255 				dest = (char *)(void *)h + h->linp[skip - 1];
    256 			else
    257 				dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
    258 			((RINTERNAL *)(void *)dest)->nrecs = rec_total(lchild);
    259 			((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
    260 
    261 			/* Update the right page count. */
    262 			h->linp[skip] = h->upper -= nbytes;
    263 			dest = (char *)(void *)h + h->linp[skip];
    264 			((RINTERNAL *)(void *)dest)->nrecs = rec_total(rchild);
    265 			((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
    266 			break;
    267 		case P_RLEAF:
    268 			/*
    269 			 * Update the left page count.  If split
    270 			 * added at index 0, fix the correct page.
    271 			 */
    272 			if (skip > 0)
    273 				dest = (char *)(void *)h + h->linp[skip - 1];
    274 			else
    275 				dest = (char *)(void *)l + l->linp[NEXTINDEX(l) - 1];
    276 			((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(lchild);
    277 			((RINTERNAL *)(void *)dest)->pgno = lchild->pgno;
    278 
    279 			/* Update the right page count. */
    280 			h->linp[skip] = h->upper -= nbytes;
    281 			dest = (char *)(void *)h + h->linp[skip];
    282 			((RINTERNAL *)(void *)dest)->nrecs = NEXTINDEX(rchild);
    283 			((RINTERNAL *)(void *)dest)->pgno = rchild->pgno;
    284 			break;
    285 		default:
    286 			abort();
    287 		}
    288 
    289 		/* Unpin the held pages. */
    290 		if (!parentsplit) {
    291 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    292 			break;
    293 		}
    294 
    295 		/* If the root page was split, make it look right. */
    296 		if (sp->pgno == P_ROOT &&
    297 		    (F_ISSET(t, R_RECNO) ?
    298 		    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
    299 			goto err1;
    300 
    301 		mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
    302 		mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
    303 	}
    304 
    305 	/* Unpin the held pages. */
    306 	mpool_put(t->bt_mp, l, MPOOL_DIRTY);
    307 	mpool_put(t->bt_mp, r, MPOOL_DIRTY);
    308 
    309 	/* Clear any pages left on the stack. */
    310 	return (RET_SUCCESS);
    311 
    312 	/*
    313 	 * If something fails in the above loop we were already walking back
    314 	 * up the tree and the tree is now inconsistent.  Nothing much we can
    315 	 * do about it but release any memory we're holding.
    316 	 */
    317 err1:	mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
    318 	mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
    319 
    320 err2:	mpool_put(t->bt_mp, l, 0);
    321 	mpool_put(t->bt_mp, r, 0);
    322 	__dbpanic(t->bt_dbp);
    323 	return (RET_ERROR);
    324 }
    325 
    326 /*
    327  * BT_PAGE -- Split a non-root page of a btree.
    328  *
    329  * Parameters:
    330  *	t:	tree
    331  *	h:	root page
    332  *	lp:	pointer to left page pointer
    333  *	rp:	pointer to right page pointer
    334  *	skip:	pointer to index to leave open
    335  *	ilen:	insert length
    336  *
    337  * Returns:
    338  *	Pointer to page in which to insert or NULL on error.
    339  */
    340 static PAGE *
    341 bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
    342 {
    343 	PAGE *l, *r, *tp;
    344 	pgno_t npg;
    345 
    346 #ifdef STATISTICS
    347 	++bt_split;
    348 #endif
    349 	/* Put the new right page for the split into place. */
    350 	if ((r = __bt_new(t, &npg)) == NULL)
    351 		return (NULL);
    352 	r->pgno = npg;
    353 	r->lower = BTDATAOFF;
    354 	r->upper = t->bt_psize;
    355 	r->nextpg = h->nextpg;
    356 	r->prevpg = h->pgno;
    357 	r->flags = h->flags & P_TYPE;
    358 
    359 	/*
    360 	 * If we're splitting the last page on a level because we're appending
    361 	 * a key to it (skip is NEXTINDEX()), it's likely that the data is
    362 	 * sorted.  Adding an empty page on the side of the level is less work
    363 	 * and can push the fill factor much higher than normal.  If we're
    364 	 * wrong it's no big deal, we'll just do the split the right way next
    365 	 * time.  It may look like it's equally easy to do a similar hack for
    366 	 * reverse sorted data, that is, split the tree left, but it's not.
    367 	 * Don't even try.
    368 	 */
    369 	if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
    370 #ifdef STATISTICS
    371 		++bt_sortsplit;
    372 #endif
    373 		h->nextpg = r->pgno;
    374 		r->lower = BTDATAOFF + sizeof(indx_t);
    375 		*skip = 0;
    376 		*lp = h;
    377 		*rp = r;
    378 		return (r);
    379 	}
    380 
    381 	/* Put the new left page for the split into place. */
    382 	if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) {
    383 		mpool_put(t->bt_mp, r, 0);
    384 		return (NULL);
    385 	}
    386 #ifdef PURIFY
    387 	memset(l, 0xff, t->bt_psize);
    388 #endif
    389 	l->pgno = h->pgno;
    390 	l->nextpg = r->pgno;
    391 	l->prevpg = h->prevpg;
    392 	l->lower = BTDATAOFF;
    393 	l->upper = t->bt_psize;
    394 	l->flags = h->flags & P_TYPE;
    395 
    396 	/* Fix up the previous pointer of the page after the split page. */
    397 	if (h->nextpg != P_INVALID) {
    398 		if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
    399 			free(l);
    400 			/* XXX mpool_free(t->bt_mp, r->pgno); */
    401 			return (NULL);
    402 		}
    403 		tp->prevpg = r->pgno;
    404 		mpool_put(t->bt_mp, tp, MPOOL_DIRTY);
    405 	}
    406 
    407 	/*
    408 	 * Split right.  The key/data pairs aren't sorted in the btree page so
    409 	 * it's simpler to copy the data from the split page onto two new pages
    410 	 * instead of copying half the data to the right page and compacting
    411 	 * the left page in place.  Since the left page can't change, we have
    412 	 * to swap the original and the allocated left page after the split.
    413 	 */
    414 	tp = bt_psplit(t, h, l, r, skip, ilen);
    415 
    416 	/* Move the new left page onto the old left page. */
    417 	memmove(h, l, t->bt_psize);
    418 	if (tp == l)
    419 		tp = h;
    420 	free(l);
    421 
    422 	*lp = h;
    423 	*rp = r;
    424 	return (tp);
    425 }
    426 
    427 /*
    428  * BT_ROOT -- Split the root page of a btree.
    429  *
    430  * Parameters:
    431  *	t:	tree
    432  *	h:	root page
    433  *	lp:	pointer to left page pointer
    434  *	rp:	pointer to right page pointer
    435  *	skip:	pointer to index to leave open
    436  *	ilen:	insert length
    437  *
    438  * Returns:
    439  *	Pointer to page in which to insert or NULL on error.
    440  */
    441 static PAGE *
    442 bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
    443 {
    444 	PAGE *l, *r, *tp;
    445 	pgno_t lnpg, rnpg;
    446 
    447 #ifdef STATISTICS
    448 	++bt_split;
    449 	++bt_rootsplit;
    450 #endif
    451 	/* Put the new left and right pages for the split into place. */
    452 	if ((l = __bt_new(t, &lnpg)) == NULL ||
    453 	    (r = __bt_new(t, &rnpg)) == NULL)
    454 		return (NULL);
    455 	l->pgno = lnpg;
    456 	r->pgno = rnpg;
    457 	l->nextpg = r->pgno;
    458 	r->prevpg = l->pgno;
    459 	l->prevpg = r->nextpg = P_INVALID;
    460 	l->lower = r->lower = BTDATAOFF;
    461 	l->upper = r->upper = t->bt_psize;
    462 	l->flags = r->flags = h->flags & P_TYPE;
    463 
    464 	/* Split the root page. */
    465 	tp = bt_psplit(t, h, l, r, skip, ilen);
    466 
    467 	*lp = l;
    468 	*rp = r;
    469 	return (tp);
    470 }
    471 
    472 /*
    473  * BT_RROOT -- Fix up the recno root page after it has been split.
    474  *
    475  * Parameters:
    476  *	t:	tree
    477  *	h:	root page
    478  *	l:	left page
    479  *	r:	right page
    480  *
    481  * Returns:
    482  *	RET_ERROR, RET_SUCCESS
    483  */
    484 static int
    485 bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
    486 {
    487 	char *dest;
    488 	uint32_t sz;
    489 	size_t temp;
    490 
    491 	temp = t->bt_psize - NRINTERNAL;
    492 	_DBFIT(temp, uint32_t);
    493 	sz = (uint32_t)temp;
    494 
    495 	/* Insert the left and right keys, set the header information. */
    496 	_DBFIT(sz, indx_t);
    497 	h->linp[0] = h->upper = (indx_t)sz;
    498 	dest = (char *)(void *)h + h->upper;
    499 	WR_RINTERNAL(dest,
    500 	    l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
    501 
    502 	h->linp[1] = h->upper -= NRINTERNAL;
    503 	dest = (char *)(void *)h + h->upper;
    504 	WR_RINTERNAL(dest,
    505 	    r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
    506 
    507 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
    508 
    509 	/* Unpin the root page, set to recno internal page. */
    510 	h->flags &= ~P_TYPE;
    511 	h->flags |= P_RINTERNAL;
    512 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    513 
    514 	return (RET_SUCCESS);
    515 }
    516 
    517 /*
    518  * BT_BROOT -- Fix up the btree root page after it has been split.
    519  *
    520  * Parameters:
    521  *	t:	tree
    522  *	h:	root page
    523  *	l:	left page
    524  *	r:	right page
    525  *
    526  * Returns:
    527  *	RET_ERROR, RET_SUCCESS
    528  */
    529 static int
    530 bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
    531 {
    532 	BINTERNAL *bi = NULL;	/* pacify gcc */
    533 	BLEAF *bl;
    534 	uint32_t nbytes;
    535 	char *dest;
    536 
    537 	/*
    538 	 * If the root page was a leaf page, change it into an internal page.
    539 	 * We copy the key we split on (but not the key's data, in the case of
    540 	 * a leaf page) to the new root page.
    541 	 *
    542 	 * The btree comparison code guarantees that the left-most key on any
    543 	 * level of the tree is never used, so it doesn't need to be filled in.
    544 	 */
    545 	nbytes = NBINTERNAL(0);
    546 	h->linp[0] = h->upper = t->bt_psize - nbytes;
    547 	dest = (char *)(void *)h + h->upper;
    548 	WR_BINTERNAL(dest, 0, l->pgno, 0);
    549 
    550 	switch (h->flags & P_TYPE) {
    551 	case P_BLEAF:
    552 		bl = GETBLEAF(r, 0);
    553 		nbytes = NBINTERNAL(bl->ksize);
    554 		h->linp[1] = h->upper -= nbytes;
    555 		dest = (char *)(void *)h + h->upper;
    556 		WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
    557 		memmove(dest, bl->bytes, bl->ksize);
    558 
    559 		/*
    560 		 * If the key is on an overflow page, mark the overflow chain
    561 		 * so it isn't deleted when the leaf copy of the key is deleted.
    562 		 */
    563 		if (bl->flags & P_BIGKEY &&
    564 		    bt_preserve(t, *(pgno_t *)(void *)bl->bytes) == RET_ERROR)
    565 			return (RET_ERROR);
    566 		break;
    567 	case P_BINTERNAL:
    568 		bi = GETBINTERNAL(r, 0);
    569 		nbytes = NBINTERNAL(bi->ksize);
    570 		h->linp[1] = h->upper -= nbytes;
    571 		dest = (char *)(void *)h + h->upper;
    572 		memmove(dest, bi, nbytes);
    573 		((BINTERNAL *)(void *)dest)->pgno = r->pgno;
    574 		break;
    575 	default:
    576 		abort();
    577 	}
    578 
    579 	/* There are two keys on the page. */
    580 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
    581 
    582 	/* Unpin the root page, set to btree internal page. */
    583 	h->flags &= ~P_TYPE;
    584 	h->flags |= P_BINTERNAL;
    585 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    586 
    587 	return (RET_SUCCESS);
    588 }
    589 
    590 /*
    591  * BT_PSPLIT -- Do the real work of splitting the page.
    592  *
    593  * Parameters:
    594  *	t:	tree
    595  *	h:	page to be split
    596  *	l:	page to put lower half of data
    597  *	r:	page to put upper half of data
    598  *	pskip:	pointer to index to leave open
    599  *	ilen:	insert length
    600  *
    601  * Returns:
    602  *	Pointer to page in which to insert.
    603  */
    604 static PAGE *
    605 bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
    606 {
    607 	BINTERNAL *bi;
    608 	BLEAF *bl;
    609 	CURSOR *c;
    610 	RLEAF *rl;
    611 	PAGE *rval;
    612 	void *src = NULL;	/* pacify gcc */
    613 	indx_t full, half, nxt, off, skip, top, used;
    614 	uint32_t nbytes;
    615 	size_t temp;
    616 	int bigkeycnt, isbigkey;
    617 
    618 	/*
    619 	 * Split the data to the left and right pages.  Leave the skip index
    620 	 * open.  Additionally, make some effort not to split on an overflow
    621 	 * key.  This makes internal page processing faster and can save
    622 	 * space as overflow keys used by internal pages are never deleted.
    623 	 */
    624 	bigkeycnt = 0;
    625 	skip = *pskip;
    626 	temp = t->bt_psize - BTDATAOFF;
    627 	_DBFIT(temp, indx_t);
    628 	full = (indx_t)temp;
    629 	half = full / 2;
    630 	used = 0;
    631 	for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
    632 		if (skip == off) {
    633 			_DBFIT(ilen, uint32_t);
    634 			nbytes = (uint32_t)ilen;
    635 			isbigkey = 0;		/* XXX: not really known. */
    636 		} else
    637 			switch (h->flags & P_TYPE) {
    638 			case P_BINTERNAL:
    639 				src = bi = GETBINTERNAL(h, nxt);
    640 				nbytes = NBINTERNAL(bi->ksize);
    641 				isbigkey = bi->flags & P_BIGKEY;
    642 				break;
    643 			case P_BLEAF:
    644 				src = bl = GETBLEAF(h, nxt);
    645 				nbytes = NBLEAF(bl);
    646 				isbigkey = bl->flags & P_BIGKEY;
    647 				break;
    648 			case P_RINTERNAL:
    649 				src = GETRINTERNAL(h, nxt);
    650 				nbytes = NRINTERNAL;
    651 				isbigkey = 0;
    652 				break;
    653 			case P_RLEAF:
    654 				src = rl = GETRLEAF(h, nxt);
    655 				nbytes = NRLEAF(rl);
    656 				isbigkey = 0;
    657 				break;
    658 			default:
    659 				abort();
    660 			}
    661 
    662 		/*
    663 		 * If the key/data pairs are substantial fractions of the max
    664 		 * possible size for the page, it's possible to get situations
    665 		 * where we decide to try and copy too much onto the left page.
    666 		 * Make sure that doesn't happen.
    667 		 */
    668 		if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) ||
    669 		    nxt == top - 1) {
    670 			--off;
    671 			break;
    672 		}
    673 
    674 		/* Copy the key/data pair, if not the skipped index. */
    675 		if (skip != off) {
    676 			++nxt;
    677 
    678 			l->linp[off] = l->upper -= nbytes;
    679 			memmove((char *)(void *)l + l->upper, src, nbytes);
    680 		}
    681 
    682 		temp = nbytes + sizeof(indx_t);
    683 		_DBFIT(temp, indx_t);
    684 		used += (indx_t)temp;
    685 		if (used >= half) {
    686 			if (!isbigkey || bigkeycnt == 3)
    687 				break;
    688 			else
    689 				++bigkeycnt;
    690 		}
    691 	}
    692 
    693 	/*
    694 	 * Off is the last offset that's valid for the left page.
    695 	 * Nxt is the first offset to be placed on the right page.
    696 	 */
    697 	temp = (off + 1) * sizeof(indx_t);
    698 	_DBFIT(temp, indx_t);
    699 	l->lower += (indx_t)temp;
    700 
    701 	/*
    702 	 * If splitting the page that the cursor was on, the cursor has to be
    703 	 * adjusted to point to the same record as before the split.  If the
    704 	 * cursor is at or past the skipped slot, the cursor is incremented by
    705 	 * one.  If the cursor is on the right page, it is decremented by the
    706 	 * number of records split to the left page.
    707 	 */
    708 	c = &t->bt_cursor;
    709 	if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) {
    710 		if (c->pg.index >= skip)
    711 			++c->pg.index;
    712 		if (c->pg.index < nxt)			/* Left page. */
    713 			c->pg.pgno = l->pgno;
    714 		else {					/* Right page. */
    715 			c->pg.pgno = r->pgno;
    716 			c->pg.index -= nxt;
    717 		}
    718 	}
    719 
    720 	/*
    721 	 * If the skipped index was on the left page, just return that page.
    722 	 * Otherwise, adjust the skip index to reflect the new position on
    723 	 * the right page.
    724 	 */
    725 	if (skip <= off) {
    726 		skip = MAX_PAGE_OFFSET;
    727 		rval = l;
    728 	} else {
    729 		rval = r;
    730 		*pskip -= nxt;
    731 	}
    732 
    733 	for (off = 0; nxt < top; ++off) {
    734 		if (skip == nxt) {
    735 			++off;
    736 			skip = MAX_PAGE_OFFSET;
    737 		}
    738 		switch (h->flags & P_TYPE) {
    739 		case P_BINTERNAL:
    740 			src = bi = GETBINTERNAL(h, nxt);
    741 			nbytes = NBINTERNAL(bi->ksize);
    742 			break;
    743 		case P_BLEAF:
    744 			src = bl = GETBLEAF(h, nxt);
    745 			nbytes = NBLEAF(bl);
    746 			break;
    747 		case P_RINTERNAL:
    748 			src = GETRINTERNAL(h, nxt);
    749 			nbytes = NRINTERNAL;
    750 			break;
    751 		case P_RLEAF:
    752 			src = rl = GETRLEAF(h, nxt);
    753 			nbytes = NRLEAF(rl);
    754 			break;
    755 		default:
    756 			abort();
    757 		}
    758 		++nxt;
    759 		r->linp[off] = r->upper -= nbytes;
    760 		memmove((char *)(void *)r + r->upper, src, nbytes);
    761 	}
    762 	temp = off * sizeof(indx_t);
    763 	_DBFIT(temp, indx_t);
    764 	r->lower += (indx_t)temp;
    765 
    766 	/* If the key is being appended to the page, adjust the index. */
    767 	if (skip == top)
    768 		r->lower += sizeof(indx_t);
    769 
    770 	return (rval);
    771 }
    772 
    773 /*
    774  * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
    775  *
    776  * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
    777  * record that references them gets deleted.  Chains pointed to by internal
    778  * pages never get deleted.  This routine marks a chain as pointed to by an
    779  * internal page.
    780  *
    781  * Parameters:
    782  *	t:	tree
    783  *	pg:	page number of first page in the chain.
    784  *
    785  * Returns:
    786  *	RET_SUCCESS, RET_ERROR.
    787  */
    788 static int
    789 bt_preserve(BTREE *t, pgno_t pg)
    790 {
    791 	PAGE *h;
    792 
    793 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
    794 		return (RET_ERROR);
    795 	h->flags |= P_PRESERVE;
    796 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    797 	return (RET_SUCCESS);
    798 }
    799 
    800 /*
    801  * REC_TOTAL -- Return the number of recno entries below a page.
    802  *
    803  * Parameters:
    804  *	h:	page
    805  *
    806  * Returns:
    807  *	The number of recno entries below a page.
    808  *
    809  * XXX
    810  * These values could be set by the bt_psplit routine.  The problem is that the
    811  * entry has to be popped off of the stack etc. or the values have to be passed
    812  * all the way back to bt_split/bt_rroot and it's not very clean.
    813  */
    814 static recno_t
    815 rec_total(PAGE *h)
    816 {
    817 	recno_t recs;
    818 	indx_t nxt, top;
    819 
    820 	for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
    821 		recs += GETRINTERNAL(h, nxt)->nrecs;
    822 	return (recs);
    823 }
    824