| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2014, 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 gis/gis0sea.cc |
| 29 |
InnoDB R-tree search interfaces |
| 30 |
|
| 31 |
Created 2014/01/16 Jimmy Yang |
| 32 |
***********************************************************************/ |
| 33 |
|
| 34 |
#include <new> |
| 35 |
|
| 36 |
#include "fsp0fsp.h" |
| 37 |
#include "gis0rtree.h" |
| 38 |
#include "my_dbug.h" |
| 39 |
#include "page0cur.h" |
| 40 |
#include "page0page.h" |
| 41 |
#include "page0zip.h" |
| 42 |
|
| 43 |
#include "btr0cur.h" |
| 44 |
#include "btr0pcur.h" |
| 45 |
#include "btr0sea.h" |
| 46 |
#include "ibuf0ibuf.h" |
| 47 |
#include "lock0lock.h" |
| 48 |
#include "rem0cmp.h" |
| 49 |
#include "srv0mon.h" |
| 50 |
#include "sync0sync.h" |
| 51 |
#include "trx0trx.h" |
| 52 |
#include "univ.i" |
| 53 |
|
| 54 |
/** Restore the stored position of a persistent cursor bufferfixing the page */ |
| 55 |
static bool rtr_cur_restore_position( |
| 56 |
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */ |
| 57 |
btr_cur_t *cursor, /*!< in: detached persistent cursor */ |
| 58 |
ulint level, /*!< in: index level */ |
| 59 |
mtr_t *mtr); /*!< in: mtr */ |
| 60 |
|
| 61 |
/** Pop out used parent path entry, until we find the parent with matching |
| 62 |
page number */ |
| 63 |
static void rtr_adjust_parent_path( |
| 64 |
rtr_info_t *rtr_info, /* R-Tree info struct */ |
| 65 |
page_no_t page_no) /* page number to look for */ |
| 66 |
{ |
| 67 |
while (!rtr_info->parent_path->empty()) { |
| 68 |
if (rtr_info->parent_path->back().child_no == page_no) { |
| 69 |
break; |
| 70 |
} else { |
| 71 |
if (rtr_info->parent_path->back().cursor) { |
| 72 |
rtr_info->parent_path->back().cursor->close(); |
| 73 |
ut::free(rtr_info->parent_path->back().cursor); |
| 74 |
} |
| 75 |
|
| 76 |
rtr_info->parent_path->pop_back(); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** Find the next matching record. This function is used by search |
| 82 |
or record locating during index delete/update. |
| 83 |
@return true if there is suitable record found, otherwise false */ |
| 84 |
static bool rtr_pcur_getnext_from_path( |
| 85 |
const dtuple_t *tuple, /*!< in: data tuple */ |
| 86 |
page_cur_mode_t mode, /*!< in: cursor search mode */ |
| 87 |
btr_cur_t *btr_cur, /*!< in: persistent cursor; NOTE that the |
| 88 |
function may release the page latch */ |
| 89 |
ulint target_level, |
| 90 |
/*!< in: target level */ |
| 91 |
ulint latch_mode, |
| 92 |
/*!< in: latch_mode */ |
| 93 |
bool index_locked, |
| 94 |
/*!< in: index tree locked */ |
| 95 |
mtr_t *mtr) /*!< in: mtr */ |
| 96 |
{ |
| 97 |
dict_index_t *index = btr_cur->index; |
| 98 |
bool found = false; |
| 99 |
space_id_t space = dict_index_get_space(index); |
| 100 |
page_cur_t *page_cursor; |
| 101 |
ulint level = 0; |
| 102 |
node_visit_t next_rec; |
| 103 |
rtr_info_t *rtr_info = btr_cur->rtr_info; |
| 104 |
node_seq_t page_ssn; |
| 105 |
ulint my_latch_mode; |
| 106 |
ulint skip_parent = false; |
| 107 |
bool new_split = false; |
| 108 |
bool need_parent; |
| 109 |
bool for_delete = false; |
| 110 |
bool for_undo_ins = false; |
| 111 |
|
| 112 |
/* exhausted all the pages to be searched */ |
| 113 |
if (rtr_info->path->empty()) { |
| 114 |
return (false); |
| 115 |
} |
| 116 |
|
| 117 |
ut_ad(dtuple_get_n_fields_cmp(tuple)); |
| 118 |
|
| 119 |
my_latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode); |
| 120 |
|
| 121 |
for_delete = latch_mode & BTR_RTREE_DELETE_MARK; |
| 122 |
for_undo_ins = latch_mode & BTR_RTREE_UNDO_INS; |
| 123 |
|
| 124 |
/* There should be no insert coming to this function. Only |
| 125 |
mode with BTR_MODIFY_* should be delete */ |
| 126 |
ut_ad(mode != PAGE_CUR_RTREE_INSERT); |
| 127 |
ut_ad(my_latch_mode == BTR_SEARCH_LEAF || my_latch_mode == BTR_MODIFY_LEAF || |
| 128 |
my_latch_mode == BTR_MODIFY_TREE || |
| 129 |
my_latch_mode == BTR_CONT_MODIFY_TREE); |
| 130 |
|
| 131 |
/* Whether need to track parent information. Only need so |
| 132 |
when we do tree altering operations (such as index page merge) */ |
| 133 |
need_parent = ((my_latch_mode == BTR_MODIFY_TREE || |
| 134 |
my_latch_mode == BTR_CONT_MODIFY_TREE) && |
| 135 |
mode == PAGE_CUR_RTREE_LOCATE); |
| 136 |
|
| 137 |
if (!index_locked) { |
| 138 |
ut_ad(latch_mode & BTR_SEARCH_LEAF || latch_mode & BTR_MODIFY_LEAF); |
| 139 |
mtr_s_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 140 |
} else { |
| 141 |
ut_ad( |
| 142 |
mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_SX_LOCK) || |
| 143 |
mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_S_LOCK) || |
| 144 |
mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_X_LOCK)); |
| 145 |
} |
| 146 |
|
| 147 |
const page_size_t &page_size = dict_table_page_size(index->table); |
| 148 |
|
| 149 |
/* Pop each node/page to be searched from "path" structure |
| 150 |
and do a search on it. Please note, any pages that are in |
| 151 |
the "path" structure are protected by "page" lock, so they |
| 152 |
cannot be shrunk away */ |
| 153 |
do { |
| 154 |
buf_block_t *block; |
| 155 |
node_seq_t path_ssn; |
| 156 |
const page_t *page; |
| 157 |
ulint rw_latch = RW_X_LATCH; |
| 158 |
ulint tree_idx; |
| 159 |
|
| 160 |
mutex_enter(&rtr_info->rtr_path_mutex); |
| 161 |
next_rec = rtr_info->path->back(); |
| 162 |
rtr_info->path->pop_back(); |
| 163 |
level = next_rec.level; |
| 164 |
path_ssn = next_rec.seq_no; |
| 165 |
tree_idx = btr_cur->tree_height - level - 1; |
| 166 |
|
| 167 |
/* Maintain the parent path info as well, if needed */ |
| 168 |
if (need_parent && !skip_parent && !new_split) { |
| 169 |
ulint old_level; |
| 170 |
ulint new_level; |
| 171 |
|
| 172 |
ut_ad(!rtr_info->parent_path->empty()); |
| 173 |
|
| 174 |
/* Cleanup unused parent info */ |
| 175 |
if (rtr_info->parent_path->back().cursor) { |
| 176 |
rtr_info->parent_path->back().cursor->close(); |
| 177 |
ut::free(rtr_info->parent_path->back().cursor); |
| 178 |
} |
| 179 |
|
| 180 |
old_level = rtr_info->parent_path->back().level; |
| 181 |
|
| 182 |
rtr_info->parent_path->pop_back(); |
| 183 |
|
| 184 |
ut_ad(!rtr_info->parent_path->empty()); |
| 185 |
|
| 186 |
/* check whether there is a level change. If so, |
| 187 |
the current parent path needs to pop enough |
| 188 |
nodes to adjust to the new search page */ |
| 189 |
new_level = rtr_info->parent_path->back().level; |
| 190 |
|
| 191 |
if (old_level < new_level) { |
| 192 |
rtr_adjust_parent_path(rtr_info, next_rec.page_no); |
| 193 |
} |
| 194 |
|
| 195 |
ut_ad(!rtr_info->parent_path->empty()); |
| 196 |
|
| 197 |
ut_ad(next_rec.page_no == rtr_info->parent_path->back().child_no); |
| 198 |
} |
| 199 |
|
| 200 |
mutex_exit(&rtr_info->rtr_path_mutex); |
| 201 |
|
| 202 |
skip_parent = false; |
| 203 |
new_split = false; |
| 204 |
|
| 205 |
/* Once we have pages in "path", these pages are |
| 206 |
predicate page locked, so they can't be shrunk away. |
| 207 |
They also have SSN (split sequence number) to detect |
| 208 |
splits, so we can directly latch single page while |
| 209 |
getting them. They can be unlatched if not qualified. |
| 210 |
One reason for pre-latch is that we might need to position |
| 211 |
some parent position (requires latch) during search */ |
| 212 |
if (level == 0) { |
| 213 |
/* S latched for SEARCH_LEAF, and X latched |
| 214 |
for MODIFY_LEAF */ |
| 215 |
if (my_latch_mode <= BTR_MODIFY_LEAF) { |
| 216 |
rw_latch = my_latch_mode; |
| 217 |
} |
| 218 |
|
| 219 |
if (my_latch_mode == BTR_CONT_MODIFY_TREE || |
| 220 |
my_latch_mode == BTR_MODIFY_TREE) { |
| 221 |
rw_latch = RW_NO_LATCH; |
| 222 |
} |
| 223 |
|
| 224 |
} else if (level == target_level) { |
| 225 |
rw_latch = RW_X_LATCH; |
| 226 |
} |
| 227 |
|
| 228 |
/* Release previous locked blocks */ |
| 229 |
if (my_latch_mode != BTR_SEARCH_LEAF) { |
| 230 |
for (ulint idx = 0; idx < btr_cur->tree_height; idx++) { |
| 231 |
if (rtr_info->tree_blocks[idx]) { |
| 232 |
mtr_release_block_at_savepoint(mtr, rtr_info->tree_savepoints[idx], |
| 233 |
rtr_info->tree_blocks[idx]); |
| 234 |
rtr_info->tree_blocks[idx] = nullptr; |
| 235 |
} |
| 236 |
} |
| 237 |
for (ulint idx = RTR_MAX_LEVELS; idx < RTR_MAX_LEVELS + 3; idx++) { |
| 238 |
if (rtr_info->tree_blocks[idx]) { |
| 239 |
mtr_release_block_at_savepoint(mtr, rtr_info->tree_savepoints[idx], |
| 240 |
rtr_info->tree_blocks[idx]); |
| 241 |
rtr_info->tree_blocks[idx] = nullptr; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/* set up savepoint to record any locks to be taken */ |
| 247 |
rtr_info->tree_savepoints[tree_idx] = mtr_set_savepoint(mtr); |
| 248 |
|
| 249 |
#ifdef UNIV_RTR_DEBUG |
| 250 |
ut_ad(!(rw_lock_own(&btr_cur->page_cur.block->lock, RW_LOCK_X) || |
| 251 |
rw_lock_own(&btr_cur->page_cur.block->lock, RW_LOCK_S)) || |
| 252 |
my_latch_mode == BTR_MODIFY_TREE || |
| 253 |
my_latch_mode == BTR_CONT_MODIFY_TREE || |
| 254 |
!page_is_leaf(buf_block_get_frame(btr_cur->page_cur.block))); |
| 255 |
#endif /* UNIV_RTR_DEBUG */ |
| 256 |
|
| 257 |
page_id_t page_id(space, next_rec.page_no); |
| 258 |
|
| 259 |
block = buf_page_get_gen(page_id, page_size, rw_latch, nullptr, |
| 260 |
Page_fetch::NORMAL, UT_LOCATION_HERE, mtr); |
| 261 |
|
| 262 |
if (block == nullptr) { |
| 263 |
continue; |
| 264 |
} else if (rw_latch != RW_NO_LATCH) { |
| 265 |
ut_ad(!dict_index_is_ibuf(index)); |
| 266 |
buf_block_dbg_add_level(block, SYNC_TREE_NODE); |
| 267 |
} |
| 268 |
|
| 269 |
rtr_info->tree_blocks[tree_idx] = block; |
| 270 |
|
| 271 |
page = buf_block_get_frame(block); |
| 272 |
page_ssn = page_get_ssn_id(page); |
| 273 |
|
| 274 |
/* If there are splits, push the split page. |
| 275 |
Note that we have SX lock on index->lock, there |
| 276 |
should not be any split/shrink happening here */ |
| 277 |
if (page_ssn > path_ssn) { |
| 278 |
page_no_t next_page_no = btr_page_get_next(page, mtr); |
| 279 |
rtr_non_leaf_stack_push(rtr_info->path, next_page_no, path_ssn, level, 0, |
| 280 |
nullptr, 0); |
| 281 |
|
| 282 |
if (!srv_read_only_mode && mode != PAGE_CUR_RTREE_INSERT && |
| 283 |
mode != PAGE_CUR_RTREE_LOCATE) { |
| 284 |
ut_ad(rtr_info->thr); |
| 285 |
lock_place_prdt_page_lock({space, next_page_no}, index, rtr_info->thr); |
| 286 |
} |
| 287 |
new_split = true; |
| 288 |
#ifdef UNIV_GIS_DEBUG |
| 289 |
fprintf(stderr, "GIS_DIAG: Splitted page found: %d, %ld\n", |
| 290 |
static_cast<int>(need_parent), next_page_no); |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
page_cursor = btr_cur_get_page_cur(btr_cur); |
| 295 |
page_cursor->rec = nullptr; |
| 296 |
|
| 297 |
if (mode == PAGE_CUR_RTREE_LOCATE) { |
| 298 |
if (level == target_level && level == 0) { |
| 299 |
ulint low_match; |
| 300 |
|
| 301 |
found = false; |
| 302 |
|
| 303 |
low_match = page_cur_search(block, index, tuple, PAGE_CUR_LE, |
| 304 |
btr_cur_get_page_cur(btr_cur)); |
| 305 |
|
| 306 |
if (low_match == dtuple_get_n_fields_cmp(tuple)) { |
| 307 |
rec_t *rec = btr_cur_get_rec(btr_cur); |
| 308 |
|
| 309 |
if (!rec_get_deleted_flag(rec, dict_table_is_comp(index->table)) || |
| 310 |
(!for_delete && !for_undo_ins)) { |
| 311 |
found = true; |
| 312 |
btr_cur->low_match = low_match; |
| 313 |
} else { |
| 314 |
/* mark we found deleted row */ |
| 315 |
btr_cur->rtr_info->fd_del = true; |
| 316 |
} |
| 317 |
} |
| 318 |
} else { |
| 319 |
page_cur_mode_t page_mode = mode; |
| 320 |
|
| 321 |
if (level == target_level && target_level != 0) { |
| 322 |
page_mode = PAGE_CUR_RTREE_GET_FATHER; |
| 323 |
} |
| 324 |
found = rtr_cur_search_with_match(block, index, tuple, page_mode, |
| 325 |
page_cursor, btr_cur->rtr_info); |
| 326 |
|
| 327 |
/* Save the position of parent if needed */ |
| 328 |
if (found && need_parent) { |
| 329 |
btr_pcur_t *r_cursor = rtr_get_parent_cursor(btr_cur, level, false); |
| 330 |
|
| 331 |
rec_t *rec = page_cur_get_rec(page_cursor); |
| 332 |
page_cur_position(rec, block, r_cursor->get_page_cur()); |
| 333 |
r_cursor->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 334 |
r_cursor->m_latch_mode = my_latch_mode; |
| 335 |
r_cursor->store_position(mtr); |
| 336 |
#ifdef UNIV_DEBUG |
| 337 |
ulint num_stored = |
| 338 |
rtr_store_parent_path(block, btr_cur, rw_latch, level, mtr); |
| 339 |
ut_ad(num_stored > 0); |
| 340 |
#else |
| 341 |
rtr_store_parent_path(block, btr_cur, rw_latch, level, mtr); |
| 342 |
#endif /* UNIV_DEBUG */ |
| 343 |
} |
| 344 |
} |
| 345 |
} else { |
| 346 |
found = rtr_cur_search_with_match(block, index, tuple, mode, page_cursor, |
| 347 |
btr_cur->rtr_info); |
| 348 |
} |
| 349 |
|
| 350 |
/* Attach predicate lock if needed, no matter whether |
| 351 |
there are matched records */ |
| 352 |
if (mode != PAGE_CUR_RTREE_INSERT && mode != PAGE_CUR_RTREE_LOCATE && |
| 353 |
mode >= PAGE_CUR_CONTAIN && btr_cur->rtr_info->need_prdt_lock) { |
| 354 |
lock_prdt_t prdt; |
| 355 |
|
| 356 |
trx_t *trx = thr_get_trx(btr_cur->rtr_info->thr); |
| 357 |
trx_mutex_enter(trx); |
| 358 |
lock_init_prdt_from_mbr(&prdt, &btr_cur->rtr_info->mbr, mode, |
| 359 |
trx->lock.lock_heap); |
| 360 |
trx_mutex_exit(trx); |
| 361 |
|
| 362 |
if (rw_latch == RW_NO_LATCH) { |
| 363 |
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE); |
| 364 |
} |
| 365 |
|
| 366 |
lock_prdt_lock(block, &prdt, index, btr_cur->rtr_info->thr); |
| 367 |
|
| 368 |
if (rw_latch == RW_NO_LATCH) { |
| 369 |
rw_lock_s_unlock(&(block->lock)); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if (found) { |
| 374 |
if (level == target_level) { |
| 375 |
page_cur_t *r_cur; |
| 376 |
; |
| 377 |
|
| 378 |
if (my_latch_mode == BTR_MODIFY_TREE && level == 0) { |
| 379 |
ut_ad(rw_latch == RW_NO_LATCH); |
| 380 |
page_id_t my_page_id(space, block->page.id.page_no()); |
| 381 |
|
| 382 |
btr_cur_latch_leaves(block, my_page_id, page_size, BTR_MODIFY_TREE, |
| 383 |
btr_cur, mtr); |
| 384 |
} |
| 385 |
|
| 386 |
r_cur = btr_cur_get_page_cur(btr_cur); |
| 387 |
|
| 388 |
page_cur_position(page_cur_get_rec(page_cursor), |
| 389 |
page_cur_get_block(page_cursor), r_cur); |
| 390 |
|
| 391 |
btr_cur->low_match = level != 0 ? DICT_INDEX_SPATIAL_NODEPTR_SIZE + 1 |
| 392 |
: btr_cur->low_match; |
| 393 |
break; |
| 394 |
} |
| 395 |
|
| 396 |
/* Keep the parent path node, which points to |
| 397 |
last node just located */ |
| 398 |
skip_parent = true; |
| 399 |
} else { |
| 400 |
/* Release latch on the current page */ |
| 401 |
ut_ad(rtr_info->tree_blocks[tree_idx]); |
| 402 |
|
| 403 |
mtr_release_block_at_savepoint(mtr, rtr_info->tree_savepoints[tree_idx], |
| 404 |
rtr_info->tree_blocks[tree_idx]); |
| 405 |
rtr_info->tree_blocks[tree_idx] = nullptr; |
| 406 |
} |
| 407 |
|
| 408 |
} while (!rtr_info->path->empty()); |
| 409 |
|
| 410 |
const rec_t *rec = btr_cur_get_rec(btr_cur); |
| 411 |
|
| 412 |
if (page_rec_is_infimum(rec) || page_rec_is_supremum(rec)) { |
| 413 |
mtr_commit(mtr); |
| 414 |
mtr_start(mtr); |
| 415 |
} else if (!index_locked) { |
| 416 |
mtr_memo_release(mtr, dict_index_get_lock(index), MTR_MEMO_X_LOCK); |
| 417 |
} |
| 418 |
|
| 419 |
return (found); |
| 420 |
} |
| 421 |
|
| 422 |
/** Find the next matching record. This function will first exhaust |
| 423 |
the copied record listed in the rtr_info->matches vector before |
| 424 |
moving to next page |
| 425 |
@param[in] tuple Data tuple; NOTE: n_fields_cmp in tuple |
| 426 |
must be set so that it cannot get compared |
| 427 |
to the node ptr page number field! |
| 428 |
@param[in] mode Cursor search mode |
| 429 |
@param[in] sel_mode Select mode: SELECT_ORDINARY, |
| 430 |
SELECT_SKIP_LOKCED, or SELECT_NO_WAIT |
| 431 |
@param[in] cursor Persistent cursor; NOTE that the function |
| 432 |
may release the page latch |
| 433 |
@param[in] cur_level Current level |
| 434 |
@param[in] mtr Mini-transaction |
| 435 |
@return true if there is next qualified record found, otherwise(if |
| 436 |
exhausted) false */ |
| 437 |
bool rtr_pcur_move_to_next(const dtuple_t *tuple, page_cur_mode_t mode, |
| 438 |
select_mode sel_mode, btr_pcur_t *cursor, |
| 439 |
ulint cur_level, mtr_t *mtr) { |
| 440 |
rtr_info_t *rtr_info = cursor->m_btr_cur.rtr_info; |
| 441 |
|
| 442 |
ut_a(cursor->m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 443 |
|
| 444 |
mutex_enter(&rtr_info->matches->rtr_match_mutex); |
| 445 |
/* First retrieve the next record on the current page */ |
| 446 |
while (!rtr_info->matches->matched_recs->empty()) { |
| 447 |
rtr_rec_t rec; |
| 448 |
rec = rtr_info->matches->matched_recs->back(); |
| 449 |
rtr_info->matches->matched_recs->pop_back(); |
| 450 |
|
| 451 |
/* Skip unlocked record |
| 452 |
Note: CHECK TABLE doesn't hold record locks. */ |
| 453 |
if (sel_mode != SELECT_ORDINARY && !rec.locked) { |
| 454 |
continue; |
| 455 |
} |
| 456 |
|
| 457 |
mutex_exit(&rtr_info->matches->rtr_match_mutex); |
| 458 |
|
| 459 |
cursor->m_btr_cur.page_cur.rec = rec.r_rec; |
| 460 |
cursor->m_btr_cur.page_cur.block = &rtr_info->matches->block; |
| 461 |
|
| 462 |
DEBUG_SYNC_C("rtr_pcur_move_to_next_return"); |
| 463 |
return (true); |
| 464 |
} |
| 465 |
|
| 466 |
mutex_exit(&rtr_info->matches->rtr_match_mutex); |
| 467 |
|
| 468 |
/* Fetch the next page */ |
| 469 |
return (rtr_pcur_getnext_from_path(tuple, mode, &cursor->m_btr_cur, cur_level, |
| 470 |
cursor->m_latch_mode, false, mtr)); |
| 471 |
} |
| 472 |
|
| 473 |
/** Check if the cursor holds record pointing to the specified child page |
| 474 |
@return true if it is (pointing to the child page) false otherwise */ |
| 475 |
static bool rtr_compare_cursor_rec( |
| 476 |
dict_index_t *index, /*!< in: index */ |
| 477 |
btr_cur_t *cursor, /*!< in: Cursor to check */ |
| 478 |
page_no_t page_no, /*!< in: desired child page number */ |
| 479 |
mem_heap_t **heap) /*!< in: memory heap */ |
| 480 |
{ |
| 481 |
const rec_t *rec; |
| 482 |
ulint *offsets; |
| 483 |
|
| 484 |
rec = btr_cur_get_rec(cursor); |
| 485 |
|
| 486 |
offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED, |
| 487 |
UT_LOCATION_HERE, heap); |
| 488 |
|
| 489 |
return (btr_node_ptr_get_child_page_no(rec, offsets) == page_no); |
| 490 |
} |
| 491 |
|
| 492 |
void rtr_pcur_open_low(dict_index_t *index, ulint level, const dtuple_t *tuple, |
| 493 |
page_cur_mode_t mode, ulint latch_mode, |
| 494 |
btr_pcur_t *cursor, ut::Location location, mtr_t *mtr) { |
| 495 |
btr_cur_t *btr_cursor; |
| 496 |
ulint n_fields; |
| 497 |
ulint low_match; |
| 498 |
rec_t *rec; |
| 499 |
bool tree_latched = false; |
| 500 |
bool for_delete = false; |
| 501 |
bool for_undo_ins = false; |
| 502 |
|
| 503 |
ut_ad(level == 0); |
| 504 |
|
| 505 |
ut_ad(latch_mode & BTR_MODIFY_LEAF || latch_mode & BTR_MODIFY_TREE); |
| 506 |
ut_ad(mode == PAGE_CUR_RTREE_LOCATE); |
| 507 |
|
| 508 |
/* Initialize the cursor */ |
| 509 |
|
| 510 |
cursor->init(); |
| 511 |
|
| 512 |
for_delete = latch_mode & BTR_RTREE_DELETE_MARK; |
| 513 |
for_undo_ins = latch_mode & BTR_RTREE_UNDO_INS; |
| 514 |
|
| 515 |
cursor->m_latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode); |
| 516 |
cursor->m_search_mode = mode; |
| 517 |
|
| 518 |
/* Search with the tree cursor */ |
| 519 |
|
| 520 |
btr_cursor = cursor->get_btr_cur(); |
| 521 |
|
| 522 |
btr_cursor->rtr_info = rtr_create_rtr_info(false, false, btr_cursor, index); |
| 523 |
|
| 524 |
/* Purge will SX lock the tree instead of take Page Locks */ |
| 525 |
if (btr_cursor->thr) { |
| 526 |
btr_cursor->rtr_info->need_page_lock = true; |
| 527 |
btr_cursor->rtr_info->thr = btr_cursor->thr; |
| 528 |
} |
| 529 |
|
| 530 |
btr_cur_search_to_nth_level(index, level, tuple, mode, latch_mode, btr_cursor, |
| 531 |
0, location.filename, location.line, mtr); |
| 532 |
cursor->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 533 |
|
| 534 |
cursor->m_trx_if_known = nullptr; |
| 535 |
|
| 536 |
low_match = cursor->get_low_match(); |
| 537 |
|
| 538 |
rec = cursor->get_rec(); |
| 539 |
|
| 540 |
n_fields = dtuple_get_n_fields(tuple); |
| 541 |
|
| 542 |
if (latch_mode & BTR_ALREADY_S_LATCHED) { |
| 543 |
ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_S_LOCK)); |
| 544 |
tree_latched = true; |
| 545 |
} |
| 546 |
|
| 547 |
if (latch_mode & BTR_MODIFY_TREE) { |
| 548 |
ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_X_LOCK) || |
| 549 |
mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_SX_LOCK)); |
| 550 |
tree_latched = true; |
| 551 |
} |
| 552 |
|
| 553 |
if (page_rec_is_infimum(rec) || low_match != n_fields || |
| 554 |
(rec_get_deleted_flag(rec, dict_table_is_comp(index->table)) && |
| 555 |
(for_delete || for_undo_ins))) { |
| 556 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(index->table)) && |
| 557 |
for_delete) { |
| 558 |
btr_cursor->rtr_info->fd_del = true; |
| 559 |
btr_cursor->low_match = 0; |
| 560 |
} |
| 561 |
/* Did not find matched row in first dive. Release |
| 562 |
latched block if any before search more pages */ |
| 563 |
if (latch_mode & BTR_MODIFY_LEAF) { |
| 564 |
ulint tree_idx = btr_cursor->tree_height - 1; |
| 565 |
rtr_info_t *rtr_info = btr_cursor->rtr_info; |
| 566 |
|
| 567 |
ut_ad(level == 0); |
| 568 |
|
| 569 |
if (rtr_info->tree_blocks[tree_idx]) { |
| 570 |
mtr_release_block_at_savepoint(mtr, rtr_info->tree_savepoints[tree_idx], |
| 571 |
rtr_info->tree_blocks[tree_idx]); |
| 572 |
rtr_info->tree_blocks[tree_idx] = nullptr; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
bool ret = rtr_pcur_getnext_from_path(tuple, mode, btr_cursor, level, |
| 577 |
latch_mode, tree_latched, mtr); |
| 578 |
|
| 579 |
if (ret) { |
| 580 |
low_match = cursor->get_low_match(); |
| 581 |
ut_ad(low_match == n_fields); |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
/** Returns the upper level node pointer to a R-Tree page. It is assumed |
| 587 |
that mtr holds an SX-latch or X-latch on the tree. |
| 588 |
@return rec_get_offsets() of the node pointer record */ |
| 589 |
static ulint *rtr_page_get_father_node_ptr( |
| 590 |
ulint *offsets, /*!< in: work area for the return value */ |
| 591 |
mem_heap_t *heap, /*!< in: memory heap to use */ |
| 592 |
btr_cur_t *sea_cur, /*!< in: search cursor */ |
| 593 |
btr_cur_t *cursor, /*!< in: cursor pointing to user record, |
| 594 |
out: cursor on node pointer record, |
| 595 |
its page x-latched */ |
| 596 |
mtr_t *mtr) /*!< in: mtr */ |
| 597 |
{ |
| 598 |
dtuple_t *tuple; |
| 599 |
rec_t *user_rec; |
| 600 |
rec_t *node_ptr; |
| 601 |
ulint level; |
| 602 |
page_no_t page_no; |
| 603 |
dict_index_t *index; |
| 604 |
rtr_mbr_t mbr; |
| 605 |
|
| 606 |
page_no = btr_cur_get_block(cursor)->page.id.page_no(); |
| 607 |
index = cursor->index; |
| 608 |
|
| 609 |
ut_ad(srv_read_only_mode || |
| 610 |
mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 611 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK)); |
| 612 |
|
| 613 |
ut_ad(dict_index_get_page(index) != page_no); |
| 614 |
|
| 615 |
level = btr_page_get_level(btr_cur_get_page(cursor)); |
| 616 |
|
| 617 |
user_rec = btr_cur_get_rec(cursor); |
| 618 |
ut_a(page_rec_is_user_rec(user_rec)); |
| 619 |
|
| 620 |
offsets = rec_get_offsets(user_rec, index, offsets, ULINT_UNDEFINED, |
| 621 |
UT_LOCATION_HERE, &heap); |
| 622 |
rtr_get_mbr_from_rec(user_rec, offsets, &mbr); |
| 623 |
|
| 624 |
tuple = rtr_index_build_node_ptr(index, &mbr, user_rec, page_no, heap); |
| 625 |
|
| 626 |
if (sea_cur && !sea_cur->rtr_info) { |
| 627 |
sea_cur = nullptr; |
| 628 |
} |
| 629 |
|
| 630 |
rtr_get_father_node(index, level + 1, tuple, sea_cur, cursor, page_no, mtr); |
| 631 |
|
| 632 |
node_ptr = btr_cur_get_rec(cursor); |
| 633 |
ut_ad(!page_rec_is_comp(node_ptr) || |
| 634 |
rec_get_status(node_ptr) == REC_STATUS_NODE_PTR); |
| 635 |
offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED, |
| 636 |
UT_LOCATION_HERE, &heap); |
| 637 |
|
| 638 |
page_no_t child_page = btr_node_ptr_get_child_page_no(node_ptr, offsets); |
| 639 |
|
| 640 |
if (child_page != page_no) { |
| 641 |
const rec_t *print_rec; |
| 642 |
|
| 643 |
ib::fatal error(UT_LOCATION_HERE); |
| 644 |
|
| 645 |
error << "Corruption of index " << index->name << " of table " |
| 646 |
<< index->table->name << " parent page " << page_no << " child page " |
| 647 |
<< child_page; |
| 648 |
|
| 649 |
print_rec = page_rec_get_next(page_get_infimum_rec(page_align(user_rec))); |
| 650 |
offsets = rec_get_offsets(print_rec, index, offsets, ULINT_UNDEFINED, |
| 651 |
UT_LOCATION_HERE, &heap); |
| 652 |
error << "; child "; |
| 653 |
rec_print(error.m_oss, print_rec, |
| 654 |
rec_get_info_bits(print_rec, rec_offs_comp(offsets)), offsets); |
| 655 |
offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED, |
| 656 |
UT_LOCATION_HERE, &heap); |
| 657 |
error << "; parent "; |
| 658 |
rec_print(error.m_oss, print_rec, |
| 659 |
rec_get_info_bits(print_rec, rec_offs_comp(offsets)), offsets); |
| 660 |
|
| 661 |
error << ". You should dump + drop + reimport the table to" |
| 662 |
" fix the corruption. If the crash happens at" |
| 663 |
" database startup, see " REFMAN |
| 664 |
"forcing-innodb-recovery.html about forcing" |
| 665 |
" recovery. Then dump + drop + reimport."; |
| 666 |
} |
| 667 |
|
| 668 |
return (offsets); |
| 669 |
} |
| 670 |
|
| 671 |
/* Get the rtree page father. |
| 672 |
@param[in] index rtree index |
| 673 |
@param[in] block child page in the index |
| 674 |
@param[in] mtr mtr |
| 675 |
@param[in] sea_cur search cursor, contains information |
| 676 |
about parent nodes in search |
| 677 |
@param[in] cursor cursor on node pointer record, |
| 678 |
its page x-latched */ |
| 679 |
void rtr_page_get_father(dict_index_t *index, buf_block_t *block, mtr_t *mtr, |
| 680 |
btr_cur_t *sea_cur, btr_cur_t *cursor) { |
| 681 |
mem_heap_t *heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 682 |
#ifdef UNIV_DEBUG |
| 683 |
ulint *offsets; |
| 684 |
|
| 685 |
offsets = rtr_page_get_father_block(nullptr, heap, index, block, mtr, sea_cur, |
| 686 |
cursor); |
| 687 |
|
| 688 |
ulint page_no = btr_node_ptr_get_child_page_no(cursor->page_cur.rec, offsets); |
| 689 |
|
| 690 |
ut_ad(page_no == block->page.id.page_no()); |
| 691 |
#else |
| 692 |
rtr_page_get_father_block(nullptr, heap, index, block, mtr, sea_cur, cursor); |
| 693 |
#endif |
| 694 |
|
| 695 |
mem_heap_free(heap); |
| 696 |
} |
| 697 |
|
| 698 |
/** Returns the father block to a page. It is assumed that mtr holds |
| 699 |
an X or SX latch on the tree. |
| 700 |
@return rec_get_offsets() of the node pointer record */ |
| 701 |
ulint *rtr_page_get_father_block( |
| 702 |
ulint *offsets, /*!< in: work area for the return value */ |
| 703 |
mem_heap_t *heap, /*!< in: memory heap to use */ |
| 704 |
dict_index_t *index, /*!< in: b-tree index */ |
| 705 |
buf_block_t *block, /*!< in: child page in the index */ |
| 706 |
mtr_t *mtr, /*!< in: mtr */ |
| 707 |
btr_cur_t *sea_cur, /*!< in: search cursor, contains information |
| 708 |
about parent nodes in search */ |
| 709 |
btr_cur_t *cursor) /*!< out: cursor on node pointer record, |
| 710 |
its page x-latched */ |
| 711 |
{ |
| 712 |
rec_t *rec = |
| 713 |
page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(block))); |
| 714 |
btr_cur_position(index, rec, block, cursor); |
| 715 |
|
| 716 |
return (rtr_page_get_father_node_ptr(offsets, heap, sea_cur, cursor, mtr)); |
| 717 |
} |
| 718 |
|
| 719 |
/** Returns the upper level node pointer to a R-Tree page. It is assumed |
| 720 |
that mtr holds an x-latch on the tree. */ |
| 721 |
void rtr_get_father_node( |
| 722 |
dict_index_t *index, /*!< in: index */ |
| 723 |
ulint level, /*!< in: the tree level of search */ |
| 724 |
const dtuple_t *tuple, /*!< in: data tuple; NOTE: n_fields_cmp in |
| 725 |
tuple must be set so that it cannot get |
| 726 |
compared to the node ptr page number field! */ |
| 727 |
btr_cur_t *sea_cur, /*!< in: search cursor */ |
| 728 |
btr_cur_t *btr_cur, /*!< in/out: tree cursor; the cursor page is |
| 729 |
s- or x-latched, but see also above! */ |
| 730 |
page_no_t page_no, /*!< Current page no */ |
| 731 |
mtr_t *mtr) /*!< in: mtr */ |
| 732 |
{ |
| 733 |
mem_heap_t *heap = nullptr; |
| 734 |
bool ret = false; |
| 735 |
const rec_t *rec; |
| 736 |
ulint n_fields; |
| 737 |
bool new_rtr = false; |
| 738 |
|
| 739 |
/* Try to optimally locate the parent node. Level should always |
| 740 |
less than sea_cur->tree_height unless the root is splitting */ |
| 741 |
if (sea_cur && sea_cur->tree_height > level) { |
| 742 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 743 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK)); |
| 744 |
ret = rtr_cur_restore_position(BTR_CONT_MODIFY_TREE, sea_cur, level, mtr); |
| 745 |
|
| 746 |
/* Once we block shrink tree nodes while there are |
| 747 |
active search on it, this optimal locating should always |
| 748 |
succeeds */ |
| 749 |
ut_ad(ret); |
| 750 |
|
| 751 |
if (ret) { |
| 752 |
btr_pcur_t *r_cursor = rtr_get_parent_cursor(sea_cur, level, false); |
| 753 |
|
| 754 |
rec = r_cursor->get_rec(); |
| 755 |
|
| 756 |
ut_ad(r_cursor->m_rel_pos == BTR_PCUR_ON); |
| 757 |
page_cur_position(rec, r_cursor->get_block(), |
| 758 |
btr_cur_get_page_cur(btr_cur)); |
| 759 |
btr_cur->rtr_info = sea_cur->rtr_info; |
| 760 |
btr_cur->m_own_rtr_info = false; |
| 761 |
btr_cur->tree_height = sea_cur->tree_height; |
| 762 |
ut_ad(rtr_compare_cursor_rec(index, btr_cur, page_no, &heap)); |
| 763 |
goto func_exit; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
/* We arrive here in one of two scenario |
| 768 |
1) check table and btr_valide |
| 769 |
2) index root page being raised */ |
| 770 |
ut_ad(!sea_cur || sea_cur->tree_height == level); |
| 771 |
|
| 772 |
if (btr_cur->rtr_info) { |
| 773 |
rtr_clean_rtr_info(btr_cur->rtr_info, true); |
| 774 |
} else { |
| 775 |
new_rtr = true; |
| 776 |
} |
| 777 |
|
| 778 |
btr_cur->rtr_info = rtr_create_rtr_info(false, false, btr_cur, index); |
| 779 |
|
| 780 |
if (sea_cur && sea_cur->tree_height == level) { |
| 781 |
/* root split, and search the new root */ |
| 782 |
btr_cur_search_to_nth_level(index, level, tuple, PAGE_CUR_RTREE_LOCATE, |
| 783 |
BTR_CONT_MODIFY_TREE, btr_cur, 0, __FILE__, |
| 784 |
__LINE__, mtr); |
| 785 |
|
| 786 |
} else { |
| 787 |
/* btr_validate */ |
| 788 |
ut_ad(level >= 1); |
| 789 |
ut_ad(!sea_cur); |
| 790 |
|
| 791 |
btr_cur_search_to_nth_level(index, level, tuple, PAGE_CUR_RTREE_LOCATE, |
| 792 |
BTR_CONT_MODIFY_TREE, btr_cur, 0, __FILE__, |
| 793 |
__LINE__, mtr); |
| 794 |
|
| 795 |
rec = btr_cur_get_rec(btr_cur); |
| 796 |
n_fields = dtuple_get_n_fields_cmp(tuple); |
| 797 |
|
| 798 |
if (page_rec_is_infimum(rec) || (btr_cur->low_match != n_fields)) { |
| 799 |
ret = rtr_pcur_getnext_from_path(tuple, PAGE_CUR_RTREE_LOCATE, btr_cur, |
| 800 |
level, BTR_CONT_MODIFY_TREE, true, mtr); |
| 801 |
|
| 802 |
ut_ad(ret && btr_cur->low_match == n_fields); |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
ret = rtr_compare_cursor_rec(index, btr_cur, page_no, &heap); |
| 807 |
|
| 808 |
ut_ad(ret); |
| 809 |
|
| 810 |
func_exit: |
| 811 |
if (heap) { |
| 812 |
mem_heap_free(heap); |
| 813 |
} |
| 814 |
|
| 815 |
if (new_rtr && btr_cur->rtr_info) { |
| 816 |
rtr_clean_rtr_info(btr_cur->rtr_info, true); |
| 817 |
btr_cur->rtr_info = nullptr; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
/** Create a RTree search info structure |
| 822 |
@param[in] need_prdt Whether predicate lock is needed |
| 823 |
@param[in] init_matches Whether to initiate the "matches" structure for |
| 824 |
collecting matched leaf records |
| 825 |
@param[in] cursor Tree search cursor |
| 826 |
@param[in] index Index struct */ |
| 827 |
rtr_info_t *rtr_create_rtr_info(bool need_prdt, bool init_matches, |
| 828 |
btr_cur_t *cursor, dict_index_t *index) { |
| 829 |
rtr_info_t *rtr_info; |
| 830 |
|
| 831 |
index = index ? index : cursor->index; |
| 832 |
ut_ad(index); |
| 833 |
|
| 834 |
rtr_info = static_cast<rtr_info_t *>( |
| 835 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*rtr_info))); |
| 836 |
|
| 837 |
rtr_info->allocated = true; |
| 838 |
rtr_info->cursor = cursor; |
| 839 |
rtr_info->index = index; |
| 840 |
rtr_info->is_dup = nullptr; |
| 841 |
|
| 842 |
if (init_matches) { |
| 843 |
rtr_info->heap = |
| 844 |
mem_heap_create(sizeof(*(rtr_info->matches)), UT_LOCATION_HERE); |
| 845 |
rtr_info->matches = static_cast<matched_rec_t *>( |
| 846 |
mem_heap_zalloc(rtr_info->heap, sizeof(*rtr_info->matches))); |
| 847 |
|
| 848 |
rtr_info->matches->matched_recs = |
| 849 |
ut::new_withkey<rtr_rec_vector>(UT_NEW_THIS_FILE_PSI_KEY); |
| 850 |
|
| 851 |
rtr_info->matches->bufp = |
| 852 |
page_align(rtr_info->matches->rec_buf + UNIV_PAGE_SIZE_MAX + 1); |
| 853 |
mutex_create(LATCH_ID_RTR_MATCH_MUTEX, &rtr_info->matches->rtr_match_mutex); |
| 854 |
rw_lock_create(PFS_NOT_INSTRUMENTED, &(rtr_info->matches->block.lock), |
| 855 |
LATCH_ID_BUF_BLOCK_LOCK); |
| 856 |
} |
| 857 |
|
| 858 |
rtr_info->path = ut::new_withkey<rtr_node_path_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 859 |
rtr_info->parent_path = |
| 860 |
ut::new_withkey<rtr_node_path_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 861 |
rtr_info->need_prdt_lock = need_prdt; |
| 862 |
mutex_create(LATCH_ID_RTR_PATH_MUTEX, &rtr_info->rtr_path_mutex); |
| 863 |
|
| 864 |
mutex_enter(&index->rtr_track->rtr_active_mutex); |
| 865 |
index->rtr_track->rtr_active->push_back(rtr_info); |
| 866 |
mutex_exit(&index->rtr_track->rtr_active_mutex); |
| 867 |
return (rtr_info); |
| 868 |
} |
| 869 |
|
| 870 |
/** Update a btr_cur_t with rtr_info |
| 871 |
@param[in,out] cursor Tree cursor |
| 872 |
@param[in] rtr_info Rtr_info to set to the cursor */ |
| 873 |
void rtr_info_update_btr(btr_cur_t *cursor, rtr_info_t *rtr_info) { |
| 874 |
ut_ad(rtr_info); |
| 875 |
|
| 876 |
cursor->rtr_info = rtr_info; |
| 877 |
} |
| 878 |
|
| 879 |
/** Initialize a R-Tree Search structure */ |
| 880 |
void rtr_init_rtr_info( |
| 881 |
/****************/ |
| 882 |
rtr_info_t *rtr_info, /*!< in: rtr_info to set to the |
| 883 |
cursor */ |
| 884 |
bool need_prdt, /*!< in: Whether predicate lock is |
| 885 |
needed */ |
| 886 |
btr_cur_t *cursor, /*!< in: tree search cursor */ |
| 887 |
dict_index_t *index, /*!< in: index structure */ |
| 888 |
bool reinit) /*!< in: Whether this is a reinit */ |
| 889 |
{ |
| 890 |
ut_ad(rtr_info); |
| 891 |
|
| 892 |
if (!reinit) { |
| 893 |
/* Reset all members. */ |
| 894 |
rtr_info->path = nullptr; |
| 895 |
rtr_info->parent_path = nullptr; |
| 896 |
rtr_info->matches = nullptr; |
| 897 |
|
| 898 |
mutex_create(LATCH_ID_RTR_PATH_MUTEX, &rtr_info->rtr_path_mutex); |
| 899 |
|
| 900 |
memset(rtr_info->tree_blocks, 0x0, sizeof(rtr_info->tree_blocks)); |
| 901 |
memset(rtr_info->tree_savepoints, 0x0, sizeof(rtr_info->tree_savepoints)); |
| 902 |
rtr_info->mbr.xmin = 0.0; |
| 903 |
rtr_info->mbr.xmax = 0.0; |
| 904 |
rtr_info->mbr.ymin = 0.0; |
| 905 |
rtr_info->mbr.ymax = 0.0; |
| 906 |
rtr_info->thr = nullptr; |
| 907 |
rtr_info->heap = nullptr; |
| 908 |
rtr_info->cursor = nullptr; |
| 909 |
rtr_info->index = nullptr; |
| 910 |
rtr_info->need_prdt_lock = false; |
| 911 |
rtr_info->need_page_lock = false; |
| 912 |
rtr_info->allocated = false; |
| 913 |
rtr_info->mbr_adj = false; |
| 914 |
rtr_info->fd_del = false; |
| 915 |
rtr_info->search_tuple = nullptr; |
| 916 |
rtr_info->search_mode = PAGE_CUR_UNSUPP; |
| 917 |
} |
| 918 |
|
| 919 |
ut_ad(!rtr_info->matches || rtr_info->matches->matched_recs->empty()); |
| 920 |
|
| 921 |
rtr_info->path = ut::new_withkey<rtr_node_path_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 922 |
rtr_info->parent_path = |
| 923 |
ut::new_withkey<rtr_node_path_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 924 |
rtr_info->need_prdt_lock = need_prdt; |
| 925 |
rtr_info->cursor = cursor; |
| 926 |
rtr_info->index = index; |
| 927 |
rtr_info->is_dup = nullptr; |
| 928 |
|
| 929 |
mutex_enter(&index->rtr_track->rtr_active_mutex); |
| 930 |
index->rtr_track->rtr_active->push_back(rtr_info); |
| 931 |
mutex_exit(&index->rtr_track->rtr_active_mutex); |
| 932 |
} |
| 933 |
|
| 934 |
/** Clean up R-Tree search structure */ |
| 935 |
void rtr_clean_rtr_info(rtr_info_t *rtr_info, /*!< in: RTree search info */ |
| 936 |
bool free_all) /*!< in: need to free rtr_info itself */ |
| 937 |
{ |
| 938 |
dict_index_t *index; |
| 939 |
bool initialized = false; |
| 940 |
|
| 941 |
if (!rtr_info) { |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
index = rtr_info->index; |
| 946 |
|
| 947 |
if (index) { |
| 948 |
mutex_enter(&index->rtr_track->rtr_active_mutex); |
| 949 |
} |
| 950 |
|
| 951 |
while (rtr_info->parent_path && !rtr_info->parent_path->empty()) { |
| 952 |
btr_pcur_t *cur = rtr_info->parent_path->back().cursor; |
| 953 |
rtr_info->parent_path->pop_back(); |
| 954 |
|
| 955 |
if (cur) { |
| 956 |
cur->close(); |
| 957 |
ut::free(cur); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
ut::delete_(rtr_info->parent_path); |
| 962 |
rtr_info->parent_path = nullptr; |
| 963 |
|
| 964 |
if (rtr_info->path != nullptr) { |
| 965 |
ut::delete_(rtr_info->path); |
| 966 |
rtr_info->path = nullptr; |
| 967 |
initialized = true; |
| 968 |
} |
| 969 |
|
| 970 |
if (rtr_info->matches) { |
| 971 |
rtr_info->matches->used = false; |
| 972 |
rtr_info->matches->locked = false; |
| 973 |
rtr_info->matches->valid = false; |
| 974 |
rtr_info->matches->matched_recs->clear(); |
| 975 |
} |
| 976 |
|
| 977 |
if (index) { |
| 978 |
index->rtr_track->rtr_active->remove(rtr_info); |
| 979 |
mutex_exit(&index->rtr_track->rtr_active_mutex); |
| 980 |
} |
| 981 |
|
| 982 |
if (free_all) { |
| 983 |
if (rtr_info->matches) { |
| 984 |
if (rtr_info->matches->matched_recs != nullptr) { |
| 985 |
ut::delete_(rtr_info->matches->matched_recs); |
| 986 |
} |
| 987 |
|
| 988 |
/* Clear any space references in the page copied. */ |
| 989 |
buf_page_prepare_for_free(&rtr_info->matches->block.page); |
| 990 |
|
| 991 |
rw_lock_free(&(rtr_info->matches->block.lock)); |
| 992 |
mutex_destroy(&rtr_info->matches->rtr_match_mutex); |
| 993 |
} |
| 994 |
|
| 995 |
if (rtr_info->heap) { |
| 996 |
mem_heap_free(rtr_info->heap); |
| 997 |
} |
| 998 |
|
| 999 |
if (initialized) { |
| 1000 |
mutex_destroy(&rtr_info->rtr_path_mutex); |
| 1001 |
} |
| 1002 |
|
| 1003 |
if (rtr_info->allocated) { |
| 1004 |
ut::free(rtr_info); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
/** Rebuilt the "path" to exclude the removing page no */ |
| 1010 |
static void rtr_rebuild_path( |
| 1011 |
rtr_info_t *rtr_info, /*!< in: RTree search info */ |
| 1012 |
page_no_t page_no) /*!< in: need to free rtr_info itself */ |
| 1013 |
{ |
| 1014 |
rtr_node_path_t *new_path = |
| 1015 |
ut::new_withkey<rtr_node_path_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 1016 |
|
| 1017 |
rtr_node_path_t::iterator rit; |
| 1018 |
#ifdef UNIV_DEBUG |
| 1019 |
ulint before_size = rtr_info->path->size(); |
| 1020 |
#endif /* UNIV_DEBUG */ |
| 1021 |
|
| 1022 |
for (rit = rtr_info->path->begin(); rit != rtr_info->path->end(); ++rit) { |
| 1023 |
node_visit_t next_rec = *rit; |
| 1024 |
|
| 1025 |
if (next_rec.page_no == page_no) { |
| 1026 |
continue; |
| 1027 |
} |
| 1028 |
|
| 1029 |
new_path->push_back(next_rec); |
| 1030 |
#ifdef UNIV_DEBUG |
| 1031 |
node_visit_t rec = new_path->back(); |
| 1032 |
ut_ad(rec.level < rtr_info->cursor->tree_height && rec.page_no > 0); |
| 1033 |
#endif /* UNIV_DEBUG */ |
| 1034 |
} |
| 1035 |
|
| 1036 |
ut::delete_(rtr_info->path); |
| 1037 |
|
| 1038 |
ut_ad(new_path->size() == before_size - 1); |
| 1039 |
|
| 1040 |
rtr_info->path = new_path; |
| 1041 |
|
| 1042 |
if (!rtr_info->parent_path->empty()) { |
| 1043 |
rtr_node_path_t *new_parent_path = |
| 1044 |
ut::new_withkey<rtr_node_path_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 1045 |
|
| 1046 |
for (rit = rtr_info->parent_path->begin(); |
| 1047 |
rit != rtr_info->parent_path->end(); ++rit) { |
| 1048 |
node_visit_t next_rec = *rit; |
| 1049 |
|
| 1050 |
if (next_rec.child_no == page_no) { |
| 1051 |
btr_pcur_t *cur = next_rec.cursor; |
| 1052 |
|
| 1053 |
if (cur) { |
| 1054 |
cur->close(); |
| 1055 |
ut::free(cur); |
| 1056 |
} |
| 1057 |
|
| 1058 |
continue; |
| 1059 |
} |
| 1060 |
|
| 1061 |
new_parent_path->push_back(next_rec); |
| 1062 |
} |
| 1063 |
ut::delete_(rtr_info->parent_path); |
| 1064 |
rtr_info->parent_path = new_parent_path; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
/** Check whether a discarding page is in anyone's search path |
| 1069 |
@param[in] index Index |
| 1070 |
@param[in,out] cursor Cursor on the page to discard: not on the root page |
| 1071 |
@param[in] block Block of page to be discarded */ |
| 1072 |
void rtr_check_discard_page(dict_index_t *index, btr_cur_t *cursor, |
| 1073 |
buf_block_t *block) { |
| 1074 |
page_no_t pageno = block->page.id.page_no(); |
| 1075 |
rtr_info_t *rtr_info; |
| 1076 |
rtr_info_active::iterator it; |
| 1077 |
|
| 1078 |
mutex_enter(&index->rtr_track->rtr_active_mutex); |
| 1079 |
|
| 1080 |
for (it = index->rtr_track->rtr_active->begin(); |
| 1081 |
it != index->rtr_track->rtr_active->end(); ++it) { |
| 1082 |
rtr_info = *it; |
| 1083 |
rtr_node_path_t::iterator rit; |
| 1084 |
bool found = false; |
| 1085 |
|
| 1086 |
if (cursor && rtr_info == cursor->rtr_info) { |
| 1087 |
continue; |
| 1088 |
} |
| 1089 |
|
| 1090 |
mutex_enter(&rtr_info->rtr_path_mutex); |
| 1091 |
for (rit = rtr_info->path->begin(); rit != rtr_info->path->end(); ++rit) { |
| 1092 |
node_visit_t node = *rit; |
| 1093 |
|
| 1094 |
if (node.page_no == pageno) { |
| 1095 |
found = true; |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (found) { |
| 1101 |
rtr_rebuild_path(rtr_info, pageno); |
| 1102 |
} |
| 1103 |
mutex_exit(&rtr_info->rtr_path_mutex); |
| 1104 |
|
| 1105 |
if (rtr_info->matches) { |
| 1106 |
mutex_enter(&rtr_info->matches->rtr_match_mutex); |
| 1107 |
|
| 1108 |
if ((&rtr_info->matches->block)->page.id.page_no() == pageno) { |
| 1109 |
if (!rtr_info->matches->matched_recs->empty()) { |
| 1110 |
rtr_info->matches->matched_recs->clear(); |
| 1111 |
} |
| 1112 |
ut_ad(rtr_info->matches->matched_recs->empty()); |
| 1113 |
rtr_info->matches->valid = false; |
| 1114 |
} |
| 1115 |
|
| 1116 |
mutex_exit(&rtr_info->matches->rtr_match_mutex); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
mutex_exit(&index->rtr_track->rtr_active_mutex); |
| 1121 |
|
| 1122 |
locksys::Shard_latch_guard guard{UT_LOCATION_HERE, block->get_page_id()}; |
| 1123 |
lock_prdt_page_free_from_discard(block, lock_sys->prdt_hash); |
| 1124 |
lock_prdt_page_free_from_discard(block, lock_sys->prdt_page_hash); |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** Restore the stored position of a persistent cursor bufferfixing the page */ |
| 1128 |
static bool rtr_cur_restore_position( |
| 1129 |
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */ |
| 1130 |
btr_cur_t *btr_cur, /*!< in: detached persistent cursor */ |
| 1131 |
ulint level, /*!< in: index level */ |
| 1132 |
mtr_t *mtr) /*!< in: mtr */ |
| 1133 |
{ |
| 1134 |
dict_index_t *index; |
| 1135 |
mem_heap_t *heap; |
| 1136 |
btr_pcur_t *r_cursor = rtr_get_parent_cursor(btr_cur, level, false); |
| 1137 |
dtuple_t *tuple; |
| 1138 |
bool ret = false; |
| 1139 |
|
| 1140 |
ut_ad(mtr); |
| 1141 |
ut_ad(r_cursor); |
| 1142 |
ut_ad(mtr->is_active()); |
| 1143 |
|
| 1144 |
index = btr_cur->index; |
| 1145 |
|
| 1146 |
if (r_cursor->m_rel_pos == BTR_PCUR_AFTER_LAST_IN_TREE || |
| 1147 |
r_cursor->m_rel_pos == BTR_PCUR_BEFORE_FIRST_IN_TREE) { |
| 1148 |
return (false); |
| 1149 |
} |
| 1150 |
|
| 1151 |
DBUG_EXECUTE_IF("rtr_pessimistic_position", r_cursor->m_modify_clock = 100;); |
| 1152 |
|
| 1153 |
ut_ad(latch_mode == BTR_CONT_MODIFY_TREE); |
| 1154 |
|
| 1155 |
if (r_cursor->m_block_when_stored.run_with_hint([&](buf_block_t *hint) { |
| 1156 |
return hint != nullptr && |
| 1157 |
buf_page_optimistic_get( |
| 1158 |
RW_X_LATCH, hint, r_cursor->m_modify_clock, |
| 1159 |
Page_fetch::NORMAL, __FILE__, __LINE__, mtr); |
| 1160 |
})) { |
| 1161 |
ut_ad(r_cursor->m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 1162 |
|
| 1163 |
ut_ad(r_cursor->m_rel_pos == BTR_PCUR_ON); |
| 1164 |
#ifdef UNIV_DEBUG |
| 1165 |
do { |
| 1166 |
const rec_t *rec; |
| 1167 |
const ulint *offsets1; |
| 1168 |
const ulint *offsets2; |
| 1169 |
ulint comp; |
| 1170 |
|
| 1171 |
rec = r_cursor->get_rec(); |
| 1172 |
|
| 1173 |
heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 1174 |
offsets1 = |
| 1175 |
rec_get_offsets(r_cursor->m_old_rec, index, nullptr, |
| 1176 |
r_cursor->m_old_n_fields, UT_LOCATION_HERE, &heap); |
| 1177 |
offsets2 = rec_get_offsets(rec, index, nullptr, r_cursor->m_old_n_fields, |
| 1178 |
UT_LOCATION_HERE, &heap); |
| 1179 |
|
| 1180 |
comp = rec_offs_comp(offsets1); |
| 1181 |
|
| 1182 |
if (rec_get_info_bits(r_cursor->m_old_rec, comp) & |
| 1183 |
REC_INFO_MIN_REC_FLAG) { |
| 1184 |
ut_ad(rec_get_info_bits(rec, comp) & REC_INFO_MIN_REC_FLAG); |
| 1185 |
} else { |
| 1186 |
ut_ad(!cmp_rec_rec(r_cursor->m_old_rec, rec, offsets1, offsets2, index, |
| 1187 |
page_is_spatial_non_leaf(rec, index))); |
| 1188 |
} |
| 1189 |
|
| 1190 |
mem_heap_free(heap); |
| 1191 |
} while (false); |
| 1192 |
#endif /* UNIV_DEBUG */ |
| 1193 |
|
| 1194 |
return (true); |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* Page has changed, for R-Tree, the page cannot be shrunk away, |
| 1198 |
so we search the page and its right siblings */ |
| 1199 |
buf_block_t *block; |
| 1200 |
node_seq_t page_ssn; |
| 1201 |
const page_t *page; |
| 1202 |
page_cur_t *page_cursor; |
| 1203 |
node_visit_t *node = rtr_get_parent_node(btr_cur, level, false); |
| 1204 |
space_id_t space = dict_index_get_space(index); |
| 1205 |
node_seq_t path_ssn = node->seq_no; |
| 1206 |
page_size_t page_size = dict_table_page_size(index->table); |
| 1207 |
|
| 1208 |
page_no_t page_no = node->page_no; |
| 1209 |
|
| 1210 |
heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 1211 |
|
| 1212 |
tuple = dict_index_build_data_tuple(index, r_cursor->m_old_rec, |
| 1213 |
r_cursor->m_old_n_fields, heap); |
| 1214 |
|
| 1215 |
page_cursor = r_cursor->get_page_cur(); |
| 1216 |
ut_ad(r_cursor == node->cursor); |
| 1217 |
|
| 1218 |
search_again: |
| 1219 |
page_id_t page_id(space, page_no); |
| 1220 |
|
| 1221 |
block = buf_page_get_gen(page_id, page_size, RW_X_LATCH, nullptr, |
| 1222 |
Page_fetch::NORMAL, UT_LOCATION_HERE, mtr); |
| 1223 |
|
| 1224 |
ut_ad(block); |
| 1225 |
|
| 1226 |
/* Get the page SSN */ |
| 1227 |
page = buf_block_get_frame(block); |
| 1228 |
page_ssn = page_get_ssn_id(page); |
| 1229 |
|
| 1230 |
ulint low_match = |
| 1231 |
page_cur_search(block, index, tuple, PAGE_CUR_LE, page_cursor); |
| 1232 |
|
| 1233 |
if (low_match == r_cursor->m_old_n_fields) { |
| 1234 |
const rec_t *rec; |
| 1235 |
const ulint *offsets1; |
| 1236 |
const ulint *offsets2; |
| 1237 |
ulint comp; |
| 1238 |
|
| 1239 |
rec = r_cursor->get_rec(); |
| 1240 |
|
| 1241 |
offsets1 = |
| 1242 |
rec_get_offsets(r_cursor->m_old_rec, index, nullptr, |
| 1243 |
r_cursor->m_old_n_fields, UT_LOCATION_HERE, &heap); |
| 1244 |
offsets2 = rec_get_offsets(rec, index, nullptr, r_cursor->m_old_n_fields, |
| 1245 |
UT_LOCATION_HERE, &heap); |
| 1246 |
|
| 1247 |
comp = rec_offs_comp(offsets1); |
| 1248 |
|
| 1249 |
if ((rec_get_info_bits(r_cursor->m_old_rec, comp) & |
| 1250 |
REC_INFO_MIN_REC_FLAG) && |
| 1251 |
(rec_get_info_bits(rec, comp) & REC_INFO_MIN_REC_FLAG)) { |
| 1252 |
r_cursor->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 1253 |
ret = true; |
| 1254 |
} else if (!cmp_rec_rec(r_cursor->m_old_rec, rec, offsets1, offsets2, index, |
| 1255 |
page_is_spatial_non_leaf(rec, index))) { |
| 1256 |
r_cursor->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 1257 |
ret = true; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
/* Check the page SSN to see if it has been split, if so, search |
| 1262 |
the right page */ |
| 1263 |
if (!ret && page_ssn > path_ssn) { |
| 1264 |
page_no = btr_page_get_next(page, mtr); |
| 1265 |
goto search_again; |
| 1266 |
} |
| 1267 |
|
| 1268 |
mem_heap_free(heap); |
| 1269 |
|
| 1270 |
return (ret); |
| 1271 |
} |
| 1272 |
|
| 1273 |
/** Copy the leaf level R-tree record, and push it to matched_rec in rtr_info */ |
| 1274 |
static void rtr_leaf_push_match_rec( |
| 1275 |
const rec_t *rec, /*!< in: record to copy */ |
| 1276 |
rtr_info_t *rtr_info, /*!< in/out: search stack */ |
| 1277 |
ulint *offsets, /*!< in: offsets */ |
| 1278 |
bool is_comp) /*!< in: is compact format */ |
| 1279 |
{ |
| 1280 |
byte *buf; |
| 1281 |
matched_rec_t *match_rec = rtr_info->matches; |
| 1282 |
rec_t *copy; |
| 1283 |
ulint data_len; |
| 1284 |
rtr_rec_t rtr_rec; |
| 1285 |
|
| 1286 |
buf = match_rec->block.frame + match_rec->used; |
| 1287 |
|
| 1288 |
copy = rec_copy(buf, rec, offsets); |
| 1289 |
|
| 1290 |
if (is_comp) { |
| 1291 |
rec_set_next_offs_new(copy, PAGE_NEW_SUPREMUM); |
| 1292 |
} else { |
| 1293 |
rec_set_next_offs_old(copy, PAGE_OLD_SUPREMUM); |
| 1294 |
} |
| 1295 |
|
| 1296 |
rtr_rec.r_rec = copy; |
| 1297 |
rtr_rec.locked = false; |
| 1298 |
|
| 1299 |
match_rec->matched_recs->push_back(rtr_rec); |
| 1300 |
match_rec->valid = true; |
| 1301 |
|
| 1302 |
data_len = rec_offs_data_size(offsets) + rec_offs_extra_size(offsets); |
| 1303 |
match_rec->used += data_len; |
| 1304 |
|
| 1305 |
ut_ad(match_rec->used < UNIV_PAGE_SIZE); |
| 1306 |
} |
| 1307 |
|
| 1308 |
/** Store the parent path cursor |
| 1309 |
@return number of cursor stored */ |
| 1310 |
ulint rtr_store_parent_path( |
| 1311 |
const buf_block_t *block, /*!< in: block of the page */ |
| 1312 |
btr_cur_t *btr_cur, /*!< in/out: persistent cursor */ |
| 1313 |
ulint latch_mode, |
| 1314 |
/*!< in: latch_mode */ |
| 1315 |
ulint level, /*!< in: index level */ |
| 1316 |
mtr_t *mtr) /*!< in: mtr */ |
| 1317 |
{ |
| 1318 |
ulint num = btr_cur->rtr_info->parent_path->size(); |
| 1319 |
ulint num_stored = 0; |
| 1320 |
|
| 1321 |
while (num >= 1) { |
| 1322 |
node_visit_t *node = &(*btr_cur->rtr_info->parent_path)[num - 1]; |
| 1323 |
btr_pcur_t *r_cursor = node->cursor; |
| 1324 |
buf_block_t *cur_block; |
| 1325 |
|
| 1326 |
if (node->level > level) { |
| 1327 |
break; |
| 1328 |
} |
| 1329 |
|
| 1330 |
r_cursor->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 1331 |
r_cursor->m_latch_mode = latch_mode; |
| 1332 |
|
| 1333 |
cur_block = r_cursor->get_block(); |
| 1334 |
|
| 1335 |
if (cur_block == block) { |
| 1336 |
r_cursor->store_position(mtr); |
| 1337 |
num_stored++; |
| 1338 |
} else { |
| 1339 |
break; |
| 1340 |
} |
| 1341 |
|
| 1342 |
num--; |
| 1343 |
} |
| 1344 |
|
| 1345 |
return (num_stored); |
| 1346 |
} |
| 1347 |
/** push a nonleaf index node to the search path for insertion */ |
| 1348 |
static void rtr_non_leaf_insert_stack_push( |
| 1349 |
dict_index_t *index, /*!< in: index descriptor */ |
| 1350 |
rtr_node_path_t *path, /*!< in/out: search path */ |
| 1351 |
ulint level, /*!< in: index page level */ |
| 1352 |
page_no_t child_no, /*!< in: child page no */ |
| 1353 |
const buf_block_t *block, /*!< in: block of the page */ |
| 1354 |
const rec_t *rec, /*!< in: positioned record */ |
| 1355 |
double mbr_inc) /*!< in: MBR needs to be enlarged */ |
| 1356 |
{ |
| 1357 |
node_seq_t new_seq; |
| 1358 |
btr_pcur_t *my_cursor; |
| 1359 |
page_no_t page_no = block->page.id.page_no(); |
| 1360 |
|
| 1361 |
my_cursor = static_cast<btr_pcur_t *>( |
| 1362 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*my_cursor))); |
| 1363 |
|
| 1364 |
my_cursor->init(); |
| 1365 |
|
| 1366 |
page_cur_position(rec, block, my_cursor->get_page_cur()); |
| 1367 |
|
| 1368 |
my_cursor->get_btr_cur()->index = index; |
| 1369 |
|
| 1370 |
new_seq = rtr_get_current_ssn_id(index); |
| 1371 |
rtr_non_leaf_stack_push(path, page_no, new_seq, level, child_no, my_cursor, |
| 1372 |
mbr_inc); |
| 1373 |
} |
| 1374 |
|
| 1375 |
/** Copy a buf_block_t structure, except "block->lock", |
| 1376 |
"block->mutex","block->debug_latch", "block->ahi.n_pointers and block->frame." |
| 1377 |
@param[in,out] matches copy to match->block |
| 1378 |
@param[in] block block to copy */ |
| 1379 |
static void rtr_copy_buf(matched_rec_t *matches, const buf_block_t *block) { |
| 1380 |
/* Clear any space references in the page if it was copied before. */ |
| 1381 |
buf_page_prepare_for_free(&matches->block.page); |
| 1382 |
|
| 1383 |
/* Copy all members of "block" to "matches->block" except "mutex" |
| 1384 |
and "lock". We skip "mutex" and "lock" because we can't copy mutex nor rwlock. |
| 1385 |
We don't copy pointer to frame as well, because we would have two rw_locks |
| 1386 |
protecting that frame (one from original block and one from dummy)*/ |
| 1387 |
new (&matches->block.page) buf_page_t(block->page); |
| 1388 |
matches->block.unzip_LRU = block->unzip_LRU; |
| 1389 |
|
| 1390 |
ut_d(matches->block.in_unzip_LRU_list = block->in_unzip_LRU_list); |
| 1391 |
ut_d(matches->block.in_withdraw_list = block->in_withdraw_list); |
| 1392 |
|
| 1393 |
/* Skip buf_block_t::mutex */ |
| 1394 |
/* Skip buf_block_t::lock as it was already initialized by rtr_create_rtr_info |
| 1395 |
*/ |
| 1396 |
ut_ad(rw_lock_validate(&matches->block.lock)); |
| 1397 |
matches->block.n_hash_helps.store(block->n_hash_helps.load()); |
| 1398 |
matches->block.ahi.recommended_prefix_info.store( |
| 1399 |
block->ahi.recommended_prefix_info.load()); |
| 1400 |
matches->block.ahi.prefix_info.store(block->ahi.prefix_info.load()); |
| 1401 |
matches->block.ahi.index.store(block->ahi.index.load()); |
| 1402 |
matches->block.made_dirty_with_no_latch = block->made_dirty_with_no_latch; |
| 1403 |
matches->block.modify_clock = block->modify_clock; |
| 1404 |
|
| 1405 |
#ifndef UNIV_HOTBACKUP |
| 1406 |
#ifdef UNIV_DEBUG |
| 1407 |
/* The buf_block_t copy does not contain a valid debug_latch object, mark it |
| 1408 |
as invalid so that we can detect any uses in our valgrind tests. Copy |
| 1409 |
semantics for locks are not well defined, so the previous code which simply |
| 1410 |
defined the default copy assignment operator was not correct. This was changed |
| 1411 |
because an std::atomic<bool> member was added and rw_lock_t became explicitly |
| 1412 |
non copyable. */ |
| 1413 |
UNIV_MEM_INVALID(&matches->block.debug_latch, |
| 1414 |
sizeof(matches->block.debug_latch)); |
| 1415 |
UNIV_MEM_INVALID(&matches->block.mutex, sizeof(matches->block.mutex)); |
| 1416 |
UNIV_MEM_INVALID(&matches->block.ahi.n_pointers, |
| 1417 |
sizeof(matches->block.ahi.n_pointers)); |
| 1418 |
#endif /* UNIV_DEBUG */ |
| 1419 |
#endif /* !UNIV_HOTBACKUP */ |
| 1420 |
} |
| 1421 |
|
| 1422 |
/** Generate a shadow copy of the page block header to save the |
| 1423 |
matched records */ |
| 1424 |
static void rtr_init_match( |
| 1425 |
matched_rec_t *matches, /*!< in/out: match to initialize */ |
| 1426 |
const buf_block_t *block, /*!< in: buffer block */ |
| 1427 |
const page_t *page) /*!< in: buffer page */ |
| 1428 |
{ |
| 1429 |
ut_ad(matches->matched_recs->empty()); |
| 1430 |
matches->locked = false; |
| 1431 |
rtr_copy_buf(matches, block); |
| 1432 |
matches->block.frame = matches->bufp; |
| 1433 |
matches->valid = false; |
| 1434 |
/* We have to copy PAGE_W*_SUPREMUM_END bytes so that we can |
| 1435 |
use infimum/supremum of this page as normal btr page for search. */ |
| 1436 |
memcpy(matches->block.frame, page, |
| 1437 |
page_is_comp(page) ? PAGE_NEW_SUPREMUM_END : PAGE_OLD_SUPREMUM_END); |
| 1438 |
matches->used = |
| 1439 |
page_is_comp(page) ? PAGE_NEW_SUPREMUM_END : PAGE_OLD_SUPREMUM_END; |
| 1440 |
#ifdef RTR_SEARCH_DIAGNOSTIC |
| 1441 |
ulint pageno = page_get_page_no(page); |
| 1442 |
fprintf(stderr, "INNODB_RTR: Searching leaf page %d\n", |
| 1443 |
static_cast<int>(pageno)); |
| 1444 |
#endif /* RTR_SEARCH_DIAGNOSTIC */ |
| 1445 |
} |
| 1446 |
|
| 1447 |
/** Get the bounding box content from an index record |
| 1448 |
@param[in] rec Data tuple |
| 1449 |
@param[in] offsets Offsets array |
| 1450 |
@param[out] mbr Mbr */ |
| 1451 |
void rtr_get_mbr_from_rec(const rec_t *rec, const ulint *offsets, |
| 1452 |
rtr_mbr_t *mbr) { |
| 1453 |
ulint rec_f_len; |
| 1454 |
const byte *data; |
| 1455 |
|
| 1456 |
data = rec_get_nth_field(nullptr, rec, offsets, 0, &rec_f_len); |
| 1457 |
|
| 1458 |
rtr_read_mbr(data, mbr); |
| 1459 |
} |
| 1460 |
|
| 1461 |
/** Get the bounding box content from a MBR data record */ |
| 1462 |
void rtr_get_mbr_from_tuple(const dtuple_t *dtuple, /*!< in: data tuple */ |
| 1463 |
rtr_mbr *mbr) /*!< out: mbr to fill */ |
| 1464 |
{ |
| 1465 |
const dfield_t *dtuple_field; |
| 1466 |
ulint dtuple_f_len; |
| 1467 |
byte *data; |
| 1468 |
|
| 1469 |
dtuple_field = dtuple_get_nth_field(dtuple, 0); |
| 1470 |
dtuple_f_len = dfield_get_len(dtuple_field); |
| 1471 |
ut_a(dtuple_f_len >= 4 * sizeof(double)); |
| 1472 |
|
| 1473 |
data = static_cast<byte *>(dfield_get_data(dtuple_field)); |
| 1474 |
|
| 1475 |
rtr_read_mbr(data, mbr); |
| 1476 |
} |
| 1477 |
|
| 1478 |
/** Searches the right position in rtree for a page cursor. |
| 1479 |
@param[in] block Buffer block |
| 1480 |
@param[in] index Index descriptor |
| 1481 |
@param[in] tuple Data tuple |
| 1482 |
@param[in] mode Page_cur_l, page_cur_le, page_cur_g, or page_cur_ge |
| 1483 |
@param[in,out] cursor Page cursor |
| 1484 |
@param[in,out] rtr_info Search stack */ |
| 1485 |
bool rtr_cur_search_with_match(const buf_block_t *block, dict_index_t *index, |
| 1486 |
const dtuple_t *tuple, page_cur_mode_t mode, |
| 1487 |
page_cur_t *cursor, rtr_info_t *rtr_info) { |
| 1488 |
bool found = false; |
| 1489 |
const page_t *page; |
| 1490 |
const rec_t *rec; |
| 1491 |
const rec_t *last_rec; |
| 1492 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1493 |
ulint *offsets = offsets_; |
| 1494 |
mem_heap_t *heap = nullptr; |
| 1495 |
int cmp = 1; |
| 1496 |
bool is_leaf; |
| 1497 |
double least_inc = DBL_MAX; |
| 1498 |
const rec_t *best_rec; |
| 1499 |
const rec_t *last_match_rec = nullptr; |
| 1500 |
ulint level; |
| 1501 |
bool match_init = false; |
| 1502 |
space_id_t space = block->page.id.space(); |
| 1503 |
page_cur_mode_t orig_mode = mode; |
| 1504 |
const rec_t *first_rec = nullptr; |
| 1505 |
|
| 1506 |
rec_offs_init(offsets_); |
| 1507 |
|
| 1508 |
ut_ad(RTREE_SEARCH_MODE(mode)); |
| 1509 |
|
| 1510 |
ut_ad(dict_index_is_spatial(index)); |
| 1511 |
|
| 1512 |
page = buf_block_get_frame(block); |
| 1513 |
|
| 1514 |
is_leaf = page_is_leaf(page); |
| 1515 |
level = btr_page_get_level(page); |
| 1516 |
|
| 1517 |
if (mode == PAGE_CUR_RTREE_LOCATE) { |
| 1518 |
ut_ad(level != 0); |
| 1519 |
mode = PAGE_CUR_WITHIN; |
| 1520 |
} |
| 1521 |
|
| 1522 |
rec = page_dir_slot_get_rec(page_dir_get_nth_slot(page, 0)); |
| 1523 |
|
| 1524 |
last_rec = rec; |
| 1525 |
best_rec = rec; |
| 1526 |
|
| 1527 |
if (page_rec_is_infimum(rec)) { |
| 1528 |
rec = page_rec_get_next_const(rec); |
| 1529 |
} |
| 1530 |
|
| 1531 |
/* Check insert tuple size is larger than first rec, and try to |
| 1532 |
avoid it if possible */ |
| 1533 |
if (mode == PAGE_CUR_RTREE_INSERT && !page_rec_is_supremum(rec)) { |
| 1534 |
ulint new_rec_size = rec_get_converted_size(index, tuple); |
| 1535 |
|
| 1536 |
offsets = |
| 1537 |
rec_get_offsets(rec, index, offsets, dtuple_get_n_fields_cmp(tuple), |
| 1538 |
UT_LOCATION_HERE, &heap); |
| 1539 |
|
| 1540 |
if (rec_offs_size(offsets) < new_rec_size) { |
| 1541 |
first_rec = rec; |
| 1542 |
} |
| 1543 |
|
| 1544 |
/* If this is the left-most page of this index level |
| 1545 |
and the table is a compressed table, try to avoid |
| 1546 |
first page as much as possible, as there will be problem |
| 1547 |
when update MIN_REC rec in compress table */ |
| 1548 |
if (buf_block_get_page_zip(block) && |
| 1549 |
mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL && |
| 1550 |
page_get_n_recs(page) >= 2) { |
| 1551 |
rec = page_rec_get_next_const(rec); |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
while (!page_rec_is_supremum(rec)) { |
| 1556 |
offsets = |
| 1557 |
rec_get_offsets(rec, index, offsets, dtuple_get_n_fields_cmp(tuple), |
| 1558 |
UT_LOCATION_HERE, &heap); |
| 1559 |
if (!is_leaf) { |
| 1560 |
switch (mode) { |
| 1561 |
case PAGE_CUR_CONTAIN: |
| 1562 |
case PAGE_CUR_INTERSECT: |
| 1563 |
case PAGE_CUR_MBR_EQUAL: |
| 1564 |
/* At non-leaf level, we will need to check |
| 1565 |
both CONTAIN and INTERSECT for either of |
| 1566 |
the search mode */ |
| 1567 |
cmp = cmp_dtuple_rec_with_gis(tuple, rec, offsets, PAGE_CUR_CONTAIN, |
| 1568 |
index->rtr_srs.get()); |
| 1569 |
|
| 1570 |
if (cmp != 0) { |
| 1571 |
cmp = cmp_dtuple_rec_with_gis( |
| 1572 |
tuple, rec, offsets, PAGE_CUR_INTERSECT, index->rtr_srs.get()); |
| 1573 |
} |
| 1574 |
break; |
| 1575 |
case PAGE_CUR_DISJOINT: |
| 1576 |
cmp = cmp_dtuple_rec_with_gis(tuple, rec, offsets, mode, |
| 1577 |
index->rtr_srs.get()); |
| 1578 |
|
| 1579 |
if (cmp != 0) { |
| 1580 |
cmp = cmp_dtuple_rec_with_gis( |
| 1581 |
tuple, rec, offsets, PAGE_CUR_INTERSECT, index->rtr_srs.get()); |
| 1582 |
} |
| 1583 |
break; |
| 1584 |
case PAGE_CUR_RTREE_INSERT: |
| 1585 |
double increase; |
| 1586 |
|
| 1587 |
cmp = cmp_dtuple_rec_with_gis(tuple, rec, offsets, PAGE_CUR_WITHIN, |
| 1588 |
index->rtr_srs.get()); |
| 1589 |
|
| 1590 |
if (cmp != 0) { |
| 1591 |
double area{0.0}; |
| 1592 |
increase = rtr_rec_cal_increase(tuple, rec, offsets, &area, |
| 1593 |
index->rtr_srs.get()); |
| 1594 |
/* Once it goes beyond DBL_MAX or |
| 1595 |
they are both DBL_MAX, it would not |
| 1596 |
make sense to record such value, |
| 1597 |
just make it DBL_MAX / 2 */ |
| 1598 |
if (increase >= DBL_MAX || (increase == 0 && std::isfinite(area))) { |
| 1599 |
increase = DBL_MAX / 2; |
| 1600 |
} |
| 1601 |
|
| 1602 |
if (increase < least_inc) { |
| 1603 |
least_inc = increase; |
| 1604 |
best_rec = rec; |
| 1605 |
} else if (best_rec && best_rec == first_rec) { |
| 1606 |
/* if first_rec is set, |
| 1607 |
we will try to avoid it */ |
| 1608 |
least_inc = increase; |
| 1609 |
best_rec = rec; |
| 1610 |
} |
| 1611 |
} |
| 1612 |
break; |
| 1613 |
case PAGE_CUR_RTREE_GET_FATHER: |
| 1614 |
cmp = cmp_dtuple_rec_with_gis_internal(tuple, rec, offsets, |
| 1615 |
index->rtr_srs.get()); |
| 1616 |
break; |
| 1617 |
default: |
| 1618 |
/* WITHIN etc. */ |
| 1619 |
cmp = cmp_dtuple_rec_with_gis(tuple, rec, offsets, mode, |
| 1620 |
index->rtr_srs.get()); |
| 1621 |
} |
| 1622 |
} else { |
| 1623 |
/* At leaf level, INSERT should translate to LE */ |
| 1624 |
ut_ad(mode != PAGE_CUR_RTREE_INSERT); |
| 1625 |
|
| 1626 |
cmp = cmp_dtuple_rec_with_gis(tuple, rec, offsets, mode, |
| 1627 |
index->rtr_srs.get()); |
| 1628 |
} |
| 1629 |
|
| 1630 |
if (cmp == 0) { |
| 1631 |
found = true; |
| 1632 |
|
| 1633 |
/* If located, the matching node/rec will be pushed |
| 1634 |
to rtr_info->path for non-leaf nodes, or |
| 1635 |
rtr_info->matches for leaf nodes */ |
| 1636 |
if (rtr_info && mode != PAGE_CUR_RTREE_INSERT) { |
| 1637 |
if (!is_leaf) { |
| 1638 |
page_no_t page_no; |
| 1639 |
node_seq_t new_seq; |
| 1640 |
bool is_loc; |
| 1641 |
|
| 1642 |
is_loc = (orig_mode == PAGE_CUR_RTREE_LOCATE || |
| 1643 |
orig_mode == PAGE_CUR_RTREE_GET_FATHER); |
| 1644 |
|
| 1645 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1646 |
UT_LOCATION_HERE, &heap); |
| 1647 |
|
| 1648 |
page_no = btr_node_ptr_get_child_page_no(rec, offsets); |
| 1649 |
|
| 1650 |
ut_ad(level >= 1); |
| 1651 |
|
| 1652 |
/* Get current SSN, before we insert |
| 1653 |
it into the path stack */ |
| 1654 |
new_seq = rtr_get_current_ssn_id(index); |
| 1655 |
|
| 1656 |
rtr_non_leaf_stack_push(rtr_info->path, page_no, new_seq, level - 1, |
| 1657 |
0, nullptr, 0); |
| 1658 |
|
| 1659 |
if (is_loc) { |
| 1660 |
rtr_non_leaf_insert_stack_push(index, rtr_info->parent_path, level, |
| 1661 |
page_no, block, rec, 0); |
| 1662 |
} |
| 1663 |
|
| 1664 |
if (!srv_read_only_mode && (rtr_info->need_page_lock || !is_loc)) { |
| 1665 |
/* Lock the page, preventing it |
| 1666 |
from being shrunk */ |
| 1667 |
lock_place_prdt_page_lock({space, page_no}, index, rtr_info->thr); |
| 1668 |
} |
| 1669 |
} else { |
| 1670 |
ut_ad(orig_mode != PAGE_CUR_RTREE_LOCATE); |
| 1671 |
|
| 1672 |
if (!match_init) { |
| 1673 |
rtr_init_match(rtr_info->matches, block, page); |
| 1674 |
match_init = true; |
| 1675 |
} |
| 1676 |
|
| 1677 |
/* Collect matched records on page */ |
| 1678 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1679 |
UT_LOCATION_HERE, &heap); |
| 1680 |
rtr_leaf_push_match_rec(rec, rtr_info, offsets, page_is_comp(page)); |
| 1681 |
} |
| 1682 |
|
| 1683 |
last_match_rec = rec; |
| 1684 |
} else { |
| 1685 |
/* This is the insertion case, it will break |
| 1686 |
once it finds the first MBR that can accommodate |
| 1687 |
the inserting rec */ |
| 1688 |
break; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
last_rec = rec; |
| 1693 |
|
| 1694 |
rec = page_rec_get_next_const(rec); |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* All records on page are searched */ |
| 1698 |
if (page_rec_is_supremum(rec)) { |
| 1699 |
if (!is_leaf) { |
| 1700 |
if (!found) { |
| 1701 |
/* No match case, if it is for insertion, |
| 1702 |
then we select the record that result in |
| 1703 |
least increased area */ |
| 1704 |
if (mode == PAGE_CUR_RTREE_INSERT) { |
| 1705 |
page_no_t child_no; |
| 1706 |
ut_ad(least_inc < DBL_MAX); |
| 1707 |
offsets = rec_get_offsets(best_rec, index, offsets, ULINT_UNDEFINED, |
| 1708 |
UT_LOCATION_HERE, &heap); |
| 1709 |
child_no = btr_node_ptr_get_child_page_no(best_rec, offsets); |
| 1710 |
|
| 1711 |
rtr_non_leaf_insert_stack_push(index, rtr_info->parent_path, level, |
| 1712 |
child_no, block, best_rec, least_inc); |
| 1713 |
|
| 1714 |
page_cur_position(best_rec, block, cursor); |
| 1715 |
rtr_info->mbr_adj = true; |
| 1716 |
} else { |
| 1717 |
/* Position at the last rec of the |
| 1718 |
page, if it is not the leaf page */ |
| 1719 |
page_cur_position(last_rec, block, cursor); |
| 1720 |
} |
| 1721 |
} else { |
| 1722 |
/* There are matching records, position |
| 1723 |
in the last matching records */ |
| 1724 |
if (rtr_info) { |
| 1725 |
rec = last_match_rec; |
| 1726 |
page_cur_position(rec, block, cursor); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
} else if (rtr_info) { |
| 1730 |
/* Leaf level, no match, position at the |
| 1731 |
last (supremum) rec */ |
| 1732 |
if (!last_match_rec) { |
| 1733 |
page_cur_position(rec, block, cursor); |
| 1734 |
goto func_exit; |
| 1735 |
} |
| 1736 |
|
| 1737 |
/* There are matched records */ |
| 1738 |
matched_rec_t *match_rec = rtr_info->matches; |
| 1739 |
|
| 1740 |
rtr_rec_t test_rec; |
| 1741 |
|
| 1742 |
test_rec = match_rec->matched_recs->back(); |
| 1743 |
#ifdef UNIV_DEBUG |
| 1744 |
ulint offsets_2[REC_OFFS_NORMAL_SIZE]; |
| 1745 |
ulint *offsets2 = offsets_2; |
| 1746 |
rec_offs_init(offsets_2); |
| 1747 |
|
| 1748 |
ut_ad(found); |
| 1749 |
|
| 1750 |
/* Verify the record to be positioned is the same |
| 1751 |
as the last record in matched_rec vector */ |
| 1752 |
offsets2 = rec_get_offsets(test_rec.r_rec, index, offsets2, |
| 1753 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 1754 |
|
| 1755 |
offsets = rec_get_offsets(last_match_rec, index, offsets, ULINT_UNDEFINED, |
| 1756 |
UT_LOCATION_HERE, &heap); |
| 1757 |
|
| 1758 |
ut_ad(cmp_rec_rec(test_rec.r_rec, last_match_rec, offsets2, offsets, |
| 1759 |
index, false) == 0); |
| 1760 |
#endif /* UNIV_DEBUG */ |
| 1761 |
/* Pop the last match record and position on it */ |
| 1762 |
match_rec->matched_recs->pop_back(); |
| 1763 |
page_cur_position(test_rec.r_rec, &match_rec->block, cursor); |
| 1764 |
} |
| 1765 |
} else { |
| 1766 |
if (mode == PAGE_CUR_RTREE_INSERT) { |
| 1767 |
page_no_t child_no; |
| 1768 |
ut_ad(!last_match_rec && rec); |
| 1769 |
|
| 1770 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1771 |
UT_LOCATION_HERE, &heap); |
| 1772 |
|
| 1773 |
child_no = btr_node_ptr_get_child_page_no(rec, offsets); |
| 1774 |
|
| 1775 |
rtr_non_leaf_insert_stack_push(index, rtr_info->parent_path, level, |
| 1776 |
child_no, block, rec, 0); |
| 1777 |
|
| 1778 |
} else if (rtr_info && found && !is_leaf) { |
| 1779 |
rec = last_match_rec; |
| 1780 |
} |
| 1781 |
|
| 1782 |
page_cur_position(rec, block, cursor); |
| 1783 |
} |
| 1784 |
|
| 1785 |
#ifdef UNIV_DEBUG |
| 1786 |
/* Verify that we are positioned at the same child page as pushed in |
| 1787 |
the path stack */ |
| 1788 |
if (!is_leaf && (!page_rec_is_supremum(rec) || found) && |
| 1789 |
mode != PAGE_CUR_RTREE_INSERT) { |
| 1790 |
page_no_t page_no; |
| 1791 |
|
| 1792 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1793 |
UT_LOCATION_HERE, &heap); |
| 1794 |
page_no = btr_node_ptr_get_child_page_no(rec, offsets); |
| 1795 |
|
| 1796 |
if (rtr_info && found) { |
| 1797 |
rtr_node_path_t *path = rtr_info->path; |
| 1798 |
node_visit_t last_visit = path->back(); |
| 1799 |
|
| 1800 |
ut_ad(last_visit.page_no == page_no); |
| 1801 |
} |
| 1802 |
} |
| 1803 |
#endif /* UNIV_DEBUG */ |
| 1804 |
|
| 1805 |
func_exit: |
| 1806 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1807 |
mem_heap_free(heap); |
| 1808 |
} |
| 1809 |
|
| 1810 |
return (found); |
| 1811 |
} |