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