xref: /csrg-svn/lib/libc/db/btree/bt_split.c (revision 61196)
146134Smao /*-
2*61196Sbostic  * Copyright (c) 1990, 1993
3*61196Sbostic  *	The Regents of the University of California.  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*61196Sbostic static char sccsid[] = "@(#)bt_split.c	8.1 (Berkeley) 06/04/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
2858078Sbostic 		    __P((BTREE *, PAGE *, PAGE **, PAGE **, u_int *, size_t));
2950989Sbostic static int	 bt_preserve __P((BTREE *, pgno_t));
3057451Sbostic static PAGE	*bt_psplit
3158078Sbostic 		    __P((BTREE *, PAGE *, PAGE *, PAGE *, u_int *, size_t));
3257451Sbostic static PAGE	*bt_root
3358078Sbostic 		    __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;
7260234Sbostic 	int parentsplit;
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;
9360048Sbostic 	if (ISSET(t, R_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 &&
10060048Sbostic 	    (ISSET(t, R_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 	 */
12760234Sbostic 	while ((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;
19860234Sbostic 			parentsplit = 1;
19946134Smao 		} else {
20050989Sbostic 			if (skip < (nxtindex = NEXTINDEX(h)))
20158017Sbostic 				memmove(h->linp + skip + 1, h->linp + skip,
20257989Sbostic 				    (nxtindex - skip) * sizeof(indx_t));
20357989Sbostic 			h->lower += sizeof(indx_t);
20460234Sbostic 			parentsplit = 0;
20546134Smao 		}
20646134Smao 
20750989Sbostic 		/* Insert the key into the parent page. */
20850989Sbostic 		switch(rchild->flags & P_TYPE) {
20950989Sbostic 		case P_BINTERNAL:
21050989Sbostic 			h->linp[skip] = h->upper -= nbytes;
21150989Sbostic 			dest = (char *)h + h->linp[skip];
21258017Sbostic 			memmove(dest, bi, nbytes);
21350989Sbostic 			((BINTERNAL *)dest)->pgno = rchild->pgno;
21450989Sbostic 			break;
21550989Sbostic 		case P_BLEAF:
21650989Sbostic 			h->linp[skip] = h->upper -= nbytes;
21750989Sbostic 			dest = (char *)h + h->linp[skip];
21850989Sbostic 			WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
21951106Sbostic 			    rchild->pgno, bl->flags & P_BIGKEY);
22058017Sbostic 			memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
22150989Sbostic 			if (bl->flags & P_BIGKEY &&
22250989Sbostic 			    bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
22350989Sbostic 				goto err1;
22450989Sbostic 			break;
22550989Sbostic 		case P_RINTERNAL:
22660234Sbostic 			/*
22760234Sbostic 			 * Update the left page count.  If split
22860234Sbostic 			 * added at index 0, fix the correct page.
22960234Sbostic 			 */
23060234Sbostic 			if (skip > 0)
23160234Sbostic 				dest = (char *)h + h->linp[skip - 1];
23260234Sbostic 			else
23360234Sbostic 				dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
23460234Sbostic 			((RINTERNAL *)dest)->nrecs = rec_total(lchild);
23560234Sbostic 			((RINTERNAL *)dest)->pgno = lchild->pgno;
23660234Sbostic 
23760234Sbostic 			/* Update the right page count. */
23850989Sbostic 			h->linp[skip] = h->upper -= nbytes;
23950989Sbostic 			dest = (char *)h + h->linp[skip];
24050989Sbostic 			((RINTERNAL *)dest)->nrecs = rec_total(rchild);
24150989Sbostic 			((RINTERNAL *)dest)->pgno = rchild->pgno;
24250989Sbostic 			break;
24350989Sbostic 		case P_RLEAF:
24460234Sbostic 			/*
24560234Sbostic 			 * Update the left page count.  If split
24660234Sbostic 			 * added at index 0, fix the correct page.
24760234Sbostic 			 */
24860234Sbostic 			if (skip > 0)
24960234Sbostic 				dest = (char *)h + h->linp[skip - 1];
25060234Sbostic 			else
25160234Sbostic 				dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
25260234Sbostic 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild);
25360234Sbostic 			((RINTERNAL *)dest)->pgno = lchild->pgno;
25460234Sbostic 
25560234Sbostic 			/* Update the right page count. */
25650989Sbostic 			h->linp[skip] = h->upper -= nbytes;
25750989Sbostic 			dest = (char *)h + h->linp[skip];
25850989Sbostic 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild);
25950989Sbostic 			((RINTERNAL *)dest)->pgno = rchild->pgno;
26050989Sbostic 			break;
26156741Sbostic 		default:
26256741Sbostic 			abort();
26350989Sbostic 		}
26446134Smao 
26550989Sbostic 		/* Unpin the held pages. */
26660234Sbostic 		if (!parentsplit) {
26750989Sbostic 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
26850989Sbostic 			break;
26950989Sbostic 		}
27057440Sbostic 
27157440Sbostic 		/* If the root page was split, make it look right. */
27257440Sbostic 		if (sp->pgno == P_ROOT &&
27360048Sbostic 		    (ISSET(t, R_RECNO) ?
27457440Sbostic 		    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
27557440Sbostic 			goto err1;
27657440Sbostic 
27750989Sbostic 		mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
27850989Sbostic 		mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
27950989Sbostic 	}
28046134Smao 
28150989Sbostic 	/* Unpin the held pages. */
28250989Sbostic 	mpool_put(t->bt_mp, l, MPOOL_DIRTY);
28350989Sbostic 	mpool_put(t->bt_mp, r, MPOOL_DIRTY);
28450989Sbostic 
28551106Sbostic 	/* Clear any pages left on the stack. */
28650989Sbostic 	return (RET_SUCCESS);
28746134Smao 
28846134Smao 	/*
28950989Sbostic 	 * If something fails in the above loop we were already walking back
29050989Sbostic 	 * up the tree and the tree is now inconsistent.  Nothing much we can
29150989Sbostic 	 * do about it but release any memory we're holding.
29246134Smao 	 */
29350989Sbostic err1:	mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
29450989Sbostic 	mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
29546134Smao 
29650989Sbostic err2:	mpool_put(t->bt_mp, l, 0);
29750989Sbostic 	mpool_put(t->bt_mp, r, 0);
29850989Sbostic 	__dbpanic(t->bt_dbp);
29950989Sbostic 	return (RET_ERROR);
30046134Smao }
30146134Smao 
30246134Smao /*
30350989Sbostic  * BT_PAGE -- Split a non-root page of a btree.
30446134Smao  *
30550989Sbostic  * Parameters:
30650989Sbostic  *	t:	tree
30750989Sbostic  *	h:	root page
30850989Sbostic  *	lp:	pointer to left page pointer
30950989Sbostic  *	rp:	pointer to right page pointer
31050989Sbostic  *	skip:	pointer to index to leave open
31157451Sbostic  *	ilen:	insert length
31246134Smao  *
31350989Sbostic  * Returns:
31450989Sbostic  *	Pointer to page in which to insert or NULL on error.
31546134Smao  */
31650989Sbostic static PAGE *
31757451Sbostic bt_page(t, h, lp, rp, skip, ilen)
31850989Sbostic 	BTREE *t;
31950989Sbostic 	PAGE *h, **lp, **rp;
32058078Sbostic 	u_int *skip;
32157451Sbostic 	size_t ilen;
32246134Smao {
32350989Sbostic 	PAGE *l, *r, *tp;
32450989Sbostic 	pgno_t npg;
32546134Smao 
32650989Sbostic #ifdef STATISTICS
32750989Sbostic 	++bt_split;
32850989Sbostic #endif
32950989Sbostic 	/* Put the new right page for the split into place. */
33056492Sbostic 	if ((r = __bt_new(t, &npg)) == NULL)
33150989Sbostic 		return (NULL);
33250989Sbostic 	r->pgno = npg;
33350989Sbostic 	r->lower = BTDATAOFF;
33450989Sbostic 	r->upper = t->bt_psize;
33550989Sbostic 	r->nextpg = h->nextpg;
33650989Sbostic 	r->prevpg = h->pgno;
33750989Sbostic 	r->flags = h->flags & P_TYPE;
33846134Smao 
33950989Sbostic 	/*
34050989Sbostic 	 * If we're splitting the last page on a level because we're appending
34150989Sbostic 	 * a key to it (skip is NEXTINDEX()), it's likely that the data is
34250989Sbostic 	 * sorted.  Adding an empty page on the side of the level is less work
34350989Sbostic 	 * and can push the fill factor much higher than normal.  If we're
34450989Sbostic 	 * wrong it's no big deal, we'll just do the split the right way next
34550989Sbostic 	 * time.  It may look like it's equally easy to do a similar hack for
34650989Sbostic 	 * reverse sorted data, that is, split the tree left, but it's not.
34750989Sbostic 	 * Don't even try.
34850989Sbostic 	 */
34950989Sbostic 	if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
35050989Sbostic #ifdef STATISTICS
35150989Sbostic 		++bt_sortsplit;
35250989Sbostic #endif
35350989Sbostic 		h->nextpg = r->pgno;
35457989Sbostic 		r->lower = BTDATAOFF + sizeof(indx_t);
35550989Sbostic 		*skip = 0;
35650989Sbostic 		*lp = h;
35750989Sbostic 		*rp = r;
35850989Sbostic 		return (r);
35950989Sbostic 	}
36046134Smao 
36150989Sbostic 	/* Put the new left page for the split into place. */
36250989Sbostic 	if ((l = malloc(t->bt_psize)) == NULL) {
36350989Sbostic 		mpool_put(t->bt_mp, r, 0);
36450989Sbostic 		return (NULL);
36550989Sbostic 	}
36650989Sbostic 	l->pgno = h->pgno;
36750989Sbostic 	l->nextpg = r->pgno;
36850989Sbostic 	l->prevpg = h->prevpg;
36950989Sbostic 	l->lower = BTDATAOFF;
37050989Sbostic 	l->upper = t->bt_psize;
37150989Sbostic 	l->flags = h->flags & P_TYPE;
37246134Smao 
37350989Sbostic 	/* Fix up the previous pointer of the page after the split page. */
37450989Sbostic 	if (h->nextpg != P_INVALID) {
37550989Sbostic 		if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
37650989Sbostic 			free(l);
37750989Sbostic 			/* XXX mpool_free(t->bt_mp, r->pgno); */
37850989Sbostic 			return (NULL);
37946134Smao 		}
38050989Sbostic 		tp->prevpg = r->pgno;
38150989Sbostic 		mpool_put(t->bt_mp, tp, 0);
38250989Sbostic 	}
38346134Smao 
38450989Sbostic 	/*
38550989Sbostic 	 * Split right.  The key/data pairs aren't sorted in the btree page so
38650989Sbostic 	 * it's simpler to copy the data from the split page onto two new pages
38750989Sbostic 	 * instead of copying half the data to the right page and compacting
38850989Sbostic 	 * the left page in place.  Since the left page can't change, we have
38950989Sbostic 	 * to swap the original and the allocated left page after the split.
39050989Sbostic 	 */
39157451Sbostic 	tp = bt_psplit(t, h, l, r, skip, ilen);
39246134Smao 
39350989Sbostic 	/* Move the new left page onto the old left page. */
39458017Sbostic 	memmove(h, l, t->bt_psize);
39550989Sbostic 	if (tp == l)
39650989Sbostic 		tp = h;
39750989Sbostic 	free(l);
39850989Sbostic 
39950989Sbostic 	*lp = h;
40050989Sbostic 	*rp = r;
40150989Sbostic 	return (tp);
40246134Smao }
40346134Smao 
40446134Smao /*
40551106Sbostic  * BT_ROOT -- Split the root page of a btree.
40646134Smao  *
40750989Sbostic  * Parameters:
40850989Sbostic  *	t:	tree
40950989Sbostic  *	h:	root page
41050989Sbostic  *	lp:	pointer to left page pointer
41150989Sbostic  *	rp:	pointer to right page pointer
41250989Sbostic  *	skip:	pointer to index to leave open
41357451Sbostic  *	ilen:	insert length
41446134Smao  *
41550989Sbostic  * Returns:
41650989Sbostic  *	Pointer to page in which to insert or NULL on error.
41746134Smao  */
41850989Sbostic static PAGE *
41957451Sbostic bt_root(t, h, lp, rp, skip, ilen)
42050989Sbostic 	BTREE *t;
42150989Sbostic 	PAGE *h, **lp, **rp;
42258078Sbostic 	u_int *skip;
42357451Sbostic 	size_t ilen;
42446134Smao {
42550989Sbostic 	PAGE *l, *r, *tp;
42650989Sbostic 	pgno_t lnpg, rnpg;
42746134Smao 
42850989Sbostic #ifdef STATISTICS
42950989Sbostic 	++bt_split;
43050989Sbostic 	++bt_rootsplit;
43150989Sbostic #endif
43250989Sbostic 	/* Put the new left and right pages for the split into place. */
43356492Sbostic 	if ((l = __bt_new(t, &lnpg)) == NULL ||
43456492Sbostic 	    (r = __bt_new(t, &rnpg)) == NULL)
43550989Sbostic 		return (NULL);
43650989Sbostic 	l->pgno = lnpg;
43750989Sbostic 	r->pgno = rnpg;
43850989Sbostic 	l->nextpg = r->pgno;
43950989Sbostic 	r->prevpg = l->pgno;
44050989Sbostic 	l->prevpg = r->nextpg = P_INVALID;
44150989Sbostic 	l->lower = r->lower = BTDATAOFF;
44250989Sbostic 	l->upper = r->upper = t->bt_psize;
44350989Sbostic 	l->flags = r->flags = h->flags & P_TYPE;
44450989Sbostic 
44550989Sbostic 	/* Split the root page. */
44657451Sbostic 	tp = bt_psplit(t, h, l, r, skip, ilen);
44750989Sbostic 
44850989Sbostic 	*lp = l;
44950989Sbostic 	*rp = r;
45050989Sbostic 	return (tp);
45146134Smao }
45246134Smao 
45346134Smao /*
45457440Sbostic  * BT_RROOT -- Fix up the recno root page after it has been split.
45546134Smao  *
45650989Sbostic  * Parameters:
45750989Sbostic  *	t:	tree
45850989Sbostic  *	h:	root page
45957440Sbostic  *	l:	left page
46057440Sbostic  *	r:	right page
46146134Smao  *
46250989Sbostic  * Returns:
46350989Sbostic  *	RET_ERROR, RET_SUCCESS
46446134Smao  */
46550989Sbostic static int
46650989Sbostic bt_rroot(t, h, l, r)
46750989Sbostic 	BTREE *t;
46850989Sbostic 	PAGE *h, *l, *r;
46946134Smao {
47050989Sbostic 	char *dest;
47146134Smao 
47250989Sbostic 	/* Insert the left and right keys, set the header information. */
47350989Sbostic 	h->linp[0] = h->upper = t->bt_psize - NRINTERNAL;
47450989Sbostic 	dest = (char *)h + h->upper;
47550989Sbostic 	WR_RINTERNAL(dest,
47650989Sbostic 	    l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
47746134Smao 
47850989Sbostic 	h->linp[1] = h->upper -= NRINTERNAL;
47950989Sbostic 	dest = (char *)h + h->upper;
48050989Sbostic 	WR_RINTERNAL(dest,
48150989Sbostic 	    r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
48246134Smao 
48357989Sbostic 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
48446134Smao 
48550989Sbostic 	/* Unpin the root page, set to recno internal page. */
48650989Sbostic 	h->flags &= ~P_TYPE;
48750989Sbostic 	h->flags |= P_RINTERNAL;
48850989Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
48946134Smao 
49050989Sbostic 	return (RET_SUCCESS);
49150989Sbostic }
49246134Smao 
49350989Sbostic /*
49457440Sbostic  * BT_BROOT -- Fix up the btree root page after it has been split.
49550989Sbostic  *
49650989Sbostic  * Parameters:
49750989Sbostic  *	t:	tree
49850989Sbostic  *	h:	root page
49957440Sbostic  *	l:	left page
50057440Sbostic  *	r:	right page
50150989Sbostic  *
50250989Sbostic  * Returns:
50350989Sbostic  *	RET_ERROR, RET_SUCCESS
50450989Sbostic  */
50550989Sbostic static int
50650989Sbostic bt_broot(t, h, l, r)
50750989Sbostic 	BTREE *t;
50850989Sbostic 	PAGE *h, *l, *r;
50950989Sbostic {
51050989Sbostic 	BINTERNAL *bi;
51150989Sbostic 	BLEAF *bl;
51250989Sbostic 	size_t nbytes;
51350989Sbostic 	char *dest;
51446134Smao 
51546134Smao 	/*
51650989Sbostic 	 * If the root page was a leaf page, change it into an internal page.
51750989Sbostic 	 * We copy the key we split on (but not the key's data, in the case of
51856401Sbostic 	 * a leaf page) to the new root page.
51950989Sbostic 	 *
52050989Sbostic 	 * The btree comparison code guarantees that the left-most key on any
52157440Sbostic 	 * level of the tree is never used, so it doesn't need to be filled in.
52246134Smao 	 */
52356401Sbostic 	nbytes = NBINTERNAL(0);
52450989Sbostic 	h->linp[0] = h->upper = t->bt_psize - nbytes;
52550989Sbostic 	dest = (char *)h + h->upper;
52650989Sbostic 	WR_BINTERNAL(dest, 0, l->pgno, 0);
52746134Smao 
52850989Sbostic 	switch(h->flags & P_TYPE) {
52950989Sbostic 	case P_BLEAF:
53050989Sbostic 		bl = GETBLEAF(r, 0);
53150989Sbostic 		nbytes = NBINTERNAL(bl->ksize);
53250989Sbostic 		h->linp[1] = h->upper -= nbytes;
53350989Sbostic 		dest = (char *)h + h->upper;
53450989Sbostic 		WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
53558017Sbostic 		memmove(dest, bl->bytes, bl->ksize);
53650989Sbostic 
53756401Sbostic 		/*
53856401Sbostic 		 * If the key is on an overflow page, mark the overflow chain
53956401Sbostic 		 * so it isn't deleted when the leaf copy of the key is deleted.
54056401Sbostic 		 */
54150989Sbostic 		if (bl->flags & P_BIGKEY &&
54250989Sbostic 		    bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
54350989Sbostic 			return (RET_ERROR);
54450989Sbostic 		break;
54550989Sbostic 	case P_BINTERNAL:
54650989Sbostic 		bi = GETBINTERNAL(r, 0);
54750989Sbostic 		nbytes = NBINTERNAL(bi->ksize);
54850989Sbostic 		h->linp[1] = h->upper -= nbytes;
54950989Sbostic 		dest = (char *)h + h->upper;
55058017Sbostic 		memmove(dest, bi, nbytes);
55150989Sbostic 		((BINTERNAL *)dest)->pgno = r->pgno;
55250989Sbostic 		break;
55356741Sbostic 	default:
55456741Sbostic 		abort();
55546134Smao 	}
55657440Sbostic 
55757440Sbostic 	/* There are two keys on the page. */
55857989Sbostic 	h->lower = BTDATAOFF + 2 * sizeof(indx_t);
55946134Smao 
56050989Sbostic 	/* Unpin the root page, set to btree internal page. */
56150989Sbostic 	h->flags &= ~P_TYPE;
56250989Sbostic 	h->flags |= P_BINTERNAL;
56350989Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
56446134Smao 
56546134Smao 	return (RET_SUCCESS);
56646134Smao }
56746134Smao 
56846134Smao /*
56950989Sbostic  * BT_PSPLIT -- Do the real work of splitting the page.
57046134Smao  *
57150989Sbostic  * Parameters:
57250989Sbostic  *	t:	tree
57350989Sbostic  *	h:	page to be split
57450989Sbostic  *	l:	page to put lower half of data
57550989Sbostic  *	r:	page to put upper half of data
57657440Sbostic  *	pskip:	pointer to index to leave open
57757451Sbostic  *	ilen:	insert length
57846134Smao  *
57950989Sbostic  * Returns:
58050989Sbostic  *	Pointer to page in which to insert.
58146134Smao  */
58250989Sbostic static PAGE *
58357451Sbostic bt_psplit(t, h, l, r, pskip, ilen)
58450989Sbostic 	BTREE *t;
58550989Sbostic 	PAGE *h, *l, *r;
58658078Sbostic 	u_int *pskip;
58757451Sbostic 	size_t ilen;
58846134Smao {
58950989Sbostic 	BINTERNAL *bi;
59050989Sbostic 	BLEAF *bl;
59150989Sbostic 	RLEAF *rl;
59250989Sbostic 	EPGNO *c;
59350989Sbostic 	PAGE *rval;
59458067Sbostic 	void *src;
59558067Sbostic 	indx_t full, half, nxt, off, skip, top, used;
59650989Sbostic 	size_t nbytes;
59758067Sbostic 	int bigkeycnt, isbigkey;
59846134Smao 
59946134Smao 	/*
60057451Sbostic 	 * Split the data to the left and right pages.  Leave the skip index
60157451Sbostic 	 * open.  Additionally, make some effort not to split on an overflow
60257451Sbostic 	 * key.  This makes internal page processing faster and can save
60357451Sbostic 	 * space as overflow keys used by internal pages are never deleted.
60446134Smao 	 */
60550989Sbostic 	bigkeycnt = 0;
60651106Sbostic 	skip = *pskip;
60757451Sbostic 	full = t->bt_psize - BTDATAOFF;
60857451Sbostic 	half = full / 2;
60957451Sbostic 	used = 0;
61050989Sbostic 	for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
61157451Sbostic 		if (skip == off) {
61257451Sbostic 			nbytes = ilen;
61357451Sbostic 			isbigkey = 0;		/* XXX: not really known. */
61457451Sbostic 		} else
61557451Sbostic 			switch (h->flags & P_TYPE) {
61657451Sbostic 			case P_BINTERNAL:
61757451Sbostic 				src = bi = GETBINTERNAL(h, nxt);
61857451Sbostic 				nbytes = NBINTERNAL(bi->ksize);
61957451Sbostic 				isbigkey = bi->flags & P_BIGKEY;
62057451Sbostic 				break;
62157451Sbostic 			case P_BLEAF:
62257451Sbostic 				src = bl = GETBLEAF(h, nxt);
62357451Sbostic 				nbytes = NBLEAF(bl);
62457451Sbostic 				isbigkey = bl->flags & P_BIGKEY;
62557451Sbostic 				break;
62657451Sbostic 			case P_RINTERNAL:
62757451Sbostic 				src = GETRINTERNAL(h, nxt);
62857451Sbostic 				nbytes = NRINTERNAL;
62957451Sbostic 				isbigkey = 0;
63057451Sbostic 				break;
63157451Sbostic 			case P_RLEAF:
63257451Sbostic 				src = rl = GETRLEAF(h, nxt);
63357451Sbostic 				nbytes = NRLEAF(rl);
63457451Sbostic 				isbigkey = 0;
63557451Sbostic 				break;
63657451Sbostic 			default:
63757451Sbostic 				abort();
63857451Sbostic 			}
63957451Sbostic 
64057451Sbostic 		/*
64157451Sbostic 		 * If the key/data pairs are substantial fractions of the max
64257451Sbostic 		 * possible size for the page, it's possible to get situations
64357451Sbostic 		 * where we decide to try and copy too much onto the left page.
64457451Sbostic 		 * Make sure that doesn't happen.
64557451Sbostic 		 */
64657451Sbostic 		if (skip <= off && used + nbytes >= full) {
64757451Sbostic 			--off;
64850989Sbostic 			break;
64950989Sbostic 		}
65046134Smao 
65157451Sbostic 		/* Copy the key/data pair, if not the skipped index. */
65257451Sbostic 		if (skip != off) {
65357451Sbostic 			++nxt;
65457451Sbostic 
65557451Sbostic 			l->linp[off] = l->upper -= nbytes;
65658017Sbostic 			memmove((char *)l + l->upper, src, nbytes);
65757451Sbostic 		}
65857451Sbostic 
65957451Sbostic 		used += nbytes;
66057451Sbostic 		if (used >= half) {
66157440Sbostic 			if (!isbigkey || bigkeycnt == 3)
66257440Sbostic 				break;
66357440Sbostic 			else
66457440Sbostic 				++bigkeycnt;
66557451Sbostic 		}
66650989Sbostic 	}
66757451Sbostic 
66857451Sbostic 	/*
66957451Sbostic 	 * Off is the last offset that's valid for the left page.
67057451Sbostic 	 * Nxt is the first offset to be placed on the right page.
67157451Sbostic 	 */
67257989Sbostic 	l->lower += (off + 1) * sizeof(indx_t);
67346134Smao 
67450989Sbostic 	/*
67551106Sbostic 	 * If splitting the page that the cursor was on, the cursor has to be
67651106Sbostic 	 * adjusted to point to the same record as before the split.  If the
67757451Sbostic 	 * cursor is at or past the skipped slot, the cursor is incremented by
67857451Sbostic 	 * one.  If the cursor is on the right page, it is decremented by the
67957451Sbostic 	 * number of records split to the left page.
68051106Sbostic 	 *
68160048Sbostic 	 * Don't bother checking for the B_SEQINIT flag, the page number will
68251106Sbostic 	 * be P_INVALID.
68350989Sbostic 	 */
68450989Sbostic 	c = &t->bt_bcursor;
68557451Sbostic 	if (c->pgno == h->pgno) {
68657451Sbostic 		if (c->index >= skip)
68757451Sbostic 			++c->index;
68857451Sbostic 		if (c->index < nxt)			/* Left page. */
68950989Sbostic 			c->pgno = l->pgno;
69057451Sbostic 		else {					/* Right page. */
69150989Sbostic 			c->pgno = r->pgno;
69257451Sbostic 			c->index -= nxt;
69346134Smao 		}
69457451Sbostic 	}
69546134Smao 
69650989Sbostic 	/*
69757451Sbostic 	 * If the skipped index was on the left page, just return that page.
69857451Sbostic 	 * Otherwise, adjust the skip index to reflect the new position on
69957451Sbostic 	 * the right page.
70050989Sbostic 	 */
70157451Sbostic 	if (skip <= off) {
70257451Sbostic 		skip = 0;
70357451Sbostic 		rval = l;
70457451Sbostic 	} else {
70550989Sbostic 		rval = r;
70657451Sbostic 		*pskip -= nxt;
70757451Sbostic 	}
70846134Smao 
70950989Sbostic 	for (off = 0; nxt < top; ++off) {
71051106Sbostic 		if (skip == nxt) {
71157451Sbostic 			++off;
71251106Sbostic 			skip = 0;
71346134Smao 		}
71450989Sbostic 		switch (h->flags & P_TYPE) {
71550989Sbostic 		case P_BINTERNAL:
71650989Sbostic 			src = bi = GETBINTERNAL(h, nxt);
71750989Sbostic 			nbytes = NBINTERNAL(bi->ksize);
71850989Sbostic 			break;
71950989Sbostic 		case P_BLEAF:
72050989Sbostic 			src = bl = GETBLEAF(h, nxt);
72150989Sbostic 			nbytes = NBLEAF(bl);
72250989Sbostic 			break;
72350989Sbostic 		case P_RINTERNAL:
72450989Sbostic 			src = GETRINTERNAL(h, nxt);
72550989Sbostic 			nbytes = NRINTERNAL;
72650989Sbostic 			break;
72750989Sbostic 		case P_RLEAF:
72850989Sbostic 			src = rl = GETRLEAF(h, nxt);
72950989Sbostic 			nbytes = NRLEAF(rl);
73050989Sbostic 			break;
73156741Sbostic 		default:
73256741Sbostic 			abort();
73350989Sbostic 		}
73450989Sbostic 		++nxt;
73550989Sbostic 		r->linp[off] = r->upper -= nbytes;
73658017Sbostic 		memmove((char *)r + r->upper, src, nbytes);
73750989Sbostic 	}
73857989Sbostic 	r->lower += off * sizeof(indx_t);
73946134Smao 
74050989Sbostic 	/* If the key is being appended to the page, adjust the index. */
74151106Sbostic 	if (skip == top)
74257989Sbostic 		r->lower += sizeof(indx_t);
74346134Smao 
74450989Sbostic 	return (rval);
74550989Sbostic }
74646134Smao 
74750989Sbostic /*
74850989Sbostic  * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
74950989Sbostic  *
75050989Sbostic  * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
75150989Sbostic  * record that references them gets deleted.  Chains pointed to by internal
75250989Sbostic  * pages never get deleted.  This routine marks a chain as pointed to by an
75350989Sbostic  * internal page.
75450989Sbostic  *
75550989Sbostic  * Parameters:
75650989Sbostic  *	t:	tree
75750989Sbostic  *	pg:	page number of first page in the chain.
75850989Sbostic  *
75950989Sbostic  * Returns:
76050989Sbostic  *	RET_SUCCESS, RET_ERROR.
76150989Sbostic  */
76250989Sbostic static int
76350989Sbostic bt_preserve(t, pg)
76450989Sbostic 	BTREE *t;
76550989Sbostic 	pgno_t pg;
76650989Sbostic {
76750989Sbostic 	PAGE *h;
76846134Smao 
76950989Sbostic 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
77050989Sbostic 		return (RET_ERROR);
77150989Sbostic 	h->flags |= P_PRESERVE;
77250989Sbostic 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
77346134Smao 	return (RET_SUCCESS);
77446134Smao }
77550989Sbostic 
77650989Sbostic /*
77750989Sbostic  * REC_TOTAL -- Return the number of recno entries below a page.
77850989Sbostic  *
77950989Sbostic  * Parameters:
78050989Sbostic  *	h:	page
78150989Sbostic  *
78250989Sbostic  * Returns:
78350989Sbostic  *	The number of recno entries below a page.
78450989Sbostic  *
78550989Sbostic  * XXX
78650989Sbostic  * These values could be set by the bt_psplit routine.  The problem is that the
78750989Sbostic  * entry has to be popped off of the stack etc. or the values have to be passed
78850989Sbostic  * all the way back to bt_split/bt_rroot and it's not very clean.
78950989Sbostic  */
79050989Sbostic static recno_t
79150989Sbostic rec_total(h)
79250989Sbostic 	PAGE *h;
79350989Sbostic {
79450989Sbostic 	recno_t recs;
79557989Sbostic 	indx_t nxt, top;
79650989Sbostic 
79750989Sbostic 	for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
79850989Sbostic 		recs += GETRINTERNAL(h, nxt)->nrecs;
79950989Sbostic 	return (recs);
80050989Sbostic }
801