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