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__delete_8c-source.html,v 1.1 2008/06/08 10:13:35 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 int
00068 CDB___bam_delete(dbp, txn, key, flags)
00069 DB *dbp;
00070 DB_TXN *txn;
00071 DBT *key;
00072 u_int32_t flags;
00073 {
00074 DBC *dbc;
00075 DBT lkey;
00076 DBT data;
00077 u_int32_t f_init, f_next;
00078 int ret, t_ret;
00079
00080 PANIC_CHECK(dbp->dbenv);
00081 DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->del");
00082
00083
00084 if ((ret =
00085 CDB___db_delchk(dbp, key, flags, F_ISSET(dbp, DB_AM_RDONLY))) != 0)
00086 return (ret);
00087
00088
00089 if ((ret = dbp->cursor(dbp, txn, &dbc, DB_WRITELOCK)) != 0)
00090 return (ret);
00091
00092 DEBUG_LWRITE(dbc, txn, "bam_delete", key, NULL, flags);
00093
00094
00095
00096
00097
00098
00099
00100 memset(&lkey, 0, sizeof(lkey));
00101 F_SET(&lkey, DB_DBT_USERMEM | DB_DBT_PARTIAL);
00102 memset(&data, 0, sizeof(data));
00103 F_SET(&data, DB_DBT_USERMEM | DB_DBT_PARTIAL);
00104
00105
00106
00107
00108
00109 f_init = DB_SET;
00110 f_next = DB_NEXT_DUP;
00111 if (STD_LOCKING(dbc)) {
00112 f_init |= DB_RMW;
00113 f_next |= DB_RMW;
00114 }
00115
00116
00117 if ((ret = dbc->c_get(dbc, key, &data, f_init)) != 0)
00118 goto err;
00119 for (;;) {
00120 if ((ret = dbc->c_del(dbc, 0)) != 0)
00121 goto err;
00122 if ((ret = dbc->c_get(dbc, &lkey, &data, f_next)) != 0) {
00123 if (ret == DB_NOTFOUND) {
00124 ret = 0;
00125 break;
00126 }
00127 goto err;
00128 }
00129 }
00130
00131 err:
00132 if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
00133 ret = t_ret;
00134
00135 return (ret);
00136 }
00137
00138
00139
00140
00141
00142
00143
00144 int
00145 CDB___bam_ditem(dbc, h, indx)
00146 DBC *dbc;
00147 PAGE *h;
00148 u_int32_t indx;
00149 {
00150 BINTERNAL *bi;
00151 BKEYDATA *bk;
00152 DB *dbp;
00153 u_int32_t nbytes;
00154 int ret;
00155
00156 dbp = dbc->dbp;
00157
00158 switch (TYPE(h)) {
00159 case P_IBTREE:
00160 bi = GET_BINTERNAL(h, indx);
00161 switch (B_TYPE(bi->type)) {
00162 case B_DUPLICATE:
00163 case B_KEYDATA:
00164 nbytes = BINTERNAL_SIZE(bi->len);
00165 break;
00166 case B_OVERFLOW:
00167 nbytes = BINTERNAL_SIZE(bi->len);
00168 if ((ret =
00169 CDB___db_doff(dbc, ((BOVERFLOW *)bi->data)->pgno)) != 0)
00170 return (ret);
00171 break;
00172 default:
00173 return (CDB___db_pgfmt(dbp, PGNO(h)));
00174 }
00175 break;
00176 case P_IRECNO:
00177 nbytes = RINTERNAL_SIZE;
00178 break;
00179 case P_LBTREE:
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 if ((indx % 2) == 0) {
00190
00191
00192
00193
00194
00195
00196 if (indx + P_INDX < (u_int32_t)NUM_ENT(h) &&
00197 h->inp[indx] == h->inp[indx + P_INDX])
00198 return (CDB___bam_adjindx(dbc,
00199 h, indx, indx + O_INDX, 0));
00200
00201
00202
00203
00204
00205 if (indx > 0 && h->inp[indx] == h->inp[indx - P_INDX])
00206 return (CDB___bam_adjindx(dbc,
00207 h, indx, indx - P_INDX, 0));
00208 }
00209
00210 case P_LDUP:
00211 case P_LRECNO:
00212 bk = GET_BKEYDATA(h, indx);
00213 switch (B_TYPE(bk->type)) {
00214 case B_DUPLICATE:
00215 nbytes = BOVERFLOW_SIZE;
00216 break;
00217 case B_OVERFLOW:
00218 nbytes = BOVERFLOW_SIZE;
00219 if ((ret = CDB___db_doff(
00220 dbc, (GET_BOVERFLOW(h, indx))->pgno)) != 0)
00221 return (ret);
00222 break;
00223 case B_KEYDATA:
00224 nbytes = BKEYDATA_SIZE(bk->len);
00225 break;
00226 default:
00227 return (CDB___db_pgfmt(dbp, PGNO(h)));
00228 }
00229 break;
00230 default:
00231 return (CDB___db_pgfmt(dbp, PGNO(h)));
00232 }
00233
00234
00235 if ((ret = CDB___db_ditem(dbc, h, indx, nbytes)) != 0)
00236 return (ret);
00237 if ((ret = CDB_memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY)) != 0)
00238 return (ret);
00239
00240 return (0);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 int
00250 CDB___bam_adjindx(dbc, h, indx, indx_copy, is_insert)
00251 DBC *dbc;
00252 PAGE *h;
00253 u_int32_t indx, indx_copy;
00254 int is_insert;
00255 {
00256 DB *dbp;
00257 db_indx_t copy;
00258 int ret;
00259
00260 dbp = dbc->dbp;
00261
00262
00263 if (DB_LOGGING(dbc) &&
00264 (ret = CDB___bam_adj_log(dbp->dbenv, dbc->txn, &LSN(h),
00265 0, dbp->log_fileid, PGNO(h), &LSN(h), indx, indx_copy,
00266 (u_int32_t)is_insert)) != 0)
00267 return (ret);
00268
00269
00270 if (is_insert) {
00271 copy = h->inp[indx_copy];
00272 if (indx != NUM_ENT(h))
00273 memmove(&h->inp[indx + O_INDX], &h->inp[indx],
00274 sizeof(db_indx_t) * (NUM_ENT(h) - indx));
00275 h->inp[indx] = copy;
00276 ++NUM_ENT(h);
00277 } else {
00278 --NUM_ENT(h);
00279 if (indx != NUM_ENT(h))
00280 memmove(&h->inp[indx], &h->inp[indx + O_INDX],
00281 sizeof(db_indx_t) * (NUM_ENT(h) - indx));
00282 }
00283 if ((ret = CDB_memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY)) != 0)
00284 return (ret);
00285
00286 return (0);
00287 }
00288
00289
00290
00291
00292
00293
00294
00295 int
00296 CDB___bam_dpages(dbc, stack_epg)
00297 DBC *dbc;
00298 EPG *stack_epg;
00299 {
00300 BTREE_CURSOR *cp;
00301 DB *dbp;
00302 DBT a, b;
00303 DB_LOCK c_lock, p_lock;
00304 EPG *epg;
00305 PAGE *child, *parent;
00306 db_indx_t nitems;
00307 db_pgno_t pgno, root_pgno;
00308 db_recno_t rcnt;
00309 int done, ret, t_ret;
00310
00311 dbp = dbc->dbp;
00312 cp = (BTREE_CURSOR *)dbc->internal;
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 ret = 0;
00328 for (epg = cp->sp; epg < stack_epg; ++epg) {
00329 if ((t_ret =
00330 CDB_memp_fput(dbp->mpf, epg->page, 0)) != 0 && ret == 0)
00331 ret = t_ret;
00332 (void)__TLPUT(dbc, epg->lock);
00333 }
00334 if (ret != 0)
00335 goto err;
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 if ((ret = CDB___db_relink(dbc, DB_REM_PAGE, cp->csp->page, NULL, 1)) != 0)
00347 goto err;
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 if ((ret = CDB___bam_ditem(dbc, epg->page, epg->indx)) != 0)
00358 goto err;
00359 CDB___bam_ca_di(dbp, PGNO(epg->page), epg->indx, -1);
00360
00361 pgno = PGNO(epg->page);
00362 nitems = NUM_ENT(epg->page);
00363
00364 if ((ret = CDB_memp_fput(dbp->mpf, epg->page, 0)) != 0)
00365 goto err_inc;
00366 (void)__TLPUT(dbc, epg->lock);
00367
00368
00369 while (++epg <= cp->csp) {
00370
00371
00372
00373
00374
00375
00376 if (NUM_ENT(epg->page) != 0) {
00377 DB_ASSERT(NUM_ENT(epg->page) == 1);
00378
00379 if ((ret = CDB___bam_ditem(dbc, epg->page, epg->indx)) != 0)
00380 goto err;
00381 }
00382
00383 if ((ret = CDB___db_free(dbc, epg->page)) != 0)
00384 goto err_inc;
00385 (void)__TLPUT(dbc, epg->lock);
00386 }
00387
00388 if (0) {
00389 err_inc: ++epg;
00390 err: for (; epg <= cp->csp; ++epg) {
00391 (void)CDB_memp_fput(dbp->mpf, epg->page, 0);
00392 (void)__TLPUT(dbc, epg->lock);
00393 }
00394 BT_STK_CLR(cp);
00395 return (ret);
00396 }
00397 BT_STK_CLR(cp);
00398
00399
00400
00401
00402
00403
00404
00405
00406 root_pgno = cp->root;
00407 if (pgno != root_pgno || nitems != 1)
00408 return (0);
00409
00410 for (done = 0; !done;) {
00411
00412 parent = child = NULL;
00413 p_lock.off = c_lock.off = LOCK_INVALID;
00414
00415
00416 pgno = root_pgno;
00417 if ((ret =
00418 CDB___db_lget(dbc, 0, pgno, DB_LOCK_WRITE, 0, &p_lock)) != 0)
00419 goto stop;
00420 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &parent)) != 0)
00421 goto stop;
00422
00423 if (NUM_ENT(parent) != 1)
00424 goto stop;
00425
00426 switch (TYPE(parent)) {
00427 case P_IBTREE:
00428 pgno = GET_BINTERNAL(parent, 0)->pgno;
00429 break;
00430 case P_IRECNO:
00431 pgno = GET_RINTERNAL(parent, 0)->pgno;
00432 break;
00433 default:
00434 goto stop;
00435 }
00436
00437
00438 if ((ret =
00439 CDB___db_lget(dbc, 0, pgno, DB_LOCK_WRITE, 0, &c_lock)) != 0)
00440 goto stop;
00441 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &child)) != 0)
00442 goto stop;
00443
00444
00445 if (DB_LOGGING(dbc)) {
00446 memset(&a, 0, sizeof(a));
00447 a.data = child;
00448 a.size = dbp->pgsize;
00449 memset(&b, 0, sizeof(b));
00450 b.data = P_ENTRY(parent, 0);
00451 b.size = BINTERNAL_SIZE(((BINTERNAL *)b.data)->len);
00452 CDB___bam_rsplit_log(dbp->dbenv, dbc->txn,
00453 &child->lsn, 0, dbp->log_fileid, PGNO(child), &a,
00454 PGNO(parent), RE_NREC(parent), &b, &parent->lsn);
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 COMPQUIET(rcnt, 0);
00469 if (F_ISSET(cp, C_RECNUM) && LEVEL(child) > LEAFLEVEL)
00470 rcnt = RE_NREC(parent);
00471 memcpy(parent, child, dbp->pgsize);
00472 PGNO(parent) = root_pgno;
00473 if (F_ISSET(cp, C_RECNUM) && LEVEL(child) > LEAFLEVEL)
00474 RE_NREC_SET(parent, rcnt);
00475
00476
00477 CDB_memp_fset(dbp->mpf, parent, DB_MPOOL_DIRTY);
00478 CDB_memp_fset(dbp->mpf, child, DB_MPOOL_DIRTY);
00479
00480
00481 CDB___bam_ca_rsplit(dbp, PGNO(child), root_pgno);
00482
00483
00484
00485
00486
00487
00488 (void)CDB___db_free(dbc, child);
00489 child = NULL;
00490
00491 if (0) {
00492 stop: done = 1;
00493 }
00494 if (p_lock.off != LOCK_INVALID)
00495 (void)__TLPUT(dbc, p_lock);
00496 if (parent != NULL)
00497 CDB_memp_fput(dbp->mpf, parent, 0);
00498 if (c_lock.off != LOCK_INVALID)
00499 (void)__TLPUT(dbc, c_lock);
00500 if (child != NULL)
00501 CDB_memp_fput(dbp->mpf, child, 0);
00502 }
00503
00504 return (0);
00505 }