| 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.h |
| 29 |
The page cursor |
| 30 |
|
| 31 |
Created 10/4/1994 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#ifndef page0cur_h |
| 35 |
#define page0cur_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "buf0types.h" |
| 40 |
#include "data0data.h" |
| 41 |
#include "gis0type.h" |
| 42 |
#include "mtr0mtr.h" |
| 43 |
#include "page0page.h" |
| 44 |
#include "rem0rec.h" |
| 45 |
#include "rem0wrec.h" |
| 46 |
|
| 47 |
#define PAGE_CUR_ADAPT |
| 48 |
|
| 49 |
#ifdef UNIV_DEBUG |
| 50 |
/** Gets pointer to the page frame where the cursor is positioned. |
| 51 |
@return page */ |
| 52 |
static inline page_t *page_cur_get_page( |
| 53 |
page_cur_t *cur); /*!< in: page cursor */ |
| 54 |
/** Gets pointer to the buffer block where the cursor is positioned. |
| 55 |
@return page */ |
| 56 |
static inline buf_block_t *page_cur_get_block( |
| 57 |
page_cur_t *cur); /*!< in: page cursor */ |
| 58 |
/** Gets pointer to the page frame where the cursor is positioned. |
| 59 |
@return page */ |
| 60 |
static inline page_zip_des_t *page_cur_get_page_zip( |
| 61 |
page_cur_t *cur); /*!< in: page cursor */ |
| 62 |
/** Gets the record where the cursor is positioned. |
| 63 |
@return record */ |
| 64 |
static inline rec_t *page_cur_get_rec(page_cur_t *cur); /*!< in: page cursor */ |
| 65 |
#else /* UNIV_DEBUG */ |
| 66 |
#define page_cur_get_page(cur) page_align((cur)->rec) |
| 67 |
#define page_cur_get_block(cur) (cur)->block |
| 68 |
#define page_cur_get_page_zip(cur) buf_block_get_page_zip((cur)->block) |
| 69 |
#define page_cur_get_rec(cur) (cur)->rec |
| 70 |
#endif /* UNIV_DEBUG */ |
| 71 |
|
| 72 |
/** Sets the cursor object to point before the first user record on the page. |
| 73 |
@param[in] block index page |
| 74 |
@param[in] cur cursor */ |
| 75 |
static inline void page_cur_set_before_first(const buf_block_t *block, |
| 76 |
page_cur_t *cur); |
| 77 |
|
| 78 |
/** Sets the cursor object to point after the last user record on the page. |
| 79 |
@param[in] block index page |
| 80 |
@param[in] cur cursor */ |
| 81 |
static inline void page_cur_set_after_last(const buf_block_t *block, |
| 82 |
page_cur_t *cur); |
| 83 |
|
| 84 |
/** Returns true if the cursor is before first user record on page. |
| 85 |
@return true if at start */ |
| 86 |
static inline bool page_cur_is_before_first( |
| 87 |
const page_cur_t *cur); /*!< in: cursor */ |
| 88 |
/** Returns true if the cursor is after last user record. |
| 89 |
@return true if at end */ |
| 90 |
static inline bool page_cur_is_after_last( |
| 91 |
const page_cur_t *cur); /*!< in: cursor */ |
| 92 |
|
| 93 |
/** Positions the cursor on the given record. |
| 94 |
@param[in] rec record on a page |
| 95 |
@param[in] block buffer block containing the record |
| 96 |
@param[out] cur page cursor */ |
| 97 |
static inline void page_cur_position(const rec_t *rec, const buf_block_t *block, |
| 98 |
page_cur_t *cur); |
| 99 |
|
| 100 |
/** Moves the cursor to the next record on page. */ |
| 101 |
static inline void page_cur_move_to_next( |
| 102 |
page_cur_t *cur); /*!< in/out: cursor; must not be after last */ |
| 103 |
/** Moves the cursor to the previous record on page. */ |
| 104 |
static inline void page_cur_move_to_prev( |
| 105 |
page_cur_t *cur); /*!< in/out: cursor; not before first */ |
| 106 |
#ifndef UNIV_HOTBACKUP |
| 107 |
/** Inserts a record next to page cursor. Returns pointer to inserted record if |
| 108 |
succeed, i.e., enough space available, NULL otherwise. The cursor stays at the |
| 109 |
same logical position, but the physical position may change if it is pointing to |
| 110 |
a compressed page that was reorganized. |
| 111 |
|
| 112 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE if this is a |
| 113 |
compressed leaf page in a secondary index. This has to be done either within the |
| 114 |
same mini-transaction, or by invoking ibuf_reset_free_bits() before |
| 115 |
mtr_commit(). |
| 116 |
|
| 117 |
@param[in,out] cursor Page cursor. |
| 118 |
@param[in] tuple Pointer to a data tuple |
| 119 |
@param[in] index Index descriptor. |
| 120 |
@param[in] offsets Offsets on *rec. |
| 121 |
@param[in,out] heap Pointer to memory heap, or to nullptr. |
| 122 |
@param[in] mtr Mini-transaction handle, or nullptr. |
| 123 |
@return pointer to record if succeed, NULL otherwise */ |
| 124 |
[[nodiscard]] static inline rec_t *page_cur_tuple_insert( |
| 125 |
page_cur_t *cursor, const dtuple_t *tuple, dict_index_t *index, |
| 126 |
ulint **offsets, mem_heap_t **heap, mtr_t *mtr); |
| 127 |
#endif /* !UNIV_HOTBACKUP */ |
| 128 |
|
| 129 |
/** Inserts a record next to page cursor. Returns pointer to inserted record |
| 130 |
if succeed, i.e., enough space available, NULL otherwise. The cursor stays at |
| 131 |
the same logical position, but the physical position may change if it is |
| 132 |
pointing to a compressed page that was reorganized. |
| 133 |
|
| 134 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE if this is a |
| 135 |
compressed leaf page in a secondary index. |
| 136 |
This has to be done either within the same mini-transaction, or by invoking |
| 137 |
ibuf_reset_free_bits() before mtr_commit(). |
| 138 |
|
| 139 |
@param[in,out] cursor A page cursor |
| 140 |
@param[in] rec record To insert |
| 141 |
@param[in] index Record descriptor |
| 142 |
@param[in,out] offsets rec_get_offsets(rec, index) |
| 143 |
@param[in] mtr Mini-transaction handle, or NULL |
| 144 |
@return pointer to record if succeed, NULL otherwise */ |
| 145 |
static inline rec_t *page_cur_rec_insert(page_cur_t *cursor, const rec_t *rec, |
| 146 |
dict_index_t *index, ulint *offsets, |
| 147 |
mtr_t *mtr); |
| 148 |
|
| 149 |
/** Inserts a record next to page cursor on an uncompressed page. |
| 150 |
Returns pointer to inserted record if succeed, i.e., enough |
| 151 |
space available, NULL otherwise. The cursor stays at the same position. |
| 152 |
@return pointer to record if succeed, NULL otherwise */ |
| 153 |
[[nodiscard]] rec_t *page_cur_insert_rec_low( |
| 154 |
rec_t *current_rec, /*!< in: pointer to current record after |
| 155 |
which the new record is inserted */ |
| 156 |
dict_index_t *index, /*!< in: record descriptor */ |
| 157 |
const rec_t *rec, /*!< in: pointer to a physical record */ |
| 158 |
ulint *offsets, /*!< in/out: rec_get_offsets(rec, index) */ |
| 159 |
mtr_t *mtr); /*!< in: mini-transaction handle, or NULL */ |
| 160 |
|
| 161 |
/** Inserts a record next to page cursor on an uncompressed page. |
| 162 |
@param[in] current_rec Pointer to current record after which |
| 163 |
the new record is inserted. |
| 164 |
@param[in] index Record descriptor |
| 165 |
@param[in] tuple Pointer to a data tuple |
| 166 |
@param[in] mtr Mini-transaction handle, or NULL |
| 167 |
@param[in] rec_size The size of new record |
| 168 |
|
| 169 |
@return pointer to record if succeed, NULL otherwise */ |
| 170 |
rec_t *page_cur_direct_insert_rec_low(rec_t *current_rec, dict_index_t *index, |
| 171 |
const dtuple_t *tuple, mtr_t *mtr, |
| 172 |
ulint rec_size); |
| 173 |
|
| 174 |
/** Inserts a record next to page cursor on a compressed and uncompressed |
| 175 |
page. Returns pointer to inserted record if succeed, i.e., |
| 176 |
enough space available, NULL otherwise. |
| 177 |
The cursor stays at the same position. |
| 178 |
|
| 179 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 180 |
if this is a compressed leaf page in a secondary index. |
| 181 |
This has to be done either within the same mini-transaction, |
| 182 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 183 |
|
| 184 |
@return pointer to record if succeed, NULL otherwise */ |
| 185 |
[[nodiscard]] rec_t *page_cur_insert_rec_zip( |
| 186 |
page_cur_t *cursor, /*!< in/out: page cursor */ |
| 187 |
dict_index_t *index, /*!< in: record descriptor */ |
| 188 |
const rec_t *rec, /*!< in: pointer to a physical record */ |
| 189 |
ulint *offsets, /*!< in/out: rec_get_offsets(rec, index) */ |
| 190 |
mtr_t *mtr); /*!< in: mini-transaction handle, or NULL */ |
| 191 |
/** Copies records from page to a newly created page, from a given record |
| 192 |
onward, including that record. Infimum and supremum records are not copied. |
| 193 |
|
| 194 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 195 |
if this is a compressed leaf page in a secondary index. |
| 196 |
This has to be done either within the same mini-transaction, |
| 197 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). */ |
| 198 |
void page_copy_rec_list_end_to_created_page( |
| 199 |
page_t *new_page, /*!< in/out: index page to copy to */ |
| 200 |
rec_t *rec, /*!< in: first record to copy */ |
| 201 |
dict_index_t *index, /*!< in: record descriptor */ |
| 202 |
mtr_t *mtr); /*!< in: mtr */ |
| 203 |
/** Deletes a record at the page cursor. The cursor is moved to the |
| 204 |
next record after the deleted one. */ |
| 205 |
void page_cur_delete_rec( |
| 206 |
page_cur_t *cursor, /*!< in/out: a page cursor */ |
| 207 |
const dict_index_t *index, /*!< in: record descriptor */ |
| 208 |
const ulint *offsets, /*!< in: rec_get_offsets( |
| 209 |
cursor->rec, index) */ |
| 210 |
mtr_t *mtr); /*!< in: mini-transaction handle */ |
| 211 |
#ifndef UNIV_HOTBACKUP |
| 212 |
/** Search the right position for a page cursor. |
| 213 |
@param[in] block buffer block |
| 214 |
@param[in] index index tree |
| 215 |
@param[in] tuple data tuple |
| 216 |
@param[in] mode PAGE_CUR_L, PAGE_CUR_LE, PAGE_CUR_G, or PAGE_CUR_GE |
| 217 |
@param[out] cursor page cursor |
| 218 |
@return number of matched fields on the left */ |
| 219 |
static inline ulint page_cur_search(const buf_block_t *block, |
| 220 |
const dict_index_t *index, |
| 221 |
const dtuple_t *tuple, page_cur_mode_t mode, |
| 222 |
page_cur_t *cursor); |
| 223 |
|
| 224 |
/** Search the right position for a page cursor. |
| 225 |
@param[in] block buffer block |
| 226 |
@param[in] index index tree |
| 227 |
@param[in] tuple data tuple |
| 228 |
@param[out] cursor page cursor |
| 229 |
@return number of matched fields on the left */ |
| 230 |
static inline ulint page_cur_search(const buf_block_t *block, |
| 231 |
const dict_index_t *index, |
| 232 |
const dtuple_t *tuple, page_cur_t *cursor); |
| 233 |
#endif /* !UNIV_HOTBACKUP */ |
| 234 |
|
| 235 |
/** Searches the right position for a page cursor. |
| 236 |
@param[in] block Buffer block |
| 237 |
@param[in] index Record descriptor |
| 238 |
@param[in] tuple Data tuple |
| 239 |
@param[in] mode PAGE_CUR_L, PAGE_CUR_LE, PAGE_CUR_G, or PAGE_CUR_GE |
| 240 |
@param[in,out] iup_matched_fields Already matched fields in upper limit record |
| 241 |
@param[in,out] ilow_matched_fields Already matched fields in lower limit record |
| 242 |
@param[out] cursor Page cursor |
| 243 |
@param[in,out] rtr_info Rtree search stack */ |
| 244 |
void page_cur_search_with_match(const buf_block_t *block, |
| 245 |
const dict_index_t *index, |
| 246 |
const dtuple_t *tuple, page_cur_mode_t mode, |
| 247 |
ulint *iup_matched_fields, |
| 248 |
|
| 249 |
ulint *ilow_matched_fields, |
| 250 |
|
| 251 |
page_cur_t *cursor, rtr_info_t *rtr_info); |
| 252 |
|
| 253 |
/** Search the right position for a page cursor. |
| 254 |
@param[in] block buffer block |
| 255 |
@param[in] index index tree |
| 256 |
@param[in] tuple key to be searched for |
| 257 |
@param[in] mode search mode |
| 258 |
@param[in,out] iup_matched_fields already matched fields in the |
| 259 |
upper limit record |
| 260 |
@param[in,out] iup_matched_bytes already matched bytes in the |
| 261 |
first partially matched field in the upper limit record |
| 262 |
@param[in,out] ilow_matched_fields already matched fields in the |
| 263 |
lower limit record |
| 264 |
@param[in,out] ilow_matched_bytes already matched bytes in the |
| 265 |
first partially matched field in the lower limit record |
| 266 |
@param[out] cursor page cursor */ |
| 267 |
void page_cur_search_with_match_bytes( |
| 268 |
const buf_block_t *block, const dict_index_t *index, const dtuple_t *tuple, |
| 269 |
page_cur_mode_t mode, ulint *iup_matched_fields, ulint *iup_matched_bytes, |
| 270 |
ulint *ilow_matched_fields, ulint *ilow_matched_bytes, page_cur_t *cursor); |
| 271 |
/** Positions a page cursor on a randomly chosen user record on a page. If there |
| 272 |
are no user records, sets the cursor on the infimum record. */ |
| 273 |
void page_cur_open_on_rnd_user_rec(buf_block_t *block, /*!< in: page */ |
| 274 |
page_cur_t *cursor); /*!< out: page cursor */ |
| 275 |
/** Parses a log record of a record insert on a page. |
| 276 |
@return end of log record or NULL */ |
| 277 |
byte *page_cur_parse_insert_rec( |
| 278 |
bool is_short, /*!< in: true if short inserts */ |
| 279 |
const byte *ptr, /*!< in: buffer */ |
| 280 |
const byte *end_ptr, /*!< in: buffer end */ |
| 281 |
buf_block_t *block, /*!< in: page or NULL */ |
| 282 |
dict_index_t *index, /*!< in: record descriptor */ |
| 283 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 284 |
/** Parses a log record of copying a record list end to a new created page. |
| 285 |
@return end of log record or NULL */ |
| 286 |
byte *page_parse_copy_rec_list_to_created_page( |
| 287 |
byte *ptr, /*!< in: buffer */ |
| 288 |
byte *end_ptr, /*!< in: buffer end */ |
| 289 |
buf_block_t *block, /*!< in: page or NULL */ |
| 290 |
dict_index_t *index, /*!< in: record descriptor */ |
| 291 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 292 |
/** Parses log record of a record delete on a page. |
| 293 |
@return pointer to record end or NULL */ |
| 294 |
byte *page_cur_parse_delete_rec( |
| 295 |
byte *ptr, /*!< in: buffer */ |
| 296 |
byte *end_ptr, /*!< in: buffer end */ |
| 297 |
buf_block_t *block, /*!< in: page or NULL */ |
| 298 |
dict_index_t *index, /*!< in: record descriptor */ |
| 299 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 300 |
/** Removes the record from a leaf page. This function does not log |
| 301 |
any changes. It is used by the IMPORT tablespace functions. |
| 302 |
@return true if success, i.e., the page did not become too empty |
| 303 |
@param[in] index The index that the record belongs to. |
| 304 |
@param[in,out] pcur Page cursor on record to delete. |
| 305 |
@param[in] offsets Offsets for record. */ |
| 306 |
bool page_delete_rec(const dict_index_t *index, page_cur_t *pcur, |
| 307 |
const ulint *offsets); |
| 308 |
|
| 309 |
/** Index page cursor */ |
| 310 |
|
| 311 |
struct page_cur_t { |
| 312 |
/** Index the cursor is on. */ |
| 313 |
const dict_index_t *index{nullptr}; |
| 314 |
|
| 315 |
/** pointer to a record on page */ |
| 316 |
rec_t *rec{nullptr}; |
| 317 |
|
| 318 |
/** Current offsets of the record. */ |
| 319 |
ulint *offsets{nullptr}; |
| 320 |
|
| 321 |
/** Pointer to the current block containing rec. */ |
| 322 |
buf_block_t *block{nullptr}; |
| 323 |
}; |
| 324 |
|
| 325 |
#include "page0cur.ic" |
| 326 |
|
| 327 |
#endif |