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