| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 38 |
static char sccsid[] = "@(#)bt_split.c 8.9 (Berkeley) 7/26/94"; |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
|
| 43 |
#include <limits.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
|
| 48 |
#include "../include/db.h" |
| 49 |
#include "btree.h" |
| 50 |
|
| 51 |
static int bt_broot __P((BTREE *, PAGE *, PAGE *, PAGE *)); |
| 52 |
static PAGE *bt_page |
| 53 |
__P((BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t)); |
| 54 |
static int bt_preserve __P((BTREE *, pgno_t)); |
| 55 |
static PAGE *bt_psplit |
| 56 |
__P((BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t)); |
| 57 |
static PAGE *bt_root |
| 58 |
__P((BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t)); |
| 59 |
static int bt_rroot __P((BTREE *, PAGE *, PAGE *, PAGE *)); |
| 60 |
static recno_t rec_total __P((PAGE *)); |
| 61 |
|
| 62 |
#ifdef STATISTICS |
| 63 |
u_long bt_rootsplit, bt_split, bt_sortsplit, bt_pfxsaved; |
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
int |
| 82 |
__bt_split(t, sp, key, data, flags, ilen, argskip) |
| 83 |
BTREE *t; |
| 84 |
PAGE *sp; |
| 85 |
const DBT *key, *data; |
| 86 |
int flags; |
| 87 |
size_t ilen; |
| 88 |
u_int32_t argskip; |
| 89 |
{ |
| 90 |
BINTERNAL *bi = 0; |
| 91 |
BLEAF *bl = 0, *tbl; |
| 92 |
DBT a, b; |
| 93 |
EPGNO *parent; |
| 94 |
PAGE *h, *l, *r, *lchild, *rchild; |
| 95 |
indx_t nxtindex; |
| 96 |
u_int16_t skip; |
| 97 |
u_int32_t n, nbytes, nksize = 0; |
| 98 |
int parentsplit; |
| 99 |
char *dest; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
skip = argskip; |
| 108 |
h = sp->pgno == P_ROOT ? |
| 109 |
bt_root(t, sp, &l, &r, &skip, ilen) : |
| 110 |
bt_page(t, sp, &l, &r, &skip, ilen); |
| 111 |
if (h == NULL) |
| 112 |
return (RET_ERROR); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
h->linp[skip] = h->upper -= ilen; |
| 119 |
dest = (char *)h + h->upper; |
| 120 |
if (F_ISSET(t, R_RECNO)) |
| 121 |
WR_RLEAF(dest, data, flags) |
| 122 |
else |
| 123 |
WR_BLEAF(dest, key, data, flags) |
| 124 |
|
| 125 |
|
| 126 |
if (sp->pgno == P_ROOT && |
| 127 |
(F_ISSET(t, R_RECNO) ? |
| 128 |
bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR) |
| 129 |
goto err2; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
while ((parent = BT_POP(t)) != NULL) { |
| 155 |
lchild = l; |
| 156 |
rchild = r; |
| 157 |
|
| 158 |
|
| 159 |
if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL) |
| 160 |
goto err2; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
skip = parent->index + 1; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
switch (rchild->flags & P_TYPE) { |
| 183 |
case P_BINTERNAL: |
| 184 |
bi = GETBINTERNAL(rchild, 0); |
| 185 |
nbytes = NBINTERNAL(bi->ksize); |
| 186 |
break; |
| 187 |
case P_BLEAF: |
| 188 |
bl = GETBLEAF(rchild, 0); |
| 189 |
nbytes = NBINTERNAL(bl->ksize); |
| 190 |
if (t->bt_pfx && !(bl->flags & P_BIGKEY) && |
| 191 |
(h->prevpg != P_INVALID || skip > 1)) { |
| 192 |
tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1); |
| 193 |
a.size = tbl->ksize; |
| 194 |
a.data = tbl->bytes; |
| 195 |
b.size = bl->ksize; |
| 196 |
b.data = bl->bytes; |
| 197 |
nksize = t->bt_pfx(&a, &b); |
| 198 |
n = NBINTERNAL(nksize); |
| 199 |
if (n < nbytes) { |
| 200 |
#ifdef STATISTICS |
| 201 |
bt_pfxsaved += nbytes - n; |
| 202 |
#endif |
| 203 |
nbytes = n; |
| 204 |
} else |
| 205 |
nksize = 0; |
| 206 |
} else |
| 207 |
nksize = 0; |
| 208 |
break; |
| 209 |
case P_RINTERNAL: |
| 210 |
case P_RLEAF: |
| 211 |
nbytes = NRINTERNAL; |
| 212 |
break; |
| 213 |
default: |
| 214 |
abort(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
if ((u_int32_t) (h->upper - h->lower) |
| 219 |
< nbytes + sizeof(indx_t)) { |
| 220 |
sp = h; |
| 221 |
h = h->pgno == P_ROOT ? |
| 222 |
bt_root(t, h, &l, &r, &skip, nbytes) : |
| 223 |
bt_page(t, h, &l, &r, &skip, nbytes); |
| 224 |
if (h == NULL) |
| 225 |
goto err1; |
| 226 |
parentsplit = 1; |
| 227 |
} else { |
| 228 |
if (skip < (nxtindex = NEXTINDEX(h))) |
| 229 |
memmove(h->linp + skip + 1, h->linp + skip, |
| 230 |
(nxtindex - skip) * sizeof(indx_t)); |
| 231 |
h->lower += sizeof(indx_t); |
| 232 |
parentsplit = 0; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
switch (rchild->flags & P_TYPE) { |
| 237 |
case P_BINTERNAL: |
| 238 |
h->linp[skip] = h->upper -= nbytes; |
| 239 |
dest = (char *)h + h->linp[skip]; |
| 240 |
memmove(dest, bi, nbytes); |
| 241 |
((BINTERNAL *)dest)->pgno = rchild->pgno; |
| 242 |
break; |
| 243 |
case P_BLEAF: |
| 244 |
h->linp[skip] = h->upper -= nbytes; |
| 245 |
dest = (char *)h + h->linp[skip]; |
| 246 |
WR_BINTERNAL(dest, nksize ? nksize : bl->ksize, |
| 247 |
rchild->pgno, bl->flags & P_BIGKEY); |
| 248 |
memmove(dest, bl->bytes, nksize ? nksize : bl->ksize); |
| 249 |
if (bl->flags & P_BIGKEY && |
| 250 |
bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) |
| 251 |
goto err1; |
| 252 |
break; |
| 253 |
case P_RINTERNAL: |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if (skip > 0) |
| 259 |
dest = (char *)h + h->linp[skip - 1]; |
| 260 |
else |
| 261 |
dest = (char *)l + l->linp[NEXTINDEX(l) - 1]; |
| 262 |
((RINTERNAL *)dest)->nrecs = rec_total(lchild); |
| 263 |
((RINTERNAL *)dest)->pgno = lchild->pgno; |
| 264 |
|
| 265 |
|
| 266 |
h->linp[skip] = h->upper -= nbytes; |
| 267 |
dest = (char *)h + h->linp[skip]; |
| 268 |
((RINTERNAL *)dest)->nrecs = rec_total(rchild); |
| 269 |
((RINTERNAL *)dest)->pgno = rchild->pgno; |
| 270 |
break; |
| 271 |
case P_RLEAF: |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if (skip > 0) |
| 277 |
dest = (char *)h + h->linp[skip - 1]; |
| 278 |
else |
| 279 |
dest = (char *)l + l->linp[NEXTINDEX(l) - 1]; |
| 280 |
((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild); |
| 281 |
((RINTERNAL *)dest)->pgno = lchild->pgno; |
| 282 |
|
| 283 |
|
| 284 |
h->linp[skip] = h->upper -= nbytes; |
| 285 |
dest = (char *)h + h->linp[skip]; |
| 286 |
((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild); |
| 287 |
((RINTERNAL *)dest)->pgno = rchild->pgno; |
| 288 |
break; |
| 289 |
default: |
| 290 |
abort(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
if (!parentsplit) { |
| 295 |
mpool_put(t->bt_mp, h, MPOOL_DIRTY); |
| 296 |
break; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
if (sp->pgno == P_ROOT && |
| 301 |
(F_ISSET(t, R_RECNO) ? |
| 302 |
bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR) |
| 303 |
goto err1; |
| 304 |
|
| 305 |
mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); |
| 306 |
mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
mpool_put(t->bt_mp, l, MPOOL_DIRTY); |
| 311 |
mpool_put(t->bt_mp, r, MPOOL_DIRTY); |
| 312 |
|
| 313 |
|
| 314 |
return (RET_SUCCESS); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY); |
| 322 |
mpool_put(t->bt_mp, rchild, MPOOL_DIRTY); |
| 323 |
|
| 324 |
err2: mpool_put(t->bt_mp, l, 0); |
| 325 |
mpool_put(t->bt_mp, r, 0); |
| 326 |
__dbpanic(t->bt_dbp); |
| 327 |
return (RET_ERROR); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
static PAGE * |
| 345 |
bt_page(t, h, lp, rp, skip, ilen) |
| 346 |
BTREE *t; |
| 347 |
PAGE *h, **lp, **rp; |
| 348 |
indx_t *skip; |
| 349 |
size_t ilen; |
| 350 |
{ |
| 351 |
PAGE *l, *r, *tp; |
| 352 |
pgno_t npg; |
| 353 |
|
| 354 |
#ifdef STATISTICS |
| 355 |
++bt_split; |
| 356 |
#endif |
| 357 |
|
| 358 |
if ((r = __bt_new(t, &npg)) == NULL) |
| 359 |
return (NULL); |
| 360 |
r->pgno = npg; |
| 361 |
r->lower = BTDATAOFF; |
| 362 |
r->upper = t->bt_psize; |
| 363 |
r->nextpg = h->nextpg; |
| 364 |
r->prevpg = h->pgno; |
| 365 |
r->flags = h->flags & P_TYPE; |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) { |
| 378 |
#ifdef STATISTICS |
| 379 |
++bt_sortsplit; |
| 380 |
#endif |
| 381 |
h->nextpg = r->pgno; |
| 382 |
r->lower = BTDATAOFF + sizeof(indx_t); |
| 383 |
*skip = 0; |
| 384 |
*lp = h; |
| 385 |
*rp = r; |
| 386 |
return (r); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) { |
| 391 |
mpool_put(t->bt_mp, r, 0); |
| 392 |
return (NULL); |
| 393 |
} |
| 394 |
#ifdef PURIFY |
| 395 |
memset(l, 0xff, t->bt_psize); |
| 396 |
#endif |
| 397 |
l->pgno = h->pgno; |
| 398 |
l->nextpg = r->pgno; |
| 399 |
l->prevpg = h->prevpg; |
| 400 |
l->lower = BTDATAOFF; |
| 401 |
l->upper = t->bt_psize; |
| 402 |
l->flags = h->flags & P_TYPE; |
| 403 |
|
| 404 |
|
| 405 |
if (h->nextpg != P_INVALID) { |
| 406 |
if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) { |
| 407 |
free(l); |
| 408 |
|
| 409 |
return (NULL); |
| 410 |
} |
| 411 |
tp->prevpg = r->pgno; |
| 412 |
mpool_put(t->bt_mp, tp, MPOOL_DIRTY); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
tp = bt_psplit(t, h, l, r, skip, ilen); |
| 423 |
|
| 424 |
|
| 425 |
memmove(h, l, t->bt_psize); |
| 426 |
if (tp == l) |
| 427 |
tp = h; |
| 428 |
free(l); |
| 429 |
|
| 430 |
*lp = h; |
| 431 |
*rp = r; |
| 432 |
return (tp); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
static PAGE * |
| 450 |
bt_root(t, h, lp, rp, skip, ilen) |
| 451 |
BTREE *t; |
| 452 |
PAGE *h, **lp, **rp; |
| 453 |
indx_t *skip; |
| 454 |
size_t ilen; |
| 455 |
{ |
| 456 |
PAGE *l, *r, *tp; |
| 457 |
pgno_t lnpg, rnpg; |
| 458 |
|
| 459 |
#ifdef STATISTICS |
| 460 |
++bt_split; |
| 461 |
++bt_rootsplit; |
| 462 |
#endif |
| 463 |
|
| 464 |
if ((l = __bt_new(t, &lnpg)) == NULL || |
| 465 |
(r = __bt_new(t, &rnpg)) == NULL) |
| 466 |
return (NULL); |
| 467 |
l->pgno = lnpg; |
| 468 |
r->pgno = rnpg; |
| 469 |
l->nextpg = r->pgno; |
| 470 |
r->prevpg = l->pgno; |
| 471 |
l->prevpg = r->nextpg = P_INVALID; |
| 472 |
l->lower = r->lower = BTDATAOFF; |
| 473 |
l->upper = r->upper = t->bt_psize; |
| 474 |
l->flags = r->flags = h->flags & P_TYPE; |
| 475 |
|
| 476 |
|
| 477 |
tp = bt_psplit(t, h, l, r, skip, ilen); |
| 478 |
|
| 479 |
*lp = l; |
| 480 |
*rp = r; |
| 481 |
return (tp); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
static int |
| 497 |
bt_rroot(t, h, l, r) |
| 498 |
BTREE *t; |
| 499 |
PAGE *h, *l, *r; |
| 500 |
{ |
| 501 |
char *dest; |
| 502 |
|
| 503 |
|
| 504 |
h->linp[0] = h->upper = t->bt_psize - NRINTERNAL; |
| 505 |
dest = (char *)h + h->upper; |
| 506 |
WR_RINTERNAL(dest, |
| 507 |
l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno); |
| 508 |
|
| 509 |
h->linp[1] = h->upper -= NRINTERNAL; |
| 510 |
dest = (char *)h + h->upper; |
| 511 |
WR_RINTERNAL(dest, |
| 512 |
r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno); |
| 513 |
|
| 514 |
h->lower = BTDATAOFF + 2 * sizeof(indx_t); |
| 515 |
|
| 516 |
|
| 517 |
h->flags &= ~P_TYPE; |
| 518 |
h->flags |= P_RINTERNAL; |
| 519 |
mpool_put(t->bt_mp, h, MPOOL_DIRTY); |
| 520 |
|
| 521 |
return (RET_SUCCESS); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
static int |
| 537 |
bt_broot(t, h, l, r) |
| 538 |
BTREE *t; |
| 539 |
PAGE *h, *l, *r; |
| 540 |
{ |
| 541 |
BINTERNAL *bi; |
| 542 |
BLEAF *bl; |
| 543 |
u_int32_t nbytes; |
| 544 |
char *dest; |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
nbytes = NBINTERNAL(0); |
| 555 |
h->linp[0] = h->upper = t->bt_psize - nbytes; |
| 556 |
dest = (char *)h + h->upper; |
| 557 |
WR_BINTERNAL(dest, 0, l->pgno, 0); |
| 558 |
|
| 559 |
switch (h->flags & P_TYPE) { |
| 560 |
case P_BLEAF: |
| 561 |
bl = GETBLEAF(r, 0); |
| 562 |
nbytes = NBINTERNAL(bl->ksize); |
| 563 |
h->linp[1] = h->upper -= nbytes; |
| 564 |
dest = (char *)h + h->upper; |
| 565 |
WR_BINTERNAL(dest, bl->ksize, r->pgno, 0); |
| 566 |
memmove(dest, bl->bytes, bl->ksize); |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
if (bl->flags & P_BIGKEY && |
| 573 |
bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR) |
| 574 |
return (RET_ERROR); |
| 575 |
break; |
| 576 |
case P_BINTERNAL: |
| 577 |
bi = GETBINTERNAL(r, 0); |
| 578 |
nbytes = NBINTERNAL(bi->ksize); |
| 579 |
h->linp[1] = h->upper -= nbytes; |
| 580 |
dest = (char *)h + h->upper; |
| 581 |
memmove(dest, bi, nbytes); |
| 582 |
((BINTERNAL *)dest)->pgno = r->pgno; |
| 583 |
break; |
| 584 |
default: |
| 585 |
abort(); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
h->lower = BTDATAOFF + 2 * sizeof(indx_t); |
| 590 |
|
| 591 |
|
| 592 |
h->flags &= ~P_TYPE; |
| 593 |
h->flags |= P_BINTERNAL; |
| 594 |
mpool_put(t->bt_mp, h, MPOOL_DIRTY); |
| 595 |
|
| 596 |
return (RET_SUCCESS); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
static PAGE * |
| 614 |
bt_psplit(t, h, l, r, pskip, ilen) |
| 615 |
BTREE *t; |
| 616 |
PAGE *h, *l, *r; |
| 617 |
indx_t *pskip; |
| 618 |
size_t ilen; |
| 619 |
{ |
| 620 |
BINTERNAL *bi; |
| 621 |
BLEAF *bl; |
| 622 |
CURSOR *c; |
| 623 |
RLEAF *rl; |
| 624 |
PAGE *rval; |
| 625 |
void *src = 0; |
| 626 |
indx_t full, half, nxt, off, skip, top, used; |
| 627 |
u_int32_t nbytes; |
| 628 |
int bigkeycnt, isbigkey; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
bigkeycnt = 0; |
| 637 |
skip = *pskip; |
| 638 |
full = t->bt_psize - BTDATAOFF; |
| 639 |
half = full / 2; |
| 640 |
used = 0; |
| 641 |
for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) { |
| 642 |
if (skip == off) { |
| 643 |
nbytes = ilen; |
| 644 |
isbigkey = 0; |
| 645 |
} else |
| 646 |
switch (h->flags & P_TYPE) { |
| 647 |
case P_BINTERNAL: |
| 648 |
src = bi = GETBINTERNAL(h, nxt); |
| 649 |
nbytes = NBINTERNAL(bi->ksize); |
| 650 |
isbigkey = bi->flags & P_BIGKEY; |
| 651 |
break; |
| 652 |
case P_BLEAF: |
| 653 |
src = bl = GETBLEAF(h, nxt); |
| 654 |
nbytes = NBLEAF(bl); |
| 655 |
isbigkey = bl->flags & P_BIGKEY; |
| 656 |
break; |
| 657 |
case P_RINTERNAL: |
| 658 |
src = GETRINTERNAL(h, nxt); |
| 659 |
nbytes = NRINTERNAL; |
| 660 |
isbigkey = 0; |
| 661 |
break; |
| 662 |
case P_RLEAF: |
| 663 |
src = rl = GETRLEAF(h, nxt); |
| 664 |
nbytes = NRLEAF(rl); |
| 665 |
isbigkey = 0; |
| 666 |
break; |
| 667 |
default: |
| 668 |
abort(); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
if ((skip <= off && used + nbytes + sizeof(indx_t) >= full) |
| 678 |
|| nxt == top - 1) { |
| 679 |
--off; |
| 680 |
break; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
if (skip != off) { |
| 685 |
++nxt; |
| 686 |
|
| 687 |
l->linp[off] = l->upper -= nbytes; |
| 688 |
memmove((char *)l + l->upper, src, nbytes); |
| 689 |
} |
| 690 |
|
| 691 |
used += nbytes + sizeof(indx_t); |
| 692 |
if (used >= half) { |
| 693 |
if (!isbigkey || bigkeycnt == 3) |
| 694 |
break; |
| 695 |
else |
| 696 |
++bigkeycnt; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
l->lower += (off + 1) * sizeof(indx_t); |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
c = &t->bt_cursor; |
| 714 |
if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) { |
| 715 |
if (c->pg.index >= skip) |
| 716 |
++c->pg.index; |
| 717 |
if (c->pg.index < nxt) |
| 718 |
c->pg.pgno = l->pgno; |
| 719 |
else { |
| 720 |
c->pg.pgno = r->pgno; |
| 721 |
c->pg.index -= nxt; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
if (skip <= off) { |
| 731 |
skip = 0; |
| 732 |
rval = l; |
| 733 |
} else { |
| 734 |
rval = r; |
| 735 |
*pskip -= nxt; |
| 736 |
} |
| 737 |
|
| 738 |
for (off = 0; nxt < top; ++off) { |
| 739 |
if (skip == nxt) { |
| 740 |
++off; |
| 741 |
skip = 0; |
| 742 |
} |
| 743 |
switch (h->flags & P_TYPE) { |
| 744 |
case P_BINTERNAL: |
| 745 |
src = bi = GETBINTERNAL(h, nxt); |
| 746 |
nbytes = NBINTERNAL(bi->ksize); |
| 747 |
break; |
| 748 |
case P_BLEAF: |
| 749 |
src = bl = GETBLEAF(h, nxt); |
| 750 |
nbytes = NBLEAF(bl); |
| 751 |
break; |
| 752 |
case P_RINTERNAL: |
| 753 |
src = GETRINTERNAL(h, nxt); |
| 754 |
nbytes = NRINTERNAL; |
| 755 |
break; |
| 756 |
case P_RLEAF: |
| 757 |
src = rl = GETRLEAF(h, nxt); |
| 758 |
nbytes = NRLEAF(rl); |
| 759 |
break; |
| 760 |
default: |
| 761 |
abort(); |
| 762 |
} |
| 763 |
++nxt; |
| 764 |
r->linp[off] = r->upper -= nbytes; |
| 765 |
memmove((char *)r + r->upper, src, nbytes); |
| 766 |
} |
| 767 |
r->lower += off * sizeof(indx_t); |
| 768 |
|
| 769 |
|
| 770 |
if (skip == top) |
| 771 |
r->lower += sizeof(indx_t); |
| 772 |
|
| 773 |
return (rval); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
static int |
| 792 |
bt_preserve(t, pg) |
| 793 |
BTREE *t; |
| 794 |
pgno_t pg; |
| 795 |
{ |
| 796 |
PAGE *h; |
| 797 |
|
| 798 |
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) |
| 799 |
return (RET_ERROR); |
| 800 |
h->flags |= P_PRESERVE; |
| 801 |
mpool_put(t->bt_mp, h, MPOOL_DIRTY); |
| 802 |
return (RET_SUCCESS); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
static recno_t |
| 820 |
rec_total(h) |
| 821 |
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 |
} |