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