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