| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 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/page0cur.ic |
| 29 |
The page cursor |
| 30 |
|
| 31 |
Created 10/4/1994 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#include "buf0types.h" |
| 35 |
#include "page0page.h" |
| 36 |
|
| 37 |
#ifdef UNIV_DEBUG |
| 38 |
#ifndef UNIV_HOTBACKUP |
| 39 |
#include "rem0cmp.h" |
| 40 |
#endif /* !UNIV_HOTBACKUP */ |
| 41 |
|
| 42 |
/** Gets pointer to the page frame where the cursor is positioned. |
| 43 |
@return page */ |
| 44 |
static inline page_t *page_cur_get_page(page_cur_t *cur) /*!< in: page cursor */ |
| 45 |
{ |
| 46 |
ut_ad(cur); |
| 47 |
ut_ad(page_align(cur->rec) == cur->block->frame); |
| 48 |
|
| 49 |
return (page_align(cur->rec)); |
| 50 |
} |
| 51 |
|
| 52 |
/** Gets pointer to the buffer block where the cursor is positioned. |
| 53 |
@return page */ |
| 54 |
static inline buf_block_t *page_cur_get_block( |
| 55 |
page_cur_t *cur) /*!< in: page cursor */ |
| 56 |
{ |
| 57 |
ut_ad(cur); |
| 58 |
ut_ad(page_align(cur->rec) == cur->block->frame); |
| 59 |
return (cur->block); |
| 60 |
} |
| 61 |
|
| 62 |
/** Gets pointer to the page frame where the cursor is positioned. |
| 63 |
@return page */ |
| 64 |
static inline page_zip_des_t *page_cur_get_page_zip( |
| 65 |
page_cur_t *cur) /*!< in: page cursor */ |
| 66 |
{ |
| 67 |
return (buf_block_get_page_zip(page_cur_get_block(cur))); |
| 68 |
} |
| 69 |
|
| 70 |
/** Gets the record where the cursor is positioned. |
| 71 |
@return record */ |
| 72 |
static inline rec_t *page_cur_get_rec(page_cur_t *cur) /*!< in: page cursor */ |
| 73 |
{ |
| 74 |
ut_ad(cur); |
| 75 |
ut_ad(page_align(cur->rec) == cur->block->frame); |
| 76 |
|
| 77 |
return (cur->rec); |
| 78 |
} |
| 79 |
#endif /* UNIV_DEBUG */ |
| 80 |
|
| 81 |
/** Sets the cursor object to point before the first user record |
| 82 |
on the page. */ |
| 83 |
static inline void page_cur_set_before_first( |
| 84 |
const buf_block_t *block, /*!< in: index page */ |
| 85 |
page_cur_t *cur) /*!< in: cursor */ |
| 86 |
{ |
| 87 |
cur->block = (buf_block_t *)block; |
| 88 |
cur->rec = page_get_infimum_rec(buf_block_get_frame(cur->block)); |
| 89 |
} |
| 90 |
|
| 91 |
/** Sets the cursor object to point after the last user record on |
| 92 |
the page. */ |
| 93 |
static inline void page_cur_set_after_last( |
| 94 |
const buf_block_t *block, /*!< in: index page */ |
| 95 |
page_cur_t *cur) /*!< in: cursor */ |
| 96 |
{ |
| 97 |
cur->block = (buf_block_t *)block; |
| 98 |
cur->rec = page_get_supremum_rec(buf_block_get_frame(cur->block)); |
| 99 |
} |
| 100 |
|
| 101 |
/** Returns true if the cursor is before first user record on page. |
| 102 |
@return true if at start */ |
| 103 |
static inline bool page_cur_is_before_first( |
| 104 |
const page_cur_t *cur) /*!< in: cursor */ |
| 105 |
{ |
| 106 |
ut_ad(cur); |
| 107 |
ut_ad(page_align(cur->rec) == cur->block->frame); |
| 108 |
return (page_rec_is_infimum(cur->rec)); |
| 109 |
} |
| 110 |
|
| 111 |
/** Returns true if the cursor is after last user record. |
| 112 |
@return true if at end */ |
| 113 |
static inline bool page_cur_is_after_last( |
| 114 |
const page_cur_t *cur) /*!< in: cursor */ |
| 115 |
{ |
| 116 |
ut_ad(cur); |
| 117 |
ut_ad(page_align(cur->rec) == cur->block->frame); |
| 118 |
return (page_rec_is_supremum(cur->rec)); |
| 119 |
} |
| 120 |
|
| 121 |
/** Positions the cursor on the given record. */ |
| 122 |
static inline void page_cur_position( |
| 123 |
const rec_t *rec, /*!< in: record on a page */ |
| 124 |
const buf_block_t *block, /*!< in: buffer block |
| 125 |
containing the record */ |
| 126 |
page_cur_t *cur) /*!< out: page cursor */ |
| 127 |
{ |
| 128 |
ut_ad(rec && block && cur); |
| 129 |
ut_ad(page_align(rec) == block->frame); |
| 130 |
|
| 131 |
cur->rec = (rec_t *)rec; |
| 132 |
cur->block = (buf_block_t *)block; |
| 133 |
} |
| 134 |
|
| 135 |
/** Moves the cursor to the next record on page. */ |
| 136 |
static inline void page_cur_move_to_next( |
| 137 |
page_cur_t *cur) /*!< in/out: cursor; must not be after last */ |
| 138 |
{ |
| 139 |
ut_ad(!page_cur_is_after_last(cur)); |
| 140 |
|
| 141 |
cur->rec = page_rec_get_next(cur->rec); |
| 142 |
} |
| 143 |
|
| 144 |
/** Moves the cursor to the previous record on page. */ |
| 145 |
static inline void page_cur_move_to_prev( |
| 146 |
page_cur_t *cur) /*!< in/out: page cursor, not before first */ |
| 147 |
{ |
| 148 |
ut_ad(!page_cur_is_before_first(cur)); |
| 149 |
|
| 150 |
cur->rec = page_rec_get_prev(cur->rec); |
| 151 |
} |
| 152 |
|
| 153 |
#ifndef UNIV_HOTBACKUP |
| 154 |
/** Search the right position for a page cursor. |
| 155 |
@param[in] block buffer block |
| 156 |
@param[in] index index tree |
| 157 |
@param[in] tuple data tuple |
| 158 |
@param[in] mode PAGE_CUR_L, PAGE_CUR_LE, PAGE_CUR_G, or PAGE_CUR_GE |
| 159 |
@param[out] cursor page cursor |
| 160 |
@return number of matched fields on the left */ |
| 161 |
static inline ulint page_cur_search(const buf_block_t *block, |
| 162 |
const dict_index_t *index, |
| 163 |
const dtuple_t *tuple, page_cur_mode_t mode, |
| 164 |
page_cur_t *cursor) { |
| 165 |
ulint low_match = 0; |
| 166 |
ulint up_match = 0; |
| 167 |
|
| 168 |
ut_ad(dtuple_check_typed(tuple)); |
| 169 |
|
| 170 |
page_cur_search_with_match(block, index, tuple, mode, &up_match, &low_match, |
| 171 |
cursor, nullptr); |
| 172 |
return (low_match); |
| 173 |
} |
| 174 |
|
| 175 |
/** Search the right position for a page cursor. |
| 176 |
@param[in] block buffer block |
| 177 |
@param[in] index index tree |
| 178 |
@param[in] tuple data tuple |
| 179 |
@param[out] cursor page cursor |
| 180 |
@return number of matched fields on the left */ |
| 181 |
static inline ulint page_cur_search(const buf_block_t *block, |
| 182 |
const dict_index_t *index, |
| 183 |
const dtuple_t *tuple, page_cur_t *cursor) { |
| 184 |
return (page_cur_search(block, index, tuple, PAGE_CUR_LE, cursor)); |
| 185 |
} |
| 186 |
|
| 187 |
static inline rec_t *page_cur_tuple_insert(page_cur_t *cursor, |
| 188 |
const dtuple_t *tuple, |
| 189 |
dict_index_t *index, ulint **offsets, |
| 190 |
mem_heap_t **heap, mtr_t *mtr) { |
| 191 |
rec_t *rec; |
| 192 |
rec_t *insert_rec; |
| 193 |
|
| 194 |
ulint size = rec_get_converted_size(index, tuple); |
| 195 |
|
| 196 |
if (!*heap) { |
| 197 |
*heap = mem_heap_create( |
| 198 |
size + (4 + REC_OFFS_HEADER_SIZE + dtuple_get_n_fields(tuple)) * |
| 199 |
sizeof **offsets, |
| 200 |
UT_LOCATION_HERE); |
| 201 |
} |
| 202 |
|
| 203 |
rec = rec_convert_dtuple_to_rec((byte *)mem_heap_alloc(*heap, size), index, |
| 204 |
tuple); |
| 205 |
|
| 206 |
*offsets = rec_get_offsets(rec, index, *offsets, ULINT_UNDEFINED, |
| 207 |
UT_LOCATION_HERE, heap); |
| 208 |
|
| 209 |
ut_ad(cmp_dtuple_rec(tuple, rec, index, *offsets) == 0); |
| 210 |
|
| 211 |
if (buf_block_get_page_zip(cursor->block)) { |
| 212 |
insert_rec = page_cur_insert_rec_zip(cursor, index, rec, *offsets, mtr); |
| 213 |
} else { |
| 214 |
insert_rec = |
| 215 |
page_cur_insert_rec_low(cursor->rec, index, rec, *offsets, mtr); |
| 216 |
} |
| 217 |
|
| 218 |
#ifdef UNIV_DEBUG |
| 219 |
if (insert_rec != nullptr) { |
| 220 |
if (cmp_dtuple_rec(tuple, insert_rec, index, *offsets) != 0) { |
| 221 |
std::cerr << "tuple=" << rec_printer(tuple).str() << std::endl; |
| 222 |
std::cerr << "rec=" << rec_printer(insert_rec, *offsets).str() |
| 223 |
<< std::endl; |
| 224 |
} |
| 225 |
} |
| 226 |
ut_ad(!insert_rec || !cmp_dtuple_rec(tuple, insert_rec, index, *offsets)); |
| 227 |
#endif /* UNIV_DEBUG */ |
| 228 |
|
| 229 |
return (insert_rec); |
| 230 |
} |
| 231 |
|
| 232 |
/** Insert a record next to page cursor. Record is directly copied to |
| 233 |
the page from tuple without creating intermediate copy of the record. |
| 234 |
|
| 235 |
@param[in,out] cursor A page cursor |
| 236 |
@param[in] tuple Pointer to a data tuple |
| 237 |
@param[in] index Record descriptor |
| 238 |
@param[in] mtr Mini-transaction handle, or NULL |
| 239 |
@param[in] rec_size The size of new record |
| 240 |
|
| 241 |
@return pointer to record if succeed, NULL otherwise */ |
| 242 |
static inline rec_t *page_cur_tuple_direct_insert(page_cur_t *cursor, |
| 243 |
const dtuple_t *tuple, |
| 244 |
dict_index_t *index, |
| 245 |
mtr_t *mtr, ulint rec_size) { |
| 246 |
ut_ad(index->table->is_intrinsic()); |
| 247 |
|
| 248 |
return page_cur_direct_insert_rec_low(cursor->rec, index, tuple, mtr, |
| 249 |
rec_size); |
| 250 |
} |
| 251 |
#endif /* !UNIV_HOTBACKUP */ |
| 252 |
|
| 253 |
/** Inserts a record next to page cursor. Returns pointer to inserted record if |
| 254 |
succeed, i.e., enough space available, NULL otherwise. The cursor stays at |
| 255 |
the same logical position, but the physical position may change if it is |
| 256 |
pointing to a compressed page that was reorganized. |
| 257 |
|
| 258 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 259 |
if this is a compressed leaf page in a secondary index. |
| 260 |
This has to be done either within the same mini-transaction, |
| 261 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 262 |
|
| 263 |
@return pointer to record if succeed, NULL otherwise */ |
| 264 |
static inline rec_t *page_cur_rec_insert( |
| 265 |
page_cur_t *cursor, /*!< in/out: a page cursor */ |
| 266 |
const rec_t *rec, /*!< in: record to insert */ |
| 267 |
dict_index_t *index, /*!< in: record descriptor */ |
| 268 |
ulint *offsets, /*!< in/out: rec_get_offsets(rec, index) */ |
| 269 |
mtr_t *mtr) /*!< in: mini-transaction handle, or NULL */ |
| 270 |
{ |
| 271 |
if (buf_block_get_page_zip(cursor->block)) { |
| 272 |
return (page_cur_insert_rec_zip(cursor, index, rec, offsets, mtr)); |
| 273 |
} else { |
| 274 |
return (page_cur_insert_rec_low(cursor->rec, index, rec, offsets, mtr)); |
| 275 |
} |
| 276 |
} |