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