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