xref: /freebsd/lib/libc/db/btree/bt_split.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
1 /*-
2  * Copyright (c) 1990, 1993
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)bt_split.c	8.3 (Berkeley) 2/21/94";
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include <sys/types.h>
42 
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include <db.h>
49 #include "btree.h"
50 
51 static int	 bt_broot __P((BTREE *, PAGE *, PAGE *, PAGE *));
52 static PAGE	*bt_page
53 		    __P((BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t));
54 static int	 bt_preserve __P((BTREE *, pgno_t));
55 static PAGE	*bt_psplit
56 		    __P((BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t));
57 static PAGE	*bt_root
58 		    __P((BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t));
59 static int	 bt_rroot __P((BTREE *, PAGE *, PAGE *, PAGE *));
60 static recno_t	 rec_total __P((PAGE *));
61 
62 #ifdef STATISTICS
63 u_long	bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved;
64 #endif
65 
66 /*
67  * __BT_SPLIT -- Split the tree.
68  *
69  * Parameters:
70  *	t:	tree
71  *	sp:	page to split
72  *	key:	key to insert
73  *	data:	data to insert
74  *	flags:	BIGKEY/BIGDATA flags
75  *	ilen:	insert length
76  *	skip:	index to leave open
77  *
78  * Returns:
79  *	RET_ERROR, RET_SUCCESS
80  */
81 int
82 __bt_split(t, sp, key, data, flags, ilen, skip)
83 	BTREE *t;
84 	PAGE *sp;
85 	const DBT *key, *data;
86 	int flags;
87 	size_t ilen;
88 	indx_t skip;
89 {
90 	BINTERNAL *bi;
91 	BLEAF *bl, *tbl;
92 	DBT a, b;
93 	EPGNO *parent;
94 	PAGE *h, *l, *r, *lchild, *rchild;
95 	indx_t nxtindex;
96 	size_t n, nbytes, nksize;
97 	int parentsplit;
98 	char *dest;
99 
100 	/*
101 	 * Split the page into two pages, l and r.  The split routines return
102 	 * a pointer to the page into which the key should be inserted and with
103 	 * skip set to the offset which should be used.  Additionally, l and r
104 	 * are pinned.
105 	 */
106 	h = sp->pgno == P_ROOT ?
107 	    bt_root(t, sp, &l, &r, &skip, ilen) :
108 	    bt_page(t, sp, &l, &r, &skip, ilen);
109 	if (h == NULL)
110 		return (RET_ERROR);
111 
112 	/*
113 	 * Insert the new key/data pair into the leaf page.  (Key inserts
114 	 * always cause a leaf page to split first.)
115 	 */
116 	h->linp[skip] = h->upper -= ilen;
117 	dest = (char *)h + h->upper;
118 	if (ISSET(t, R_RECNO))
119 		WR_RLEAF(dest, data, flags)
120 	else
121 		WR_BLEAF(dest, key, data, flags)
122 
123 	/* If the root page was split, make it look right. */
124 	if (sp->pgno == P_ROOT &&
125 	    (ISSET(t, R_RECNO) ?
126 	    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
127 		goto err2;
128 
129 	/*
130 	 * Now we walk the parent page stack -- a LIFO stack of the pages that
131 	 * were traversed when we searched for the page that split.  Each stack
132 	 * entry is a page number and a page index offset.  The offset is for
133 	 * the page traversed on the search.  We've just split a page, so we
134 	 * have to insert a new key into the parent page.
135 	 *
136 	 * If the insert into the parent page causes it to split, may have to
137 	 * continue splitting all the way up the tree.  We stop if the root
138 	 * splits or the page inserted into didn't have to split to hold the
139 	 * new key.  Some algorithms replace the key for the old page as well
140 	 * as the new page.  We don't, as there's no reason to believe that the
141 	 * first key on the old page is any better than the key we have, and,
142 	 * in the case of a key being placed at index 0 causing the split, the
143 	 * key is unavailable.
144 	 *
145 	 * There are a maximum of 5 pages pinned at any time.  We keep the left
146 	 * and right pages pinned while working on the parent.   The 5 are the
147 	 * two children, left parent and right parent (when the parent splits)
148 	 * and the root page or the overflow key page when calling bt_preserve.
149 	 * This code must make sure that all pins are released other than the
150 	 * root page or overflow page which is unlocked elsewhere.
151 	 */
152 	while ((parent = BT_POP(t)) != NULL) {
153 		lchild = l;
154 		rchild = r;
155 
156 		/* Get the parent page. */
157 		if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
158 			goto err2;
159 
160 	 	/*
161 		 * The new key goes ONE AFTER the index, because the split
162 		 * was to the right.
163 		 */
164 		skip = parent->index + 1;
165 
166 		/*
167 		 * Calculate the space needed on the parent page.
168 		 *
169 		 * Prefix trees: space hack when inserting into BINTERNAL
170 		 * pages.  Retain only what's needed to distinguish between
171 		 * the new entry and the LAST entry on the page to its left.
172 		 * If the keys compare equal, retain the entire key.  Note,
173 		 * we don't touch overflow keys, and the entire key must be
174 		 * retained for the next-to-left most key on the leftmost
175 		 * page of each level, or the search will fail.  Applicable
176 		 * ONLY to internal pages that have leaf pages as children.
177 		 * Further reduction of the key between pairs of internal
178 		 * pages loses too much information.
179 		 */
180 		switch (rchild->flags & P_TYPE) {
181 		case P_BINTERNAL:
182 			bi = GETBINTERNAL(rchild, 0);
183 			nbytes = NBINTERNAL(bi->ksize);
184 			break;
185 		case P_BLEAF:
186 			bl = GETBLEAF(rchild, 0);
187 			nbytes = NBINTERNAL(bl->ksize);
188 			if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
189 			    (h->prevpg != P_INVALID || skip > 1)) {
190 				tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
191 				a.size = tbl->ksize;
192 				a.data = tbl->bytes;
193 				b.size = bl->ksize;
194 				b.data = bl->bytes;
195 				nksize = t->bt_pfx(&a, &b);
196 				n = NBINTERNAL(nksize);
197 				if (n < nbytes) {
198 #ifdef STATISTICS
199 					bt_pfxsaved += nbytes - n;
200 #endif
201 					nbytes = n;
202 				} else
203 					nksize = 0;
204 			} else
205 				nksize = 0;
206 			break;
207 		case P_RINTERNAL:
208 		case P_RLEAF:
209 			nbytes = NRINTERNAL;
210 			break;
211 		default:
212 			abort();
213 		}
214 
215 		/* Split the parent page if necessary or shift the indices. */
216 		if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
217 			sp = h;
218 			h = h->pgno == P_ROOT ?
219 			    bt_root(t, h, &l, &r, &skip, nbytes) :
220 			    bt_page(t, h, &l, &r, &skip, nbytes);
221 			if (h == NULL)
222 				goto err1;
223 			parentsplit = 1;
224 		} else {
225 			if (skip < (nxtindex = NEXTINDEX(h)))
226 				memmove(h->linp + skip + 1, h->linp + skip,
227 				    (nxtindex - skip) * sizeof(indx_t));
228 			h->lower += sizeof(indx_t);
229 			parentsplit = 0;
230 		}
231 
232 		/* Insert the key into the parent page. */
233 		switch(rchild->flags & P_TYPE) {
234 		case P_BINTERNAL:
235 			h->linp[skip] = h->upper -= nbytes;
236 			dest = (char *)h + h->linp[skip];
237 			memmove(dest, bi, nbytes);
238 			((BINTERNAL *)dest)->pgno = rchild->pgno;
239 			break;
240 		case P_BLEAF:
241 			h->linp[skip] = h->upper -= nbytes;
242 			dest = (char *)h + h->linp[skip];
243 			WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
244 			    rchild->pgno, bl->flags & P_BIGKEY);
245 			memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
246 			if (bl->flags & P_BIGKEY &&
247 			    bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
248 				goto err1;
249 			break;
250 		case P_RINTERNAL:
251 			/*
252 			 * Update the left page count.  If split
253 			 * added at index 0, fix the correct page.
254 			 */
255 			if (skip > 0)
256 				dest = (char *)h + h->linp[skip - 1];
257 			else
258 				dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
259 			((RINTERNAL *)dest)->nrecs = rec_total(lchild);
260 			((RINTERNAL *)dest)->pgno = lchild->pgno;
261 
262 			/* Update the right page count. */
263 			h->linp[skip] = h->upper -= nbytes;
264 			dest = (char *)h + h->linp[skip];
265 			((RINTERNAL *)dest)->nrecs = rec_total(rchild);
266 			((RINTERNAL *)dest)->pgno = rchild->pgno;
267 			break;
268 		case P_RLEAF:
269 			/*
270 			 * Update the left page count.  If split
271 			 * added at index 0, fix the correct page.
272 			 */
273 			if (skip > 0)
274 				dest = (char *)h + h->linp[skip - 1];
275 			else
276 				dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
277 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild);
278 			((RINTERNAL *)dest)->pgno = lchild->pgno;
279 
280 			/* Update the right page count. */
281 			h->linp[skip] = h->upper -= nbytes;
282 			dest = (char *)h + h->linp[skip];
283 			((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild);
284 			((RINTERNAL *)dest)->pgno = rchild->pgno;
285 			break;
286 		default:
287 			abort();
288 		}
289 
290 		/* Unpin the held pages. */
291 		if (!parentsplit) {
292 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
293 			break;
294 		}
295 
296 		/* If the root page was split, make it look right. */
297 		if (sp->pgno == P_ROOT &&
298 		    (ISSET(t, R_RECNO) ?
299 		    bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
300 			goto err1;
301 
302 		mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
303 		mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
304 	}
305 
306 	/* Unpin the held pages. */
307 	mpool_put(t->bt_mp, l, MPOOL_DIRTY);
308 	mpool_put(t->bt_mp, r, MPOOL_DIRTY);
309 
310 	/* Clear any pages left on the stack. */
311 	return (RET_SUCCESS);
312 
313 	/*
314 	 * If something fails in the above loop we were already walking back
315 	 * up the tree and the tree is now inconsistent.  Nothing much we can
316 	 * do about it but release any memory we're holding.
317 	 */
318 err1:	mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
319 	mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
320 
321 err2:	mpool_put(t->bt_mp, l, 0);
322 	mpool_put(t->bt_mp, r, 0);
323 	__dbpanic(t->bt_dbp);
324 	return (RET_ERROR);
325 }
326 
327 /*
328  * BT_PAGE -- Split a non-root page of a btree.
329  *
330  * Parameters:
331  *	t:	tree
332  *	h:	root page
333  *	lp:	pointer to left page pointer
334  *	rp:	pointer to right page pointer
335  *	skip:	pointer to index to leave open
336  *	ilen:	insert length
337  *
338  * Returns:
339  *	Pointer to page in which to insert or NULL on error.
340  */
341 static PAGE *
342 bt_page(t, h, lp, rp, skip, ilen)
343 	BTREE *t;
344 	PAGE *h, **lp, **rp;
345 	indx_t *skip;
346 	size_t ilen;
347 {
348 	PAGE *l, *r, *tp;
349 	pgno_t npg;
350 
351 #ifdef STATISTICS
352 	++bt_split;
353 #endif
354 	/* Put the new right page for the split into place. */
355 	if ((r = __bt_new(t, &npg)) == NULL)
356 		return (NULL);
357 	r->pgno = npg;
358 	r->lower = BTDATAOFF;
359 	r->upper = t->bt_psize;
360 	r->nextpg = h->nextpg;
361 	r->prevpg = h->pgno;
362 	r->flags = h->flags & P_TYPE;
363 
364 	/*
365 	 * If we're splitting the last page on a level because we're appending
366 	 * a key to it (skip is NEXTINDEX()), it's likely that the data is
367 	 * sorted.  Adding an empty page on the side of the level is less work
368 	 * and can push the fill factor much higher than normal.  If we're
369 	 * wrong it's no big deal, we'll just do the split the right way next
370 	 * time.  It may look like it's equally easy to do a similar hack for
371 	 * reverse sorted data, that is, split the tree left, but it's not.
372 	 * Don't even try.
373 	 */
374 	if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
375 #ifdef STATISTICS
376 		++bt_sortsplit;
377 #endif
378 		h->nextpg = r->pgno;
379 		r->lower = BTDATAOFF + sizeof(indx_t);
380 		*skip = 0;
381 		*lp = h;
382 		*rp = r;
383 		return (r);
384 	}
385 
386 	/* Put the new left page for the split into place. */
387 	if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) {
388 		mpool_put(t->bt_mp, r, 0);
389 		return (NULL);
390 	}
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, 0);
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 	size_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 	RLEAF *rl;
617 	EPGNO *c;
618 	PAGE *rval;
619 	void *src;
620 	indx_t full, half, nxt, off, skip, top, used;
621 	size_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 >= full) {
672 			--off;
673 			break;
674 		}
675 
676 		/* Copy the key/data pair, if not the skipped index. */
677 		if (skip != off) {
678 			++nxt;
679 
680 			l->linp[off] = l->upper -= nbytes;
681 			memmove((char *)l + l->upper, src, nbytes);
682 		}
683 
684 		used += nbytes;
685 		if (used >= half) {
686 			if (!isbigkey || bigkeycnt == 3)
687 				break;
688 			else
689 				++bigkeycnt;
690 		}
691 	}
692 
693 	/*
694 	 * Off is the last offset that's valid for the left page.
695 	 * Nxt is the first offset to be placed on the right page.
696 	 */
697 	l->lower += (off + 1) * sizeof(indx_t);
698 
699 	/*
700 	 * If splitting the page that the cursor was on, the cursor has to be
701 	 * adjusted to point to the same record as before the split.  If the
702 	 * cursor is at or past the skipped slot, the cursor is incremented by
703 	 * one.  If the cursor is on the right page, it is decremented by the
704 	 * number of records split to the left page.
705 	 *
706 	 * Don't bother checking for the B_SEQINIT flag, the page number will
707 	 * be P_INVALID.
708 	 */
709 	c = &t->bt_bcursor;
710 	if (c->pgno == h->pgno) {
711 		if (c->index >= skip)
712 			++c->index;
713 		if (c->index < nxt)			/* Left page. */
714 			c->pgno = l->pgno;
715 		else {					/* Right page. */
716 			c->pgno = r->pgno;
717 			c->index -= nxt;
718 		}
719 	}
720 
721 	/*
722 	 * If the skipped index was on the left page, just return that page.
723 	 * Otherwise, adjust the skip index to reflect the new position on
724 	 * the right page.
725 	 */
726 	if (skip <= off) {
727 		skip = 0;
728 		rval = l;
729 	} else {
730 		rval = r;
731 		*pskip -= nxt;
732 	}
733 
734 	for (off = 0; nxt < top; ++off) {
735 		if (skip == nxt) {
736 			++off;
737 			skip = 0;
738 		}
739 		switch (h->flags & P_TYPE) {
740 		case P_BINTERNAL:
741 			src = bi = GETBINTERNAL(h, nxt);
742 			nbytes = NBINTERNAL(bi->ksize);
743 			break;
744 		case P_BLEAF:
745 			src = bl = GETBLEAF(h, nxt);
746 			nbytes = NBLEAF(bl);
747 			break;
748 		case P_RINTERNAL:
749 			src = GETRINTERNAL(h, nxt);
750 			nbytes = NRINTERNAL;
751 			break;
752 		case P_RLEAF:
753 			src = rl = GETRLEAF(h, nxt);
754 			nbytes = NRLEAF(rl);
755 			break;
756 		default:
757 			abort();
758 		}
759 		++nxt;
760 		r->linp[off] = r->upper -= nbytes;
761 		memmove((char *)r + r->upper, src, nbytes);
762 	}
763 	r->lower += off * sizeof(indx_t);
764 
765 	/* If the key is being appended to the page, adjust the index. */
766 	if (skip == top)
767 		r->lower += sizeof(indx_t);
768 
769 	return (rval);
770 }
771 
772 /*
773  * BT_PRESERVE -- Mark a chain of pages as used by an internal node.
774  *
775  * Chains of indirect blocks pointed to by leaf nodes get reclaimed when the
776  * record that references them gets deleted.  Chains pointed to by internal
777  * pages never get deleted.  This routine marks a chain as pointed to by an
778  * internal page.
779  *
780  * Parameters:
781  *	t:	tree
782  *	pg:	page number of first page in the chain.
783  *
784  * Returns:
785  *	RET_SUCCESS, RET_ERROR.
786  */
787 static int
788 bt_preserve(t, pg)
789 	BTREE *t;
790 	pgno_t pg;
791 {
792 	PAGE *h;
793 
794 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
795 		return (RET_ERROR);
796 	h->flags |= P_PRESERVE;
797 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
798 	return (RET_SUCCESS);
799 }
800 
801 /*
802  * REC_TOTAL -- Return the number of recno entries below a page.
803  *
804  * Parameters:
805  *	h:	page
806  *
807  * Returns:
808  *	The number of recno entries below a page.
809  *
810  * XXX
811  * These values could be set by the bt_psplit routine.  The problem is that the
812  * entry has to be popped off of the stack etc. or the values have to be passed
813  * all the way back to bt_split/bt_rroot and it's not very clean.
814  */
815 static recno_t
816 rec_total(h)
817 	PAGE *h;
818 {
819 	recno_t recs;
820 	indx_t nxt, top;
821 
822 	for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
823 		recs += GETRINTERNAL(h, nxt)->nrecs;
824 	return (recs);
825 }
826