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