00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "config.h"
00044
00045 #ifndef lint
00046 static const char revid[] = "$Id: bt__search_8c-source.html,v 1.1 2008/06/08 10:13:44 sebdiaz Exp $";
00047 #endif
00048
00049 #ifndef NO_SYSTEM_INCLUDES
00050 #include <sys/types.h>
00051
00052 #include <string.h>
00053 #endif
00054
00055 #include "db_int.h"
00056 #include "db_page.h"
00057 #include "db_shash.h"
00058 #include "btree.h"
00059 #include "lock.h"
00060
00061
00062
00063
00064
00065
00066
00067
00068 int
00069 CDB___bam_search(dbc, key, flags, stop, recnop, exactp)
00070 DBC *dbc;
00071 const DBT *key;
00072 u_int32_t flags;
00073 int stop, *exactp;
00074 db_recno_t *recnop;
00075 {
00076 BTREE *t;
00077 BTREE_CURSOR *cp;
00078 DB *dbp;
00079 DB_LOCK lock;
00080 PAGE *h;
00081 db_indx_t base, i, indx, lim;
00082 db_lockmode_t lock_mode;
00083 db_pgno_t pg;
00084 db_recno_t recno;
00085 int adjust, cmp, deloffset, ret, stack;
00086 int (*func) __P((const DBT *, const DBT *));
00087
00088 dbp = dbc->dbp;
00089 cp = (BTREE_CURSOR *)dbc->internal;
00090 t = dbp->bt_internal;
00091 recno = 0;
00092
00093 BT_STK_CLR(cp);
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 try_again:
00112 pg = cp->root;
00113 stack = LF_ISSET(S_STACK) && F_ISSET(cp, C_RECNUM);
00114 lock_mode = stack ? DB_LOCK_WRITE : DB_LOCK_READ;
00115 if ((ret = CDB___db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0)
00116 return (ret);
00117 if ((ret = CDB_memp_fget(dbp->mpf, &pg, 0, &h)) != 0) {
00118
00119 (void)__LPUT(dbc, lock);
00120 return (ret);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 if (!stack &&
00132 ((LF_ISSET(S_PARENT) && (u_int8_t)(stop + 1) >= h->level) ||
00133 (LF_ISSET(S_WRITE) && h->level == LEAFLEVEL))) {
00134 (void)CDB_memp_fput(dbp->mpf, h, 0);
00135 (void)__LPUT(dbc, lock);
00136 lock_mode = DB_LOCK_WRITE;
00137 if ((ret = CDB___db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0)
00138 return (ret);
00139 if ((ret = CDB_memp_fget(dbp->mpf, &pg, 0, &h)) != 0) {
00140
00141 (void)__LPUT(dbc, lock);
00142 return (ret);
00143 }
00144 if (!((LF_ISSET(S_PARENT)
00145 && (u_int8_t)(stop + 1) >= h->level) ||
00146 (LF_ISSET(S_WRITE) && h->level == LEAFLEVEL))) {
00147
00148 (void)CDB_memp_fput(dbp->mpf, h, 0);
00149 (void)__LPUT(dbc, lock);
00150 goto try_again;
00151 }
00152 stack = 1;
00153 }
00154
00155
00156 func = F_ISSET(dbc, DBC_OPD) ?
00157 (dbp->dup_compare == NULL ? CDB___bam_defcmp : dbp->dup_compare) :
00158 t->bt_compare;
00159
00160 for (;;) {
00161
00162
00163
00164
00165
00166
00167
00168 adjust = TYPE(h) == P_LBTREE ? P_INDX : O_INDX;
00169 for (base = 0,
00170 lim = NUM_ENT(h) / (db_indx_t)adjust; lim != 0; lim >>= 1) {
00171 indx = base + ((lim >> 1) * adjust);
00172 if ((ret = CDB___bam_cmp(dbp,
00173 key, h, indx, func, &cmp)) != 0)
00174 goto err;
00175 if (cmp == 0) {
00176 if (TYPE(h) == P_LBTREE || TYPE(h) == P_LDUP)
00177 goto found;
00178 goto next;
00179 }
00180 if (cmp > 0) {
00181 base = indx + adjust;
00182 --lim;
00183 }
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 if (TYPE(h) == P_LBTREE || TYPE(h) == P_LDUP) {
00194 *exactp = 0;
00195
00196 if (LF_ISSET(S_EXACT))
00197 goto notfound;
00198
00199 if (LF_ISSET(S_STK_ONLY)) {
00200 BT_STK_NUM(dbp->dbenv, cp, h, base, ret);
00201 __LPUT(dbc, lock);
00202 (void)CDB_memp_fput(dbp->mpf, h, 0);
00203 return (ret);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 BT_STK_ENTER(dbp->dbenv,
00215 cp, h, base, lock, lock_mode, ret);
00216 if (ret != 0)
00217 goto err;
00218 return (0);
00219 }
00220
00221
00222
00223
00224
00225
00226
00227 indx = base > 0 ? base - O_INDX : base;
00228
00229
00230
00231
00232
00233 next: if (recnop != NULL)
00234 for (i = 0; i < indx; ++i)
00235 recno += GET_BINTERNAL(h, i)->nrecs;
00236
00237 pg = GET_BINTERNAL(h, indx)->pgno;
00238
00239 if (LF_ISSET(S_STK_ONLY)) {
00240 if (stop == h->level) {
00241 BT_STK_NUM(dbp->dbenv, cp, h, indx, ret);
00242 __LPUT(dbc, lock);
00243 (void)CDB_memp_fput(dbp->mpf, h, 0);
00244 return (ret);
00245 }
00246 BT_STK_NUMPUSH(dbp->dbenv, cp, h, indx, ret);
00247 (void)CDB_memp_fput(dbp->mpf, h, 0);
00248 if ((ret = CDB___db_lget(dbc,
00249 LCK_COUPLE, pg, lock_mode, 0, &lock)) != 0) {
00250
00251
00252
00253
00254
00255 __LPUT(dbc, lock);
00256 return (ret);
00257 }
00258 } else if (stack) {
00259
00260 if (LF_ISSET(S_PARENT) && stop == h->level) {
00261 BT_STK_ENTER(dbp->dbenv,
00262 cp, h, indx, lock, lock_mode, ret);
00263 if (ret != 0)
00264 goto err;
00265 return (0);
00266 }
00267 BT_STK_PUSH(dbp->dbenv,
00268 cp, h, indx, lock, lock_mode, ret);
00269 if (ret != 0)
00270 goto err;
00271
00272 lock_mode = DB_LOCK_WRITE;
00273 if ((ret =
00274 CDB___db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0)
00275 goto err;
00276 } else {
00277
00278
00279
00280
00281
00282 if ((LF_ISSET(S_PARENT) &&
00283 (u_int8_t)(stop + 1) >= (u_int8_t)(h->level - 1)) ||
00284 (h->level - 1) == LEAFLEVEL)
00285 stack = 1;
00286
00287 (void)CDB_memp_fput(dbp->mpf, h, 0);
00288
00289 lock_mode = stack &&
00290 LF_ISSET(S_WRITE) ? DB_LOCK_WRITE : DB_LOCK_READ;
00291 if ((ret = CDB___db_lget(dbc,
00292 LCK_COUPLE, pg, lock_mode, 0, &lock)) != 0) {
00293
00294
00295
00296
00297
00298 __LPUT(dbc, lock);
00299 goto err;
00300 }
00301 }
00302 if ((ret = CDB_memp_fget(dbp->mpf, &pg, 0, &h)) != 0)
00303 goto err;
00304 }
00305
00306
00307 found: *exactp = 1;
00308
00309
00310
00311
00312
00313
00314 if (recnop != NULL)
00315 *recnop = recno + (indx / P_INDX) + 1;
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 if (TYPE(h) == P_LBTREE) {
00328 if (LF_ISSET(S_DUPLAST))
00329 while (indx < (db_indx_t)(NUM_ENT(h) - P_INDX) &&
00330 h->inp[indx] == h->inp[indx + P_INDX])
00331 indx += P_INDX;
00332 else
00333 while (indx > 0 &&
00334 h->inp[indx] == h->inp[indx - P_INDX])
00335 indx -= P_INDX;
00336 }
00337
00338
00339
00340
00341
00342
00343
00344 if (LF_ISSET(S_DELNO)) {
00345 deloffset = TYPE(h) == P_LBTREE ? O_INDX : 0;
00346 if (LF_ISSET(S_DUPLAST))
00347 while (B_DISSET(GET_BKEYDATA(
00348 h, indx + deloffset)->type) && indx > 0 &&
00349 h->inp[indx] == h->inp[indx - adjust])
00350 indx -= adjust;
00351 else
00352 while (B_DISSET(GET_BKEYDATA(
00353 h, indx + deloffset)->type) &&
00354 indx < (db_indx_t)(NUM_ENT(h) - adjust) &&
00355 h->inp[indx] == h->inp[indx + adjust])
00356 indx += adjust;
00357
00358
00359
00360
00361
00362 if (B_DISSET(GET_BKEYDATA(h, indx + deloffset)->type))
00363 goto notfound;
00364 }
00365
00366 if (LF_ISSET(S_STK_ONLY)) {
00367 BT_STK_NUM(dbp->dbenv, cp, h, indx, ret);
00368 __LPUT(dbc, lock);
00369 (void)CDB_memp_fput(dbp->mpf, h, 0);
00370 } else {
00371 BT_STK_ENTER(dbp->dbenv, cp, h, indx, lock, lock_mode, ret);
00372 if (ret != 0)
00373 goto err;
00374 }
00375 return (0);
00376
00377 notfound:
00378
00379 (void)CDB_memp_fput(dbp->mpf, h, 0);
00380 (void)__TLPUT(dbc, lock);
00381 ret = DB_NOTFOUND;
00382
00383 err: if (cp->csp > cp->sp) {
00384 BT_STK_POP(cp);
00385 CDB___bam_stkrel(dbc, 0);
00386 }
00387 return (ret);
00388 }
00389
00390
00391
00392
00393
00394
00395
00396 int
00397 CDB___bam_stkrel(dbc, flags)
00398 DBC *dbc;
00399 u_int32_t flags;
00400 {
00401 BTREE_CURSOR *cp;
00402 DB *dbp;
00403 EPG *epg;
00404 int ret, t_ret;
00405
00406 dbp = dbc->dbp;
00407 cp = (BTREE_CURSOR *)dbc->internal;
00408
00409
00410
00411
00412
00413
00414
00415 for (ret = 0, epg = cp->sp; epg <= cp->csp; ++epg) {
00416 if (epg->page != NULL) {
00417 if (LF_ISSET(STK_CLRDBC) && cp->page == epg->page) {
00418 cp->page = NULL;
00419 cp->lock.off = LOCK_INVALID;
00420 }
00421 if ((t_ret = CDB_memp_fput(
00422 dbp->mpf, epg->page, 0)) != 0 && ret == 0)
00423 ret = t_ret;
00424 }
00425 if (epg->lock.off != LOCK_INVALID) {
00426 if (LF_ISSET(STK_NOLOCK))
00427 (void)__LPUT(dbc, epg->lock);
00428 else
00429 (void)__TLPUT(dbc, epg->lock);
00430 }
00431 }
00432
00433
00434 BT_STK_CLR(cp);
00435
00436 return (ret);
00437 }
00438
00439
00440
00441
00442
00443
00444
00445 int
00446 CDB___bam_stkgrow(dbenv, cp)
00447 DB_ENV *dbenv;
00448 BTREE_CURSOR *cp;
00449 {
00450 EPG *p;
00451 size_t entries;
00452 int ret;
00453
00454 entries = cp->esp - cp->sp;
00455
00456 if ((ret = CDB___os_calloc(dbenv, entries * 2, sizeof(EPG), &p)) != 0)
00457 return (ret);
00458 memcpy(p, cp->sp, entries * sizeof(EPG));
00459 if (cp->sp != cp->stack)
00460 CDB___os_free(cp->sp, entries * sizeof(EPG));
00461 cp->sp = p;
00462 cp->csp = p + entries;
00463 cp->esp = p + entries * 2;
00464 return (0);
00465 }