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