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