2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996-2009 Oracle. All rights reserved.
12 #include "dbinc/db_page.h"
13 #include "dbinc/btree.h"
16 static int __bam_opd_cursor __P((DB *, DBC *, db_pgno_t, u_int32_t, u_int32_t));
19 * Cursor adjustments are logged if they are for subtransactions. This is
20 * because it's possible for a subtransaction to adjust cursors which will
21 * still be active after the subtransaction aborts, and so which must be
22 * restored to their previous locations. Cursors that can be both affected
23 * by our cursor adjustments and active after our transaction aborts can
24 * only be found in our parent transaction -- cursors in other transactions,
25 * including other child transactions of our parent, must have conflicting
26 * locker IDs, and so cannot be affected by adjustments in this transaction.
31 * Update the cursors when items are deleted and when already deleted
32 * items are overwritten. Return the number of relevant cursors found.
34 * PUBLIC: int __bam_ca_delete __P((DB *, db_pgno_t, u_int32_t, int, int *));
37 __bam_ca_delete(dbp, pgno, indx, delete, countp)
47 int count; /* !!!: Has to contain max number of cursors. */
52 * Adjust the cursors. We have the page write locked, so the
53 * only other cursors that can be pointing at a page are
54 * those in the same thread of control. Unfortunately, we don't
55 * know that they're using the same DB handle, so traverse
56 * all matching DB handles in the same ENV, then all cursors
57 * on each matching DB handle.
59 * Each cursor is single-threaded, so we only need to lock the
60 * list of DBs and then the list of cursors in each DB.
62 MUTEX_LOCK(env, env->mtx_dblist);
63 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
65 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
66 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
67 MUTEX_LOCK(env, dbp->mutex);
68 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
69 cp = (BTREE_CURSOR *)dbc->internal;
70 if (cp->pgno == pgno && cp->indx == indx &&
71 !MVCC_SKIP_CURADJ(dbc, pgno)) {
73 * [#8032] This assert is checking
74 * for possible race conditions where we
75 * hold a cursor position without a lock.
76 * Unfortunately, there are paths in the
77 * Btree code that do not satisfy these
78 * conditions. None of them are known to
79 * be a problem, but this assert should
80 * be re-activated when the Btree stack
82 DB_ASSERT(env, !STD_LOCKING(dbc) ||
83 cp->lock_mode != DB_LOCK_NG);
88 * If we're deleting the item, we can't
89 * keep a streaming offset cached.
91 cp->stream_start_pgno = PGNO_INVALID;
95 #ifdef HAVE_COMPRESSION
97 * We also set the C_COMPRESS_MODIFIED flag,
98 * which prompts the compression code to look
99 * for it's current entry again if it needs to.
101 * The flag isn't cleared, because the
102 * compression code still needs to do that even
103 * for an entry that becomes undeleted.
105 * This flag also needs to be set if an entry is
106 * updated, but since the compression code
107 * always deletes before an update, setting it
108 * here is sufficient.
110 F_SET(cp, C_COMPRESS_MODIFIED);
116 MUTEX_UNLOCK(env, dbp->mutex);
118 MUTEX_UNLOCK(env, env->mtx_dblist);
127 * Return if any relevant cursors found.
129 * PUBLIC: int __ram_ca_delete __P((DB *, db_pgno_t, int *));
132 __ram_ca_delete(dbp, root_pgno, foundp)
145 * Review the cursors. See the comment in __bam_ca_delete().
147 MUTEX_LOCK(env, env->mtx_dblist);
148 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
150 found == 0 && ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
151 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
152 MUTEX_LOCK(env, dbp->mutex);
153 TAILQ_FOREACH(dbc, &ldbp->active_queue, links)
154 if (dbc->internal->root == root_pgno &&
155 !MVCC_SKIP_CURADJ(dbc, root_pgno)) {
159 MUTEX_UNLOCK(env, dbp->mutex);
161 MUTEX_UNLOCK(env, env->mtx_dblist);
169 * Adjust the cursors during a delete or insert.
171 * PUBLIC: int __bam_ca_di __P((DBC *, db_pgno_t, u_int32_t, int));
174 __bam_ca_di(my_dbc, pgno, indx, adjust)
191 my_txn = IS_SUBTRANSACTION(my_dbc->txn) ? my_dbc->txn : NULL;
194 * Adjust the cursors. See the comment in __bam_ca_delete().
196 MUTEX_LOCK(env, env->mtx_dblist);
197 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
199 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
200 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
201 MUTEX_LOCK(env, dbp->mutex);
202 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
203 if (dbc->dbtype == DB_RECNO)
206 if (cp->pgno == pgno && cp->indx >= indx &&
207 (dbc == my_dbc || !MVCC_SKIP_CURADJ(dbc, pgno))) {
208 /* Cursor indices should never be negative. */
209 DB_ASSERT(env, cp->indx != 0 || adjust > 0);
211 DB_ASSERT(env, !STD_LOCKING(dbc) ||
212 cp->lock_mode != DB_LOCK_NG);
215 if (my_txn != NULL && dbc->txn != my_txn)
219 MUTEX_UNLOCK(env, dbp->mutex);
221 MUTEX_UNLOCK(env, env->mtx_dblist);
223 if (found != 0 && DBC_LOGGING(my_dbc)) {
224 if ((ret = __bam_curadj_log(dbp, my_dbc->txn, &lsn, 0,
225 DB_CA_DI, pgno, 0, 0, (u_int32_t)adjust, indx, 0)) != 0)
233 * __bam_opd_cursor -- create a new opd cursor.
236 __bam_opd_cursor(dbp, dbc, first, tpgno, ti)
242 BTREE_CURSOR *cp, *orig_cp;
246 orig_cp = (BTREE_CURSOR *)dbc->internal;
250 * Allocate a new cursor and create the stack. If duplicates
251 * are sorted, we've just created an off-page duplicate Btree.
252 * If duplicates aren't sorted, we've just created a Recno tree.
254 * Note that in order to get here at all, there shouldn't be
255 * an old off-page dup cursor--to augment the checking dbc_newopd
256 * will do, assert this.
258 DB_ASSERT(dbp->env, orig_cp->opd == NULL);
259 if ((ret = __dbc_newopd(dbc, tpgno, orig_cp->opd, &dbc_nopd)) != 0)
262 cp = (BTREE_CURSOR *)dbc_nopd->internal;
266 if (dbp->dup_compare == NULL) {
268 * Converting to off-page Recno trees is tricky. The
269 * record number for the cursor is the index + 1 (to
270 * convert to 1-based record numbers).
276 * Transfer the deleted flag from the top-level cursor to the
279 if (F_ISSET(orig_cp, C_DELETED)) {
280 F_SET(cp, C_DELETED);
281 F_CLR(orig_cp, C_DELETED);
284 /* Stack the cursors and reset the initial cursor's index. */
285 orig_cp->opd = dbc_nopd;
286 orig_cp->indx = first;
292 * Adjust the cursors when moving items from a leaf page to a duplicates
295 * PUBLIC: int __bam_ca_dup __P((DBC *,
296 * PUBLIC: u_int32_t, db_pgno_t, u_int32_t, db_pgno_t, u_int32_t));
299 __bam_ca_dup(my_dbc, first, fpgno, fi, tpgno, ti)
301 db_pgno_t fpgno, tpgno;
302 u_int32_t first, fi, ti;
304 BTREE_CURSOR *orig_cp;
310 int found, ret, t_ret;
314 my_txn = IS_SUBTRANSACTION(my_dbc->txn) ? my_dbc->txn : NULL;
318 * Adjust the cursors. See the comment in __bam_ca_delete().
320 MUTEX_LOCK(env, env->mtx_dblist);
321 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
323 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
324 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
325 loop: MUTEX_LOCK(env, dbp->mutex);
326 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
327 /* Find cursors pointing to this record. */
328 orig_cp = (BTREE_CURSOR *)dbc->internal;
329 if (orig_cp->pgno != fpgno || orig_cp->indx != fi ||
330 MVCC_SKIP_CURADJ(dbc, fpgno))
334 * Since we rescan the list see if this is already
337 if (orig_cp->opd != NULL)
340 MUTEX_UNLOCK(env, dbp->mutex);
342 DB_ASSERT(env, !STD_LOCKING(dbc) ||
343 orig_cp->lock_mode != DB_LOCK_NG);
345 if ((ret = __bam_opd_cursor(dbp,
346 dbc, first, tpgno, ti)) != 0)
348 if (my_txn != NULL && dbc->txn != my_txn)
350 /* We released the mutex to get a cursor, start over. */
353 MUTEX_UNLOCK(env, dbp->mutex);
355 err: MUTEX_UNLOCK(env, env->mtx_dblist);
357 if (found != 0 && DBC_LOGGING(my_dbc)) {
358 if ((t_ret = __bam_curadj_log(dbp, my_dbc->txn,
359 &lsn, 0, DB_CA_DUP, fpgno, tpgno, 0, first, fi, ti)) != 0 &&
368 * __bam_ca_undodup --
369 * Adjust the cursors when returning items to a leaf page
370 * from a duplicate page.
371 * Called only during undo processing.
373 * PUBLIC: int __bam_ca_undodup __P((DB *,
374 * PUBLIC: u_int32_t, db_pgno_t, u_int32_t, u_int32_t));
377 __bam_ca_undodup(dbp, first, fpgno, fi, ti)
380 u_int32_t first, fi, ti;
382 BTREE_CURSOR *orig_cp;
392 * Adjust the cursors. See the comment in __bam_ca_delete().
394 MUTEX_LOCK(env, env->mtx_dblist);
395 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
397 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
398 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
399 loop: MUTEX_LOCK(env, dbp->mutex);
400 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
401 orig_cp = (BTREE_CURSOR *)dbc->internal;
404 * A note on the orig_cp->opd != NULL requirement here:
405 * it's possible that there's a cursor that refers to
406 * the same duplicate set, but which has no opd cursor,
407 * because it refers to a different item and we took
408 * care of it while processing a previous record.
410 if (orig_cp->pgno != fpgno ||
411 orig_cp->indx != first ||
412 orig_cp->opd == NULL || ((BTREE_CURSOR *)
413 orig_cp->opd->internal)->indx != ti ||
414 MVCC_SKIP_CURADJ(dbc, fpgno))
416 MUTEX_UNLOCK(env, dbp->mutex);
417 if ((ret = __dbc_close(orig_cp->opd)) != 0)
422 * We released the mutex to free a cursor,
427 MUTEX_UNLOCK(env, dbp->mutex);
429 err: MUTEX_UNLOCK(env, env->mtx_dblist);
436 * Adjust the cursors when doing reverse splits.
438 * PUBLIC: int __bam_ca_rsplit __P((DBC *, db_pgno_t, db_pgno_t));
441 __bam_ca_rsplit(my_dbc, fpgno, tpgno)
443 db_pgno_t fpgno, tpgno;
454 my_txn = IS_SUBTRANSACTION(my_dbc->txn) ? my_dbc->txn : NULL;
457 * Adjust the cursors. See the comment in __bam_ca_delete().
459 MUTEX_LOCK(env, env->mtx_dblist);
460 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
462 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
463 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
464 MUTEX_LOCK(env, dbp->mutex);
465 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
466 if (dbc->dbtype == DB_RECNO)
468 if (dbc->internal->pgno == fpgno &&
469 !MVCC_SKIP_CURADJ(dbc, fpgno)) {
470 dbc->internal->pgno = tpgno;
472 DB_ASSERT(env, !STD_LOCKING(dbc) ||
473 dbc->internal->lock_mode != DB_LOCK_NG);
475 if (my_txn != NULL && dbc->txn != my_txn)
479 MUTEX_UNLOCK(env, dbp->mutex);
481 MUTEX_UNLOCK(env, env->mtx_dblist);
483 if (found != 0 && DBC_LOGGING(my_dbc)) {
484 if ((ret = __bam_curadj_log(dbp, my_dbc->txn,
485 &lsn, 0, DB_CA_RSPLIT, fpgno, tpgno, 0, 0, 0, 0)) != 0)
493 * Adjust the cursors when splitting a page.
495 * PUBLIC: int __bam_ca_split __P((DBC *,
496 * PUBLIC: db_pgno_t, db_pgno_t, db_pgno_t, u_int32_t, int));
499 __bam_ca_split(my_dbc, ppgno, lpgno, rpgno, split_indx, cleft)
501 db_pgno_t ppgno, lpgno, rpgno;
502 u_int32_t split_indx;
515 my_txn = IS_SUBTRANSACTION(my_dbc->txn) ? my_dbc->txn : NULL;
518 * Adjust the cursors. See the comment in __bam_ca_delete().
520 * If splitting the page that a cursor was on, the cursor has to be
521 * adjusted to point to the same record as before the split. Most
522 * of the time we don't adjust pointers to the left page, because
523 * we're going to copy its contents back over the original page. If
524 * the cursor is on the right page, it is decremented by the number of
525 * records split to the left page.
527 MUTEX_LOCK(env, env->mtx_dblist);
528 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
530 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
531 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
532 MUTEX_LOCK(env, dbp->mutex);
533 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
534 if (dbc->dbtype == DB_RECNO)
537 if (cp->pgno == ppgno &&
538 !MVCC_SKIP_CURADJ(dbc, ppgno)) {
540 DB_ASSERT(env, !STD_LOCKING(dbc) ||
541 cp->lock_mode != DB_LOCK_NG);
543 if (my_txn != NULL && dbc->txn != my_txn)
545 if (cp->indx < split_indx) {
550 cp->indx -= split_indx;
554 MUTEX_UNLOCK(env, dbp->mutex);
556 MUTEX_UNLOCK(env, env->mtx_dblist);
558 if (found != 0 && DBC_LOGGING(my_dbc)) {
559 if ((ret = __bam_curadj_log(dbp,
560 my_dbc->txn, &lsn, 0, DB_CA_SPLIT, ppgno, rpgno,
561 cleft ? lpgno : PGNO_INVALID, 0, split_indx, 0)) != 0)
569 * __bam_ca_undosplit --
570 * Adjust the cursors when undoing a split of a page.
571 * If we grew a level we will execute this for both the
572 * left and the right pages.
573 * Called only during undo processing.
575 * PUBLIC: int __bam_ca_undosplit __P((DB *,
576 * PUBLIC: db_pgno_t, db_pgno_t, db_pgno_t, u_int32_t));
579 __bam_ca_undosplit(dbp, frompgno, topgno, lpgno, split_indx)
581 db_pgno_t frompgno, topgno, lpgno;
582 u_int32_t split_indx;
592 * Adjust the cursors. See the comment in __bam_ca_delete().
594 * When backing out a split, we move the cursor back
595 * to the original offset and bump it by the split_indx.
597 MUTEX_LOCK(env, env->mtx_dblist);
598 FIND_FIRST_DB_MATCH(env, dbp, ldbp);
600 ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
601 ldbp = TAILQ_NEXT(ldbp, dblistlinks)) {
602 MUTEX_LOCK(env, dbp->mutex);
603 TAILQ_FOREACH(dbc, &ldbp->active_queue, links) {
604 if (dbc->dbtype == DB_RECNO)
607 if (cp->pgno == topgno &&
608 !MVCC_SKIP_CURADJ(dbc, topgno)) {
610 cp->indx += split_indx;
611 } else if (cp->pgno == lpgno &&
612 !MVCC_SKIP_CURADJ(dbc, lpgno))
615 MUTEX_UNLOCK(env, dbp->mutex);
617 MUTEX_UNLOCK(env, env->mtx_dblist);