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