1 /* $OpenBSD: bt_split.c,v 1.12 2005/03/23 19:34:58 otto Exp $ */ 2 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 #if 0 37 static char sccsid[] = "@(#)bt_split.c 8.10 (Berkeley) 1/9/95"; 38 #else 39 static const char rcsid[] = "$OpenBSD: bt_split.c,v 1.12 2005/03/23 19:34:58 otto Exp $"; 40 #endif 41 #endif /* LIBC_SCCS and not lint */ 42 43 #include <sys/types.h> 44 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(BTREE *, PAGE *, PAGE *, PAGE *); 54 static PAGE *bt_page(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t); 55 static int bt_preserve(BTREE *, pgno_t); 56 static PAGE *bt_psplit(BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t); 57 static PAGE *bt_root(BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t); 58 static int bt_rroot(BTREE *, PAGE *, PAGE *, PAGE *); 59 static recno_t rec_total(PAGE *); 60 61 #ifdef STATISTICS 62 u_long bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved; 63 #endif 64 65 /* 66 * __BT_SPLIT -- Split the tree. 67 * 68 * Parameters: 69 * t: tree 70 * sp: page to split 71 * key: key to insert 72 * data: data to insert 73 * flags: BIGKEY/BIGDATA flags 74 * ilen: insert length 75 * skip: index to leave open 76 * 77 * Returns: 78 * RET_ERROR, RET_SUCCESS 79 */ 80 int 81 __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags, 82 size_t ilen, u_int32_t argskip) 83 { 84 BINTERNAL *bi; 85 BLEAF *bl, *tbl; 86 DBT a, b; 87 EPGNO *parent; 88 PAGE *h, *l, *r, *lchild, *rchild; 89 indx_t nxtindex; 90 u_int16_t skip; 91 u_int32_t n, nbytes, nksize; 92 int parentsplit; 93 char *dest; 94 95 /* 96 * Split the page into two pages, l and r. The split routines return 97 * a pointer to the page into which the key should be inserted and with 98 * skip set to the offset which should be used. Additionally, l and r 99 * are pinned. 100 */ 101 skip = argskip; 102 h = sp->pgno == P_ROOT ? 103 bt_root(t, sp, &l, &r, &skip, ilen) : 104 bt_page(t, sp, &l, &r, &skip, ilen); 105 if (h == NULL) 106 return (RET_ERROR); 107 108 /* 109 * Insert the new key/data pair into the leaf page. (Key inserts 110 * always cause a leaf page to split first.) 111 */ 112 h->linp[skip] = h->upper -= ilen; 113 dest = (char *)h + h->upper; 114 if (F_ISSET(t, R_RECNO)) 115 WR_RLEAF(dest, data, flags) 116 else 117 WR_BLEAF(dest, key, data, flags) 118 119 /* If the root page was split, make it look right. */ 120 if (sp->pgno == P_ROOT && 121 (F_ISSET(t, R_RECNO) ? 122 bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR) 123 goto err2; 124 125 /* 126 * Now we walk the parent page stack -- a LIFO stack of the pages that 127 * were traversed when we searched for the page that split. Each stack 128 * entry is a page number and a page index offset. The offset is for 129 * the page traversed on the search. We've just split a page, so we 130 * have to insert a new key into the parent page. 131 * 132 * If the insert into the parent page causes it to split, may have to 133 * continue splitting all the way up the tree. We stop if the root 134 * splits or the page inserted into didn't have to split to hold the 135 * new key. Some algorithms replace the key for the old page as well 136 * as the new page. We don't, as there's no reason to believe that the 137 * first key on the old page is any better than the key we have, and, 138 * in the case of a key being placed at index 0 causing the split, the 139 * key is unavailable. 140 * 141 * There are a maximum of 5 pages pinned at any time. We keep the left 142 * and right pages pinned while working on the parent. The 5 are the 143 * two children, left parent and right parent (when the parent splits) 144 * and the root page or the overflow key page when calling bt_preserve. 145 * This code must make sure that all pins are released other than the 146 * root page or overflow page which is unlocked elsewhere. 147 */ 148 while ((parent = BT_POP(t)) != NULL) { 149 lchild = l; 150 rchild = r; 151 152 /* Get the parent page. */ 153 if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) 154 goto err2; 155 156 /* 157 * The new key goes ONE AFTER the index, because the split 158 * was to the right. 159 */ 160 skip = parent->index + 1; 161 162 /* 163 * Calculate the space needed on the parent page. 164 * 165 * Prefix trees: space hack when inserting into BINTERNAL 166 * pages. Retain only what's needed to distinguish between 167 * the new entry and the LAST entry on the page to its left. 168 * If the keys compare equal, retain the entire key. Note, 169 * we don't touch overflow keys, and the entire key must be 170 * retained for the next-to-left most key on the leftmost 171 * page of each level, or the search will fail. Applicable 172 * ONLY to internal pages that have leaf pages as children. 173 * Further reduction of the key between pairs of internal 174 * pages loses too much information. 175 */ 176 switch (rchild->flags & P_TYPE) { 177 case P_BINTERNAL: 178 bi = GETBINTERNAL(rchild, 0); 179 nbytes = NBINTERNAL(bi->ksize); 180 break; 181 case P_BLEAF: 182 bl = GETBLEAF(rchild, 0); 183 nbytes = NBINTERNAL(bl->ksize); 184 if (t->bt_pfx && !(bl->flags & P_BIGKEY) && 185 (h->prevpg != P_INVALID || skip > 1)) { 186 tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1); 187 a.size = tbl->ksize; 188 a.data = tbl->bytes; 189 b.size = bl->ksize; 190 b.data = bl->bytes; 191 nksize = t->bt_pfx(&a, &b); 192 n = NBINTERNAL(nksize); 193 if (n < nbytes) { 194 #ifdef STATISTICS 195 bt_pfxsaved += nbytes - n; 196 #endif 197 nbytes = n; 198 } else 199 nksize = 0; 200 } else 201 nksize = 0; 202 break; 203 case P_RINTERNAL: 204 case P_RLEAF: 205 nbytes = NRINTERNAL; 206 break; 207 default: 208 abort(); 209 } 210 211 /* Split the parent page if necessary or shift the indices. */ 212 if (h->upper - h->lower < nbytes + sizeof(indx_t)) { 213 sp = h; 214 h = h->pgno == P_ROOT ? 215 bt_root(t, h, &l, &r, &skip, nbytes) : 216 bt_page(t, h, &l, &r, &skip, nbytes); 217 if (h == NULL) 218 goto err1; 219 parentsplit = 1; 220 } else { 221 if (skip < (nxtindex = NEXTINDEX(h))) 222 memmove(h->linp + skip + 1, h->linp + skip, 223 (nxtindex - skip) * sizeof(indx_t)); 224 h->lower += sizeof(indx_t); 225 parentsplit = 0; 226 } 227 228 /* Insert the key into the parent page. */ 229 switch (rchild->flags & P_TYPE) { 230 case P_BINTERNAL: 231 h->linp[skip] = h->upper -= nbytes; 232 dest = (char *)h + h->linp[skip]; 233 memmove(dest, bi, nbytes); 234 ((BINTERNAL *)dest)->pgno = rchild->pgno; 235 break; 236 case P_BLEAF: 237 h->linp[skip] = h->upper -= nbytes; 238 dest = (char *)h + h->linp[skip]; 239 WR_BINTERNAL(dest, nksize ? nksize : bl->ksize, 240 rchild->pgno, bl->flags & P_BIGKEY); 241 memmove(dest, bl->bytes, nksize ? nksize : bl->ksize); 242 if (bl->flags & P_BIGKEY && 243 bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) 244 goto err1; 245 break; 246 case P_RINTERNAL: 247 /* 248 * Update the left page count. If split 249 * added at index 0, fix the correct page. 250 */ 251 if (skip > 0) 252 dest = (char *)h + h->linp[skip - 1]; 253 else 254 dest = (char *)l + l->linp[NEXTINDEX(l) - 1]; 255 ((RINTERNAL *)dest)->nrecs = rec_total(lchild); 256 ((RINTERNAL *)dest)->pgno = lchild->pgno; 257 258 /* Update the right page count. */ 259 h->linp[skip] = h->upper -= nbytes; 260 dest = (char *)h + h->linp[skip]; 261 ((RINTERNAL *)dest)->nrecs = rec_total(rchild); 262 ((RINTERNAL *)dest)->pgno = rchild->pgno; 263 break; 264 case P_RLEAF: 265 /* 266 * Update the left page count. If split 267 * added at index 0, fix the correct page. 268 */ 269 if (skip > 0) 270 dest = (char *)h + h->linp[skip - 1]; 271 else 272 dest = (char *)l + l->linp[NEXTINDEX(l) - 1]; 273 ((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild); 274 ((RINTERNAL *)dest)->pgno = lchild->pgno; 275 276 /* Update the right page count. */ 277 h->linp[skip] = h->upper -= nbytes; 278 dest = (char *)h + h->linp[skip]; 279 ((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild); 280 ((RINTERNAL *)dest)->pgno = rchild->pgno; 281 break; 282 default: 283 abort(); 284 } 285 286 /* Unpin the held pages. */ 287 if (!parentsplit) { 288 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 289 break; 290 } 291 292 /* If the root page was split, make it look right. */ 293 if (sp->pgno == P_ROOT && 294 (F_ISSET(t, R_RECNO) ? 295 bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR) 296 goto err1; 297 298 mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); 299 mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); 300 } 301 302 /* Unpin the held pages. */ 303 mpool_put(t->bt_mp, l, MPOOL_DIRTY); 304 mpool_put(t->bt_mp, r, MPOOL_DIRTY); 305 306 /* Clear any pages left on the stack. */ 307 return (RET_SUCCESS); 308 309 /* 310 * If something fails in the above loop we were already walking back 311 * up the tree and the tree is now inconsistent. Nothing much we can 312 * do about it but release any memory we're holding. 313 */ 314 err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); 315 mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); 316 317 err2: mpool_put(t->bt_mp, l, 0); 318 mpool_put(t->bt_mp, r, 0); 319 __dbpanic(t->bt_dbp); 320 return (RET_ERROR); 321 } 322 323 /* 324 * BT_PAGE -- Split a non-root page of a btree. 325 * 326 * Parameters: 327 * t: tree 328 * h: root page 329 * lp: pointer to left page pointer 330 * rp: pointer to right page pointer 331 * skip: pointer to index to leave open 332 * ilen: insert length 333 * 334 * Returns: 335 * Pointer to page in which to insert or NULL on error. 336 */ 337 static PAGE * 338 bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen) 339 { 340 PAGE *l, *r, *tp; 341 pgno_t npg; 342 343 #ifdef STATISTICS 344 ++bt_split; 345 #endif 346 /* Put the new right page for the split into place. */ 347 if ((r = __bt_new(t, &npg)) == NULL) 348 return (NULL); 349 r->pgno = npg; 350 r->lower = BTDATAOFF; 351 r->upper = t->bt_psize; 352 r->nextpg = h->nextpg; 353 r->prevpg = h->pgno; 354 r->flags = h->flags & P_TYPE; 355 356 /* 357 * If we're splitting the last page on a level because we're appending 358 * a key to it (skip is NEXTINDEX()), it's likely that the data is 359 * sorted. Adding an empty page on the side of the level is less work 360 * and can push the fill factor much higher than normal. If we're 361 * wrong it's no big deal, we'll just do the split the right way next 362 * time. It may look like it's equally easy to do a similar hack for 363 * reverse sorted data, that is, split the tree left, but it's not. 364 * Don't even try. 365 */ 366 if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) { 367 #ifdef STATISTICS 368 ++bt_sortsplit; 369 #endif 370 h->nextpg = r->pgno; 371 r->lower = BTDATAOFF + sizeof(indx_t); 372 *skip = 0; 373 *lp = h; 374 *rp = r; 375 return (r); 376 } 377 378 /* Put the new left page for the split into place. */ 379 if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) { 380 mpool_put(t->bt_mp, r, 0); 381 return (NULL); 382 } 383 memset(l, 0xff, t->bt_psize); 384 l->pgno = h->pgno; 385 l->nextpg = r->pgno; 386 l->prevpg = h->prevpg; 387 l->lower = BTDATAOFF; 388 l->upper = t->bt_psize; 389 l->flags = h->flags & P_TYPE; 390 391 /* Fix up the previous pointer of the page after the split page. */ 392 if (h->nextpg != P_INVALID) { 393 if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) { 394 free(l); 395 /* XXX mpool_free(t->bt_mp, r->pgno); */ 396 return (NULL); 397 } 398 tp->prevpg = r->pgno; 399 mpool_put(t->bt_mp, tp, MPOOL_DIRTY); 400 } 401 402 /* 403 * Split right. The key/data pairs aren't sorted in the btree page so 404 * it's simpler to copy the data from the split page onto two new pages 405 * instead of copying half the data to the right page and compacting 406 * the left page in place. Since the left page can't change, we have 407 * to swap the original and the allocated left page after the split. 408 */ 409 tp = bt_psplit(t, h, l, r, skip, ilen); 410 411 /* Move the new left page onto the old left page. */ 412 memmove(h, l, t->bt_psize); 413 if (tp == l) 414 tp = h; 415 free(l); 416 417 *lp = h; 418 *rp = r; 419 return (tp); 420 } 421 422 /* 423 * BT_ROOT -- Split the root page of a btree. 424 * 425 * Parameters: 426 * t: tree 427 * h: root page 428 * lp: pointer to left page pointer 429 * rp: pointer to right page pointer 430 * skip: pointer to index to leave open 431 * ilen: insert length 432 * 433 * Returns: 434 * Pointer to page in which to insert or NULL on error. 435 */ 436 static PAGE * 437 bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen) 438 { 439 PAGE *l, *r, *tp; 440 pgno_t lnpg, rnpg; 441 442 #ifdef STATISTICS 443 ++bt_split; 444 ++bt_rootsplit; 445 #endif 446 /* Put the new left and right pages for the split into place. */ 447 if ((l = __bt_new(t, &lnpg)) == NULL || 448 (r = __bt_new(t, &rnpg)) == NULL) 449 return (NULL); 450 l->pgno = lnpg; 451 r->pgno = rnpg; 452 l->nextpg = r->pgno; 453 r->prevpg = l->pgno; 454 l->prevpg = r->nextpg = P_INVALID; 455 l->lower = r->lower = BTDATAOFF; 456 l->upper = r->upper = t->bt_psize; 457 l->flags = r->flags = h->flags & P_TYPE; 458 459 /* Split the root page. */ 460 tp = bt_psplit(t, h, l, r, skip, ilen); 461 462 *lp = l; 463 *rp = r; 464 return (tp); 465 } 466 467 /* 468 * BT_RROOT -- Fix up the recno root page after it has been split. 469 * 470 * Parameters: 471 * t: tree 472 * h: root page 473 * l: left page 474 * r: right page 475 * 476 * Returns: 477 * RET_ERROR, RET_SUCCESS 478 */ 479 static int 480 bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r) 481 { 482 char *dest; 483 484 /* Insert the left and right keys, set the header information. */ 485 h->linp[0] = h->upper = t->bt_psize - NRINTERNAL; 486 dest = (char *)h + h->upper; 487 WR_RINTERNAL(dest, 488 l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno); 489 490 h->linp[1] = h->upper -= NRINTERNAL; 491 dest = (char *)h + h->upper; 492 WR_RINTERNAL(dest, 493 r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno); 494 495 h->lower = BTDATAOFF + 2 * sizeof(indx_t); 496 497 /* Unpin the root page, set to recno internal page. */ 498 h->flags &= ~P_TYPE; 499 h->flags |= P_RINTERNAL; 500 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 501 502 return (RET_SUCCESS); 503 } 504 505 /* 506 * BT_BROOT -- Fix up the btree root page after it has been split. 507 * 508 * Parameters: 509 * t: tree 510 * h: root page 511 * l: left page 512 * r: right page 513 * 514 * Returns: 515 * RET_ERROR, RET_SUCCESS 516 */ 517 static int 518 bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r) 519 { 520 BINTERNAL *bi; 521 BLEAF *bl; 522 u_int32_t nbytes; 523 char *dest; 524 525 /* 526 * If the root page was a leaf page, change it into an internal page. 527 * We copy the key we split on (but not the key's data, in the case of 528 * a leaf page) to the new root page. 529 * 530 * The btree comparison code guarantees that the left-most key on any 531 * level of the tree is never used, so it doesn't need to be filled in. 532 */ 533 nbytes = NBINTERNAL(0); 534 h->linp[0] = h->upper = t->bt_psize - nbytes; 535 dest = (char *)h + h->upper; 536 WR_BINTERNAL(dest, 0, l->pgno, 0); 537 538 switch (h->flags & P_TYPE) { 539 case P_BLEAF: 540 bl = GETBLEAF(r, 0); 541 nbytes = NBINTERNAL(bl->ksize); 542 h->linp[1] = h->upper -= nbytes; 543 dest = (char *)h + h->upper; 544 WR_BINTERNAL(dest, bl->ksize, r->pgno, 0); 545 memmove(dest, bl->bytes, bl->ksize); 546 547 /* 548 * If the key is on an overflow page, mark the overflow chain 549 * so it isn't deleted when the leaf copy of the key is deleted. 550 */ 551 if (bl->flags & P_BIGKEY && 552 bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) 553 return (RET_ERROR); 554 break; 555 case P_BINTERNAL: 556 bi = GETBINTERNAL(r, 0); 557 nbytes = NBINTERNAL(bi->ksize); 558 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