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