00001
00002
00003
00004
00005
00006
00007
00008 #include "config.h"
00009
00010 #ifndef lint
00011 static const char revid[] = "$Id: bt__cursor_8c-source.html,v 1.1 2008/06/08 10:13:31 sebdiaz Exp $";
00012 #endif
00013
00014 #ifndef NO_SYSTEM_INCLUDES
00015 #include <sys/types.h>
00016
00017 #include <errno.h>
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #endif
00021
00022 #include "db_int.h"
00023 #include "db_page.h"
00024 #include "db_shash.h"
00025 #include "btree.h"
00026 #include "lock.h"
00027 #include "qam.h"
00028 #include "common_ext.h"
00029
00030 static int __bam_c_close __P((DBC *, db_pgno_t, int *));
00031 static int __bam_c_del __P((DBC *));
00032 static int __bam_c_destroy __P((DBC *));
00033 static int __bam_c_first __P((DBC *));
00034 static int __bam_c_get __P((DBC *, DBT *, DBT *, u_int32_t, db_pgno_t *));
00035 static int __bam_c_getstack __P((DBC *));
00036 static int __bam_c_last __P((DBC *));
00037 static int __bam_c_next __P((DBC *, int));
00038 static int __bam_c_physdel __P((DBC *));
00039 static int __bam_c_prev __P((DBC *));
00040 static int __bam_c_put __P((DBC *, DBT *, DBT *, u_int32_t, db_pgno_t *));
00041 static void __bam_c_reset __P((BTREE_CURSOR *));
00042 static int __bam_c_search __P((DBC *, const DBT *, u_int32_t, int *));
00043 static int __bam_c_writelock __P((DBC *));
00044 static int __bam_getboth_finddatum __P((DBC *, DBT *));
00045 static int __bam_getbothc __P((DBC *, DBT *));
00046 static int __bam_isopd __P((DBC *, db_pgno_t *));
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #undef ACQUIRE
00059 #define ACQUIRE(dbc, mode, lpgno, lock, fpgno, pagep, ret) {\
00060 if ((pagep) != NULL) { \
00061 ret = CDB_memp_fput((dbc)->dbp->mpf, pagep, 0); \
00062 pagep = NULL; \
00063 } else \
00064 ret = 0; \
00065 if ((ret) == 0 && STD_LOCKING(dbc)) \
00066 ret = CDB___db_lget(dbc, \
00067 (lock).off == LOCK_INVALID ? 0 : LCK_COUPLE, \
00068 lpgno, mode, 0, &lock); \
00069 else \
00070 (lock).off = LOCK_INVALID; \
00071 if ((ret) == 0) \
00072 ret = CDB_memp_fget((dbc)->dbp->mpf, &(fpgno), 0, &(pagep));\
00073 }
00074
00075
00076 #undef ACQUIRE_CUR
00077 #define ACQUIRE_CUR(dbc, mode, ret) { \
00078 BTREE_CURSOR *__cp = (BTREE_CURSOR *)(dbc)->internal; \
00079 ACQUIRE(dbc, mode, \
00080 __cp->pgno, __cp->lock, __cp->pgno, __cp->page, ret); \
00081 if ((ret) == 0) \
00082 __cp->lock_mode = (mode); \
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092 #undef ACQUIRE_CUR_SET
00093 #define ACQUIRE_CUR_SET(dbc, mode, p, ret) { \
00094 BTREE_CURSOR *__cp = (BTREE_CURSOR *)(dbc)->internal; \
00095 ACQUIRE(dbc, mode, p, __cp->lock, p, __cp->page, ret); \
00096 if ((ret) == 0) { \
00097 __cp->pgno = p; \
00098 __cp->indx = 0; \
00099 __cp->lock_mode = (mode); \
00100 } \
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 #undef ACQUIRE_WRITE_LOCK
00110 #define ACQUIRE_WRITE_LOCK(dbc, ret) { \
00111 BTREE_CURSOR *__cp = (BTREE_CURSOR *)(dbc)->internal; \
00112 ret = 0; \
00113 if (STD_LOCKING(dbc) && \
00114 __cp->lock_mode != DB_LOCK_WRITE && \
00115 ((ret) = CDB___db_lget(dbc, \
00116 __cp->lock.off == LOCK_INVALID ? 0 : LCK_COUPLE, \
00117 __cp->pgno, DB_LOCK_WRITE, 0, &__cp->lock)) == 0) \
00118 __cp->lock_mode = DB_LOCK_WRITE; \
00119 }
00120
00121
00122 #undef DISCARD
00123 #define DISCARD(dbc, ldiscard, lock, pagep, ret) { \
00124 int __t_ret; \
00125 if ((pagep) != NULL) { \
00126 ret = CDB_memp_fput((dbc)->dbp->mpf, pagep, 0); \
00127 pagep = NULL; \
00128 } else \
00129 ret = 0; \
00130 if ((lock).off != LOCK_INVALID) { \
00131 __t_ret = ldiscard ? \
00132 __LPUT((dbc), lock): __TLPUT((dbc), lock); \
00133 if (__t_ret != 0 && (ret) == 0) \
00134 ret = __t_ret; \
00135 (lock).off = LOCK_INVALID; \
00136 } \
00137 }
00138
00139
00140 #undef DISCARD_CUR
00141 #define DISCARD_CUR(dbc, ret) { \
00142 BTREE_CURSOR *__cp = (BTREE_CURSOR *)(dbc)->internal; \
00143 DISCARD(dbc, 0, __cp->lock, __cp->page, ret); \
00144 if ((ret) == 0) \
00145 __cp->lock_mode = DB_LOCK_NG; \
00146 }
00147
00148
00149 #undef IS_DELETED
00150 #define IS_DELETED(page, indx) \
00151 B_DISSET(GET_BKEYDATA(page, \
00152 (indx) + (TYPE(page) == P_LBTREE ? O_INDX : 0))->type)
00153 #undef IS_CUR_DELETED
00154 #define IS_CUR_DELETED(dbc) \
00155 IS_DELETED((dbc)->internal->page, (dbc)->internal->indx)
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 #undef IS_DUPLICATE
00166 #define IS_DUPLICATE(dbc, i1, i2) \
00167 (((PAGE *)(dbc)->internal->page)->inp[i1] == \
00168 ((PAGE *)(dbc)->internal->page)->inp[i2])
00169 #undef IS_CUR_DUPLICATE
00170 #define IS_CUR_DUPLICATE(dbc, orig_pgno, orig_indx) \
00171 (F_ISSET(dbc, DBC_OPD) || \
00172 (orig_pgno == (dbc)->internal->pgno && \
00173 IS_DUPLICATE(dbc, (dbc)->internal->indx, orig_indx)))
00174
00175
00176
00177
00178
00179 static void
00180 __bam_c_reset(cp)
00181 BTREE_CURSOR *cp;
00182 {
00183 cp->sp = cp->csp = cp->stack;
00184 cp->esp = cp->stack + sizeof(cp->stack) / sizeof(cp->stack[0]);
00185 cp->lock.off = LOCK_INVALID;
00186 cp->lock_mode = DB_LOCK_NG;
00187 cp->recno = RECNO_OOB;
00188 cp->flags = 0;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197 int
00198 CDB___bam_c_init(dbc, dbtype)
00199 DBC *dbc;
00200 DBTYPE dbtype;
00201 {
00202 BTREE *t;
00203 BTREE_CURSOR *cp;
00204 DB *dbp;
00205 u_int32_t minkey;
00206 int ret;
00207
00208 dbp = dbc->dbp;
00209
00210
00211 if (dbc->internal == NULL) {
00212 if ((ret = CDB___os_malloc(dbp->dbenv,
00213 sizeof(BTREE_CURSOR), NULL, &cp)) != 0)
00214 return (ret);
00215 dbc->internal = (DBC_INTERNAL *)cp;
00216 } else
00217 cp = (BTREE_CURSOR *)dbc->internal;
00218 __bam_c_reset(cp);
00219
00220
00221 dbc->c_close = CDB___db_c_close;
00222 dbc->c_count = CDB___db_c_count;
00223 dbc->c_del = CDB___db_c_del;
00224 dbc->c_dup = CDB___db_c_dup;
00225 dbc->c_get = CDB___db_c_get;
00226 dbc->c_put = CDB___db_c_put;
00227 if (dbtype == DB_BTREE) {
00228 dbc->c_am_close = __bam_c_close;
00229 dbc->c_am_del = __bam_c_del;
00230 dbc->c_am_destroy = __bam_c_destroy;
00231 dbc->c_am_get = __bam_c_get;
00232 dbc->c_am_put = __bam_c_put;
00233 dbc->c_am_writelock = __bam_c_writelock;
00234 } else {
00235 dbc->c_am_close = __bam_c_close;
00236 dbc->c_am_del = CDB___ram_c_del;
00237 dbc->c_am_destroy = __bam_c_destroy;
00238 dbc->c_am_get = CDB___ram_c_get;
00239 dbc->c_am_put = CDB___ram_c_put;
00240 dbc->c_am_writelock = __bam_c_writelock;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 t = dbp->bt_internal;
00256 minkey = F_ISSET(dbc, DBC_OPD) ? 2 : t->bt_minkey;
00257 cp->ovflsize = (dbp->pgsize - P_OVERHEAD) / (minkey * P_INDX)
00258 - (BKEYDATA_PSIZE(0) + ALIGN(1, 4));
00259
00260 return (0);
00261 }
00262
00263
00264
00265
00266
00267
00268 int
00269 CDB___bam_c_refresh(dbc)
00270 DBC *dbc;
00271 {
00272 BTREE_CURSOR *cp;
00273 DB *dbp;
00274
00275 dbp = dbc->dbp;
00276 cp = (BTREE_CURSOR *)dbc->internal;
00277 __bam_c_reset(cp);
00278
00279
00280
00281
00282
00283
00284 if (cp->root == PGNO_INVALID)
00285 cp->root = ((BTREE *)dbp->bt_internal)->bt_root;
00286
00287
00288 if (F_ISSET(dbc, DBC_OPD) ||
00289 dbc->dbtype == DB_RECNO || F_ISSET(dbp, DB_BT_RECNUM)) {
00290 F_SET(cp, C_RECNUM);
00291
00292
00293
00294
00295
00296
00297 if ((F_ISSET(dbc, DBC_OPD) && dbc->dbtype == DB_RECNO) ||
00298 F_ISSET(dbp, DB_BT_RECNUM | DB_RE_RENUMBER))
00299 F_SET(cp, C_RENUMBER);
00300 }
00301
00302 return (0);
00303 }
00304
00305
00306
00307
00308
00309 static int
00310 __bam_c_close(dbc, root_pgno, rmroot)
00311 DBC *dbc;
00312 db_pgno_t root_pgno;
00313 int *rmroot;
00314 {
00315 BTREE_CURSOR *cp, *cp_opd, *cp_c;
00316 DB *dbp;
00317 DBC *dbc_opd, *dbc_c;
00318 PAGE *h;
00319 u_int32_t num;
00320 int cdb_lock, ret, t_ret;
00321
00322 dbp = dbc->dbp;
00323 cp = (BTREE_CURSOR *)dbc->internal;
00324 cp_opd = (dbc_opd = cp->opd) == NULL ?
00325 NULL : (BTREE_CURSOR *)dbc_opd->internal;
00326 cdb_lock = ret = 0;
00327
00328 if (dbc_opd != NULL)
00329 DB_ASSERT(cp_opd->csp == cp_opd->stack);
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 if (F_ISSET(cp, C_DELETED)) {
00389 dbc_c = dbc;
00390 switch (dbc->dbtype) {
00391 case DB_BTREE:
00392 if (CDB___bam_ca_delete(dbp, cp->pgno, cp->indx, 1) == 0)
00393 goto lock;
00394 goto done;
00395 case DB_RECNO:
00396 if (!F_ISSET(dbc, DBC_OPD))
00397 goto done;
00398
00399 if (CDB___ram_ca_delete(dbp, cp->root) == 0)
00400 goto lock;
00401 goto done;
00402 default:
00403 return (CDB___db_unknown_type(dbp->dbenv,
00404 "__bam_c_close", dbc->dbtype));
00405 }
00406 }
00407
00408 if (dbc_opd == NULL)
00409 goto done;
00410
00411 if (F_ISSET(cp_opd, C_DELETED)) {
00412
00413
00414
00415
00416 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &h)) != 0)
00417 goto err;
00418 root_pgno = GET_BOVERFLOW(h, cp->indx + O_INDX)->pgno;
00419 if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
00420 goto err;
00421
00422 dbc_c = dbc_opd;
00423 switch (dbc_opd->dbtype) {
00424 case DB_BTREE:
00425 if (CDB___bam_ca_delete(
00426 dbp, cp_opd->pgno, cp_opd->indx, 1) == 0)
00427 goto lock;
00428 goto done;
00429 case DB_RECNO:
00430 if (CDB___ram_ca_delete(dbp, cp_opd->root) == 0)
00431 goto lock;
00432 goto done;
00433 default:
00434 return (CDB___db_unknown_type(dbp->dbenv,
00435 "__bam_c_close", dbc->dbtype));
00436 }
00437 }
00438 goto done;
00439
00440 lock: cp_c = (BTREE_CURSOR *)dbc_c->internal;
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 if (LOCKING(dbp->dbenv)) {
00461 DB_ASSERT(!F_ISSET(dbc, DBC_OPD) || F_ISSET(dbc, DBC_WRITEDUP));
00462 if (!F_ISSET(dbc, DBC_WRITER)) {
00463 if ((ret =
00464 CDB_lock_get(dbp->dbenv, dbc->locker, DB_LOCK_UPGRADE,
00465 &dbc->lock_dbt, DB_LOCK_WRITE, &dbc->mylock)) != 0)
00466 goto err;
00467 cdb_lock = 1;
00468 }
00469
00470 cp_c->lock.off = LOCK_INVALID;
00471 if ((ret =
00472 CDB_memp_fget(dbp->mpf, &cp_c->pgno, 0, &cp_c->page)) != 0)
00473 goto err;
00474
00475 goto delete;
00476 }
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 if (F_ISSET(dbc, DBC_OPD)) {
00488 cp_c->lock.off = LOCK_INVALID;
00489 if ((ret =
00490 CDB_memp_fget(dbp->mpf, &cp_c->pgno, 0, &cp_c->page)) != 0)
00491 goto err;
00492 goto delete;
00493 }
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 ACQUIRE(dbc, DB_LOCK_WRITE,
00523 cp->pgno, cp_c->lock, cp_c->pgno, cp_c->page, ret);
00524 if (ret != 0)
00525 goto err;
00526
00527 delete:
00528
00529
00530
00531 if (dbc_c->dbtype == DB_BTREE && (ret = __bam_c_physdel(dbc_c)) != 0)
00532 goto err;
00533
00534 if (dbc_opd != NULL)
00535 DB_ASSERT(cp_opd->csp == cp_opd->stack);
00536
00537
00538
00539
00540
00541 if (!F_ISSET(dbc_c, DBC_OPD) || root_pgno == PGNO_INVALID)
00542 goto done;
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 if ((ret = CDB_memp_fget(dbp->mpf, &root_pgno, 0, &h)) != 0)
00554 goto err;
00555 if ((num = NUM_ENT(h)) == 0) {
00556 if ((ret = CDB___db_free(dbc, h)) != 0)
00557 goto err;
00558 } else {
00559 if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
00560 goto err;
00561 goto done;
00562 }
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 if (dbc_opd != NULL) {
00577 cp->lock.off = LOCK_INVALID;
00578 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
00579 goto err;
00580 if ((ret = __bam_c_physdel(dbc)) != 0)
00581 goto err;
00582 } else
00583 *rmroot = 1;
00584 err:
00585 done:
00586
00587
00588
00589 if (dbc_opd != NULL) {
00590 DISCARD_CUR(dbc_opd, t_ret);
00591 if (t_ret != 0 && ret == 0)
00592 ret = t_ret;
00593 DB_ASSERT(cp_opd->csp == cp_opd->stack);
00594 }
00595 DISCARD_CUR(dbc, t_ret);
00596 if (t_ret != 0 && ret == 0)
00597 ret = t_ret;
00598 DB_ASSERT(cp->csp == cp->stack);
00599
00600
00601 if (cdb_lock)
00602 (void)CDB___lock_downgrade(
00603 dbp->dbenv, &dbc->mylock, DB_LOCK_IWRITE, 0);
00604
00605 return (ret);
00606 }
00607
00608
00609
00610
00611
00612 static int
00613 __bam_c_destroy(dbc)
00614 DBC *dbc;
00615 {
00616
00617 CDB___os_free(dbc->internal, sizeof(BTREE_CURSOR));
00618
00619 return (0);
00620 }
00621
00622
00623
00624
00625
00626
00627
00628 int
00629 CDB___bam_c_count(dbc, recnop)
00630 DBC *dbc;
00631 db_recno_t *recnop;
00632 {
00633 BTREE_CURSOR *cp;
00634 DB *dbp;
00635 db_indx_t indx, top;
00636 db_recno_t recno;
00637 int ret;
00638
00639 dbp = dbc->dbp;
00640 cp = (BTREE_CURSOR *)dbc->internal;
00641
00642
00643
00644
00645
00646
00647
00648
00649 if (cp->opd == NULL) {
00650 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
00651 return (ret);
00652
00653
00654
00655
00656
00657 for (indx = cp->indx;; indx -= P_INDX)
00658 if (indx == 0 ||
00659 !IS_DUPLICATE(dbc, indx, indx - P_INDX))
00660 break;
00661 for (recno = 1, top = NUM_ENT(cp->page);
00662 indx < top; ++recno, indx += P_INDX)
00663 if (!IS_DUPLICATE(dbc, indx, indx + P_INDX))
00664 break;
00665 *recnop = recno;
00666 } else {
00667 if ((ret = CDB_memp_fget(dbp->mpf,
00668 &cp->opd->internal->root, 0, &cp->page)) != 0)
00669 return (ret);
00670
00671 *recnop = RE_NREC(cp->page);
00672 }
00673
00674 ret = CDB_memp_fput(dbp->mpf, cp->page, 0);
00675 cp->page = NULL;
00676
00677 return (ret);
00678 }
00679
00680
00681
00682
00683
00684 static int
00685 __bam_c_del(dbc)
00686 DBC *dbc;
00687 {
00688 BTREE_CURSOR *cp;
00689 DB *dbp;
00690 int ret, t_ret;
00691
00692 dbp = dbc->dbp;
00693 cp = (BTREE_CURSOR *)dbc->internal;
00694 ret = 0;
00695
00696
00697 if (F_ISSET(cp, C_DELETED))
00698 return (DB_KEYEMPTY);
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709 if (F_ISSET(cp, C_RECNUM)) {
00710 if ((ret = __bam_c_getstack(dbc)) != 0)
00711 goto err;
00712 } else {
00713 ACQUIRE_CUR(dbc, DB_LOCK_WRITE, ret);
00714 if (ret != 0)
00715 goto err;
00716 }
00717
00718
00719 if (DB_LOGGING(dbc) &&
00720 (ret = CDB___bam_cdel_log(dbp->dbenv, dbc->txn, &LSN(cp->page), 0,
00721 dbp->log_fileid, PGNO(cp->page), &LSN(cp->page), cp->indx)) != 0)
00722 goto err;
00723
00724
00725 if (TYPE(cp->page) == P_LBTREE)
00726 B_DSET(GET_BKEYDATA(cp->page, cp->indx + O_INDX)->type);
00727 else
00728 B_DSET(GET_BKEYDATA(cp->page, cp->indx)->type);
00729
00730
00731 ret = CDB_memp_fset(dbp->mpf, cp->page, DB_MPOOL_DIRTY);
00732
00733 err:
00734
00735
00736
00737 if (F_ISSET(cp, C_RECNUM)) {
00738 if (ret == 0)
00739 ret = CDB___bam_adjust(dbc, -1);
00740 (void)CDB___bam_stkrel(dbc, STK_CLRDBC);
00741 } else {
00742 DISCARD_CUR(dbc, t_ret);
00743 if (t_ret != 0 && ret == 0)
00744 ret = t_ret;
00745 }
00746
00747
00748 if (ret == 0)
00749 (void)CDB___bam_ca_delete(dbp, cp->pgno, cp->indx, 1);
00750
00751 return (ret);
00752 }
00753
00754
00755
00756
00757
00758
00759
00760
00761 int
00762 CDB___bam_c_dup(orig_dbc, new_dbc)
00763 DBC *orig_dbc, *new_dbc;
00764 {
00765 BTREE_CURSOR *orig, *new;
00766 int ret;
00767
00768 orig = (BTREE_CURSOR *)orig_dbc->internal;
00769 new = (BTREE_CURSOR *)new_dbc->internal;
00770
00771
00772
00773
00774
00775
00776
00777 if (orig->lock.off != LOCK_INVALID && orig_dbc->txn == NULL) {
00778 if ((ret = CDB___db_lget(new_dbc,
00779 0, new->pgno, new->lock_mode, 0, &new->lock)) != 0)
00780 return (ret);
00781 }
00782 new->ovflsize = orig->ovflsize;
00783 new->recno = orig->recno;
00784 new->flags = orig->flags;
00785
00786 return (0);
00787 }
00788
00789
00790
00791
00792
00793 static int
00794 __bam_c_get(dbc, key, data, flags, pgnop)
00795 DBC *dbc;
00796 DBT *key, *data;
00797 u_int32_t flags;
00798 db_pgno_t *pgnop;
00799 {
00800 BTREE_CURSOR *cp;
00801 DB *dbp;
00802 db_pgno_t orig_pgno;
00803 db_indx_t orig_indx;
00804 int exact, newopd, ret;
00805
00806 dbp = dbc->dbp;
00807 cp = (BTREE_CURSOR *)dbc->internal;
00808 orig_pgno = cp->pgno;
00809 orig_indx = cp->indx;
00810
00811 newopd = 0;
00812 switch (flags) {
00813 case DB_CURRENT:
00814
00815 if (F_ISSET(cp, C_DELETED)) {
00816 ret = DB_KEYEMPTY;
00817 goto err;
00818 }
00819
00820
00821
00822
00823
00824
00825
00826 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
00827 goto err;
00828 break;
00829 case DB_FIRST:
00830 newopd = 1;
00831 if ((ret = __bam_c_first(dbc)) != 0)
00832 goto err;
00833 break;
00834 case DB_GET_BOTH:
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848 if (F_ISSET(dbc, DBC_OPD)) {
00849 if ((ret = __bam_c_search(
00850 dbc, data, DB_GET_BOTH, &exact)) != 0)
00851 goto err;
00852 if (!exact) {
00853 ret = DB_NOTFOUND;
00854 goto err;
00855 }
00856 } else {
00857 if ((ret = __bam_c_search(
00858 dbc, key, DB_GET_BOTH, &exact)) != 0)
00859 return (ret);
00860 if (!exact) {
00861 ret = DB_NOTFOUND;
00862 goto err;
00863 }
00864
00865 if (pgnop != NULL && __bam_isopd(dbc, pgnop)) {
00866 newopd = 1;
00867 break;
00868 }
00869 if ((ret = __bam_getboth_finddatum(dbc, data)) != 0)
00870 goto err;
00871 }
00872 break;
00873 case DB_GET_BOTHC:
00874 if ((ret = __bam_getbothc(dbc, data)) != 0)
00875 goto err;
00876 break;
00877 case DB_LAST:
00878 newopd = 1;
00879 if ((ret = __bam_c_last(dbc)) != 0)
00880 goto err;
00881 break;
00882 case DB_NEXT:
00883 newopd = 1;
00884 if (cp->pgno == PGNO_INVALID) {
00885 if ((ret = __bam_c_first(dbc)) != 0)
00886 goto err;
00887 } else
00888 if ((ret = __bam_c_next(dbc, 1)) != 0)
00889 goto err;
00890 break;
00891 case DB_NEXT_DUP:
00892 if ((ret = __bam_c_next(dbc, 1)) != 0)
00893 goto err;
00894 if (!IS_CUR_DUPLICATE(dbc, orig_pgno, orig_indx)) {
00895 ret = DB_NOTFOUND;
00896 goto err;
00897 }
00898 break;
00899 case DB_NEXT_NODUP:
00900 newopd = 1;
00901 if (cp->pgno == PGNO_INVALID) {
00902 if ((ret = __bam_c_first(dbc)) != 0)
00903 goto err;
00904 } else
00905 do {
00906 if ((ret = __bam_c_next(dbc, 1)) != 0)
00907 goto err;
00908 } while (IS_CUR_DUPLICATE(dbc, orig_pgno, orig_indx));
00909 break;
00910 case DB_PREV:
00911 newopd = 1;
00912 if (cp->pgno == PGNO_INVALID) {
00913 if ((ret = __bam_c_last(dbc)) != 0)
00914 goto err;
00915 } else
00916 if ((ret = __bam_c_prev(dbc)) != 0)
00917 goto err;
00918 break;
00919 case DB_PREV_NODUP:
00920 newopd = 1;
00921 if (cp->pgno == PGNO_INVALID) {
00922 if ((ret = __bam_c_last(dbc)) != 0)
00923 goto err;
00924 } else
00925 do {
00926 if ((ret = __bam_c_prev(dbc)) != 0)
00927 goto err;
00928 } while (IS_CUR_DUPLICATE(dbc, orig_pgno, orig_indx));
00929 break;
00930 case DB_SET:
00931 case DB_SET_RECNO:
00932 newopd = 1;
00933 if ((ret = __bam_c_search(dbc, key, flags, &exact)) != 0)
00934 goto err;
00935 break;
00936 case DB_SET_RANGE:
00937 newopd = 1;
00938 if ((ret = __bam_c_search(dbc, key, flags, &exact)) != 0)
00939 goto err;
00940
00941
00942
00943
00944
00945
00946
00947 if (cp->indx == NUM_ENT(cp->page) || IS_CUR_DELETED(dbc))
00948 if ((ret = __bam_c_next(dbc, 0)) != 0)
00949 goto err;
00950 break;
00951 default:
00952 ret = CDB___db_unknown_flag(dbp->dbenv, "__bam_c_get", flags);
00953 goto err;
00954 }
00955
00956
00957
00958
00959
00960 if (newopd && pgnop != NULL)
00961 (void)__bam_isopd(dbc, pgnop);
00962
00963
00964 if (flags == DB_SET)
00965 F_SET(key, DB_DBT_ISSET);
00966
00967 err:
00968
00969
00970
00971
00972 if (F_ISSET(cp, C_DELETED)
00973 && (cp->pgno != orig_pgno || cp->indx != orig_indx))
00974 F_CLR(cp, C_DELETED);
00975
00976 return (ret);
00977 }
00978
00979
00980
00981
00982
00983 static int
00984 __bam_getbothc(dbc, data)
00985 DBC *dbc;
00986 DBT *data;
00987 {
00988 BTREE_CURSOR *cp;
00989 DB *dbp;
00990 int cmp, exact, ret;
00991
00992 dbp = dbc->dbp;
00993 cp = (BTREE_CURSOR *)dbc->internal;
00994
00995
00996
00997
00998
00999
01000
01001 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
01002 return (ret);
01003
01004
01005
01006
01007
01008
01009 if (F_ISSET(dbc, DBC_OPD)) {
01010
01011
01012
01013
01014 if ((ret = CDB___bam_cmp(dbp, data, cp->page, cp->indx,
01015 dbp->dup_compare == NULL ? CDB___bam_defcmp : dbp->dup_compare,
01016 &cmp)) != 0)
01017 return (ret);
01018
01019 if (cmp <= 0)
01020 return (DB_NOTFOUND);
01021
01022
01023 if ((ret = CDB_memp_fput(dbp->mpf, cp->page, 0)) != 0)
01024 return (ret);
01025 cp->page = NULL;
01026
01027 return (__bam_c_search(dbc, data, DB_GET_BOTH, &exact));
01028 }
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039 if (cp->indx + P_INDX >= NUM_ENT(cp->page) ||
01040 !IS_DUPLICATE(dbc, cp->indx, cp->indx + P_INDX))
01041 return (DB_NOTFOUND);
01042 cp->indx += P_INDX;
01043
01044 return (__bam_getboth_finddatum(dbc, data));
01045 }
01046
01047
01048
01049
01050
01051 static int
01052 __bam_getboth_finddatum(dbc, data)
01053 DBC *dbc;
01054 DBT *data;
01055 {
01056 BTREE_CURSOR *cp;
01057 DB *dbp;
01058 db_indx_t base, lim, top;
01059 int cmp, ret;
01060
01061 dbp = dbc->dbp;
01062 cp = (BTREE_CURSOR *)dbc->internal;
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078 if (dbp->dup_compare == NULL) {
01079 for (;; cp->indx += P_INDX) {
01080 if (!IS_CUR_DELETED(dbc) &&
01081 (ret = CDB___bam_cmp(dbp, data, cp->page,
01082 cp->indx + O_INDX, CDB___bam_defcmp, &cmp)) != 0)
01083 return (ret);
01084 if (cmp == 0)
01085 return (0);
01086
01087 if (cp->indx + P_INDX >= NUM_ENT(cp->page) ||
01088 !IS_DUPLICATE(dbc, cp->indx, cp->indx + P_INDX))
01089 break;
01090 }
01091 } else {
01092
01093
01094
01095
01096 for (base = top = cp->indx;
01097 top < NUM_ENT(cp->page); top += P_INDX)
01098 if (!IS_DUPLICATE(dbc, cp->indx, top))
01099 break;
01100 if (base == (top - P_INDX)) {
01101 if ((ret = CDB___bam_cmp(dbp, data,
01102 cp->page, cp->indx + O_INDX,
01103 dbp->dup_compare, &cmp)) != 0)
01104 return (ret);
01105 return (cmp == 0 ? 0 : DB_NOTFOUND);
01106 }
01107
01108 for (lim =
01109 (top - base) / (db_indx_t)P_INDX; lim != 0; lim >>= 1) {
01110 cp->indx = base + ((lim >> 1) * P_INDX);
01111 if ((ret = CDB___bam_cmp(dbp, data, cp->page,
01112 cp->indx + O_INDX, dbp->dup_compare, &cmp)) != 0)
01113 return (ret);
01114 if (cmp == 0) {
01115 if (!IS_CUR_DELETED(dbc))
01116 return (0);
01117 break;
01118 }
01119 if (cmp > 0) {
01120 base = cp->indx + P_INDX;
01121 --lim;
01122 }
01123 }
01124 }
01125 return (DB_NOTFOUND);
01126 }
01127
01128
01129
01130
01131
01132 static int
01133 __bam_c_put(dbc, key, data, flags, pgnop)
01134 DBC *dbc;
01135 DBT *key, *data;
01136 u_int32_t flags;
01137 db_pgno_t *pgnop;
01138 {
01139 BTREE_CURSOR *cp;
01140 DB *dbp;
01141 DBT dbt;
01142 u_int32_t iiop;
01143 int cmp, exact, needkey, ret, stack;
01144 void *arg;
01145
01146 dbp = dbc->dbp;
01147 cp = (BTREE_CURSOR *)dbc->internal;
01148
01149 split: needkey = ret = stack = 0;
01150 switch (flags) {
01151 case DB_AFTER:
01152 case DB_BEFORE:
01153 case DB_CURRENT:
01154 needkey = 1;
01155 iiop = flags;
01156
01157
01158
01159
01160
01161
01162 if (F_ISSET(cp, C_RECNUM) &&
01163 (flags != DB_CURRENT || F_ISSET(cp, C_DELETED))) {
01164 if ((ret = __bam_c_getstack(dbc)) != 0)
01165 goto err;
01166 stack = 1;
01167 } else {
01168
01169 ACQUIRE_WRITE_LOCK(dbc, ret);
01170 if (ret != 0)
01171 goto err;
01172 if ((ret = CDB_memp_fget(
01173 dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
01174 goto err;
01175 }
01176 break;
01177 case DB_KEYFIRST:
01178 case DB_KEYLAST:
01179 case DB_NODUPDATA:
01180
01181
01182
01183
01184
01185 if (F_ISSET(dbc, DBC_OPD)) {
01186 if ((ret =
01187 __bam_c_search(dbc, data, flags, &exact)) != 0)
01188 goto err;
01189 stack = 1;
01190
01191
01192 if (exact) {
01193 ret = CDB___db_duperr(dbp, flags);
01194 goto err;
01195 }
01196 iiop = DB_BEFORE;
01197 break;
01198 }
01199
01200
01201 if ((ret = __bam_c_search(dbc, key,
01202 flags == DB_KEYFIRST || dbp->dup_compare != NULL ?
01203 DB_KEYFIRST : DB_KEYLAST, &exact)) != 0)
01204 goto err;
01205 stack = 1;
01206
01207
01208
01209
01210
01211 if (!exact) {
01212 iiop = DB_KEYFIRST;
01213 break;
01214 }
01215
01216
01217
01218
01219
01220
01221 if (!F_ISSET(dbp, DB_AM_DUP)) {
01222 iiop = DB_CURRENT;
01223 break;
01224 }
01225
01226
01227
01228
01229
01230
01231 if (pgnop != NULL && __bam_isopd(dbc, pgnop))
01232 goto done;
01233
01234
01235 if (dbp->dup_compare == NULL) {
01236 if (flags == DB_KEYFIRST)
01237 iiop = DB_BEFORE;
01238 else
01239 for (;; cp->indx += P_INDX)
01240 if (cp->indx + P_INDX >=
01241 NUM_ENT(cp->page) ||
01242 !IS_DUPLICATE(dbc, cp->indx,
01243 cp->indx + P_INDX)) {
01244 iiop = DB_AFTER;
01245 break;
01246 }
01247 break;
01248 }
01249
01250
01251
01252
01253
01254 for (;; cp->indx += P_INDX) {
01255 if ((ret = CDB___bam_cmp(dbp, data, cp->page,
01256 cp->indx + O_INDX, dbp->dup_compare, &cmp)) !=0)
01257 return (ret);
01258 if (cmp < 0) {
01259 iiop = DB_BEFORE;
01260 break;
01261 }
01262
01263
01264 if (cmp == 0) {
01265 ret = CDB___db_duperr(dbp, flags);
01266 goto err;
01267 }
01268
01269 if (cp->indx + P_INDX >= NUM_ENT(cp->page) ||
01270 ((PAGE *)cp->page)->inp[cp->indx] !=
01271 ((PAGE *)cp->page)->inp[cp->indx + P_INDX]) {
01272 iiop = DB_AFTER;
01273 break;
01274 }
01275 }
01276 break;
01277 default:
01278 ret = CDB___db_unknown_flag(dbp->dbenv, "__bam_c_put", flags);
01279 goto err;
01280 }
01281
01282 switch (ret = CDB___bam_iitem(dbc, key, data, iiop, 0)) {
01283 case 0:
01284 break;
01285 case DB_NEEDSPLIT:
01286
01287
01288
01289
01290 if (flags == DB_AFTER ||
01291 flags == DB_BEFORE || flags == DB_CURRENT) {
01292 memset(&dbt, 0, sizeof(DBT));
01293 if ((ret = CDB___db_ret(dbp, cp->page, 0, &dbt,
01294 &dbc->rkey.data, &dbc->rkey.ulen)) != 0)
01295 goto err;
01296 arg = &dbt;
01297 } else
01298 arg = F_ISSET(dbc, DBC_OPD) ? data : key;
01299
01300
01301
01302
01303
01304
01305
01306
01307 if (stack)
01308 ret = CDB___bam_stkrel(dbc, STK_CLRDBC | STK_NOLOCK);
01309 else
01310 DISCARD_CUR(dbc, ret);
01311 if (ret != 0)
01312 goto err;
01313
01314
01315 if ((ret = CDB___bam_split(dbc, arg)) != 0)
01316 return (ret);
01317
01318 goto split;
01319 default:
01320 goto err;
01321 }
01322
01323 err:
01324 done:
01325
01326
01327
01328
01329
01330
01331 if (stack && BT_STK_POP(cp) != NULL)
01332 (void)CDB___bam_stkrel(dbc, 0);
01333
01334
01335
01336
01337
01338
01339
01340 F_CLR(cp, C_DELETED);
01341
01342 return (ret);
01343 }
01344
01345
01346
01347
01348
01349
01350
01351 int
01352 CDB___bam_c_rget(dbc, data, flags)
01353 DBC *dbc;
01354 DBT *data;
01355 u_int32_t flags;
01356 {
01357 BTREE_CURSOR *cp;
01358 DB *dbp;
01359 DBT dbt;
01360 db_recno_t recno;
01361 int exact, ret;
01362
01363 COMPQUIET(flags, 0);
01364 dbp = dbc->dbp;
01365 cp = (BTREE_CURSOR *)dbc->internal;
01366
01367
01368
01369
01370
01371
01372 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &cp->page)) != 0)
01373 return (ret);
01374 memset(&dbt, 0, sizeof(DBT));
01375 if ((ret = CDB___db_ret(dbp, cp->page,
01376 cp->indx, &dbt, &dbc->rkey.data, &dbc->rkey.ulen)) != 0)
01377 goto err;
01378 ret = CDB_memp_fput(dbp->mpf, cp->page, 0);
01379 cp->page = NULL;
01380 if (ret != 0)
01381 return (ret);
01382
01383 if ((ret = CDB___bam_search(dbc, &dbt,
01384 F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND,
01385 1, &recno, &exact)) != 0)
01386 goto err;
01387
01388 ret = CDB___db_retcopy(dbp, data,
01389 &recno, sizeof(recno), &dbc->rdata.data, &dbc->rdata.ulen);
01390
01391
01392 err: CDB___bam_stkrel(dbc, 0);
01393
01394 return (ret);
01395 }
01396
01397
01398
01399
01400
01401 static int
01402 __bam_c_writelock(dbc)
01403 DBC *dbc;
01404 {
01405 BTREE_CURSOR *cp;
01406 int ret;
01407
01408 cp = (BTREE_CURSOR *)dbc->internal;
01409
01410 if (cp->lock_mode == DB_LOCK_WRITE)
01411 return (0);
01412
01413
01414
01415
01416
01417
01418
01419 ACQUIRE_WRITE_LOCK(dbc, ret);
01420 return (ret);
01421 }
01422
01423
01424
01425
01426
01427 static int
01428 __bam_c_first(dbc)
01429 DBC *dbc;
01430 {
01431 BTREE_CURSOR *cp;
01432 DB *dbp;
01433 db_pgno_t pgno;
01434 int ret;
01435
01436 dbp = dbc->dbp;
01437 cp = (BTREE_CURSOR *)dbc->internal;
01438 ret = 0;
01439
01440
01441 for (pgno = cp->root;;) {
01442 ACQUIRE_CUR_SET(dbc, DB_LOCK_READ, pgno, ret);
01443 if (ret != 0)
01444 return (ret);
01445
01446
01447 if (ISLEAF(cp->page))
01448 break;
01449
01450 pgno = GET_BINTERNAL(cp->page, 0)->pgno;
01451 }
01452
01453
01454 if (F_ISSET(dbc, DBC_RMW)) {
01455 ACQUIRE_WRITE_LOCK(dbc, ret);
01456 if (ret != 0)
01457 return (ret);
01458 }
01459
01460
01461 if (NUM_ENT(cp->page) == 0 || IS_CUR_DELETED(dbc))
01462 if ((ret = __bam_c_next(dbc, 0)) != 0)
01463 return (ret);
01464
01465 return (0);
01466 }
01467
01468
01469
01470
01471
01472 static int
01473 __bam_c_last(dbc)
01474 DBC *dbc;
01475 {
01476 BTREE_CURSOR *cp;
01477 DB *dbp;
01478 db_pgno_t pgno;
01479 int ret;
01480
01481 dbp = dbc->dbp;
01482 cp = (BTREE_CURSOR *)dbc->internal;
01483 ret = 0;
01484
01485
01486 for (pgno = cp->root;;) {
01487 ACQUIRE_CUR_SET(dbc, DB_LOCK_READ, pgno, ret);
01488 if (ret != 0)
01489 return (ret);
01490
01491
01492 if (ISLEAF(cp->page))
01493 break;
01494
01495 pgno =
01496 GET_BINTERNAL(cp->page, NUM_ENT(cp->page) - O_INDX)->pgno;
01497 }
01498
01499
01500 if (F_ISSET(dbc, DBC_RMW)) {
01501 ACQUIRE_WRITE_LOCK(dbc, ret);
01502 if (ret != 0)
01503 return (ret);
01504 }
01505
01506 cp->indx = NUM_ENT(cp->page) == 0 ? 0 :
01507 NUM_ENT(cp->page) -
01508 (TYPE(cp->page) == P_LBTREE ? P_INDX : O_INDX);
01509
01510
01511 if (NUM_ENT(cp->page) == 0 || IS_CUR_DELETED(dbc))
01512 if ((ret = __bam_c_prev(dbc)) != 0)
01513 return (ret);
01514
01515 return (0);
01516 }
01517
01518
01519
01520
01521
01522 static int
01523 __bam_c_next(dbc, initial_move)
01524 DBC *dbc;
01525 int initial_move;
01526 {
01527 BTREE_CURSOR *cp;
01528 DB *dbp;
01529 db_indx_t adjust;
01530 db_lockmode_t lock_mode;
01531 db_pgno_t pgno;
01532 int ret;
01533
01534 dbp = dbc->dbp;
01535 cp = (BTREE_CURSOR *)dbc->internal;
01536 ret = 0;
01537
01538
01539
01540
01541
01542
01543
01544
01545 if (F_ISSET(dbc, DBC_OPD)) {
01546 adjust = O_INDX;
01547 lock_mode = DB_LOCK_NG;
01548 } else {
01549 adjust = dbc->dbtype == DB_BTREE ? P_INDX : O_INDX;
01550 lock_mode =
01551 F_ISSET(dbc, DBC_RMW) ? DB_LOCK_WRITE : DB_LOCK_READ;
01552 }
01553 if (cp->page == NULL) {
01554 ACQUIRE_CUR(dbc, lock_mode, ret);
01555 if (ret != 0)
01556 return (ret);
01557 }
01558
01559 if (initial_move)
01560 cp->indx += adjust;
01561
01562 for (;;) {
01563
01564
01565
01566
01567
01568
01569
01570 if (cp->indx >= NUM_ENT(cp->page)) {
01571 if ((pgno
01572 = NEXT_PGNO(cp->page)) == PGNO_INVALID)
01573 return (DB_NOTFOUND);
01574
01575 ACQUIRE_CUR_SET(dbc, lock_mode, pgno, ret);
01576 if (ret != 0)
01577 return (ret);
01578 continue;
01579 }
01580 if (IS_CUR_DELETED(dbc)) {
01581 cp->indx += adjust;
01582 continue;
01583 }
01584 break;
01585 }
01586 return (0);
01587 }
01588
01589
01590
01591
01592
01593 static int
01594 __bam_c_prev(dbc)
01595 DBC *dbc;
01596 {
01597 BTREE_CURSOR *cp;
01598 DB *dbp;
01599 db_indx_t adjust;
01600 db_lockmode_t lock_mode;
01601 db_pgno_t pgno;
01602 int ret;
01603
01604 dbp = dbc->dbp;
01605 cp = (BTREE_CURSOR *)dbc->internal;
01606 ret = 0;
01607
01608
01609
01610
01611
01612
01613
01614
01615 if (F_ISSET(dbc, DBC_OPD)) {
01616 adjust = O_INDX;
01617 lock_mode = DB_LOCK_NG;
01618 } else {
01619 adjust = dbc->dbtype == DB_BTREE ? P_INDX : O_INDX;
01620 lock_mode =
01621 F_ISSET(dbc, DBC_RMW) ? DB_LOCK_WRITE : DB_LOCK_READ;
01622 }
01623 if (cp->page == NULL) {
01624 ACQUIRE_CUR(dbc, lock_mode, ret);
01625 if (ret != 0)
01626 return (ret);
01627 }
01628
01629 for (;;) {
01630
01631 if (cp->indx == 0) {
01632 if ((pgno =
01633 PREV_PGNO(cp->page)) == PGNO_INVALID)
01634 return (DB_NOTFOUND);
01635
01636 ACQUIRE_CUR_SET(dbc, lock_mode, pgno, ret);
01637 if (ret != 0)
01638 return (ret);
01639
01640 if ((cp->indx = NUM_ENT(cp->page)) == 0)
01641 continue;
01642 }
01643
01644
01645 cp->indx -= adjust;
01646 if (IS_CUR_DELETED(dbc))
01647 continue;
01648
01649 break;
01650 }
01651 return (0);
01652 }
01653
01654
01655
01656
01657
01658 static int
01659 __bam_c_search(dbc, key, flags, exactp)
01660 DBC *dbc;
01661 const DBT *key;
01662 u_int32_t flags;
01663 int *exactp;
01664 {
01665 BTREE *t;
01666 BTREE_CURSOR *cp;
01667 DB *dbp;
01668 PAGE *h;
01669 db_indx_t indx;
01670 db_recno_t recno;
01671 u_int32_t sflags;
01672 int cmp, ret;
01673
01674 dbp = dbc->dbp;
01675 cp = (BTREE_CURSOR *)dbc->internal;
01676 t = dbp->bt_internal;
01677 ret = 0;
01678
01679
01680
01681
01682
01683 DISCARD_CUR(dbc, ret);
01684 if (ret != 0)
01685 return (ret);
01686
01687 switch (flags) {
01688 case DB_SET_RECNO:
01689 if ((ret = CDB___ram_getno(dbc, key, &recno, 0)) != 0)
01690 return (ret);
01691 sflags = (F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND) | S_EXACT;
01692 if ((ret = CDB___bam_rsearch(dbc, &recno, sflags, 1, exactp)) != 0)
01693 return (ret);
01694 break;
01695 case DB_SET:
01696 case DB_GET_BOTH:
01697 sflags = (F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND) | S_EXACT;
01698 goto search;
01699 case DB_SET_RANGE:
01700 sflags =
01701 (F_ISSET(dbc, DBC_RMW) ? S_WRITE : S_READ) | S_DUPFIRST;
01702 goto search;
01703 case DB_KEYFIRST:
01704 sflags = S_KEYFIRST;
01705 goto fast_search;
01706 case DB_KEYLAST:
01707 case DB_NODUPDATA:
01708 sflags = S_KEYLAST;
01709 fast_search:
01710
01711
01712
01713
01714
01715
01716
01717
01718 if (F_ISSET(cp, C_RECNUM))
01719 goto search;
01720
01721
01722
01723
01724 if (t->bt_lpgno == PGNO_INVALID)
01725 goto search;
01726
01727
01728 h = NULL;
01729 ACQUIRE(dbc, DB_LOCK_WRITE,
01730 t->bt_lpgno, cp->lock, t->bt_lpgno, h, ret);
01731 if (ret != 0)
01732 return (ret);
01733
01734
01735
01736
01737
01738 if (TYPE(h) != P_LBTREE || NUM_ENT(h) == 0)
01739 goto fast_miss;
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749 if (h->next_pgno == PGNO_INVALID) {
01750 indx = NUM_ENT(h) - P_INDX;
01751 if ((ret = CDB___bam_cmp(dbp,
01752 key, h, indx, t->bt_compare, &cmp)) != 0)
01753 return (ret);
01754
01755 if (cmp < 0)
01756 goto try_begin;
01757 if (cmp > 0) {
01758 indx += P_INDX;
01759 goto fast_hit;
01760 }
01761
01762
01763
01764
01765
01766
01767
01768
01769 if (flags == DB_KEYLAST)
01770 goto fast_hit;
01771 for (;
01772 indx > 0 && h->inp[indx - P_INDX] == h->inp[indx];
01773 indx -= P_INDX)
01774 ;
01775 goto fast_hit;
01776 }
01777 try_begin: if (h->prev_pgno == PGNO_INVALID) {
01778 indx = 0;
01779 if ((ret = CDB___bam_cmp(dbp,
01780 key, h, indx, t->bt_compare, &cmp)) != 0)
01781 return (ret);
01782
01783 if (cmp > 0)
01784 goto fast_miss;
01785 if (cmp < 0)
01786 goto fast_hit;
01787
01788
01789
01790
01791
01792
01793
01794
01795 if (flags == DB_KEYFIRST)
01796 goto fast_hit;
01797 for (;
01798 indx < (db_indx_t)(NUM_ENT(h) - P_INDX) &&
01799 h->inp[indx] == h->inp[indx + P_INDX];
01800 indx += P_INDX)
01801 ;
01802 goto fast_hit;
01803 }
01804 goto fast_miss;
01805
01806 fast_hit:
01807 *exactp = cmp == 0;
01808
01809
01810
01811
01812
01813 BT_STK_CLR(cp);
01814 BT_STK_ENTER(dbp->dbenv,
01815 cp, h, indx, cp->lock, cp->lock_mode, ret);
01816 if (ret != 0)
01817 return (ret);
01818 break;
01819
01820 fast_miss:
01821
01822
01823
01824 DISCARD(dbc, 1, cp->lock, h, ret);
01825 if (ret != 0)
01826 return (ret);
01827
01828 search: if ((ret =
01829 CDB___bam_search(dbc, key, sflags, 1, NULL, exactp)) != 0)
01830 return (ret);
01831 break;
01832 default:
01833 return (CDB___db_unknown_flag(dbp->dbenv, "__bam_c_search", flags));
01834 }
01835
01836
01837 cp->page = cp->csp->page;
01838 cp->pgno = cp->csp->page->pgno;
01839 cp->indx = cp->csp->indx;
01840 cp->lock = cp->csp->lock;
01841 cp->lock_mode = cp->csp->lock_mode;
01842
01843
01844
01845
01846
01847 if (TYPE(cp->page) == P_LBTREE &&
01848 (flags == DB_KEYFIRST || flags == DB_KEYLAST))
01849 t->bt_lpgno =
01850 (NEXT_PGNO(cp->page) == PGNO_INVALID &&
01851 cp->indx >= NUM_ENT(cp->page)) ||
01852 (PREV_PGNO(cp->page) == PGNO_INVALID &&
01853 cp->indx == 0) ? cp->pgno : PGNO_INVALID;
01854 return (0);
01855 }
01856
01857
01858
01859
01860
01861 static int
01862 __bam_c_physdel(dbc)
01863 DBC *dbc;
01864 {
01865 BTREE_CURSOR *cp;
01866 DB *dbp;
01867 DBT key;
01868 DB_LOCK lock;
01869 PAGE *h;
01870 db_pgno_t pgno;
01871 int delete_page, empty_page, exact, level, ret;
01872
01873 dbp = dbc->dbp;
01874 cp = (BTREE_CURSOR *)dbc->internal;
01875 delete_page = empty_page = ret = 0;
01876
01877
01878 delete_page = empty_page =
01879 NUM_ENT(cp->page) == (TYPE(cp->page) == P_LBTREE ? 2 : 1);
01880
01881
01882
01883
01884
01885
01886 if (delete_page &&
01887 !F_ISSET(dbc, DBC_OPD) && F_ISSET(dbp, DB_BT_REVSPLIT))
01888 delete_page = 0;
01889
01890
01891
01892
01893
01894
01895 if (delete_page && cp->pgno == cp->root)
01896 delete_page = 0;
01897
01898
01899
01900
01901
01902
01903 if (delete_page) {
01904 memset(&key, 0, sizeof(DBT));
01905 key.app_private = dbp->dbenv->app_private;
01906 if ((ret = CDB___db_ret(dbp, cp->page,
01907 0, &key, &dbc->rkey.data, &dbc->rkey.ulen)) != 0)
01908 return (ret);
01909 }
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926 if (TYPE(cp->page) == P_LBTREE) {
01927 if ((ret = CDB___bam_ditem(dbc, cp->page, cp->indx)) != 0)
01928 return (ret);
01929 if (!empty_page)
01930 CDB___bam_ca_di(dbp, PGNO(cp->page), cp->indx, -1);
01931 }
01932 if ((ret = CDB___bam_ditem(dbc, cp->page, cp->indx)) != 0)
01933 return (ret);
01934 if (!empty_page)
01935 CDB___bam_ca_di(dbp, PGNO(cp->page), cp->indx, -1);
01936
01937
01938 if (!delete_page)
01939 return (0);
01940
01941
01942
01943
01944
01945
01946
01947
01948
01949 for (level = LEAFLEVEL;; ++level) {
01950
01951 key.app_private = dbp->dbenv->app_private;
01952 if ((ret = CDB___bam_search(
01953 dbc, &key, S_WRPAIR, level, NULL, &exact)) != 0)
01954 return (ret);
01955
01956
01957
01958
01959
01960 h = cp->csp[-1].page;
01961 if (h->pgno == cp->root || NUM_ENT(h) != 1)
01962 break;
01963
01964
01965 (void)CDB___bam_stkrel(dbc, STK_NOLOCK);
01966 }
01967
01968
01969
01970
01971
01972 ++cp->csp;
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991 for (h = cp->csp[-1].page;;) {
01992 if (ISLEAF(h)) {
01993 if (NUM_ENT(h) != 0)
01994 break;
01995 break;
01996 } else
01997 if (NUM_ENT(h) != 1)
01998 break;
01999
02000
02001
02002
02003
02004 switch (TYPE(h)) {
02005 case P_IBTREE:
02006 pgno = GET_BINTERNAL(h, 0)->pgno;
02007 break;
02008 case P_IRECNO:
02009 pgno = GET_RINTERNAL(h, 0)->pgno;
02010 break;
02011 default:
02012 return (CDB___db_pgfmt(dbp, PGNO(h)));
02013 }
02014
02015 if ((ret =
02016 CDB___db_lget(dbc, 0, pgno, DB_LOCK_WRITE, 0, &lock)) != 0)
02017 break;
02018 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
02019 break;
02020 BT_STK_PUSH(dbp->dbenv, cp, h, 0, lock, DB_LOCK_WRITE, ret);
02021 if (ret != 0)
02022 break;
02023 }
02024
02025
02026 BT_STK_POP(cp);
02027
02028
02029
02030
02031
02032 if (ret == 0)
02033 ret = CDB___bam_dpages(dbc, cp->sp);
02034 else
02035 (void)CDB___bam_stkrel(dbc, 0);
02036
02037 return (ret);
02038 }
02039
02040
02041
02042
02043
02044 static int
02045 __bam_c_getstack(dbc)
02046 DBC *dbc;
02047 {
02048 BTREE_CURSOR *cp;
02049 DB *dbp;
02050 DBT dbt;
02051 PAGE *h;
02052 int exact, ret, t_ret;
02053
02054 dbp = dbc->dbp;
02055 cp = (BTREE_CURSOR *)dbc->internal;
02056
02057
02058
02059
02060
02061
02062 if ((ret = CDB_memp_fget(dbp->mpf, &cp->pgno, 0, &h)) != 0)
02063 return (ret);
02064
02065
02066 memset(&dbt, 0, sizeof(DBT));
02067 if ((ret = CDB___db_ret(dbp,
02068 h, 0, &dbt, &dbc->rkey.data, &dbc->rkey.ulen)) != 0)
02069 goto err;
02070
02071
02072 exact = 0;
02073 ret = CDB___bam_search(dbc, &dbt, S_KEYFIRST, 1, NULL, &exact);
02074
02075 err:
02076 if ((t_ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0 && ret == 0)
02077 ret = t_ret;
02078
02079 if (ret == 0) {
02080
02081
02082
02083
02084
02085
02086 cp->page = cp->csp->page;
02087 cp->lock = cp->csp->lock;
02088 cp->lock_mode = cp->csp->lock_mode;
02089 }
02090
02091 return (ret);
02092 }
02093
02094
02095
02096
02097
02098
02099 static int
02100 __bam_isopd(dbc, pgnop)
02101 DBC *dbc;
02102 db_pgno_t *pgnop;
02103 {
02104 BOVERFLOW *bo;
02105
02106 if (TYPE(dbc->internal->page) != P_LBTREE)
02107 return (0);
02108
02109 bo = GET_BOVERFLOW(dbc->internal->page, dbc->internal->indx + O_INDX);
02110 if (B_TYPE(bo->type) == B_DUPLICATE) {
02111 *pgnop = bo->pgno;
02112 return (1);
02113 }
02114 return (0);
02115 }