| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2012, Facebook Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 8 |
Free Software Foundation. |
| 9 |
|
| 10 |
This program is designed to work with certain software (including |
| 11 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 12 |
as designated in a particular file or component or in included license |
| 13 |
documentation. The authors of MySQL hereby grant you an additional |
| 14 |
permission to link the program and your derivative works with the |
| 15 |
separately licensed software that they have either included with |
| 16 |
the program or referenced in the documentation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 21 |
for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License along with |
| 24 |
this program; if not, write to the Free Software Foundation, Inc., |
| 25 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/** @file include/btr0btr.h |
| 30 |
The B-tree |
| 31 |
|
| 32 |
Created 6/2/1994 Heikki Tuuri |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef btr0btr_h |
| 36 |
#define btr0btr_h |
| 37 |
|
| 38 |
#include "btr0types.h" |
| 39 |
#include "data0data.h" |
| 40 |
#include "dict0dict.h" |
| 41 |
#include "gis0type.h" |
| 42 |
#include "mtr0mtr.h" |
| 43 |
#include "page0cur.h" |
| 44 |
#include "univ.i" |
| 45 |
|
| 46 |
/** Maximum record size which can be stored on a page, without using the |
| 47 |
special big record storage structure */ |
| 48 |
#define BTR_PAGE_MAX_REC_SIZE (UNIV_PAGE_SIZE / 2 - 200) |
| 49 |
|
| 50 |
/** @brief Maximum depth of a B-tree in InnoDB. |
| 51 |
|
| 52 |
Note that this isn't a maximum as such; none of the tree operations |
| 53 |
avoid producing trees bigger than this. It is instead a "max depth |
| 54 |
that other code must work with", useful for e.g. fixed-size arrays |
| 55 |
that must store some information about each level in a tree. In other |
| 56 |
words: if a B-tree with bigger depth than this is encountered, it is |
| 57 |
not acceptable for it to lead to mysterious memory corruption, but it |
| 58 |
is acceptable for the program to die with a clear assert failure. */ |
| 59 |
constexpr uint32_t BTR_MAX_LEVELS = 100; |
| 60 |
|
| 61 |
/** Latching modes for btr_cur_search_to_nth_level(). */ |
| 62 |
enum btr_latch_mode : size_t { |
| 63 |
/** Search a record on a leaf page and S-latch it. */ |
| 64 |
BTR_SEARCH_LEAF = RW_S_LATCH, |
| 65 |
/** (Prepare to) modify a record on a leaf page and X-latch it. */ |
| 66 |
BTR_MODIFY_LEAF = RW_X_LATCH, |
| 67 |
/** Obtain no latches. */ |
| 68 |
BTR_NO_LATCHES = RW_NO_LATCH, |
| 69 |
/** Start modifying the entire B-tree. */ |
| 70 |
BTR_MODIFY_TREE = 33, |
| 71 |
/** Continue modifying the entire B-tree. */ |
| 72 |
BTR_CONT_MODIFY_TREE = 34, |
| 73 |
/** Search the previous record. */ |
| 74 |
BTR_SEARCH_PREV = 35, |
| 75 |
/** Modify the previous record. */ |
| 76 |
BTR_MODIFY_PREV = 36, |
| 77 |
/** Start searching the entire B-tree. */ |
| 78 |
BTR_SEARCH_TREE = 37, |
| 79 |
/** Continue searching the entire B-tree. */ |
| 80 |
BTR_CONT_SEARCH_TREE = 38 |
| 81 |
}; |
| 82 |
|
| 83 |
/* BTR_INSERT, BTR_DELETE and BTR_DELETE_MARK are mutually exclusive. */ |
| 84 |
|
| 85 |
/** If this is ORed to btr_latch_mode, it means that the search tuple |
| 86 |
will be inserted to the index, at the searched position. |
| 87 |
When the record is not in the buffer pool, try to use the insert buffer. */ |
| 88 |
constexpr size_t BTR_INSERT = 512; |
| 89 |
|
| 90 |
/** This flag ORed to btr_latch_mode says that we do the search in query |
| 91 |
optimization */ |
| 92 |
constexpr size_t BTR_ESTIMATE = 1024; |
| 93 |
|
| 94 |
/** This flag ORed to BTR_INSERT says that we can ignore possible |
| 95 |
UNIQUE definition on secondary indexes when we decide if we can use |
| 96 |
the insert buffer to speed up inserts */ |
| 97 |
constexpr size_t BTR_IGNORE_SEC_UNIQUE = 2048; |
| 98 |
|
| 99 |
/** Try to delete mark the record at the searched position using the |
| 100 |
insert/delete buffer when the record is not in the buffer pool. */ |
| 101 |
constexpr size_t BTR_DELETE_MARK = 4096; |
| 102 |
|
| 103 |
/** Try to purge the record at the searched position using the insert/delete |
| 104 |
buffer when the record is not in the buffer pool. */ |
| 105 |
constexpr size_t BTR_DELETE = 8192; |
| 106 |
|
| 107 |
/** In the case of BTR_SEARCH_LEAF or BTR_MODIFY_LEAF, the caller is |
| 108 |
already holding an S latch on the index tree */ |
| 109 |
constexpr size_t BTR_ALREADY_S_LATCHED = 16384; |
| 110 |
|
| 111 |
/** In the case of BTR_MODIFY_TREE, the caller specifies the intention |
| 112 |
to insert record only. It is used to optimize block->lock range.*/ |
| 113 |
constexpr size_t BTR_LATCH_FOR_INSERT = 32768; |
| 114 |
|
| 115 |
/** In the case of BTR_MODIFY_TREE, the caller specifies the intention |
| 116 |
to delete record only. It is used to optimize block->lock range.*/ |
| 117 |
constexpr size_t BTR_LATCH_FOR_DELETE = 65536; |
| 118 |
|
| 119 |
/** This flag is for undo insert of rtree. For rtree, we need this flag |
| 120 |
to find proper rec to undo insert.*/ |
| 121 |
constexpr size_t BTR_RTREE_UNDO_INS = 131072; |
| 122 |
|
| 123 |
/** In the case of BTR_MODIFY_LEAF, the caller intends to allocate or |
| 124 |
free the pages of externally stored fields. */ |
| 125 |
constexpr size_t BTR_MODIFY_EXTERNAL = 262144; |
| 126 |
|
| 127 |
/** Try to delete mark the record at the searched position when the |
| 128 |
record is in spatial index */ |
| 129 |
constexpr size_t BTR_RTREE_DELETE_MARK = 524288; |
| 130 |
|
| 131 |
constexpr ulint BTR_LATCH_MODE_WITHOUT_FLAGS(ulint latch_mode) { |
| 132 |
return latch_mode & |
| 133 |
~(BTR_INSERT | BTR_DELETE_MARK | BTR_RTREE_UNDO_INS | |
| 134 |
BTR_RTREE_DELETE_MARK | BTR_DELETE | BTR_ESTIMATE | |
| 135 |
BTR_IGNORE_SEC_UNIQUE | BTR_ALREADY_S_LATCHED | |
| 136 |
BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE | BTR_MODIFY_EXTERNAL); |
| 137 |
} |
| 138 |
|
| 139 |
inline ulint BTR_LATCH_MODE_WITHOUT_INTENTION(ulint latch_mode) { |
| 140 |
return latch_mode & |
| 141 |
~(BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE | BTR_MODIFY_EXTERNAL); |
| 142 |
} |
| 143 |
|
| 144 |
/** Report that an index page is corrupted. */ |
| 145 |
void btr_corruption_report(const buf_block_t *block, /*!< in: corrupted block */ |
| 146 |
const dict_index_t *index) /*!< in: index tree */ |
| 147 |
UNIV_COLD; |
| 148 |
|
| 149 |
/** Assert that a B-tree page is not corrupted. |
| 150 |
@param block buffer block containing a B-tree page |
| 151 |
@param index the B-tree index */ |
| 152 |
inline void btr_assert_not_corrupted(const buf_block_t *block, |
| 153 |
const dict_index_t *index) { |
| 154 |
if (page_is_comp(buf_block_get_frame(block)) != |
| 155 |
dict_table_is_comp((index)->table)) { |
| 156 |
btr_corruption_report(block, index); |
| 157 |
ut_error; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** Gets the root node of a tree and sx-latches it for segment access. |
| 162 |
@return root page, sx-latched */ |
| 163 |
page_t *btr_root_get(const dict_index_t *index, /*!< in: index tree */ |
| 164 |
mtr_t *mtr); /*!< in: mtr */ |
| 165 |
|
| 166 |
/** Checks and adjusts the root node of a tree during IMPORT TABLESPACE. |
| 167 |
@return error code, or DB_SUCCESS */ |
| 168 |
[[nodiscard]] dberr_t btr_root_adjust_on_import( |
| 169 |
const dict_index_t *index); /*!< in: index tree */ |
| 170 |
|
| 171 |
/** Gets the height of the B-tree (the level of the root, when the leaf |
| 172 |
level is assumed to be 0). The caller must hold an S or X latch on |
| 173 |
the index. |
| 174 |
@return tree height (level of the root) */ |
| 175 |
[[nodiscard]] ulint btr_height_get(dict_index_t *index, /*!< in: index tree */ |
| 176 |
mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 177 |
|
| 178 |
#ifndef UNIV_HOTBACKUP |
| 179 |
/** Gets a buffer page and declares its latching order level. |
| 180 |
@param[in] page_id Page id |
| 181 |
@param[in] page_size Page size |
| 182 |
@param[in] mode Latch mode |
| 183 |
@param[in] location Location from where this method is called. |
| 184 |
@param[in] index Index tree, may be NULL if it is not an insert |
| 185 |
buffer tree |
| 186 |
@param[in,out] mtr Mini-transaction |
| 187 |
@return block */ |
| 188 |
static inline buf_block_t *btr_block_get_func( |
| 189 |
const page_id_t &page_id, const page_size_t &page_size, ulint mode, |
| 190 |
ut::Location location, IF_DEBUG(const dict_index_t *index, ) mtr_t *mtr); |
| 191 |
|
| 192 |
/** Gets a buffer page and declares its latching order level. |
| 193 |
@param page_id Tablespace/page identifier |
| 194 |
@param page_size Page size |
| 195 |
@param mode Latch mode |
| 196 |
@param[in] location Location from where this method is called. |
| 197 |
@param index Index tree, may be NULL if not the insert buffer tree |
| 198 |
@param mtr Mini-transaction handle |
| 199 |
@return the block descriptor */ |
| 200 |
static inline buf_block_t *btr_block_get(const page_id_t &page_id, |
| 201 |
const page_size_t &page_size, |
| 202 |
ulint mode, ut::Location location, |
| 203 |
const dict_index_t *index, |
| 204 |
mtr_t *mtr) { |
| 205 |
return btr_block_get_func(page_id, page_size, mode, location, |
| 206 |
IF_DEBUG(index, ) mtr); |
| 207 |
} |
| 208 |
|
| 209 |
#endif /* !UNIV_HOTBACKUP */ |
| 210 |
|
| 211 |
/** Gets the index id field of a page. |
| 212 |
@return index id */ |
| 213 |
[[nodiscard]] static inline space_index_t btr_page_get_index_id( |
| 214 |
const page_t *page); /*!< in: index page */ |
| 215 |
/** Gets the node level field in an index page. |
| 216 |
@param[in] page index page |
| 217 |
@return level, leaf level == 0 */ |
| 218 |
[[nodiscard]] static inline ulint btr_page_get_level(const page_t *page); |
| 219 |
/** Gets the next index page number. |
| 220 |
@param[in] page Index page. |
| 221 |
@param[in] mtr Mini-transaction handle. |
| 222 |
@return next page number */ |
| 223 |
[[nodiscard]] static inline page_no_t btr_page_get_next(const page_t *page, |
| 224 |
mtr_t *mtr); |
| 225 |
/** Gets the previous index page number. |
| 226 |
@param[in] page Index page. |
| 227 |
@param[in] mtr Mini-transaction handle. |
| 228 |
@return prev page number */ |
| 229 |
[[nodiscard]] static inline page_no_t btr_page_get_prev(const page_t *page, |
| 230 |
mtr_t *mtr); |
| 231 |
|
| 232 |
#ifndef UNIV_HOTBACKUP |
| 233 |
/** Releases the latch on a leaf page and bufferunfixes it. |
| 234 |
@param[in] block buffer block |
| 235 |
@param[in] latch_mode BTR_SEARCH_LEAF or BTR_MODIFY_LEAF |
| 236 |
@param[in] mtr mtr */ |
| 237 |
static inline void btr_leaf_page_release(buf_block_t *block, ulint latch_mode, |
| 238 |
mtr_t *mtr); |
| 239 |
#endif /* !UNIV_HOTBACKUP */ |
| 240 |
|
| 241 |
/** Gets the child node file address in a node pointer. |
| 242 |
NOTE: the offsets array must contain all offsets for the record since |
| 243 |
we read the last field according to offsets and assume that it contains |
| 244 |
the child page number. In other words offsets must have been retrieved |
| 245 |
with rec_get_offsets(n_fields=ULINT_UNDEFINED). |
| 246 |
@param[in] rec node Pointer record |
| 247 |
@param[in] offsets Array returned by rec_get_offsets() |
| 248 |
@return child node address */ |
| 249 |
[[nodiscard]] static inline page_no_t btr_node_ptr_get_child_page_no( |
| 250 |
const rec_t *rec, const ulint *offsets); |
| 251 |
|
| 252 |
/** Returns the child page of a node pointer and sx-latches it. |
| 253 |
@param[in] node_ptr node pointer |
| 254 |
@param[in] index index |
| 255 |
@param[in] offsets array returned by rec_get_offsets() |
| 256 |
@param[in] mtr mtr |
| 257 |
@param[in] type latch type |
| 258 |
@return child page, latched as per the type */ |
| 259 |
buf_block_t *btr_node_ptr_get_child(const rec_t *node_ptr, dict_index_t *index, |
| 260 |
const ulint *offsets, mtr_t *mtr, |
| 261 |
rw_lock_type_t type = RW_SX_LATCH); |
| 262 |
|
| 263 |
/** Create the root node for a new index tree. |
| 264 |
@param[in] type Type of the index |
| 265 |
@param[in] space Space where created |
| 266 |
@param[in] index_id Index id |
| 267 |
@param[in] index Index tree |
| 268 |
@param[in,out] mtr Mini-transaction |
| 269 |
@return page number of the created root |
| 270 |
@retval FIL_NULL if did not succeed */ |
| 271 |
ulint btr_create(ulint type, space_id_t space, space_index_t index_id, |
| 272 |
dict_index_t *index, mtr_t *mtr); |
| 273 |
|
| 274 |
/** Free a persistent index tree if it exists. |
| 275 |
@param[in] page_id Root page id |
| 276 |
@param[in] page_size Page size |
| 277 |
@param[in] index_id PAGE_INDEX_ID contents |
| 278 |
@param[in,out] mtr Mini-transaction */ |
| 279 |
void btr_free_if_exists(const page_id_t &page_id, const page_size_t &page_size, |
| 280 |
space_index_t index_id, mtr_t *mtr); |
| 281 |
|
| 282 |
/** Free an index tree in a temporary tablespace. |
| 283 |
@param[in] page_id root page id |
| 284 |
@param[in] page_size page size */ |
| 285 |
void btr_free(const page_id_t &page_id, const page_size_t &page_size); |
| 286 |
|
| 287 |
/** Truncate an index tree. We just free all except the root. |
| 288 |
Currently, this function is only specific for clustered indexes and the only |
| 289 |
caller is DDTableBuffer which manages a table with only a clustered index. |
| 290 |
It is up to the caller to ensure atomicity and to ensure correct recovery by |
| 291 |
calling btr_truncate_recover(). |
| 292 |
@param[in] index clustered index */ |
| 293 |
void btr_truncate(const dict_index_t *index); |
| 294 |
|
| 295 |
/** Recovery function for btr_truncate. We will check if there is a |
| 296 |
crash during btr_truncate, if so, do recover it, if not, do nothing. |
| 297 |
@param[in] index clustered index */ |
| 298 |
void btr_truncate_recover(const dict_index_t *index); |
| 299 |
|
| 300 |
/** Makes tree one level higher by splitting the root, and inserts |
| 301 |
the tuple. It is assumed that mtr contains an x-latch on the tree. |
| 302 |
NOTE that the operation of this function must always succeed, |
| 303 |
we cannot reverse it: therefore enough free disk space must be |
| 304 |
guaranteed to be available before this function is called. |
| 305 |
@return inserted record */ |
| 306 |
[[nodiscard]] rec_t *btr_root_raise_and_insert( |
| 307 |
uint32_t flags, /*!< in: undo logging and locking flags */ |
| 308 |
btr_cur_t *cursor, /*!< in: cursor at which to insert: must be |
| 309 |
on the root page; when the function returns, |
| 310 |
the cursor is positioned on the predecessor |
| 311 |
of the inserted record */ |
| 312 |
ulint **offsets, /*!< out: offsets on inserted record */ |
| 313 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap |
| 314 |
that can be emptied, or NULL */ |
| 315 |
const dtuple_t *tuple, /*!< in: tuple to insert */ |
| 316 |
mtr_t *mtr); /*!< in: mtr */ |
| 317 |
/** Reorganizes an index page. |
| 318 |
|
| 319 |
IMPORTANT: On success, the caller will have to update IBUF_BITMAP_FREE |
| 320 |
if this is a compressed leaf page in a secondary index. This has to |
| 321 |
be done either within the same mini-transaction, or by invoking |
| 322 |
ibuf_reset_free_bits() before mtr_commit(). On uncompressed pages, |
| 323 |
IBUF_BITMAP_FREE is unaffected by reorganization. |
| 324 |
|
| 325 |
@param[in] recovery True if called in recovery: locks should not be updated, |
| 326 |
i.e., there cannot exist locks on the page, and a hash index should not be |
| 327 |
dropped: it cannot exist. |
| 328 |
@param[in] z_level Compression level to be used if dealing with compressed |
| 329 |
page. |
| 330 |
@param[in,out] cursor Page cursor. |
| 331 |
@param[in] index The index tree of the page. |
| 332 |
@param[in,out] mtr Mini-transaction |
| 333 |
@retval true if the operation was successful |
| 334 |
@retval false if it is a compressed page, and re-compression failed */ |
| 335 |
[[nodiscard]] bool btr_page_reorganize_low(bool recovery, ulint z_level, |
| 336 |
page_cur_t *cursor, |
| 337 |
dict_index_t *index, mtr_t *mtr); |
| 338 |
|
| 339 |
/** Reorganizes an index page. |
| 340 |
|
| 341 |
IMPORTANT: On success, the caller will have to update IBUF_BITMAP_FREE |
| 342 |
if this is a compressed leaf page in a secondary index. This has to |
| 343 |
be done either within the same mini-transaction, or by invoking |
| 344 |
ibuf_reset_free_bits() before mtr_commit(). On uncompressed pages, |
| 345 |
IBUF_BITMAP_FREE is unaffected by reorganization. |
| 346 |
|
| 347 |
@param[in,out] cursor Page cursor |
| 348 |
@param[in] index The index tree of the page |
| 349 |
@param[in,out] mtr Mini-transaction |
| 350 |
@retval true if the operation was successful |
| 351 |
@retval false if it is a compressed page, and recompression failed */ |
| 352 |
bool btr_page_reorganize(page_cur_t *cursor, dict_index_t *index, mtr_t *mtr); |
| 353 |
|
| 354 |
/** Decides if the page should be split at the convergence point of |
| 355 |
inserts converging to left. |
| 356 |
@return true if split recommended */ |
| 357 |
[[nodiscard]] bool btr_page_get_split_rec_to_left( |
| 358 |
btr_cur_t *cursor, /*!< in: cursor at which to insert */ |
| 359 |
rec_t **split_rec); /*!< out: if split recommended, |
| 360 |
the first record on upper half page, |
| 361 |
or NULL if tuple should be first */ |
| 362 |
/** Decides if the page should be split at the convergence point of |
| 363 |
inserts converging to right. |
| 364 |
@return true if split recommended */ |
| 365 |
[[nodiscard]] bool btr_page_get_split_rec_to_right( |
| 366 |
btr_cur_t *cursor, /*!< in: cursor at which to insert */ |
| 367 |
rec_t **split_rec); /*!< out: if split recommended, |
| 368 |
the first record on upper half page, |
| 369 |
or NULL if tuple should be first */ |
| 370 |
|
| 371 |
/** Splits an index page to halves and inserts the tuple. It is assumed |
| 372 |
that mtr holds an x-latch to the index tree. NOTE: the tree x-latch is |
| 373 |
released within this function! NOTE that the operation of this |
| 374 |
function must always succeed, we cannot reverse it: therefore enough |
| 375 |
free disk space (2 pages) must be guaranteed to be available before |
| 376 |
this function is called. |
| 377 |
|
| 378 |
@return inserted record */ |
| 379 |
[[nodiscard]] rec_t *btr_page_split_and_insert( |
| 380 |
uint32_t flags, /*!< in: undo logging and locking flags */ |
| 381 |
btr_cur_t *cursor, /*!< in: cursor at which to insert; when the |
| 382 |
function returns, the cursor is positioned |
| 383 |
on the predecessor of the inserted record */ |
| 384 |
ulint **offsets, /*!< out: offsets on inserted record */ |
| 385 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap |
| 386 |
that can be emptied, or NULL */ |
| 387 |
const dtuple_t *tuple, /*!< in: tuple to insert */ |
| 388 |
mtr_t *mtr); /*!< in: mtr */ |
| 389 |
/** Inserts a data tuple to a tree on a non-leaf level. It is assumed |
| 390 |
that mtr holds an x-latch on the tree. |
| 391 |
@param[in] flags undo logging and locking flags |
| 392 |
@param[in] index index |
| 393 |
@param[in] level level, must be > 0 |
| 394 |
@param[in] tuple the record to be inserted |
| 395 |
@param[in] location location where called |
| 396 |
@param[in] mtr mtr */ |
| 397 |
void btr_insert_on_non_leaf_level(uint32_t flags, dict_index_t *index, |
| 398 |
ulint level, dtuple_t *tuple, |
| 399 |
ut::Location location, mtr_t *mtr); |
| 400 |
|
| 401 |
/** Sets a record as the predefined minimum record. |
| 402 |
@param[in,out] rec Record |
| 403 |
@param[in] mtr Mini-transaction |
| 404 |
*/ |
| 405 |
void btr_set_min_rec_mark(rec_t *rec, mtr_t *mtr); |
| 406 |
|
| 407 |
/** Deletes on the upper level the node pointer to a page. |
| 408 |
@param[in] index Index tree |
| 409 |
@param[in] block Page whose node pointer is deleted |
| 410 |
@param[in] mtr Mini-transaction |
| 411 |
*/ |
| 412 |
void btr_node_ptr_delete(dict_index_t *index, buf_block_t *block, mtr_t *mtr); |
| 413 |
#ifdef UNIV_DEBUG |
| 414 |
/** Asserts that the node pointer to a page is appropriate. |
| 415 |
@param[in] index index tree |
| 416 |
@param[in] block index page |
| 417 |
@param[in] mtr mtr |
| 418 |
@return true */ |
| 419 |
bool btr_check_node_ptr(dict_index_t *index, buf_block_t *block, mtr_t *mtr); |
| 420 |
#endif /* UNIV_DEBUG */ |
| 421 |
/** Tries to merge the page first to the left immediate brother if such a |
| 422 |
brother exists, and the node pointers to the current page and to the brother |
| 423 |
reside on the same page. If the left brother does not satisfy these |
| 424 |
conditions, looks at the right brother. If the page is the only one on that |
| 425 |
level lifts the records of the page to the father page, thus reducing the |
| 426 |
tree height. It is assumed that mtr holds an x-latch on the tree and on the |
| 427 |
page. If cursor is on the leaf level, mtr must also hold x-latches to the |
| 428 |
brothers, if they exist. |
| 429 |
@param[in,out] cursor cursor on the page to merge or lift; the page must not be |
| 430 |
empty: when deleting records, use btr_discard_page() if the page would become |
| 431 |
empty |
| 432 |
@param[in] adjust true if should adjust the cursor position even if compression |
| 433 |
occurs. |
| 434 |
@param[in,out] mtr mini-transaction |
| 435 |
@return true on success */ |
| 436 |
bool btr_compress(btr_cur_t *cursor, bool adjust, mtr_t *mtr); |
| 437 |
/** Discards a page from a B-tree. This is used to remove the last record from |
| 438 |
a B-tree page: the whole page must be removed at the same time. This cannot |
| 439 |
be used for the root page, which is allowed to be empty. */ |
| 440 |
void btr_discard_page(btr_cur_t *cursor, /*!< in: cursor on the page to |
| 441 |
discard: not on the root page */ |
| 442 |
mtr_t *mtr); /*!< in: mtr */ |
| 443 |
/** Parses the redo log record for setting an index record as the predefined |
| 444 |
minimum record. |
| 445 |
@return end of log record or NULL */ |
| 446 |
[[nodiscard]] byte *btr_parse_set_min_rec_mark( |
| 447 |
byte *ptr, /*!< in: buffer */ |
| 448 |
byte *end_ptr, /*!< in: buffer end */ |
| 449 |
ulint comp, /*!< in: nonzero=compact page format */ |
| 450 |
page_t *page, /*!< in: page or NULL */ |
| 451 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 452 |
/** Parses a redo log record of reorganizing a page. |
| 453 |
@return end of log record or NULL */ |
| 454 |
[[nodiscard]] byte *btr_parse_page_reorganize( |
| 455 |
byte *ptr, /*!< in: buffer */ |
| 456 |
byte *end_ptr, /*!< in: buffer end */ |
| 457 |
dict_index_t *index, /*!< in: record descriptor */ |
| 458 |
bool compressed, /*!< in: true if compressed page */ |
| 459 |
buf_block_t *block, /*!< in: page to be reorganized, or NULL */ |
| 460 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 461 |
/** Gets the number of pages in a B-tree. |
| 462 |
@return number of pages, or ULINT_UNDEFINED if the index is unavailable */ |
| 463 |
[[nodiscard]] ulint btr_get_size( |
| 464 |
dict_index_t *index, /*!< in: index */ |
| 465 |
ulint flag, /*!< in: BTR_N_LEAF_PAGES or BTR_TOTAL_SIZE */ |
| 466 |
mtr_t *mtr); /*!< in/out: mini-transaction where index |
| 467 |
is s-latched */ |
| 468 |
|
| 469 |
#ifdef UNIV_DEBUG |
| 470 |
#define btr_page_alloc(index, hint_page_no, file_direction, level, mtr, \ |
| 471 |
init_mtr) \ |
| 472 |
btr_page_alloc_priv(index, hint_page_no, file_direction, level, mtr, \ |
| 473 |
init_mtr, UT_LOCATION_HERE) |
| 474 |
#else /* UNIV_DEBUG */ |
| 475 |
#define btr_page_alloc(index, hint_page_no, file_direction, level, mtr, \ |
| 476 |
init_mtr) \ |
| 477 |
btr_page_alloc_priv(index, hint_page_no, file_direction, level, mtr, init_mtr) |
| 478 |
#endif /* UNIV_DEBUG */ |
| 479 |
|
| 480 |
/** Allocates a new file page to be used in an index tree. NOTE: we assume |
| 481 |
that the caller has made the reservation for free extents! |
| 482 |
@param[in] index Index tree |
| 483 |
@param[in] hint_page_no Hint of a good page |
| 484 |
@param[in] file_direction Direction where a possible page split is made |
| 485 |
@param[in] level Level where the page is placed in the tree |
| 486 |
@param[in,out] mtr Mini-transaction for the allocation |
| 487 |
@param[in,out] init_mtr Mini-transaction for x-latching and initializing the |
| 488 |
page |
| 489 |
@param[in] loc debug only parameter providing caller source location. |
| 490 |
@retval NULL if no page could be allocated |
| 491 |
@retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded |
| 492 |
(init_mtr == mtr, or the page was not previously freed in mtr), |
| 493 |
returned block is not allocated nor initialized otherwise */ |
| 494 |
[[nodiscard]] buf_block_t *btr_page_alloc_priv( |
| 495 |
dict_index_t *index, page_no_t hint_page_no, byte file_direction, |
| 496 |
ulint level, mtr_t *mtr, mtr_t *init_mtr IF_DEBUG(, const ut::Location &loc) |
| 497 |
|
| 498 |
); |
| 499 |
|
| 500 |
/** Frees a file page used in an index tree. NOTE: cannot free field external |
| 501 |
storage pages because the page must contain info on its level. */ |
| 502 |
void btr_page_free(dict_index_t *index, /*!< in: index tree */ |
| 503 |
buf_block_t *block, /*!< in: block to be freed, x-latched */ |
| 504 |
mtr_t *mtr); /*!< in: mtr */ |
| 505 |
/** Creates a new index page (not the root, and also not |
| 506 |
used in page reorganization). @see btr_page_empty(). */ |
| 507 |
void btr_page_create( |
| 508 |
buf_block_t *block, /*!< in/out: page to be created */ |
| 509 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ |
| 510 |
dict_index_t *index, /*!< in: index */ |
| 511 |
ulint level, /*!< in: the B-tree level of the page */ |
| 512 |
mtr_t *mtr); /*!< in: mtr */ |
| 513 |
|
| 514 |
/** Frees a file page used in an index tree. Can be used also to BLOB |
| 515 |
external storage pages. |
| 516 |
@param[in] index the index to which the page belongs |
| 517 |
@param[in] block block to be freed, x-latched |
| 518 |
@param[in] level page level (ULINT_UNDEFINED=BLOB) |
| 519 |
@param[in] mtr mini transaction context. */ |
| 520 |
void btr_page_free_low(dict_index_t *index, buf_block_t *block, ulint level, |
| 521 |
mtr_t *mtr); |
| 522 |
|
| 523 |
/** Gets the root node of a tree and x- or s-latches it. |
| 524 |
@return root page, x- or s-latched */ |
| 525 |
buf_block_t *btr_root_block_get( |
| 526 |
const dict_index_t *index, /*!< in: index tree */ |
| 527 |
ulint mode, /*!< in: either RW_S_LATCH |
| 528 |
or RW_X_LATCH */ |
| 529 |
mtr_t *mtr); /*!< in: mtr */ |
| 530 |
|
| 531 |
#ifdef UNIV_BTR_PRINT |
| 532 |
/** Prints size info of a B-tree. */ |
| 533 |
void btr_print_size(dict_index_t *index); /*!< in: index tree */ |
| 534 |
/** Prints directories and other info of all nodes in the index. */ |
| 535 |
void btr_print_index(dict_index_t *index, /*!< in: index */ |
| 536 |
ulint width); /*!< in: print this many entries from start |
| 537 |
and end */ |
| 538 |
#endif /* UNIV_BTR_PRINT */ |
| 539 |
/** Checks the size and number of fields in a record based on the definition of |
| 540 |
the index. |
| 541 |
@return true if ok */ |
| 542 |
[[nodiscard]] bool btr_index_rec_validate( |
| 543 |
const rec_t *rec, /*!< in: index record */ |
| 544 |
const dict_index_t *index, /*!< in: index */ |
| 545 |
bool dump_on_error); /*!< in: true if the function |
| 546 |
should print hex dump of |
| 547 |
record and page on error */ |
| 548 |
/** Checks the consistency of an index tree. |
| 549 |
@return true if ok */ |
| 550 |
[[nodiscard]] bool btr_validate_index( |
| 551 |
dict_index_t *index, /*!< in: index */ |
| 552 |
const trx_t *trx, /*!< in: transaction or 0 */ |
| 553 |
bool lockout); /*!< in: true if X-latch index is intended */ |
| 554 |
|
| 555 |
/** Creates SDI index and stores the root page numbers in page 1 & 2 |
| 556 |
@param[in] space_id tablespace id |
| 557 |
@param[in] dict_locked true if dict_sys mutex is acquired |
| 558 |
@return DB_SUCCESS on success, else DB_ERROR on failure */ |
| 559 |
dberr_t btr_sdi_create_index(space_id_t space_id, bool dict_locked); |
| 560 |
|
| 561 |
constexpr uint32_t BTR_N_LEAF_PAGES = 1; |
| 562 |
constexpr uint32_t BTR_TOTAL_SIZE = 2; |
| 563 |
|
| 564 |
/** Check if the given index is empty. An index is considered empty if it |
| 565 |
has only the root page with no user records, including del-marked records. |
| 566 |
@param[in] index index |
| 567 |
@return true if index is empty, false otherwise. */ |
| 568 |
bool btr_is_index_empty(const dict_index_t *index); |
| 569 |
|
| 570 |
#include "btr0btr.ic" |
| 571 |
|
| 572 |
#endif |