| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/btr0pcur.h |
| 29 |
The index tree persistent cursor |
| 30 |
|
| 31 |
Created 2/23/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef btr0pcur_h |
| 35 |
#define btr0pcur_h |
| 36 |
|
| 37 |
#include "btr0btr.h" |
| 38 |
#include "btr0cur.h" |
| 39 |
#include "btr0types.h" |
| 40 |
#include "buf0block_hint.h" |
| 41 |
#include "data0data.h" |
| 42 |
#include "dict0dict.h" |
| 43 |
#include "mtr0mtr.h" |
| 44 |
#include "page0cur.h" |
| 45 |
#include "univ.i" |
| 46 |
#ifndef UNIV_HOTBACKUP |
| 47 |
#include "gis0rtree.h" |
| 48 |
#endif /* UNIV_HOTBACKUP */ |
| 49 |
|
| 50 |
/** Relative positions for a stored cursor position */ |
| 51 |
enum btr_pcur_pos_t { |
| 52 |
BTR_PCUR_UNSET = 0, |
| 53 |
BTR_PCUR_ON = 1, |
| 54 |
BTR_PCUR_BEFORE = 2, |
| 55 |
BTR_PCUR_AFTER = 3, |
| 56 |
/* Note that if the tree is not empty, btr_pcur::store_position does |
| 57 |
not use the following, but only uses the above three alternatives, |
| 58 |
where the position is stored relative to a specific record: this makes |
| 59 |
implementation of a scroll cursor easier */ |
| 60 |
BTR_PCUR_BEFORE_FIRST_IN_TREE = 4, /* in an empty tree */ |
| 61 |
BTR_PCUR_AFTER_LAST_IN_TREE = 5 /* in an empty tree */ |
| 62 |
}; |
| 63 |
|
| 64 |
/** Position state of persistent B-tree cursor. */ |
| 65 |
enum pcur_pos_t { |
| 66 |
|
| 67 |
/** The persistent cursor is not positioned. */ |
| 68 |
BTR_PCUR_NOT_POSITIONED = 0, |
| 69 |
|
| 70 |
/** The persistent cursor was previously positioned. |
| 71 |
TODO: currently, the state can be BTR_PCUR_IS_POSITIONED, |
| 72 |
though it really should be BTR_PCUR_WAS_POSITIONED, |
| 73 |
because we have no obligation to commit the cursor with |
| 74 |
mtr; similarly latch_mode may be out of date. This can |
| 75 |
lead to problems if btr_pcur is not used the right way; |
| 76 |
all current code should be ok. */ |
| 77 |
BTR_PCUR_WAS_POSITIONED, |
| 78 |
|
| 79 |
/** The persistent cursor is positioned by optimistic get to the same |
| 80 |
record as it was positioned at. Not used for rel_pos == BTR_PCUR_ON. |
| 81 |
It may need adjustment depending on previous/current search direction |
| 82 |
and rel_pos. */ |
| 83 |
BTR_PCUR_IS_POSITIONED_OPTIMISTIC, |
| 84 |
|
| 85 |
/** The persistent cursor is positioned by index search. |
| 86 |
Or optimistic get for rel_pos == BTR_PCUR_ON. */ |
| 87 |
BTR_PCUR_IS_POSITIONED |
| 88 |
}; |
| 89 |
|
| 90 |
/* Import tablespace context for persistent B-tree cursor. */ |
| 91 |
struct import_ctx_t { |
| 92 |
/* true if cursor fails to move to the next page during import. */ |
| 93 |
bool is_error{false}; |
| 94 |
}; |
| 95 |
|
| 96 |
/* The persistent B-tree cursor structure. This is used mainly for SQL |
| 97 |
selects, updates, and deletes. */ |
| 98 |
|
| 99 |
struct btr_pcur_t { |
| 100 |
/** Sets the old_rec_buf field to nullptr. |
| 101 |
@param[in] read_level read level where the cursor would be positioned or |
| 102 |
re-positioned. */ |
| 103 |
void init(size_t read_level = 0); |
| 104 |
|
| 105 |
/** @return the index of this persistent cursor */ |
| 106 |
dict_index_t *index() { return (m_btr_cur.index); } |
| 107 |
|
| 108 |
/** Positions a cursor at a randomly chosen position within a B-tree. |
| 109 |
@param[in] index Index to position on. |
| 110 |
@param[in] latch_mode BTR_SEARCH_LEAF, ... |
| 111 |
@param[in,out] mtr Mini-transaction. |
| 112 |
@param[in] location Location from where called. |
| 113 |
@return true if the index is available and we have put the cursor, false |
| 114 |
if the index is unavailable */ |
| 115 |
bool set_random_position(dict_index_t *index, ulint latch_mode, mtr_t *mtr, |
| 116 |
ut::Location location); |
| 117 |
|
| 118 |
/** Opens a persistent cursor at either end of an index. |
| 119 |
@param[in] from_left True if open to the low end, false |
| 120 |
if to the high end. |
| 121 |
@param[in] index Index |
| 122 |
@param[in] latch_mode Latch mode |
| 123 |
@param[in] init_pcur Whether to initialize pcur. |
| 124 |
@param[in] level Level to search for (0=leaf). |
| 125 |
@param[in,out] mtr Mini-transaction */ |
| 126 |
void open_at_side(bool from_left, dict_index_t *index, ulint latch_mode, |
| 127 |
bool init_pcur, ulint level, mtr_t *mtr); |
| 128 |
|
| 129 |
/** Opens a persistent cursor at first leaf page (low end). It will not call |
| 130 |
init(). |
| 131 |
@param[in] index Index |
| 132 |
@param[in] latch_mode Latch mode |
| 133 |
@param[in,out] mtr Mini-transaction */ |
| 134 |
void begin_leaf(dict_index_t *index, ulint latch_mode, mtr_t *mtr) { |
| 135 |
open_at_side(true, index, latch_mode, false, 0, mtr); |
| 136 |
} |
| 137 |
|
| 138 |
/** Opens an persistent cursor to an index tree without initializing |
| 139 |
the cursor. |
| 140 |
@param[in] index Index. |
| 141 |
@param[in] tuple Tuple on which search done. |
| 142 |
@param[in] mode PAGE_CUR_L, ...; |
| 143 |
NOTE that if the search is made using a unique |
| 144 |
prefix of a record, mode should be |
| 145 |
PAGE_CUR_LE, not PAGE_CUR_GE, as the latter |
| 146 |
may end up on the previous page of the |
| 147 |
record! |
| 148 |
@param[in] latch_mode BTR_SEARCH_LEAF, ...; |
| 149 |
NOTE that if has_search_latch != 0 then |
| 150 |
we maybe do not acquire a latch on the cursor |
| 151 |
page, but assume that the caller uses his |
| 152 |
btr search latch to protect the record! |
| 153 |
@param[in] has_search_latch latch mode the caller |
| 154 |
currently has on search system: RW_S_LATCH, or 0 |
| 155 |
@param[in] mtr Mini-transaction |
| 156 |
@param[in] location Location where called */ |
| 157 |
void open_no_init(dict_index_t *index, const dtuple_t *tuple, |
| 158 |
page_cur_mode_t mode, ulint latch_mode, |
| 159 |
ulint has_search_latch, mtr_t *mtr, ut::Location location); |
| 160 |
|
| 161 |
/** If mode is PAGE_CUR_G or PAGE_CUR_GE, opens a persistent cursor |
| 162 |
on the first user record satisfying the search condition, in the case |
| 163 |
PAGE_CUR_L or PAGE_CUR_LE, on the last user record. If no such user |
| 164 |
record exists, then in the first case sets the cursor after last in |
| 165 |
tree, and in the latter case before first in tree. The latching mode |
| 166 |
must be BTR_SEARCH_LEAF or BTR_MODIFY_LEAF. |
| 167 |
@param[in] index Index |
| 168 |
@param[in] tuple Tuple on which search done. |
| 169 |
@param[in] mode PAGE_CUR_L, ... |
| 170 |
@param[in] latch_mode BTR_SEARCH_LEAF or BTR_MODIFY_LEAF |
| 171 |
@param[in] mtr Mini-transaction. |
| 172 |
@param[in] location Location in file from where called.*/ |
| 173 |
void open_on_user_rec(dict_index_t *index, const dtuple_t *tuple, |
| 174 |
page_cur_mode_t mode, ulint latch_mode, mtr_t *mtr, |
| 175 |
ut::Location location); |
| 176 |
|
| 177 |
/** Allows setting the persistent cursor manually. |
| 178 |
@param[in] cursor Page cursor where positioned. |
| 179 |
@param[in] mode PAGE_CUR_L, ... |
| 180 |
@param[in] latch_mode BTR_SEARCH_LEAF or BTR_MODIFY_LEAF */ |
| 181 |
void open_on_user_rec(const page_cur_t &cursor, page_cur_mode_t mode, |
| 182 |
ulint latch_mode); |
| 183 |
|
| 184 |
/** Initializes and opens a persistent cursor to an index tree |
| 185 |
It should be closed with btr_pcur::close. |
| 186 |
@param[in] index Index. |
| 187 |
@param[in] level Level in the btree. |
| 188 |
@param[in] tuple Tuple on which search done. |
| 189 |
@param[in] mode PAGE_CUR_L, ...; NOTE that if the search |
| 190 |
is made using a unique prefix of a record, mode should be PAGE_CUR_LE, not |
| 191 |
PAGE_CUR_GE, as the latter may end up on the previous page from the record! |
| 192 |
@param[in] latch_mode BTR_SEARCH_LEAF, ... |
| 193 |
@param[in] mtr Mini-transaction. |
| 194 |
@param[in] location Location where called. */ |
| 195 |
void open(dict_index_t *index, ulint level, const dtuple_t *tuple, |
| 196 |
page_cur_mode_t mode, ulint latch_mode, mtr_t *mtr, |
| 197 |
ut::Location location); |
| 198 |
|
| 199 |
/** Restores the stored position of a persistent cursor bufferfixing |
| 200 |
the page and obtaining the specified latches. If the cursor position |
| 201 |
was saved when the |
| 202 |
(1) cursor was positioned on a user record: this function restores |
| 203 |
the position to the last record LESS OR EQUAL to the stored record; |
| 204 |
(2) cursor was positioned on a page infimum record: restores the |
| 205 |
position to the last record LESS than the user record which was the |
| 206 |
successor of the page infimum; |
| 207 |
(3) cursor was positioned on the page supremum: restores to the first |
| 208 |
record GREATER than the user record which was the predecessor of the |
| 209 |
supremum. |
| 210 |
(4) cursor was positioned before the first or after the last in an |
| 211 |
empty tree: restores to before first or after the last in the tree. |
| 212 |
@param[in] latch_mode BTR_SEARCH_LEAF, ... |
| 213 |
@param[in,out] mtr Mini-transaction |
| 214 |
@param[in] location Location where called. |
| 215 |
@return true if the cursor position was stored when it was on a user |
| 216 |
record and it can be restored on a user record whose ordering |
| 217 |
fields are identical to the ones of the original user record */ |
| 218 |
bool restore_position(ulint latch_mode, mtr_t *mtr, ut::Location location); |
| 219 |
|
| 220 |
/** Frees the possible memory heap of a persistent cursor and |
| 221 |
sets the latch mode of the persistent cursor to BTR_NO_LATCHES. |
| 222 |
WARNING: this function does not release the latch on the page where the |
| 223 |
cursor is currently positioned. The latch is acquired by the |
| 224 |
"move to next/previous" family of functions. Since recursive shared |
| 225 |
locks are not allowed, you must take care (if using the cursor in |
| 226 |
S-mode) to manually release the latch by either calling |
| 227 |
btr_leaf_page_release(pcur.get_block(), pcur.latch_mode, mtr) |
| 228 |
or by committing the mini-transaction right after btr_pcur::close(). |
| 229 |
A subsequent attempt to crawl the same page in the same mtr would |
| 230 |
cause an assertion failure. */ |
| 231 |
void close(); |
| 232 |
|
| 233 |
/** Free old_rec_buf. */ |
| 234 |
void free_rec_buf() { |
| 235 |
ut::free(m_old_rec_buf); |
| 236 |
m_old_rec_buf = nullptr; |
| 237 |
} |
| 238 |
|
| 239 |
#ifndef UNIV_HOTBACKUP |
| 240 |
|
| 241 |
/** Gets the rel_pos field for a cursor whose position has been stored. |
| 242 |
@return BTR_PCUR_ON, ... */ |
| 243 |
ulint get_rel_pos() const; |
| 244 |
|
| 245 |
#endif /* !UNIV_HOTBACKUP */ |
| 246 |
|
| 247 |
/** @return the btree cursor (const version). */ |
| 248 |
const btr_cur_t *get_btr_cur() const; |
| 249 |
|
| 250 |
/** @return the btree cursor (non const version). */ |
| 251 |
btr_cur_t *get_btr_cur(); |
| 252 |
|
| 253 |
/** @return the btree page cursor (non const version). */ |
| 254 |
page_cur_t *get_page_cur(); |
| 255 |
|
| 256 |
/** @return the btree cursor (const version). */ |
| 257 |
const page_cur_t *get_page_cur() const; |
| 258 |
|
| 259 |
/** Returns the page of a persistent pcur (non const version). |
| 260 |
@return pointer to the page */ |
| 261 |
page_t *get_page(); |
| 262 |
|
| 263 |
/** Returns the page of a persistent pcur (const version). |
| 264 |
@return pointer to the page */ |
| 265 |
const page_t *get_page() const; |
| 266 |
|
| 267 |
/** Returns the current buffer block (non const version). |
| 268 |
@return pointer to the block */ |
| 269 |
buf_block_t *get_block(); |
| 270 |
|
| 271 |
/** Returns the current buffer block (const version). |
| 272 |
@return pointer to the block */ |
| 273 |
const buf_block_t *get_block() const; |
| 274 |
|
| 275 |
/** Returns the current record (non const version). |
| 276 |
@return pointer to the record */ |
| 277 |
rec_t *get_rec(); |
| 278 |
|
| 279 |
/** Returns the current record (const version). |
| 280 |
@return pointer to the record */ |
| 281 |
const rec_t *get_rec() const; |
| 282 |
|
| 283 |
#ifndef UNIV_HOTBACKUP |
| 284 |
/** Gets the up_match value for a pcur after a search. |
| 285 |
@return number of matched fields at the cursor or to the right if |
| 286 |
search mode was PAGE_CUR_GE, otherwise undefined */ |
| 287 |
ulint get_up_match() const; |
| 288 |
|
| 289 |
/** Gets the low_match value for a pcur after a search. |
| 290 |
@return number of matched fields at the cursor or to the right if |
| 291 |
search mode was PAGE_CUR_LE, otherwise undefined */ |
| 292 |
ulint get_low_match() const; |
| 293 |
|
| 294 |
/** Checks if the persistent cursor is after the last user record |
| 295 |
on a page. |
| 296 |
@return true if after last on page. */ |
| 297 |
bool is_after_last_on_page() const; |
| 298 |
|
| 299 |
/** Checks if the persistent cursor is before the first user record |
| 300 |
on a page. |
| 301 |
@return true if before first on page. */ |
| 302 |
bool is_before_first_on_page() const; |
| 303 |
|
| 304 |
/** Checks if the persistent cursor is on a user record. |
| 305 |
@return true if on user record. */ |
| 306 |
bool is_on_user_rec() const; |
| 307 |
|
| 308 |
/** Checks if the persistent cursor is before the first user record |
| 309 |
in the index tree. |
| 310 |
@param[in,out] mtr Mini-transaction. |
| 311 |
@return true if is before first in tree. */ |
| 312 |
bool is_before_first_in_tree(mtr_t *mtr) const; |
| 313 |
|
| 314 |
/** Checks if the persistent cursor is after the last user record in |
| 315 |
the index tree. |
| 316 |
@param[in,out] mtr Mini-transaction. |
| 317 |
@return is after last in tree. */ |
| 318 |
bool is_after_last_in_tree(mtr_t *mtr) const; |
| 319 |
|
| 320 |
/** Moves the persistent cursor to the next record on the same page. */ |
| 321 |
void move_to_next_on_page(); |
| 322 |
|
| 323 |
/** Moves the persistent cursor to the prev record on the same page. */ |
| 324 |
void move_to_prev_on_page(); |
| 325 |
|
| 326 |
/** Moves the persistent cursor to the last record on the same page. |
| 327 |
@param[in,out] mtr Mini-transaction. */ |
| 328 |
void move_to_last_on_page(mtr_t *mtr); |
| 329 |
|
| 330 |
/** Moves the persistent cursor to the next user record in the tree. |
| 331 |
If no user records are left, the cursor ends up 'after last in tree'. |
| 332 |
@param[in,out] mtr Mini-transaction. |
| 333 |
@return DB_SUCCESS or DB_END_OF_INDEX. */ |
| 334 |
dberr_t move_to_next_user_rec(mtr_t *mtr); |
| 335 |
|
| 336 |
/** Moves the persistent cursor to the next record in the tree. If no |
| 337 |
records are left, the cursor stays 'after last in tree'. |
| 338 |
Note: Function may release the page latch. |
| 339 |
@param[in,out] mtr Mini-transaction. |
| 340 |
@return true if the cursor was not after last in tree */ |
| 341 |
bool move_to_next(mtr_t *mtr); |
| 342 |
|
| 343 |
/** Moves the persistent cursor to the previous record in the tree. |
| 344 |
If no records are left, the cursor stays 'before first in tree'. |
| 345 |
Note: Function may release the page latch. |
| 346 |
@param[in,out] mtr Mini-transaction. |
| 347 |
@return true if the cursor was not before first in tree */ |
| 348 |
bool move_to_prev(mtr_t *mtr); |
| 349 |
|
| 350 |
/** Moves the persistent cursor to the first record on the next page. |
| 351 |
Releases the latch on the current page, and bufferunfixes it. |
| 352 |
Note that there must not be modifications on the current page, as |
| 353 |
then the x-latch can be released only in mtr_commit. |
| 354 |
@param[in,out] mtr Mini-transaction. */ |
| 355 |
void move_to_next_page(mtr_t *mtr); |
| 356 |
|
| 357 |
/** Commits the mtr and sets the pcur latch mode to BTR_NO_LATCHES, |
| 358 |
that is, the cursor becomes detached. |
| 359 |
Function btr_pcur::store_position should be used before calling this, |
| 360 |
if restoration of cursor is wanted later. |
| 361 |
@param[in,out] mtr Mini-transaction. */ |
| 362 |
void commit_specify_mtr(mtr_t *mtr); |
| 363 |
|
| 364 |
/** Moves the persistent cursor to the infimum record on the same page. */ |
| 365 |
void move_before_first_on_page(); |
| 366 |
#endif /* !UNIV_HOTBACKUP */ |
| 367 |
|
| 368 |
/** The position of the cursor is stored by taking an initial segment |
| 369 |
of the record the cursor is positioned on, before, or after, and |
| 370 |
copying it to the cursor data structure, or just setting a flag if |
| 371 |
the cursor id before the first in an EMPTY tree, or after the last |
| 372 |
in an EMPTY tree. NOTE that the page where the cursor is positioned |
| 373 |
must not be empty if the index tree is not totally empty! |
| 374 |
@param[in,out] mtr Mini-transaction. */ |
| 375 |
void store_position(mtr_t *mtr); |
| 376 |
|
| 377 |
/** @return true if the cursor is positioned. */ |
| 378 |
bool is_positioned() const { |
| 379 |
return (m_old_stored && (m_pos_state == BTR_PCUR_IS_POSITIONED || |
| 380 |
m_pos_state == BTR_PCUR_WAS_POSITIONED)); |
| 381 |
} |
| 382 |
|
| 383 |
/** @return true if the cursor is for a clustered index. */ |
| 384 |
bool is_clustered() const { return (m_btr_cur.index->is_clustered()); } |
| 385 |
|
| 386 |
/** Resets a persistent cursor object, freeing "::old_rec_buf" if it is |
| 387 |
allocated and resetting the other members to their initial values. */ |
| 388 |
void reset(); |
| 389 |
|
| 390 |
/** Copies the stored position of a pcur to another pcur. |
| 391 |
@param[in,out] dst Which will receive the position |
| 392 |
info. |
| 393 |
@param[in] src From which the info is copied */ |
| 394 |
static void copy_stored_position(btr_pcur_t *dst, const btr_pcur_t *src); |
| 395 |
|
| 396 |
/** Allocates memory for a persistent cursor object and initializes |
| 397 |
the cursor. |
| 398 |
@return own: persistent cursor */ |
| 399 |
static btr_pcur_t *create_for_mysql() { |
| 400 |
auto pcur = ut::new_withkey<btr_pcur_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 401 |
|
| 402 |
pcur->m_btr_cur.index = nullptr; |
| 403 |
|
| 404 |
pcur->init(); |
| 405 |
|
| 406 |
return (pcur); |
| 407 |
} |
| 408 |
|
| 409 |
/** Frees the memory for a persistent cursor object and the cursor itself. |
| 410 |
@param[in,out] pcur Cursor to free. */ |
| 411 |
static void free_for_mysql(btr_pcur_t *&pcur) { |
| 412 |
pcur->free_rec_buf(); |
| 413 |
|
| 414 |
ut::delete_(pcur); |
| 415 |
|
| 416 |
pcur = nullptr; |
| 417 |
} |
| 418 |
|
| 419 |
/** Set the cursor access type: Normal or Scan. |
| 420 |
@param[in] fetch_mode One of Page_fetch::NORMAL or Page_fetch::SCAN. |
| 421 |
@return the old fetch mode. */ |
| 422 |
Page_fetch set_fetch_type(Page_fetch fetch_mode) { |
| 423 |
ut_ad(fetch_mode == Page_fetch::NORMAL || fetch_mode == Page_fetch::SCAN); |
| 424 |
|
| 425 |
auto old_fetch_mode = m_btr_cur.m_fetch_mode; |
| 426 |
|
| 427 |
m_btr_cur.m_fetch_mode = fetch_mode; |
| 428 |
|
| 429 |
return (old_fetch_mode); |
| 430 |
} |
| 431 |
|
| 432 |
private: |
| 433 |
/** Moves the persistent cursor backward if it is on the first record |
| 434 |
of the page. Commits mtr. Note that to prevent a possible deadlock, the |
| 435 |
operation first stores the position of the cursor, commits mtr, acquires |
| 436 |
the necessary latches and restores the cursor position again before |
| 437 |
returning. The alphabetical position of the cursor is guaranteed to |
| 438 |
be sensible on return, but it may happen that the cursor is not |
| 439 |
positioned on the last record of any page, because the structure |
| 440 |
of the tree may have changed during the time when the cursor had |
| 441 |
no latches. |
| 442 |
@param[in,out] mtr Mini-tranaction. */ |
| 443 |
void move_backward_from_page(mtr_t *mtr); |
| 444 |
|
| 445 |
public: |
| 446 |
/** a B-tree cursor */ |
| 447 |
btr_cur_t m_btr_cur; |
| 448 |
|
| 449 |
/** see TODO note below! |
| 450 |
BTR_SEARCH_LEAF, BTR_MODIFY_LEAF, BTR_MODIFY_TREE or BTR_NO_LATCHES, |
| 451 |
depending on the latching state of the page and tree where the cursor |
| 452 |
is positioned; BTR_NO_LATCHES means that the cursor is not currently |
| 453 |
positioned: |
| 454 |
we say then that the cursor is detached; it can be restored to |
| 455 |
attached if the old position was stored in old_rec */ |
| 456 |
ulint m_latch_mode{0}; |
| 457 |
|
| 458 |
/** true if old_rec is stored */ |
| 459 |
bool m_old_stored{false}; |
| 460 |
|
| 461 |
/** if cursor position is stored, contains an initial segment of the |
| 462 |
latest record cursor was positioned either on, before or after */ |
| 463 |
rec_t *m_old_rec{nullptr}; |
| 464 |
|
| 465 |
/** number of fields in old_rec */ |
| 466 |
ulint m_old_n_fields{0}; |
| 467 |
|
| 468 |
/** BTR_PCUR_ON, BTR_PCUR_BEFORE, or BTR_PCUR_AFTER, depending on |
| 469 |
whether cursor was on, before, or after the old_rec record */ |
| 470 |
btr_pcur_pos_t m_rel_pos{BTR_PCUR_UNSET}; |
| 471 |
|
| 472 |
/** buffer block when the position was stored */ |
| 473 |
buf::Block_hint m_block_when_stored; |
| 474 |
|
| 475 |
/** the modify clock value of the buffer block when the cursor position |
| 476 |
was stored */ |
| 477 |
uint64_t m_modify_clock{0}; |
| 478 |
|
| 479 |
/** position() and restore_position() state. */ |
| 480 |
pcur_pos_t m_pos_state{BTR_PCUR_NOT_POSITIONED}; |
| 481 |
|
| 482 |
/** PAGE_CUR_G, ... */ |
| 483 |
page_cur_mode_t m_search_mode{PAGE_CUR_UNSUPP}; |
| 484 |
|
| 485 |
/** the transaction, if we know it; otherwise this field is not defined; |
| 486 |
can ONLY BE USED in error prints in fatal assertion failures! */ |
| 487 |
trx_t *m_trx_if_known{nullptr}; |
| 488 |
|
| 489 |
/* NOTE that the following fields may possess dynamically allocated |
| 490 |
memory which should be freed if not needed anymore! */ |
| 491 |
|
| 492 |
/** nullptr, or a dynamically allocated buffer for old_rec */ |
| 493 |
byte *m_old_rec_buf{nullptr}; |
| 494 |
|
| 495 |
/** old_rec_buf size if old_rec_buf is not nullptr */ |
| 496 |
size_t m_buf_size{0}; |
| 497 |
|
| 498 |
/** Read level where the cursor would be positioned or re-positioned. */ |
| 499 |
ulint m_read_level{0}; |
| 500 |
|
| 501 |
/* NOTE that the following field is initialized only during import |
| 502 |
tablespace, otherwise undefined */ |
| 503 |
import_ctx_t *import_ctx{nullptr}; |
| 504 |
}; |
| 505 |
|
| 506 |
inline void btr_pcur_t::init(size_t read_level) { |
| 507 |
set_fetch_type(Page_fetch::NORMAL); |
| 508 |
|
| 509 |
m_old_stored = false; |
| 510 |
m_old_rec_buf = nullptr; |
| 511 |
m_old_rec = nullptr; |
| 512 |
m_btr_cur.rtr_info = nullptr; |
| 513 |
m_read_level = read_level; |
| 514 |
import_ctx = nullptr; |
| 515 |
m_block_when_stored.clear(); |
| 516 |
} |
| 517 |
|
| 518 |
inline void btr_pcur_t::open(dict_index_t *index, ulint level, |
| 519 |
const dtuple_t *tuple, page_cur_mode_t mode, |
| 520 |
ulint latch_mode, mtr_t *mtr, |
| 521 |
ut::Location location) { |
| 522 |
init(); |
| 523 |
|
| 524 |
m_search_mode = mode; |
| 525 |
m_latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode); |
| 526 |
|
| 527 |
/* Search with the tree cursor */ |
| 528 |
|
| 529 |
auto cur = get_btr_cur(); |
| 530 |
|
| 531 |
ut_ad(!dict_index_is_spatial(index)); |
| 532 |
|
| 533 |
if (index->table->is_intrinsic()) { |
| 534 |
ut_ad((latch_mode & BTR_MODIFY_LEAF) || (latch_mode & BTR_SEARCH_LEAF) || |
| 535 |
(latch_mode & BTR_MODIFY_TREE)); |
| 536 |
|
| 537 |
btr_cur_search_to_nth_level_with_no_latch( |
| 538 |
index, level, tuple, mode, cur, location.filename, location.line, mtr, |
| 539 |
(((latch_mode & BTR_MODIFY_LEAF) || (latch_mode & BTR_MODIFY_TREE)) |
| 540 |
? true |
| 541 |
: false)); |
| 542 |
} else { |
| 543 |
btr_cur_search_to_nth_level(index, level, tuple, mode, latch_mode, cur, 0, |
| 544 |
location.filename, location.line, mtr); |
| 545 |
} |
| 546 |
|
| 547 |
m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 548 |
|
| 549 |
m_trx_if_known = nullptr; |
| 550 |
} |
| 551 |
|
| 552 |
inline void btr_pcur_t::open_at_side(bool from_left, dict_index_t *index, |
| 553 |
ulint latch_mode, bool init_pcur, |
| 554 |
ulint level, mtr_t *mtr) { |
| 555 |
m_latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode); |
| 556 |
|
| 557 |
m_search_mode = from_left ? PAGE_CUR_G : PAGE_CUR_L; |
| 558 |
|
| 559 |
if (init_pcur) { |
| 560 |
init(); |
| 561 |
} |
| 562 |
|
| 563 |
if (index->table->is_intrinsic()) { |
| 564 |
btr_cur_open_at_index_side_with_no_latch(from_left, index, get_btr_cur(), |
| 565 |
level, UT_LOCATION_HERE, mtr); |
| 566 |
} else { |
| 567 |
btr_cur_open_at_index_side(from_left, index, latch_mode, get_btr_cur(), |
| 568 |
level, UT_LOCATION_HERE, mtr); |
| 569 |
} |
| 570 |
|
| 571 |
m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 572 |
|
| 573 |
m_old_stored = false; |
| 574 |
|
| 575 |
m_trx_if_known = nullptr; |
| 576 |
} |
| 577 |
|
| 578 |
inline bool btr_pcur_t::set_random_position(dict_index_t *index, |
| 579 |
ulint latch_mode, mtr_t *mtr, |
| 580 |
ut::Location location) { |
| 581 |
m_latch_mode = latch_mode; |
| 582 |
m_search_mode = PAGE_CUR_G; |
| 583 |
|
| 584 |
init(); |
| 585 |
|
| 586 |
auto positioned = btr_cur_open_at_rnd_pos( |
| 587 |
index, latch_mode, get_btr_cur(), location.filename, location.line, mtr); |
| 588 |
|
| 589 |
m_old_stored = false; |
| 590 |
|
| 591 |
m_trx_if_known = nullptr; |
| 592 |
|
| 593 |
m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 594 |
|
| 595 |
return (positioned); |
| 596 |
} |
| 597 |
|
| 598 |
inline void btr_pcur_t::open_no_init(dict_index_t *index, const dtuple_t *tuple, |
| 599 |
page_cur_mode_t mode, ulint latch_mode, |
| 600 |
ulint has_search_latch, mtr_t *mtr, |
| 601 |
ut::Location location) { |
| 602 |
m_latch_mode = BTR_LATCH_MODE_WITHOUT_INTENTION(latch_mode); |
| 603 |
|
| 604 |
m_search_mode = mode; |
| 605 |
|
| 606 |
/* Search with the tree cursor */ |
| 607 |
|
| 608 |
auto cur = get_btr_cur(); |
| 609 |
|
| 610 |
if (index->table->is_intrinsic()) { |
| 611 |
ut_ad((latch_mode & BTR_MODIFY_LEAF) || (latch_mode & BTR_SEARCH_LEAF)); |
| 612 |
|
| 613 |
btr_cur_search_to_nth_level_with_no_latch( |
| 614 |
index, m_read_level, tuple, mode, cur, location.filename, location.line, |
| 615 |
mtr, ((latch_mode & BTR_MODIFY_LEAF) ? true : false)); |
| 616 |
} else { |
| 617 |
btr_cur_search_to_nth_level(index, m_read_level, tuple, mode, latch_mode, |
| 618 |
cur, has_search_latch, location.filename, |
| 619 |
location.line, mtr); |
| 620 |
} |
| 621 |
|
| 622 |
m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 623 |
|
| 624 |
m_old_stored = false; |
| 625 |
|
| 626 |
m_trx_if_known = nullptr; |
| 627 |
} |
| 628 |
|
| 629 |
inline const btr_cur_t *btr_pcur_t::get_btr_cur() const { return (&m_btr_cur); } |
| 630 |
|
| 631 |
inline btr_cur_t *btr_pcur_t::get_btr_cur() { |
| 632 |
return (const_cast<btr_cur_t *>(&m_btr_cur)); |
| 633 |
} |
| 634 |
|
| 635 |
#ifdef UNIV_DEBUG |
| 636 |
inline page_cur_t *btr_pcur_t::get_page_cur() { |
| 637 |
return (btr_cur_get_page_cur(get_btr_cur())); |
| 638 |
} |
| 639 |
|
| 640 |
inline const page_cur_t *btr_pcur_t::get_page_cur() const { |
| 641 |
return (btr_cur_get_page_cur(get_btr_cur())); |
| 642 |
} |
| 643 |
|
| 644 |
inline page_t *btr_pcur_t::get_page() { |
| 645 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 646 |
|
| 647 |
return (btr_cur_get_page(get_btr_cur())); |
| 648 |
} |
| 649 |
|
| 650 |
inline const page_t *btr_pcur_t::get_page() const { |
| 651 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 652 |
|
| 653 |
return (btr_cur_get_page(const_cast<btr_pcur_t *>(this)->get_btr_cur())); |
| 654 |
} |
| 655 |
|
| 656 |
inline buf_block_t *btr_pcur_t::get_block() { |
| 657 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 658 |
|
| 659 |
return (btr_cur_get_block(get_btr_cur())); |
| 660 |
} |
| 661 |
|
| 662 |
inline const buf_block_t *btr_pcur_t::get_block() const { |
| 663 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 664 |
|
| 665 |
return (btr_cur_get_block(get_btr_cur())); |
| 666 |
} |
| 667 |
|
| 668 |
inline rec_t *btr_pcur_t::get_rec() { |
| 669 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 670 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 671 |
|
| 672 |
return (btr_cur_get_rec(get_btr_cur())); |
| 673 |
} |
| 674 |
|
| 675 |
inline const rec_t *btr_pcur_t::get_rec() const { |
| 676 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 677 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 678 |
|
| 679 |
return (btr_cur_get_rec(get_btr_cur())); |
| 680 |
} |
| 681 |
#else |
| 682 |
|
| 683 |
inline page_cur_t *btr_pcur_t::get_page_cur() { return (&m_btr_cur.page_cur); } |
| 684 |
|
| 685 |
inline const page_cur_t *btr_pcur_t::get_page_cur() const { |
| 686 |
return (&m_btr_cur.page_cur); |
| 687 |
} |
| 688 |
|
| 689 |
inline page_t *btr_pcur_t::get_page() { |
| 690 |
return (m_btr_cur.page_cur.block->frame); |
| 691 |
} |
| 692 |
|
| 693 |
inline const page_t *btr_pcur_t::get_page() const { |
| 694 |
return (m_btr_cur.page_cur.block->frame); |
| 695 |
} |
| 696 |
|
| 697 |
inline buf_block_t *btr_pcur_t::get_block() { |
| 698 |
return (m_btr_cur.page_cur.block); |
| 699 |
} |
| 700 |
|
| 701 |
inline const buf_block_t *btr_pcur_t::get_block() const { |
| 702 |
return (m_btr_cur.page_cur.block); |
| 703 |
} |
| 704 |
|
| 705 |
inline rec_t *btr_pcur_t::get_rec() { return (m_btr_cur.page_cur.rec); } |
| 706 |
|
| 707 |
inline const rec_t *btr_pcur_t::get_rec() const { |
| 708 |
return (m_btr_cur.page_cur.rec); |
| 709 |
} |
| 710 |
|
| 711 |
#endif /* UNIV_DEBUG */ |
| 712 |
|
| 713 |
#ifndef UNIV_HOTBACKUP |
| 714 |
|
| 715 |
inline ulint btr_pcur_t::get_rel_pos() const { |
| 716 |
ut_ad(m_old_rec != nullptr); |
| 717 |
ut_ad(m_old_stored); |
| 718 |
ut_ad(m_pos_state == BTR_PCUR_WAS_POSITIONED || |
| 719 |
m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 720 |
|
| 721 |
return (m_rel_pos); |
| 722 |
} |
| 723 |
|
| 724 |
inline void btr_pcur_t::close() { |
| 725 |
free_rec_buf(); |
| 726 |
|
| 727 |
if (m_btr_cur.rtr_info != nullptr) { |
| 728 |
rtr_clean_rtr_info(m_btr_cur.rtr_info, true); |
| 729 |
m_btr_cur.rtr_info = nullptr; |
| 730 |
} |
| 731 |
|
| 732 |
m_old_rec = nullptr; |
| 733 |
m_btr_cur.page_cur.rec = nullptr; |
| 734 |
m_btr_cur.page_cur.block = nullptr; |
| 735 |
|
| 736 |
m_old_rec = nullptr; |
| 737 |
m_old_stored = false; |
| 738 |
|
| 739 |
m_latch_mode = BTR_NO_LATCHES; |
| 740 |
m_pos_state = BTR_PCUR_NOT_POSITIONED; |
| 741 |
|
| 742 |
m_trx_if_known = nullptr; |
| 743 |
} |
| 744 |
|
| 745 |
inline ulint btr_pcur_t::get_up_match() const { |
| 746 |
ut_ad(m_pos_state == BTR_PCUR_WAS_POSITIONED || |
| 747 |
m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 748 |
|
| 749 |
const auto cur = get_btr_cur(); |
| 750 |
|
| 751 |
ut_ad(cur->up_match != ULINT_UNDEFINED); |
| 752 |
|
| 753 |
return (cur->up_match); |
| 754 |
} |
| 755 |
|
| 756 |
inline ulint btr_pcur_t::get_low_match() const { |
| 757 |
ut_ad(m_pos_state == BTR_PCUR_WAS_POSITIONED || |
| 758 |
m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 759 |
|
| 760 |
const auto cur = get_btr_cur(); |
| 761 |
|
| 762 |
ut_ad(cur->low_match != ULINT_UNDEFINED); |
| 763 |
|
| 764 |
return (cur->low_match); |
| 765 |
} |
| 766 |
|
| 767 |
inline bool btr_pcur_t::is_after_last_on_page() const { |
| 768 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 769 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 770 |
|
| 771 |
return (page_cur_is_after_last(get_page_cur())); |
| 772 |
} |
| 773 |
|
| 774 |
inline bool btr_pcur_t::is_before_first_on_page() const { |
| 775 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 776 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 777 |
|
| 778 |
return (page_cur_is_before_first(get_page_cur())); |
| 779 |
} |
| 780 |
|
| 781 |
inline bool btr_pcur_t::is_on_user_rec() const { |
| 782 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 783 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 784 |
|
| 785 |
return (!is_before_first_on_page() && !is_after_last_on_page()); |
| 786 |
} |
| 787 |
|
| 788 |
inline bool btr_pcur_t::is_before_first_in_tree(mtr_t *mtr) const { |
| 789 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 790 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 791 |
|
| 792 |
if (btr_page_get_prev(get_page(), mtr) != FIL_NULL) { |
| 793 |
return (false); |
| 794 |
} |
| 795 |
|
| 796 |
return (page_cur_is_before_first(get_page_cur())); |
| 797 |
} |
| 798 |
|
| 799 |
inline bool btr_pcur_t::is_after_last_in_tree(mtr_t *mtr) const { |
| 800 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 801 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 802 |
|
| 803 |
if (btr_page_get_next(get_page(), mtr) != FIL_NULL) { |
| 804 |
return (false); |
| 805 |
} |
| 806 |
|
| 807 |
return (page_cur_is_after_last(get_page_cur())); |
| 808 |
} |
| 809 |
|
| 810 |
inline void btr_pcur_t::move_to_next_on_page() { |
| 811 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 812 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 813 |
|
| 814 |
page_cur_move_to_next(get_page_cur()); |
| 815 |
|
| 816 |
m_old_stored = false; |
| 817 |
} |
| 818 |
|
| 819 |
inline void btr_pcur_t::move_to_prev_on_page() { |
| 820 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 821 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 822 |
|
| 823 |
page_cur_move_to_prev(get_page_cur()); |
| 824 |
|
| 825 |
m_old_stored = false; |
| 826 |
} |
| 827 |
|
| 828 |
inline void btr_pcur_t::move_to_last_on_page(mtr_t *mtr) { |
| 829 |
UT_NOT_USED(mtr); |
| 830 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 831 |
|
| 832 |
page_cur_set_after_last(get_block(), get_page_cur()); |
| 833 |
|
| 834 |
m_old_stored = false; |
| 835 |
} |
| 836 |
|
| 837 |
inline dberr_t btr_pcur_t::move_to_next_user_rec(mtr_t *mtr) { |
| 838 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 839 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 840 |
|
| 841 |
for (;;) { |
| 842 |
m_old_stored = false; |
| 843 |
|
| 844 |
if (is_after_last_on_page()) { |
| 845 |
if (is_after_last_in_tree(mtr)) { |
| 846 |
return (DB_END_OF_INDEX); |
| 847 |
} |
| 848 |
|
| 849 |
move_to_next_page(mtr); |
| 850 |
} else { |
| 851 |
move_to_next_on_page(); |
| 852 |
} |
| 853 |
|
| 854 |
if (is_on_user_rec()) { |
| 855 |
return (DB_SUCCESS); |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
inline bool btr_pcur_t::move_to_next(mtr_t *mtr) { |
| 861 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 862 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 863 |
|
| 864 |
m_old_stored = false; |
| 865 |
|
| 866 |
if (is_after_last_on_page()) { |
| 867 |
if (is_after_last_in_tree(mtr)) { |
| 868 |
return (false); |
| 869 |
} |
| 870 |
|
| 871 |
move_to_next_page(mtr); |
| 872 |
|
| 873 |
return (true); |
| 874 |
} |
| 875 |
|
| 876 |
move_to_next_on_page(); |
| 877 |
|
| 878 |
return (true); |
| 879 |
} |
| 880 |
|
| 881 |
inline void btr_pcur_t::commit_specify_mtr(mtr_t *mtr) { |
| 882 |
ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 883 |
|
| 884 |
m_latch_mode = BTR_NO_LATCHES; |
| 885 |
|
| 886 |
mtr_commit(mtr); |
| 887 |
|
| 888 |
m_pos_state = BTR_PCUR_WAS_POSITIONED; |
| 889 |
} |
| 890 |
|
| 891 |
inline void btr_pcur_t::move_before_first_on_page() { |
| 892 |
ut_ad(m_latch_mode != BTR_NO_LATCHES); |
| 893 |
|
| 894 |
page_cur_set_before_first(get_block(), get_page_cur()); |
| 895 |
|
| 896 |
m_old_stored = false; |
| 897 |
} |
| 898 |
|
| 899 |
inline void btr_pcur_t::reset() { |
| 900 |
free_rec_buf(); |
| 901 |
|
| 902 |
m_old_rec_buf = nullptr; |
| 903 |
m_btr_cur.index = nullptr; |
| 904 |
m_btr_cur.page_cur.rec = nullptr; |
| 905 |
m_old_rec = nullptr; |
| 906 |
m_old_n_fields = 0; |
| 907 |
m_old_stored = false; |
| 908 |
|
| 909 |
m_latch_mode = BTR_NO_LATCHES; |
| 910 |
m_pos_state = BTR_PCUR_NOT_POSITIONED; |
| 911 |
} |
| 912 |
|
| 913 |
#endif /* !UNIV_HOTBACKUP */ |
| 914 |
|
| 915 |
#endif /* !btr0pcur_h */ |