/**************************************************************//**
Resets a persistent cursor object, freeing ::old_rec_buf if it is
allocated and resetting the other members to their initial values. */ void
btr_pcur_reset( /*===========*/
btr_pcur_t* cursor) /*!< in, out: persistent cursor */
{
ut_free(cursor->old_rec_buf);
memset(&cursor->btr_cur.page_cur, 0, sizeof(page_cur_t));
cursor->old_rec_buf = NULL;
cursor->old_rec = NULL;
cursor->old_n_core_fields = 0;
cursor->old_n_fields = 0;
/**************************************************************//**
The position of the cursor is stored by taking an initial segment of the
record the cursor is positioned on, before, or after, and copying it to the
cursor data structure, or just setting a flag if the cursor id before the
first in an EMPTY tree, or after the last in an EMPTY tree. NOTE that the
page where the cursor is positioned must not be empty if the index tree is not totally empty! */ void
btr_pcur_store_position( /*====================*/
btr_pcur_t* cursor, /*!< in: persistent cursor */
mtr_t* mtr) /*!< in: mtr */
{
page_cur_t* page_cursor;
buf_block_t* block;
rec_t* rec;
dict_index_t* index;
ulint offs;
block = btr_pcur_get_block(cursor);
index = btr_cur_get_index(btr_pcur_get_btr_cur(cursor));
page_cursor = btr_pcur_get_page_cur(cursor);
rec = page_cur_get_rec(page_cursor);
offs = rec - block->page.frame;
ut_ad(block->page.id().page_no()
== page_get_page_no(block->page.frame));
ut_ad(block->page.buf_fix_count()); /* For spatial index, when we do positioning on parent bufferifnecessary,itmightnotholdlatches,butthe
tree must be locked to prevent change on the page */
ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_S_FIX
| MTR_MEMO_PAGE_X_FIX)
|| (index->is_spatial()
&& mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK)));
if (page_is_empty(block->page.frame)) { /* It must be an empty index tree; NOTE that in this case wedonotstorethemodify_clock,butalwaysdoasearch
if we restore the cursor position */
if (page_rec_is_supremum_low(offs)) {
rec = page_rec_get_prev(rec); if (UNIV_UNLIKELY(!rec || page_rec_is_infimum(rec))) {
ut_ad("corrupted index" == 0);
cursor->rel_pos = BTR_PCUR_AFTER_LAST_IN_TREE; return;
}
ut_ad(!page_rec_is_infimum(rec)); if (UNIV_UNLIKELY(rec_is_metadata(rec, *index))) { #if0/* MDEV-22867 had to relax this */ /* If the table is emptied during an ALGORITHM=NOCOPY DROPCOLUMN...thatisnotALGORITHM=INSTANT,
then we must preserve any instant ADD metadata. */
ut_ad(index->table->instant
|| block->page.id().page_no() != index->page); #endif
ut_ad(index->is_instant()
|| block->page.id().page_no() != index->page);
ut_ad(page_get_n_recs(block->page.frame) == 1);
ut_ad(page_is_leaf(block->page.frame));
ut_ad(!page_has_prev(block->page.frame));
cursor->rel_pos = BTR_PCUR_AFTER_LAST_IN_TREE; return;
}
cursor->old_n_fields = static_cast<uint16>(
dict_index_get_n_unique_in_tree(index)); if (index->is_spatial() && !page_rec_is_leaf(rec)) {
ut_ad(dict_index_get_n_unique_in_tree_nonleaf(index)
== DICT_INDEX_SPATIAL_NODEPTR_SIZE); /* For R-tree, we have to compare
the child page numbers as well. */
cursor->old_n_fields = DICT_INDEX_SPATIAL_NODEPTR_SIZE + 1;
}
/**************************************************************//**
Copies the stored position of a pcur to another pcur. */ void
btr_pcur_copy_stored_position( /*==========================*/
btr_pcur_t* pcur_receive, /*!< in: pcur which will receive the
position info */
btr_pcur_t* pcur_donate) /*!< in: pcur from which the info is
copied */
{
ut_free(pcur_receive->old_rec_buf);
memcpy(pcur_receive, pcur_donate, sizeof(btr_pcur_t));
/** Restores the stored position of a persistent cursor bufferfixing thepageandobtainingthespecifiedlatches.Ifthecursorposition wassavedwhenthe (1)cursorwaspositionedonauserrecord:thisfunctionrestoresthe positiontothelastrecordLESSOREQUALtothestoredrecord; (2)cursorwaspositionedonapageinfimumrecord:restoresthe positiontothelastrecordLESSthantheuserrecordwhichwasthe successorofthepageinfimum; (3)cursorwaspositionedonthepagesupremum:restorestothefirst recordGREATERthantheuserrecordwhichwasthepredecessorofthe supremum. (4)cursorwaspositionedbeforethefirstorafterthelastinan emptytree:restorestobeforefirstorafterthelastinthetree. @paramlatch_modeBTR_SEARCH_LEAF,... @parammtrmini-transaction @returnbtr_pcur_t::SAME_ALLcursorpositiononuserrecandpointson therecordwiththesamefieldvaluesasinthestoredrecord, btr_pcur_t::SAME_UNIQcursorpositionisonuserrecandpointsonthe recordwiththesameuniquefieldvaluesasinthestoredrecord, btr_pcur_t::NOT_SAMEcursorpositionisnotonuserrecorpointson
the record with not the samebuniq field values as in the stored */
btr_pcur_t::restore_status
btr_pcur_t::restore_position(btr_latch_mode restore_latch_mode, mtr_t *mtr)
{
dict_index_t* index;
dtuple_t* tuple;
page_cur_mode_t mode;
page_cur_mode_t old_mode;
mem_heap_t* heap;
if (UNIV_UNLIKELY
(rel_pos == BTR_PCUR_AFTER_LAST_IN_TREE
|| rel_pos == BTR_PCUR_BEFORE_FIRST_IN_TREE)) { /* In these cases we do not try an optimistic restoration,
but always do a search */
ut_ad(!cmp_rec_rec(old_rec,
rec, offsets1, offsets2,
index));
mem_heap_free(heap); #endif/* UNIV_DEBUG */ return restore_status::SAME_ALL;
} /* This is the same record as stored, mayneedtobeadjustedforBTR_PCUR_BEFORE/AFTER,
depending on search mode and direction. */ if (btr_pcur_is_on_user_rec(this)) {
pos_state
= BTR_PCUR_IS_POSITIONED_OPTIMISTIC;
} return restore_status::NOT_SAME;
}
}
/* If optimistic restoration did not succeed, open the cursor anew */
return restore_status::SAME_ALL;
} if (n_matched_fields >= index->n_uniq /* Unique indexes can contain "NULL" keys, and if all uniquefieldsareNULLandnotalltuple fieldsmatchtorecordfields,thentreatitasif restoredcursorpositionpointstotherecordwith
not the same unique key. */
&& !(index->n_nullable
&& dtuple_contains_null(tuple, index->n_uniq)))
ret_val= restore_status::SAME_UNIQ;
}
mem_heap_free(heap);
/* We have to store new position information, modify_clock etc., tothecursorbecauseitcannowbeonadifferentpage,therecord
under it may have been removed, etc. */
btr_pcur_store_position(this, mtr);
return ret_val;
}
/*********************************************************//**
Moves the persistent cursor to the first record on the next page. Releases the
latch on the current page, and bufferunfixes it. Note that there must not be
modifications on the current page, as then the x-latch can be released only in
mtr_commit. */
dberr_t
btr_pcur_move_to_next_page( /*=======================*/
btr_pcur_t* cursor, /*!< in: persistent cursor; must be on the
last record of the current page */
mtr_t* mtr) /*!< in: mtr */
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
ut_ad(btr_pcur_is_after_last_on_page(cursor));
constauto s = mtr->get_savepoint();
mtr->rollback_to_savepoint(s - 2, s - 1); if (first_access) {
buf_read_ahead_linear(next_block->page.id());
} return DB_SUCCESS;
}
MY_ATTRIBUTE((nonnull,warn_unused_result)) /*********************************************************//**
Moves the persistent cursor backward if it is on the first record of the page.
Commits mtr. Note that to prevent a possible deadlock, the operation
first stores the position of the cursor, commits mtr, acquires the necessary
latches and restores the cursor position again before returning. The
alphabetical position of the cursor is guaranteed to be sensible on return, but it may happen that the cursor is not positioned on the last
record of any page, because the structure of the tree may have changed
during the time when the cursor had no latches. */ static bool
btr_pcur_move_backward_from_page( /*=============================*/
btr_pcur_t* cursor, /*!< in: persistent cursor, must be on the first
record of the current page */
mtr_t* mtr) /*!< in: mtr */
{
ut_ad(btr_pcur_is_before_first_on_page(cursor));
ut_ad(!btr_pcur_is_before_first_in_tree(cursor));
btr_pcur_store_position(cursor, mtr);
mtr_commit(mtr);
mtr_start(mtr);
if (UNIV_UNLIKELY(cursor->restore_position(BTR_SEARCH_PREV, mtr)
== btr_pcur_t::CORRUPTED)) { returntrue;
}
buf_block_t* block = mtr->at_savepoint(0);
ut_ad(block == btr_pcur_get_block(cursor)); const page_t* const page = block->page.frame; /* btr_pcur_optimistic_latch_leaves() will acquire a latch on theprecedingpageifoneexists; ifthatfails,btr_cur_t::search_leaf()invokedby btr_pcur_open_with_no_init()willalsoacquirealatchonthe
succeeding page. Our caller only needs one page latch. */
ut_ad(mtr->get_savepoint() <= 3);
if (page_has_prev(page)) {
buf_block_t* const left_block = mtr->at_savepoint(1);
ut_ad(!memcmp_aligned<4>(page + FIL_PAGE_OFFSET,
left_block->page.frame
+ FIL_PAGE_NEXT, 4)); if (btr_pcur_is_before_first_on_page(cursor)) { /* Reposition on the previous page. */
page_cur_set_after_last(left_block,
&cursor->btr_cur.page_cur); /* Release the right sibling. */
mtr->rollback_to_savepoint(0, 1);
block = left_block;
}
}
/*********************************************************//**
Moves the persistent cursor to the previous record in the tree. If no records
are left, the cursor stays 'before first in tree'.
@returnTRUEif the cursor was not before first in tree */ bool
btr_pcur_move_to_prev( /*==================*/
btr_pcur_t* cursor, /*!< in: persistent cursor; NOTE that the
function may release the page latch */
mtr_t* mtr) /*!< in: mtr */
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode == BTR_SEARCH_LEAF);
cursor->old_rec = nullptr;
if (btr_pcur_is_before_first_on_page(cursor)) { return (!btr_pcur_is_before_first_in_tree(cursor)
&& !btr_pcur_move_backward_from_page(cursor, mtr));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.