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