Debian Codegraph @

storage/innobase/btr/btr0pcur.cc

raw
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 btr/btr0pcur.cc
29 The index tree persistent cursor
30
31 Created 2/23/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "btr0pcur.h"
35
36 #include <stddef.h>
37
38 #include "rem0cmp.h"
39 #include "trx0trx.h"
40 #include "ut0byte.h"
41
42 void btr_pcur_t::store_position(mtr_t *mtr) {
43 ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED);
44 ut_ad(m_latch_mode != BTR_NO_LATCHES);
45
46 auto block = get_block();
47 auto index = get_btr_cur()->index;
48
49 auto page_cursor = get_page_cur();
50
51 auto rec = page_cur_get_rec(page_cursor);
52 auto page = page_align(rec);
53 auto offs = page_offset(rec);
54
55 #ifdef UNIV_DEBUG
56 if (dict_index_is_spatial(index)) {
57 /* For spatial index, when we do positioning on parent
58 buffer if necessary, it might not hold latches, but the
59 tree must be locked to prevent change on the page */
60 ut_ad((mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
61 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
62 mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_S_FIX) ||
63 mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX)) &&
64 (block->page.buf_fix_count > 0));
65 } else {
66 ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_S_FIX) ||
67 mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX) ||
68 index->table->is_intrinsic());
69 }
70 #endif /* UNIV_DEBUG */
71
72 if (page_is_empty(page)) {
73 /* It must be an empty index tree; NOTE that in this case
74 we do not store the modify_clock, but always do a search
75 if we restore the cursor position */
76
77 ut_a(btr_page_get_next(page, mtr) == FIL_NULL);
78 ut_a(btr_page_get_prev(page, mtr) == FIL_NULL);
79 ut_ad(page_is_leaf(page));
80 ut_ad(page_get_page_no(page) == index->page);
81
82 m_old_stored = true;
83
84 if (page_rec_is_supremum_low(offs)) {
85 m_rel_pos = BTR_PCUR_AFTER_LAST_IN_TREE;
86 } else {
87 m_rel_pos = BTR_PCUR_BEFORE_FIRST_IN_TREE;
88 }
89
90 return;
91 }
92
93 if (page_rec_is_supremum_low(offs)) {
94 rec = page_rec_get_prev(rec);
95
96 m_rel_pos = BTR_PCUR_AFTER;
97
98 } else if (page_rec_is_infimum_low(offs)) {
99 rec = page_rec_get_next(rec);
100
101 m_rel_pos = BTR_PCUR_BEFORE;
102 } else {
103 m_rel_pos = BTR_PCUR_ON;
104 }
105
106 m_old_stored = true;
107
108 m_old_rec = dict_index_copy_rec_order_prefix(index, rec, &m_old_n_fields,
109 &m_old_rec_buf, &m_buf_size);
110 m_block_when_stored.store(block);
111
112 m_modify_clock = block->get_modify_clock(
113 IF_DEBUG(fsp_is_system_temporary(block->page.id.space())));
114 }
115
116 void btr_pcur_t::copy_stored_position(btr_pcur_t *dst, const btr_pcur_t *src) {
117 {
118 const auto dst_old_rec_buf = dst->m_old_rec_buf;
119 const auto dst_buf_size = dst->m_buf_size;
120
121 memcpy(dst, src, sizeof(*dst));
122
123 dst->m_old_rec_buf = dst_old_rec_buf;
124 dst->m_buf_size = dst_buf_size;
125 }
126
127 if (src->m_old_rec != nullptr) {
128 /* We have an old buffer, but it is too small. */
129 if (dst->m_old_rec_buf != nullptr && dst->m_buf_size < src->m_buf_size) {
130 ut::free(dst->m_old_rec_buf);
131 dst->m_old_rec_buf = nullptr;
132 }
133 /* We don't have a buffer, but we should have one. */
134 if (dst->m_old_rec_buf == nullptr) {
135 dst->m_old_rec_buf = static_cast<byte *>(
136 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, src->m_buf_size));
137 dst->m_buf_size = src->m_buf_size;
138 }
139
140 memcpy(dst->m_old_rec_buf, src->m_old_rec_buf, src->m_buf_size);
141
142 dst->m_old_rec = dst->m_old_rec_buf + (src->m_old_rec - src->m_old_rec_buf);
143 }
144 dst->m_old_n_fields = src->m_old_n_fields;
145 }
146
147 bool btr_pcur_t::restore_position(ulint latch_mode, mtr_t *mtr,
148 ut::Location location) {
149 dtuple_t *tuple;
150 page_cur_mode_t mode;
151
152 ut_ad(mtr->is_active());
153 ut_ad(m_old_stored);
154 ut_ad(is_positioned());
155
156 auto index = get_btr_cur()->index;
157
158 if (m_rel_pos == BTR_PCUR_AFTER_LAST_IN_TREE ||
159 m_rel_pos == BTR_PCUR_BEFORE_FIRST_IN_TREE) {
160 /* In these cases we do not try an optimistic restoration,
161 but always do a search */
162
163 btr_cur_open_at_index_side(m_rel_pos == BTR_PCUR_BEFORE_FIRST_IN_TREE,
164 index, latch_mode, get_btr_cur(), m_read_level,
165 UT_LOCATION_HERE, mtr);
166
167 m_latch_mode = BTR_LATCH_MODE_WITHOUT_INTENTION(latch_mode);
168
169 m_pos_state = BTR_PCUR_IS_POSITIONED;
170
171 m_block_when_stored.clear();
172
173 return (false);
174 }
175
176 ut_a(m_old_rec != nullptr);
177 ut_a(m_old_n_fields > 0);
178
179 /* Optimistic latching involves S/X latch not required for
180 intrinsic table instead we would prefer to search fresh. */
181 if ((latch_mode == BTR_SEARCH_LEAF || latch_mode == BTR_MODIFY_LEAF ||
182 latch_mode == BTR_SEARCH_PREV || latch_mode == BTR_MODIFY_PREV) &&
183 !m_btr_cur.index->table->is_intrinsic()) {
184 /* Try optimistic restoration. */
185 if (m_block_when_stored.run_with_hint([&](buf_block_t *hint) {
186 return hint != nullptr &&
187 btr_cur_optimistic_latch_leaves(
188 hint, m_modify_clock, &latch_mode, &m_btr_cur,
189 location.filename, location.line, mtr);
190 })) {
191 m_pos_state = BTR_PCUR_IS_POSITIONED;
192
193 m_latch_mode = latch_mode;
194
195 buf_block_dbg_add_level(get_block(), dict_index_is_ibuf(index)
196 ? SYNC_IBUF_TREE_NODE
197 : SYNC_TREE_NODE);
198
199 if (m_rel_pos == BTR_PCUR_ON) {
200 #ifdef UNIV_DEBUG
201 const rec_t *rec;
202 const ulint *offsets1;
203 const ulint *offsets2;
204
205 rec = get_rec();
206
207 auto heap = mem_heap_create(256, UT_LOCATION_HERE);
208
209 offsets1 = rec_get_offsets(m_old_rec, index, nullptr, m_old_n_fields,
210 UT_LOCATION_HERE, &heap);
211
212 offsets2 = rec_get_offsets(rec, index, nullptr, m_old_n_fields,
213 UT_LOCATION_HERE, &heap);
214
215 ut_ad(!cmp_rec_rec(m_old_rec, rec, offsets1, offsets2, index,
216 page_is_spatial_non_leaf(rec, index), nullptr,
217 false));
218 mem_heap_free(heap);
219 #endif /* UNIV_DEBUG */
220 return (true);
221 }
222
223 /* This is the same record as stored,
224 may need to be adjusted for BTR_PCUR_BEFORE/AFTER,
225 depending on search mode and direction. */
226 if (is_on_user_rec()) {
227 m_pos_state = BTR_PCUR_IS_POSITIONED_OPTIMISTIC;
228 }
229 return (false);
230 }
231 }
232
233 /* If optimistic restoration did not succeed, open the cursor anew */
234
235 auto heap = mem_heap_create(256, UT_LOCATION_HERE);
236
237 tuple = dict_index_build_data_tuple(index, m_old_rec, m_old_n_fields, heap);
238
239 /* Save the old search mode of the cursor */
240 auto old_mode = m_search_mode;
241
242 switch (m_rel_pos) {
243 case BTR_PCUR_ON:
244 mode = PAGE_CUR_LE;
245 break;
246 case BTR_PCUR_AFTER:
247 mode = PAGE_CUR_G;
248 break;
249 case BTR_PCUR_BEFORE:
250 mode = PAGE_CUR_L;
251 break;
252 default:
253 ut_error;
254 }
255
256 open_no_init(index, tuple, mode, latch_mode, 0, mtr, location);
257
258 /* Restore the old search mode */
259 m_search_mode = old_mode;
260
261 ut_ad(m_rel_pos == BTR_PCUR_ON || m_rel_pos == BTR_PCUR_BEFORE ||
262 m_rel_pos == BTR_PCUR_AFTER);
263
264 if (m_rel_pos == BTR_PCUR_ON && is_on_user_rec() &&
265 !cmp_dtuple_rec(
266 tuple, get_rec(), index,
267 rec_get_offsets(get_rec(), index, nullptr, ULINT_UNDEFINED,
268 UT_LOCATION_HERE, &heap))) {
269 /* We have to store the NEW value for the modify clock,
270 since the cursor can now be on a different page!
271 But we can retain the value of old_rec */
272 auto block = get_block();
273 m_block_when_stored.store(block);
274
275 m_modify_clock = block->get_modify_clock(
276 IF_DEBUG(fsp_is_system_temporary(block->page.id.space())));
277
278 m_old_stored = true;
279
280 mem_heap_free(heap);
281
282 return (true);
283 }
284
285 mem_heap_free(heap);
286
287 /* We have to store new position information, modify_clock etc.,
288 to the cursor because it can now be on a different page, the record
289 under it may have been removed, etc. */
290
291 store_position(mtr);
292
293 return (false);
294 }
295
296 void btr_pcur_t::move_to_next_page(mtr_t *mtr) {
297 dict_table_t *table = get_btr_cur()->index->table;
298
299 ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED);
300 ut_ad(m_latch_mode != BTR_NO_LATCHES);
301 ut_ad(is_after_last_on_page());
302
303 m_old_stored = false;
304
305 auto page = get_page();
306 auto next_page_no = btr_page_get_next(page, mtr);
307
308 ut_ad(next_page_no != FIL_NULL);
309
310 auto mode = m_latch_mode;
311
312 switch (mode) {
313 case BTR_SEARCH_TREE:
314 mode = BTR_SEARCH_LEAF;
315 break;
316 case BTR_MODIFY_TREE:
317 mode = BTR_MODIFY_LEAF;
318 }
319
320 /* For intrinsic tables we avoid taking any latches as table is
321 accessed by only one thread at any given time. */
322 if (table->is_intrinsic()) {
323 mode = BTR_NO_LATCHES;
324 }
325
326 auto block = get_block();
327
328 auto next_block = btr_block_get(
329 page_id_t(block->page.id.space(), next_page_no), block->page.size, mode,
330 UT_LOCATION_HERE, get_btr_cur()->index, mtr);
331
332 auto next_page = buf_block_get_frame(next_block);
333
334 #ifdef UNIV_BTR_DEBUG
335 if (!import_ctx) {
336 ut_a(page_is_comp(next_page) == page_is_comp(page));
337 ut_a(btr_page_get_prev(next_page, mtr) == get_block()->page.id.page_no());
338 } else {
339 if (page_is_comp(next_page) != page_is_comp(page) ||
340 btr_page_get_prev(next_page, mtr) != get_block()->page.id.page_no()) {
341 /* next page does not contain valid previous page number,
342 next page is corrupted, can't move cursor to the next page*/
343 import_ctx->is_error = true;
344 }
345 DBUG_EXECUTE_IF("ib_import_page_corrupt", import_ctx->is_error = true;);
346 }
347 #endif /* UNIV_BTR_DEBUG */
348
349 btr_leaf_page_release(get_block(), mode, mtr);
350
351 page_cur_set_before_first(next_block, get_page_cur());
352
353 ut_d(page_check_dir(next_page));
354 }
355
356 void btr_pcur_t::move_backward_from_page(mtr_t *mtr) {
357 ut_ad(m_latch_mode != BTR_NO_LATCHES);
358 ut_ad(is_before_first_on_page());
359 ut_ad(!is_before_first_in_tree(mtr));
360
361 ulint latch_mode2;
362 auto old_latch_mode = m_latch_mode;
363
364 if (m_latch_mode == BTR_SEARCH_LEAF) {
365 latch_mode2 = BTR_SEARCH_PREV;
366
367 } else if (m_latch_mode == BTR_MODIFY_LEAF) {
368 latch_mode2 = BTR_MODIFY_PREV;
369 } else {
370 latch_mode2 = 0; /* To eliminate compiler warning */
371 ut_error;
372 }
373
374 store_position(mtr);
375
376 mtr_commit(mtr);
377
378 mtr_start(mtr);
379
380 restore_position(latch_mode2, mtr, UT_LOCATION_HERE);
381
382 auto page = get_page();
383 auto prev_page_no = btr_page_get_prev(page, mtr);
384
385 /* For intrinsic table we don't do optimistic restore and so there is
386 no left block that is pinned that needs to be released. */
387 if (!get_btr_cur()->index->table->is_intrinsic()) {
388 buf_block_t *prev_block;
389
390 if (prev_page_no == FIL_NULL) {
391 ;
392 } else if (is_before_first_on_page()) {
393 prev_block = get_btr_cur()->left_block;
394
395 btr_leaf_page_release(get_block(), old_latch_mode, mtr);
396
397 page_cur_set_after_last(prev_block, get_page_cur());
398 } else {
399 /* The repositioned cursor did not end on an infimum
400 record on a page. Cursor repositioning acquired a latch
401 also on the previous page, but we do not need the latch:
402 release it. */
403
404 prev_block = get_btr_cur()->left_block;
405
406 btr_leaf_page_release(prev_block, old_latch_mode, mtr);
407 }
408 }
409
410 m_latch_mode = old_latch_mode;
411 m_old_stored = false;
412 }
413
414 bool btr_pcur_t::move_to_prev(mtr_t *mtr) {
415 ut_ad(m_pos_state == BTR_PCUR_IS_POSITIONED);
416 ut_ad(m_latch_mode != BTR_NO_LATCHES);
417
418 m_old_stored = false;
419
420 if (is_before_first_on_page()) {
421 if (is_before_first_in_tree(mtr)) {
422 return (false);
423 }
424
425 move_backward_from_page(mtr);
426
427 return (true);
428 }
429
430 move_to_prev_on_page();
431
432 return (true);
433 }
434
435 void btr_pcur_t::open_on_user_rec(dict_index_t *index, const dtuple_t *tuple,
436 page_cur_mode_t mode, ulint latch_mode,
437 mtr_t *mtr, ut::Location location) {
438 open(index, 0, tuple, mode, latch_mode, mtr, location);
439
440 if (mode == PAGE_CUR_GE || mode == PAGE_CUR_G) {
441 if (is_after_last_on_page()) {
442 move_to_next_user_rec(mtr);
443 }
444 } else {
445 ut_ad(mode == PAGE_CUR_LE || mode == PAGE_CUR_L);
446
447 /* Not implemented yet */
448
449 ut_error;
450 }
451 }
452
453 void btr_pcur_t::open_on_user_rec(const page_cur_t &page_cursor,
454 page_cur_mode_t mode, ulint latch_mode) {
455 auto btr_cur = get_btr_cur();
456
457 btr_cur->index = const_cast<dict_index_t *>(page_cursor.index);
458
459 auto page_cur = get_page_cur();
460
461 memcpy(page_cur, &page_cursor, sizeof(*page_cur));
462
463 m_search_mode = mode;
464
465 m_pos_state = BTR_PCUR_IS_POSITIONED;
466
467 m_latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode);
468
469 m_trx_if_known = nullptr;
470 }