xref: /csrg-svn/lib/libc/db/btree/bt_split.c (revision 58078)
146134Smao /*-
246134Smao  * Copyright (c) 1990 The Regents of the University of California.
346134Smao  * All rights reserved.
446134Smao  *
546134Smao  * This code is derived from software contributed to Berkeley by
646134Smao  * Mike Olson.
746134Smao  *
846134Smao  * %sccs.include.redist.c%
946134Smao  */
1046134Smao 
1146134Smao #if defined(LIBC_SCCS) && !defined(lint)
12*58078Sbostic static char sccsid[] = "@(#)bt_split.c	5.15 (Berkeley) 02/19/93";
1346134Smao #endif /* LIBC_SCCS and not lint */
1446134Smao 
1546134Smao #include <sys/types.h>
1656741Sbostic 
1750989Sbostic #define	__DBINTERFACE_PRIVATE
1850989Sbostic #include <limits.h>
1950989Sbostic #include <stdio.h>
2046561Sbostic #include <stdlib.h>
2146561Sbostic #include <string.h>
2256741Sbostic 
2357932Sbostic #include <db.h>
2446134Smao #include "btree.h"
2546134Smao 
2657440Sbostic static int	 bt_broot __P((BTREE *, PAGE *, PAGE *, PAGE *));
2757451Sbostic static PAGE	*bt_page
28*58078Sbostic 		    __P((BTREE *, PAGE *, PAGE **, PAGE **, u_int *, size_t));
2950989Sbostic static int	 bt_preserve __P((BTREE *, pgno_t));
3057451Sbostic static PAGE	*bt_psplit
31*58078Sbostic 		    __P((BTREE *, PAGE *, PAGE *, PAGE *, u_int *, size_t));
3257451Sbostic static PAGE	*bt_root
33*58078Sbostic 		    __P((BTREE *, PAGE *, PAGE **, PAGE **, u_int *, size_t));
3450989Sbostic static int	 bt_rroot __P((BTREE *, PAGE *, PAGE *, PAGE *));
3550989Sbostic static recno_t	 rec_total __P((PAGE *));
3650989Sbostic 
3750989Sbostic #ifdef STATISTICS
3850989Sbostic u_long	bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
3950989Sbostic #endif
4050989Sbostic 
4146134Smao /*
4250989Sbostic  * __BT_SPLIT -- Split the tree.
4346134Smao  *
4450989Sbostic  * Parameters:
4550989Sbostic  *	t:	tree
4657440Sbostic  *	sp:	page to split
4750989Sbostic  *	key:	key to insert
4850989Sbostic  *	data:	data to insert
4950989Sbostic  *	flags:	BIGKEY/BIGDATA flags
5057451Sbostic  *	ilen:	insert length
5150989Sbostic  *	skip:	index to leave open
5246134Smao  *
5350989Sbostic  * Returns:
5450989Sbostic  *	RET_ERROR, RET_SUCCESS
5546134Smao  */
5646134Smao int
5757451Sbostic __bt_split(t, sp, key, data, flags, ilen, skip)
5850989Sbostic 	BTREE *t;
5957440Sbostic 	PAGE *sp;
6050989Sbostic 	const DBT *key, *data;
6150989Sbostic 	u_long flags;
6257451Sbostic 	size_t ilen;
6358067Sbostic 	u_int skip;
6446134Smao {
6550989Sbostic 	BINTERNAL *bi;
6657451Sbostic 	BLEAF *bl, *tbl;
6750989Sbostic 	DBT a, b;
6850989Sbostic 	EPGNO *parent;
6957440Sbostic 	PAGE *h, *l, *r, *lchild, *rchild;
7057989Sbostic 	indx_t nxtindex;
7157451Sbostic 	size_t n, nbytes, nksize;
7250989Sbostic 	int nosplit;
7350989Sbostic 	char *dest;
7446134Smao 
7546134Smao 	/*
7650989Sbostic 	 * Split the page into two pages, l and r.  The split routines return
7757440Sbostic 	 * a pointer to the page into which the key should be inserted and with
7857440Sbostic 	 * skip set to the offset which should be used.  Additionally, l and r
7957440Sbostic 	 * are pinned.
8046134Smao 	 */
8157440Sbostic 	h = sp->pgno == P_ROOT ?
8257451Sbostic 	    bt_root(t, sp, &l, &r, &skip, ilen) :
8357451Sbostic 	    bt_page(t, sp, &l, &r, &skip, ilen);
8450989Sbostic 	if (h == NULL)
8546134Smao 		return (RET_ERROR);
8646134Smao 
8750989Sbostic 	/*
8857440Sbostic 	 * Insert the new key/data pair into the leaf page.  (Key inserts
8957440Sbostic 	 * always cause a leaf page to split first.)
9050989Sbostic 	 */
9157451Sbostic 	h->linp[skip] = h->upper -= ilen;
9250989Sbostic 	dest = (char *)h + h->upper;
9351106Sbostic 	if (ISSET(t, BTF_RECNO))
9450989Sbostic 		WR_RLEAF(dest, data, flags)
9551106Sbostic 	else
9650989Sbostic 		WR_BLEAF(dest, key, data, flags)
9746134Smao 
9857440Sbostic 	/* If the root page was split, make it look right. */
9957440Sbostic 	if (sp->pgno == P_ROOT &&
10057440Sbostic 	    (ISSET(t, BTF_RECNO) ?
10157440Sbostic 	    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
10257440Sbostic 		goto err2;
10357440Sbostic 
10446134Smao 	/*
10550989Sbostic 	 * Now we walk the parent page stack -- a LIFO stack of the pages that
10650989Sbostic 	 * were traversed when we searched for the page that split.  Each stack
10750989Sbostic 	 * entry is a page number and a page index offset.  The offset is for
10850989Sbostic 	 * the page traversed on the search.  We've just split a page, so we
10950989Sbostic 	 * have to insert a new key into the parent page.
11050989Sbostic 	 *
11150989Sbostic 	 * If the insert into the parent page causes it to split, may have to
11250989Sbostic 	 * continue splitting all the way up the tree.  We stop if the root
11350989Sbostic 	 * splits or the page inserted into didn't have to split to hold the
11450989Sbostic 	 * new key.  Some algorithms replace the key for the old page as well
11550989Sbostic 	 * as the new page.  We don't, as there's no reason to believe that the
11650989Sbostic 	 * first key on the old page is any better than the key we have, and,
11750989Sbostic 	 * in the case of a key being placed at index 0 causing the split, the
11850989Sbostic 	 * key is unavailable.
11950989Sbostic 	 *
12050989Sbostic 	 * There are a maximum of 5 pages pinned at any time.  We keep the left
12150989Sbostic 	 * and right pages pinned while working on the parent.   The 5 are the
12250989Sbostic 	 * two children, left parent and right parent (when the parent splits)
12350989Sbostic 	 * and the root page or the overflow key page when calling bt_preserve.
12450989Sbostic 	 * This code must make sure that all pins are released other than the
12550989Sbostic 	 * root page or overflow page which is unlocked elsewhere.
12646134Smao 	 */
12750989Sbostic 	for (nosplit = 0; (parent = BT_POP(t)) != NULL;) {
12850989Sbostic 		lchild = l;
12950989Sbostic 		rchild = r;
13046134Smao 
13150989Sbostic 		/* Get the parent page. */
13250989Sbostic 		if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
13350989Sbostic 			goto err2;
13446134Smao 
13557451Sbostic 	 	/*
13657451Sbostic 		 * The new key goes ONE AFTER the index, because the split
13757451Sbostic 		 * was to the right.
13857451Sbostic 		 */
13950989Sbostic 		skip = parent->index + 1;
14046134Smao 
14150989Sbostic 		/*
14250989Sbostic 		 * Calculate the space needed on the parent page.
14350989Sbostic 		 *
14457451Sbostic 		 * Prefix trees: space hack when inserting into BINTERNAL
14557451Sbostic 		 * pages.  Retain only what's needed to distinguish between
14657451Sbostic 		 * the new entry and the LAST entry on the page to its left.
14757451Sbostic 		 * If the keys compare equal, retain the entire key.  Note,
14857451Sbostic 		 * we don't touch overflow keys, and the entire key must be
14957451Sbostic 		 * retained for the next-to-left most key on the leftmost
15057451Sbostic 		 * page of each level, or the search will fail.  Applicable
15157451Sbostic 		 * ONLY to internal pages that have leaf pages as children.
15257451Sbostic 		 * Further reduction of the key between pairs of internal
15357451Sbostic 		 * pages loses too much information.
15450989Sbostic 		 */
15550989Sbostic 		switch (rchild->flags & P_TYPE) {
15650989Sbostic 		case P_BINTERNAL:
15750989Sbostic 			bi = GETBINTERNAL(rchild, 0);
15850989Sbostic 			nbytes = NBINTERNAL(bi->ksize);
15950989Sbostic 			break;
16050989Sbostic 		case P_BLEAF:
16150989Sbostic 			bl = GETBLEAF(rchild, 0);
16256401Sbostic 			nbytes = NBINTERNAL(bl->ksize);
16357451Sbostic 			if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
16457451Sbostic 			    (h->prevpg != P_INVALID || skip > 1)) {
16550989Sbostic 				tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
16650989Sbostic 				a.size = tbl->ksize;
16750989Sbostic 				a.data = tbl->bytes;
16850989Sbostic 				b.size = bl->ksize;
16950989Sbostic 				b.data = bl->bytes;
17057451Sbostic 				nksize = t->bt_pfx(&a, &b);
17150989Sbostic 				n = NBINTERNAL(nksize);
17250989Sbostic 				if (n < nbytes) {
17350989Sbostic #ifdef STATISTICS
17450989Sbostic 					bt_pfxsaved += nbytes - n;
17550989Sbostic #endif
17650989Sbostic 					nbytes = n;
17750989Sbostic 				} else
17850989Sbostic 					nksize = 0;
17950989Sbostic 			} else
18050989Sbostic 				nksize = 0;
18150989Sbostic 			break;
18250989Sbostic 		case P_RINTERNAL:
18350989Sbostic 		case P_RLEAF:
18450989Sbostic 			nbytes = NRINTERNAL;
18550989Sbostic 			break;
18656741Sbostic 		default:
18756741Sbostic 			abort();
18850989Sbostic 		}
18950989Sbostic 
19050989Sbostic 		/* Split the parent page if necessary or shift the indices. */
19157989Sbostic 		if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
19257440Sbostic 			sp = h;
19350989Sbostic 			h = h->pgno == P_ROOT ?
19457451Sbostic 			    bt_root(t, h, &l, &r, &skip, nbytes) :
19557451Sbostic 			    bt_page(t, h, &l, &r, &skip, nbytes);
19650989Sbostic 			if (h == NULL)
19750989Sbostic 				goto err1;
19846134Smao 		} else {
19950989Sbostic 			if (skip < (nxtindex = NEXTINDEX(h)))
20058017Sbostic 				memmove(h->linp + skip + 1, h->linp + skip,
20157989Sbostic 				    (nxtindex - skip) * sizeof(indx_t));
20257989Sbostic 			h->lower += sizeof(indx_t);
20350989Sbostic 			nosplit = 1;
20446134Smao 		}
20546134Smao 
20650989Sbostic 		/* Insert the key into the parent page. */
20750989Sbostic 		switch(rchild->flags & P_TYPE) {
20850989Sbostic 		case P_BINTERNAL:
20950989Sbostic 			h->linp[skip] = h->upper -= nbytes;
21050989Sbostic 			dest = (char *)h + h->linp[skip];
21158017Sbostic 			memmove(dest, bi, nbytes);
21250989Sbostic 			((BINTERNAL *)dest)->pgno = rchild->pgno;
21350989Sbostic 			break;
21450989Sbostic 		case P_BLEAF:
21550989Sbostic 			h->linp[skip] = h->upper -= nbytes;
21650989Sbostic 			dest = (char *)h + h->linp[skip];
21750989Sbostic 			WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
21851106Sbostic 			    rchild->pgno, bl->flags & P_BIGKEY);
21958017Sbostic 			memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
22050989Sbostic 			if (bl->flags & P_BIGKEY &&
22150989Sbostic 			    bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
22250989Sbostic 				goto err1;
22350989Sbostic 			break;
22450989Sbostic 		case P_RINTERNAL:
22550989Sbostic 			/* Update both left and right page counts. */
22650989Sbostic 			h->linp[skip] = h->upper -= nbytes;
22750989Sbostic 			dest = (char *)h + h->linp[skip];
22850989Sbostic 			((RINTERNAL *)dest)->nrecs = rec_total(rchild);
22950989Sbostic 			((RINTERNAL *)dest)->pgno = rchild->pgno;
23050989Sbostic 			dest = (char *)h + h->linp[skip - 1];
23150989Sbostic 			((RINTERNAL *)dest)->nrecs = rec_total(lchild);
23250989Sbostic 			((RINTERNAL *)dest)->pgno = lchild->pgno;
23350989Sbostic 			break;
23450989Sbostic 		case P_RLEAF:
23550989Sbostic 			/* Update both left and right page counts. */
23650989Sbostic 			h->linp[skip] = h->upper -= nbytes;
23750989Sbostic 			dest = (char *)h + h->linp[skip];
23850989Sbostic 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild);
23950989Sbostic 			((RINTERNAL *)dest)->pgno = rchild->pgno;
24050989Sbostic 			dest = (char *)h + h->linp[skip - 1];
24150989Sbostic 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild);
24250989Sbostic 			((RINTERNAL *)dest)->pgno = lchild->pgno;
24350989Sbostic 			break;
24456741Sbostic 		default:
24556741Sbostic 			abort();
24650989Sbostic 		}
24746134Smao 
24850989Sbostic 		/* Unpin the held pages. */
24950989Sbostic 		if (nosplit) {
25050989Sbostic 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
25150989Sbostic 			break;
25250989Sbostic 		}
25357440Sbostic 
25457440Sbostic 		/* If the root page was split, make it look right. */
25557440Sbostic 		if (sp->pgno == P_ROOT &&
25657440Sbostic 		    (ISSET(t, BTF_RECNO) ?
25757440Sbostic 		    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
25857440Sbostic 			goto err1;
25957440Sbostic 
26050989Sbostic 		mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
26150989Sbostic 		mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
26250989Sbostic 	}
26346134Smao 
26450989Sbostic 	/* Unpin the held pages. */
26550989Sbostic 	mpool_put(t->bt_mp, l, MPOOL_DIRTY);
26650989Sbostic 	mpool_put(t->bt_mp, r, MPOOL_DIRTY);
26750989Sbostic 
26851106Sbostic 	/* Clear any pages left on the stack. */
26950989Sbostic 	return (RET_SUCCESS);
27046134Smao 
27146134Smao 	/*
27250989Sbostic 	 * If something fails in the above loop we were already walking back
27350989Sbostic 	 * up the tree and the tree is now inconsistent.  Nothing much we can
27450989Sbostic 	 * do about it but release any memory we're holding.
27546134Smao 	 */
27650989Sbostic err1:	mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
27750989Sbostic 	mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
27846134Smao 
27950989Sbostic err2:	mpool_put(t->bt_mp, l, 0);
28050989Sbostic 	mpool_put(t->bt_mp, r, 0);
28150989Sbostic 	__dbpanic(t->bt_dbp);
28250989Sbostic 	return (RET_ERROR);
28346134Smao }
28446134Smao 
28546134Smao /*
28650989Sbostic  * BT_PAGE -- Split a non-root page of a btree.
28746134Smao  *
28850989Sbostic  * Parameters:
28950989Sbostic  *	t:	tree
29050989Sbostic  *	h:	root page
29150989Sbostic  *	lp:	pointer to left page pointer
29250989Sbostic  *	rp:	pointer to right page pointer
29350989Sbostic  *	skip:	pointer to index to leave open
29457451Sbostic  *	ilen:	insert length
29546134Smao  *
29650989Sbostic  * Returns:
29750989Sbostic  *	Pointer to page in which to insert or NULL on error.
29846134Smao  */
29950989Sbostic static PAGE *
30057451Sbostic bt_page(t, h, lp, rp, skip, ilen)
30150989Sbostic 	BTREE *t;
30250989Sbostic 	PAGE *h, **lp, **rp;
303*58078Sbostic 	u_int *skip;
30457451Sbostic 	size_t ilen;
30546134Smao {
30650989Sbostic 	PAGE *l, *r, *tp;
30750989Sbostic 	pgno_t npg;
30846134Smao 
30950989Sbostic #ifdef STATISTICS
31050989Sbostic 	++bt_split;
31150989Sbostic #endif
31250989Sbostic 	/* Put the new right page for the split into place. */
31356492Sbostic 	if ((r = __bt_new(t, &npg)) == NULL)
31450989Sbostic 		return (NULL);
31550989Sbostic 	r->pgno = npg;
31650989Sbostic 	r->lower = BTDATAOFF;
31750989Sbostic 	r->upper = t->bt_psize;
31850989Sbostic 	r->nextpg = h->nextpg;
31950989Sbostic 	r->prevpg = h->pgno;
32050989Sbostic 	r->flags = h->flags & P_TYPE;
32146134Smao 
32250989Sbostic 	/*
32350989Sbostic 	 * If we're splitting the last page on a level because we're appending
32450989Sbostic 	 * a key to it (skip is NEXTINDEX()), it's likely that the data is
32550989Sbostic 	 * sorted.  Adding an empty page on the side of the level is less work
32650989Sbostic 	 * and can push the fill factor much higher than normal.  If we're
32750989Sbostic 	 * wrong it's no big deal, we'll just do the split the right way next
32850989Sbostic 	 * time.  It may look like it's equally easy to do a similar hack for
32950989Sbostic 	 * reverse sorted data, that is, split the tree left, but it's not.
33050989Sbostic 	 * Don't even try.
33150989Sbostic 	 */
33250989Sbostic 	if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
33350989Sbostic #ifdef STATISTICS
33450989Sbostic 		++bt_sortsplit;
33550989Sbostic #endif
33650989Sbostic 		h->nextpg = r->pgno;
33757989Sbostic 		r->lower = BTDATAOFF + sizeof(indx_t);
33850989Sbostic 		*skip = 0;
33950989Sbostic 		*lp = h;
34050989Sbostic 		*rp = r;
34150989Sbostic 		return (r);
34250989Sbostic 	}
34346134Smao 
34450989Sbostic 	/* Put the new left page for the split into place. */
34550989Sbostic 	if ((l = malloc(t->bt_psize)) == NULL) {
34650989Sbostic 		mpool_put(t->bt_mp, r, 0);
34750989Sbostic 		return (NULL);
34850989Sbostic 	}
34950989Sbostic 	l->pgno = h->pgno;
35050989Sbostic 	l->nextpg = r->pgno;
35150989Sbostic 	l->prevpg = h->prevpg;
35250989Sbostic 	l->lower = BTDATAOFF;
35350989Sbostic 	l->upper = t->bt_psize;
35450989Sbostic 	l->flags = h->flags & P_TYPE;
35546134Smao 
35650989Sbostic 	/* Fix up the previous pointer of the page after the split page. */
35750989Sbostic 	if (h->nextpg != P_INVALID) {
35850989Sbostic 		if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
35950989Sbostic 			free(l);
36050989Sbostic 			/* XXX mpool_free(t->bt_mp, r->pgno); */
36150989Sbostic 			return (NULL);
36246134Smao 		}
36350989Sbostic 		tp->prevpg = r->pgno;
36450989Sbostic 		mpool_put(t->bt_mp, tp, 0);
36550989Sbostic 	}
36646134Smao 
36750989Sbostic 	/*
36850989Sbostic 	 * Split right.  The key/data pairs aren't sorted in the btree page so
36950989Sbostic 	 * it's simpler to copy the data from the split page onto two new pages
37050989Sbostic 	 * instead of copying half the data to the right page and compacting
37150989Sbostic 	 * the left page in place.  Since the left page can't change, we have
37250989Sbostic 	 * to swap the original and the allocated left page after the split.
37350989Sbostic 	 */
37457451Sbostic 	tp = bt_psplit(t, h, l, r, skip, ilen);
37546134Smao 
37650989Sbostic 	/* Move the new left page onto the old left page. */
37758017Sbostic 	memmove(h, l, t->bt_psize);
37850989Sbostic 	if (tp == l)
37950989Sbostic 		tp = h;
38050989Sbostic 	free(l);
38150989Sbostic 
38250989Sbostic 	*lp = h;
38350989Sbostic 	*rp = r;
38450989Sbostic 	return (tp);
38546134Smao }
38646134Smao 
38746134Smao /*
38851106Sbostic  * BT_ROOT -- Split the root page of a btree.
38946134Smao  *
39050989Sbostic  * Parameters:
39150989Sbostic  *	t:	tree
39250989Sbostic  *	h:	root page
39350989Sbostic  *	lp:	pointer to left page pointer
39450989Sbostic  *	rp:	pointer to right page pointer
39550989Sbostic  *	skip:	pointer to index to leave open
39657451Sbostic  *	ilen:	insert length
39746134Smao  *
39850989Sbostic  * Returns:
39950989Sbostic  *	Pointer to page in which to insert or NULL on error.
40046134Smao  */
40150989Sbostic static PAGE *
40257451Sbostic bt_root(t, h, lp, rp, skip, ilen)
40350989Sbostic 	BTREE *t;
40450989Sbostic 	PAGE *h, **lp, **rp;
405*58078Sbostic 	u_int *skip;
40657451Sbostic 	size_t ilen;
40746134Smao {
40850989Sbostic 	PAGE *l, *r, *tp;
40950989Sbostic 	pgno_t lnpg, rnpg;
41046134Smao 
41150989Sbostic #ifdef STATISTICS
41250989Sbostic 	++bt_split;
41350989Sbostic 	++bt_rootsplit;
41450989Sbostic #endif
41550989Sbostic 	/* Put the new left and right pages for the split into place. */
41656492Sbostic 	if ((l = __bt_new(t, &lnpg)) == NULL ||
41756492Sbostic 	    (r = __bt_new(t, &rnpg)) == NULL)
41850989Sbostic 		return (NULL);
41950989Sbostic 	l->pgno = lnpg;
42050989Sbostic 	r->pgno = rnpg;
42150989Sbostic 	l->nextpg = r->pgno;
42250989Sbostic 	r->prevpg = l->pgno;
42350989Sbostic 	l->prevpg = r->nextpg = P_INVALID;
42450989Sbostic 	l->lower = r->lower = BTDATAOFF;
42550989Sbostic 	l->upper = r->upper = t->bt_psize;
42650989Sbostic 	l->flags = r->flags = h->flags & P_TYPE;
42750989Sbostic 
42850989Sbostic 	/* Split the root page. */
42957451Sbostic 	tp = bt_psplit(t, h, l, r, skip, ilen);
43050989Sbostic 
43150989Sbostic 	*lp = l;
43250989Sbostic 	*rp = r;
43350989Sbostic 	return (tp);
43446134Smao }
43546134Smao 
43646134Smao /*
43757440Sbostic  * BT_RROOT -- Fix up the recno root page after it has been split.
43846134Smao  *
43950989Sbostic  * Parameters:
44050989Sbostic  *	t:	tree
44150989Sbostic  *	h:	root page
44257440Sbostic  *	l:	left page
44357440Sbostic  *	r:	right page
44446134Smao  *
44550989Sbostic  * Returns:
44650989Sbostic  *	RET_ERROR, RET_SUCCESS
44746134Smao  */
44850989Sbostic static int
44950989Sbostic bt_rroot(t, h, l, r)
45050989Sbostic 	BTREE *t;
45150989Sbostic 	PAGE *h, *l, *r;
45246134Smao {
45350989Sbostic 	char *dest;
45446134Smao 
45550989Sbostic 	/* Insert the left and right keys, set the header information. */
45650989Sbostic 	h->linp[0] = h->upper = t->bt_psize - NRINTERNAL;
45750989Sbostic 	dest = (char *)h + h->upper;
45850989Sbostic 	WR_RINTERNAL(dest,
45950989Sbostic 	    l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
46046134Smao 
46150989Sbostic 	h->linp[1] = h->upper -= NRINTERNAL;
46250989Sbostic 	dest = (char *)h + h->upper;
46350989Sbostic 	WR_RINTERNAL(dest,
46450989Sbostic 	    r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
46546134Smao 
46657989Sbostic 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
46746134Smao 
46850989Sbostic 	/* Unpin the root page, set to recno internal page. */
46950989Sbostic 	h->flags &= ~P_TYPE;
47050989Sbostic 	h->flags |= P_RINTERNAL;
47150989Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
47246134Smao 
47350989Sbostic 	return (RET_SUCCESS);
47450989Sbostic }
47546134Smao 
47650989Sbostic /*
47757440Sbostic  * BT_BROOT -- Fix up the btree root page after it has been split.
47850989Sbostic  *
47950989Sbostic  * Parameters:
48050989Sbostic  *	t:	tree
48150989Sbostic  *	h:	root page
48257440Sbostic  *	l:	left page
48357440Sbostic  *	r:	right page
48450989Sbostic  *
48550989Sbostic  * Returns:
48650989Sbostic  *	RET_ERROR, RET_SUCCESS
48750989Sbostic  */
48850989Sbostic static int
48950989Sbostic bt_broot(t, h, l, r)
49050989Sbostic 	BTREE *t;
49150989Sbostic 	PAGE *h, *l, *r;
49250989Sbostic {
49350989Sbostic 	BINTERNAL *bi;
49450989Sbostic 	BLEAF *bl;
49550989Sbostic 	size_t nbytes;
49650989Sbostic 	char *dest;
49746134Smao 
49846134Smao 	/*
49950989Sbostic 	 * If the root page was a leaf page, change it into an internal page.
50050989Sbostic 	 * We copy the key we split on (but not the key's data, in the case of
50156401Sbostic 	 * a leaf page) to the new root page.
50250989Sbostic 	 *
50350989Sbostic 	 * The btree comparison code guarantees that the left-most key on any
50457440Sbostic 	 * level of the tree is never used, so it doesn't need to be filled in.
50546134Smao 	 */
50656401Sbostic 	nbytes = NBINTERNAL(0);
50750989Sbostic 	h->linp[0] = h->upper = t->bt_psize - nbytes;
50850989Sbostic 	dest = (char *)h + h->upper;
50950989Sbostic 	WR_BINTERNAL(dest, 0, l->pgno, 0);
51046134Smao 
51150989Sbostic 	switch(h->flags & P_TYPE) {
51250989Sbostic 	case P_BLEAF:
51350989Sbostic 		bl = GETBLEAF(r, 0);
51450989Sbostic 		nbytes = NBINTERNAL(bl->ksize);
51550989Sbostic 		h->linp[1] = h->upper -= nbytes;
51650989Sbostic 		dest = (char *)h + h->upper;
51750989Sbostic 		WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
51858017Sbostic 		memmove(dest, bl->bytes, bl->ksize);
51950989Sbostic 
52056401Sbostic 		/*
52156401Sbostic 		 * If the key is on an overflow page, mark the overflow chain
52256401Sbostic 		 * so it isn't deleted when the leaf copy of the key is deleted.
52356401Sbostic 		 */
52450989Sbostic 		if (bl->flags & P_BIGKEY &&
52550989Sbostic 		    bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
52650989Sbostic 			return (RET_ERROR);
52750989Sbostic 		break;
52850989Sbostic 	case P_BINTERNAL:
52950989Sbostic 		bi = GETBINTERNAL(r, 0);
53050989Sbostic 		nbytes = NBINTERNAL(bi->ksize);
53150989Sbostic 		h->linp[1] = h->upper -= nbytes;
53250989Sbostic 		dest = (char *)h + h->upper;
53358017Sbostic 		memmove(dest, bi, nbytes);
53450989Sbostic 		((BINTERNAL *)dest)->pgno = r->pgno;
53550989Sbostic 		break;
53656741Sbostic 	default:
53756741Sbostic 		abort();
53846134Smao 	}
53957440Sbostic 
54057440Sbostic 	/* There are two keys on the page. */
54157989Sbostic 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
54246134Smao 
54350989Sbostic 	/* Unpin the root page, set to btree internal page. */
54450989Sbostic 	h->flags &= ~P_TYPE;
54550989Sbostic 	h->flags |= P_BINTERNAL;
54650989Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
54746134Smao 
54846134Smao 	return (RET_SUCCESS);
54946134Smao }
55046134Smao 
55146134Smao /*
55250989Sbostic  * BT_PSPLIT -- Do the real work of splitting the page.
55346134Smao  *
55450989Sbostic  * Parameters:
55550989Sbostic  *	t:	tree
55650989Sbostic  *	h:	page to be split
55750989Sbostic  *	l:	page to put lower half of data
55850989Sbostic  *	r:	page to put upper half of data
55957440Sbostic  *	pskip:	pointer to index to leave open
56057451Sbostic  *	ilen:	insert length
56146134Smao  *
56250989Sbostic  * Returns:
56350989Sbostic  *	Pointer to page in which to insert.
56446134Smao  */
56550989Sbostic static PAGE *
56657451Sbostic bt_psplit(t, h, l, r, pskip, ilen)
56750989Sbostic 	BTREE *t;
56850989Sbostic 	PAGE *h, *l, *r;
569*58078Sbostic 	u_int *pskip;
57057451Sbostic 	size_t ilen;
57146134Smao {
57250989Sbostic 	BINTERNAL *bi;
57350989Sbostic 	BLEAF *bl;
57450989Sbostic 	RLEAF *rl;
57550989Sbostic 	EPGNO *c;
57650989Sbostic 	PAGE *rval;
57758067Sbostic 	void *src;
57858067Sbostic 	indx_t full, half, nxt, off, skip, top, used;
57950989Sbostic 	size_t nbytes;
58058067Sbostic 	int bigkeycnt, isbigkey;
58146134Smao 
58246134Smao 	/*
58357451Sbostic 	 * Split the data to the left and right pages.  Leave the skip index
58457451Sbostic 	 * open.  Additionally, make some effort not to split on an overflow
58557451Sbostic 	 * key.  This makes internal page processing faster and can save
58657451Sbostic 	 * space as overflow keys used by internal pages are never deleted.
58746134Smao 	 */
58850989Sbostic 	bigkeycnt = 0;
58951106Sbostic 	skip = *pskip;
59057451Sbostic 	full = t->bt_psize - BTDATAOFF;
59157451Sbostic 	half = full / 2;
59257451Sbostic 	used = 0;
59350989Sbostic 	for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
59457451Sbostic 		if (skip == off) {
59557451Sbostic 			nbytes = ilen;
59657451Sbostic 			isbigkey = 0;		/* XXX: not really known. */
59757451Sbostic 		} else
59857451Sbostic 			switch (h->flags & P_TYPE) {
59957451Sbostic 			case P_BINTERNAL:
60057451Sbostic 				src = bi = GETBINTERNAL(h, nxt);
60157451Sbostic 				nbytes = NBINTERNAL(bi->ksize);
60257451Sbostic 				isbigkey = bi->flags & P_BIGKEY;
60357451Sbostic 				break;
60457451Sbostic 			case P_BLEAF:
60557451Sbostic 				src = bl = GETBLEAF(h, nxt);
60657451Sbostic 				nbytes = NBLEAF(bl);
60757451Sbostic 				isbigkey = bl->flags & P_BIGKEY;
60857451Sbostic 				break;
60957451Sbostic 			case P_RINTERNAL:
61057451Sbostic 				src = GETRINTERNAL(h, nxt);
61157451Sbostic 				nbytes = NRINTERNAL;
61257451Sbostic 				isbigkey = 0;
61357451Sbostic 				break;
61457451Sbostic 			case P_RLEAF:
61557451Sbostic 				src = rl = GETRLEAF(h, nxt);
61657451Sbostic 				nbytes = NRLEAF(rl);
61757451Sbostic 				isbigkey = 0;
61857451Sbostic 				break;
61957451Sbostic 			default:
62057451Sbostic 				abort();
62157451Sbostic 			}
62257451Sbostic 
62357451Sbostic 		/*
62457451Sbostic 		 * If the key/data pairs are substantial fractions of the max
62557451Sbostic 		 * possible size for the page, it's possible to get situations
62657451Sbostic 		 * where we decide to try and copy too much onto the left page.
62757451Sbostic 		 * Make sure that doesn't happen.
62857451Sbostic 		 */
62957451Sbostic 		if (skip <= off && used + nbytes >= full) {
63057451Sbostic 			--off;
63150989Sbostic 			break;
63250989Sbostic 		}
63346134Smao 
63457451Sbostic 		/* Copy the key/data pair, if not the skipped index. */
63557451Sbostic 		if (skip != off) {
63657451Sbostic 			++nxt;
63757451Sbostic 
63857451Sbostic 			l->linp[off] = l->upper -= nbytes;
63958017Sbostic 			memmove((char *)l + l->upper, src, nbytes);
64057451Sbostic 		}
64157451Sbostic 
64257451Sbostic 		used += nbytes;
64357451Sbostic 		if (used >= half) {
64457440Sbostic 			if (!isbigkey || bigkeycnt == 3)
64557440Sbostic 				break;
64657440Sbostic 			else
64757440Sbostic 				++bigkeycnt;
64857451Sbostic 		}
64950989Sbostic 	}
65057451Sbostic 
65157451Sbostic 	/*
65257451Sbostic 	 * Off is the last offset that's valid for the left page.
65357451Sbostic 	 * Nxt is the first offset to be placed on the right page.
65457451Sbostic 	 */
65557989Sbostic 	l->lower += (off + 1) * sizeof(indx_t);
65646134Smao 
65750989Sbostic 	/*
65851106Sbostic 	 * If splitting the page that the cursor was on, the cursor has to be
65951106Sbostic 	 * adjusted to point to the same record as before the split.  If the
66057451Sbostic 	 * cursor is at or past the skipped slot, the cursor is incremented by
66157451Sbostic 	 * one.  If the cursor is on the right page, it is decremented by the
66257451Sbostic 	 * number of records split to the left page.
66351106Sbostic 	 *
66451106Sbostic 	 * Don't bother checking for the BTF_SEQINIT flag, the page number will
66551106Sbostic 	 * be P_INVALID.
66650989Sbostic 	 */
66750989Sbostic 	c = &t->bt_bcursor;
66857451Sbostic 	if (c->pgno == h->pgno) {
66957451Sbostic 		if (c->index >= skip)
67057451Sbostic 			++c->index;
67157451Sbostic 		if (c->index < nxt)			/* Left page. */
67250989Sbostic 			c->pgno = l->pgno;
67357451Sbostic 		else {					/* Right page. */
67450989Sbostic 			c->pgno = r->pgno;
67557451Sbostic 			c->index -= nxt;
67646134Smao 		}
67757451Sbostic 	}
67846134Smao 
67950989Sbostic 	/*
68057451Sbostic 	 * If the skipped index was on the left page, just return that page.
68157451Sbostic 	 * Otherwise, adjust the skip index to reflect the new position on
68257451Sbostic 	 * the right page.
68350989Sbostic 	 */
68457451Sbostic 	if (skip <= off) {
68557451Sbostic 		skip = 0;
68657451Sbostic 		rval = l;
68757451Sbostic 	} else {
68850989Sbostic 		rval = r;
68957451Sbostic 		*pskip -= nxt;
69057451Sbostic 	}
69146134Smao 
69250989Sbostic 	for (off = 0; nxt < top; ++off) {
69351106Sbostic 		if (skip == nxt) {
69457451Sbostic 			++off;
69551106Sbostic 			skip = 0;
69646134Smao 		}
69750989Sbostic 		switch (h->flags & P_TYPE) {
69850989Sbostic 		case P_BINTERNAL:
69950989Sbostic 			src = bi = GETBINTERNAL(h, nxt);
70050989Sbostic 			nbytes = NBINTERNAL(bi->ksize);
70150989Sbostic 			break;
70250989Sbostic 		case P_BLEAF:
70350989Sbostic 			src = bl = GETBLEAF(h, nxt);
70450989Sbostic 			nbytes = NBLEAF(bl);
70550989Sbostic 			break;
70650989Sbostic 		case P_RINTERNAL:
70750989Sbostic 			src = GETRINTERNAL(h, nxt);
70850989Sbostic 			nbytes = NRINTERNAL;
70950989Sbostic 			break;
71050989Sbostic 		case P_RLEAF:
71150989Sbostic 			src = rl = GETRLEAF(h, nxt);
71250989Sbostic 			nbytes = NRLEAF(rl);
71350989Sbostic 			break;
71456741Sbostic 		default:
71556741Sbostic 			abort();
71650989Sbostic 		}
71750989Sbostic 		++nxt;
71850989Sbostic 		r->linp[off] = r->upper -= nbytes;
71958017Sbostic 		memmove((char *)r + r->upper, src, nbytes);
72050989Sbostic 	}
72157989Sbostic 	r->lower += off * sizeof(indx_t);
72246134Smao 
72350989Sbostic 	/* If the key is being appended to the page, adjust the index. */
72451106Sbostic 	if (skip == top)
72557989Sbostic 		r->lower += sizeof(indx_t);
72646134Smao 
72750989Sbostic 	return (rval);
72850989Sbostic }
72946134Smao 
73050989Sbostic /*
73150989Sbostic  * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
73250989Sbostic  *
73350989Sbostic  * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
73450989Sbostic  * record that references them gets deleted.  Chains pointed to by internal
73550989Sbostic  * pages never get deleted.  This routine marks a chain as pointed to by an
73650989Sbostic  * internal page.
73750989Sbostic  *
73850989Sbostic  * Parameters:
73950989Sbostic  *	t:	tree
74050989Sbostic  *	pg:	page number of first page in the chain.
74150989Sbostic  *
74250989Sbostic  * Returns:
74350989Sbostic  *	RET_SUCCESS, RET_ERROR.
74450989Sbostic  */
74550989Sbostic static int
74650989Sbostic bt_preserve(t, pg)
74750989Sbostic 	BTREE *t;
74850989Sbostic 	pgno_t pg;
74950989Sbostic {
75050989Sbostic 	PAGE *h;
75146134Smao 
75250989Sbostic 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
75350989Sbostic 		return (RET_ERROR);
75450989Sbostic 	h->flags |= P_PRESERVE;
75550989Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
75646134Smao 	return (RET_SUCCESS);
75746134Smao }
75850989Sbostic 
75950989Sbostic /*
76050989Sbostic  * REC_TOTAL -- Return the number of recno entries below a page.
76150989Sbostic  *
76250989Sbostic  * Parameters:
76350989Sbostic  *	h:	page
76450989Sbostic  *
76550989Sbostic  * Returns:
76650989Sbostic  *	The number of recno entries below a page.
76750989Sbostic  *
76850989Sbostic  * XXX
76950989Sbostic  * These values could be set by the bt_psplit routine.  The problem is that the
77050989Sbostic  * entry has to be popped off of the stack etc. or the values have to be passed
77150989Sbostic  * all the way back to bt_split/bt_rroot and it's not very clean.
77250989Sbostic  */
77350989Sbostic static recno_t
77450989Sbostic rec_total(h)
77550989Sbostic 	PAGE *h;
77650989Sbostic {
77750989Sbostic 	recno_t recs;
77857989Sbostic 	indx_t nxt, top;
77950989Sbostic 
78050989Sbostic 	for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
78150989Sbostic 		recs += GETRINTERNAL(h, nxt)->nrecs;
78250989Sbostic 	return (recs);
78350989Sbostic }
784