| 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/page0page.ic |
| 29 |
Index page routines |
| 30 |
|
| 31 |
Created 2/2/1994 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "mach0data.h" |
| 35 |
#ifdef UNIV_DEBUG |
| 36 |
#include "log0recv.h" |
| 37 |
#endif /* !UNIV_DEBUG */ |
| 38 |
#ifndef UNIV_HOTBACKUP |
| 39 |
#include "rem0cmp.h" |
| 40 |
#endif /* !UNIV_HOTBACKUP */ |
| 41 |
#include "mtr0log.h" |
| 42 |
#include "page0zip.h" |
| 43 |
|
| 44 |
/** Gets the start of a page. |
| 45 |
@return start of the page */ |
| 46 |
static inline page_t *page_align( |
| 47 |
const void *ptr) /*!< in: pointer to page frame */ |
| 48 |
{ |
| 49 |
return ((page_t *)ut_align_down(ptr, UNIV_PAGE_SIZE)); |
| 50 |
} |
| 51 |
/** Gets the offset within a page. |
| 52 |
@return offset from the start of the page */ |
| 53 |
static inline ulint page_offset( |
| 54 |
const void *ptr) /*!< in: pointer to page frame */ |
| 55 |
{ |
| 56 |
return (ut_align_offset(ptr, UNIV_PAGE_SIZE)); |
| 57 |
} |
| 58 |
/** Returns the max trx id field value. */ |
| 59 |
static inline trx_id_t page_get_max_trx_id(const page_t *page) /*!< in: page */ |
| 60 |
{ |
| 61 |
ut_ad(page); |
| 62 |
|
| 63 |
return (mach_read_from_8(page + PAGE_HEADER + PAGE_MAX_TRX_ID)); |
| 64 |
} |
| 65 |
|
| 66 |
/** Sets the max trx id field value if trx_id is bigger than the previous |
| 67 |
value. */ |
| 68 |
static inline void page_update_max_trx_id( |
| 69 |
buf_block_t *block, /*!< in/out: page */ |
| 70 |
page_zip_des_t *page_zip, /*!< in/out: compressed page whose |
| 71 |
uncompressed part will be updated, or NULL */ |
| 72 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 73 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 74 |
{ |
| 75 |
ut_ad(block); |
| 76 |
#ifndef UNIV_HOTBACKUP |
| 77 |
ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX)); |
| 78 |
#endif /* !UNIV_HOTBACKUP */ |
| 79 |
/* During crash recovery, this function may be called on |
| 80 |
something else than a leaf page of a secondary index or the |
| 81 |
insert buffer index tree (dict_index_is_sec_or_ibuf() returns |
| 82 |
true for the dummy indexes constructed during redo log |
| 83 |
application). In that case, PAGE_MAX_TRX_ID is unused, |
| 84 |
and trx_id is usually zero. */ |
| 85 |
ut_ad(trx_id || recv_recovery_is_on()); |
| 86 |
ut_ad(page_is_leaf(buf_block_get_frame(block))); |
| 87 |
|
| 88 |
if (page_get_max_trx_id(buf_block_get_frame(block)) < trx_id) { |
| 89 |
page_set_max_trx_id(block, page_zip, trx_id, mtr); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** Returns the RTREE SPLIT SEQUENCE NUMBER (FIL_RTREE_SPLIT_SEQ_NUM). |
| 94 |
@param[in] page page |
| 95 |
@return SPLIT SEQUENCE NUMBER */ |
| 96 |
static inline node_seq_t page_get_ssn_id(const page_t *page) { |
| 97 |
ut_ad(page); |
| 98 |
|
| 99 |
return (static_cast<node_seq_t>( |
| 100 |
mach_read_from_8(page + FIL_RTREE_SPLIT_SEQ_NUM))); |
| 101 |
} |
| 102 |
|
| 103 |
/** Sets the RTREE SPLIT SEQUENCE NUMBER field value |
| 104 |
@param[in,out] block Page |
| 105 |
@param[in,out] page_zip Compressed page whose uncompressed part will |
| 106 |
be updated, or NULL |
| 107 |
@param[in] ssn_id Transaction id |
| 108 |
@param[in,out] mtr Mini-transaction */ |
| 109 |
static inline void page_set_ssn_id(buf_block_t *block, page_zip_des_t *page_zip, |
| 110 |
node_seq_t ssn_id, mtr_t *mtr) { |
| 111 |
page_t *page = buf_block_get_frame(block); |
| 112 |
#ifndef UNIV_HOTBACKUP |
| 113 |
ut_ad(!mtr || mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_SX_FIX) || |
| 114 |
mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX)); |
| 115 |
#endif /* !UNIV_HOTBACKUP */ |
| 116 |
|
| 117 |
if (page_zip) { |
| 118 |
mach_write_to_8(page + FIL_RTREE_SPLIT_SEQ_NUM, ssn_id); |
| 119 |
page_zip_write_header(page_zip, page + FIL_RTREE_SPLIT_SEQ_NUM, 8, mtr); |
| 120 |
#ifndef UNIV_HOTBACKUP |
| 121 |
} else if (mtr) { |
| 122 |
mlog_write_ull(page + FIL_RTREE_SPLIT_SEQ_NUM, ssn_id, mtr); |
| 123 |
#endif /* !UNIV_HOTBACKUP */ |
| 124 |
} else { |
| 125 |
mach_write_to_8(page + FIL_RTREE_SPLIT_SEQ_NUM, ssn_id); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** Sets the given header field. */ |
| 130 |
static inline void page_header_set_field( |
| 131 |
page_t *page, /*!< in/out: page */ |
| 132 |
page_zip_des_t *page_zip, /*!< in/out: compressed page whose |
| 133 |
uncompressed part will be updated, or NULL */ |
| 134 |
ulint field, /*!< in: PAGE_N_DIR_SLOTS, ... */ |
| 135 |
ulint val) /*!< in: value */ |
| 136 |
{ |
| 137 |
ut_ad(page); |
| 138 |
ut_ad(field <= PAGE_N_RECS); |
| 139 |
ut_ad(field == PAGE_N_HEAP || val < UNIV_PAGE_SIZE); |
| 140 |
ut_ad(field != PAGE_N_HEAP || (val & 0x7fff) < UNIV_PAGE_SIZE); |
| 141 |
|
| 142 |
mach_write_to_2(page + PAGE_HEADER + field, val); |
| 143 |
if (page_zip) { |
| 144 |
page_zip_write_header(page_zip, page + PAGE_HEADER + field, 2, nullptr); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
static inline ulint page_header_get_offs(const page_t *page, ulint field) { |
| 149 |
ulint offs; |
| 150 |
|
| 151 |
ut_ad(page); |
| 152 |
ut_ad((field == PAGE_FREE) || (field == PAGE_LAST_INSERT) || |
| 153 |
(field == PAGE_HEAP_TOP)); |
| 154 |
|
| 155 |
offs = page_header_get_field(page, field); |
| 156 |
|
| 157 |
ut_ad((field != PAGE_HEAP_TOP) || offs); |
| 158 |
|
| 159 |
return (offs); |
| 160 |
} |
| 161 |
|
| 162 |
/** Sets the pointer stored in the given header field. */ |
| 163 |
static inline void page_header_set_ptr( |
| 164 |
page_t *page, /*!< in: page */ |
| 165 |
page_zip_des_t *page_zip, /*!< in/out: compressed page whose |
| 166 |
uncompressed part will be updated, or NULL */ |
| 167 |
ulint field, /*!< in: PAGE_FREE, ... */ |
| 168 |
const byte *ptr) /*!< in: pointer or NULL*/ |
| 169 |
{ |
| 170 |
ulint offs; |
| 171 |
|
| 172 |
ut_ad(page); |
| 173 |
ut_ad((field == PAGE_FREE) || (field == PAGE_LAST_INSERT) || |
| 174 |
(field == PAGE_HEAP_TOP)); |
| 175 |
|
| 176 |
if (ptr == nullptr) { |
| 177 |
offs = 0; |
| 178 |
} else { |
| 179 |
offs = ptr - page; |
| 180 |
} |
| 181 |
|
| 182 |
ut_ad((field != PAGE_HEAP_TOP) || offs); |
| 183 |
|
| 184 |
page_header_set_field(page, page_zip, field, offs); |
| 185 |
} |
| 186 |
|
| 187 |
#ifndef UNIV_HOTBACKUP |
| 188 |
/** Resets the last insert info field in the page header. Writes to mlog |
| 189 |
about this operation. */ |
| 190 |
static inline void page_header_reset_last_insert( |
| 191 |
page_t *page, /*!< in/out: page */ |
| 192 |
page_zip_des_t *page_zip, /*!< in/out: compressed page whose |
| 193 |
uncompressed part will be updated, or NULL */ |
| 194 |
mtr_t *mtr) /*!< in: mtr */ |
| 195 |
{ |
| 196 |
ut_ad(page != nullptr); |
| 197 |
ut_ad(mtr != nullptr); |
| 198 |
|
| 199 |
if (page_zip) { |
| 200 |
mach_write_to_2(page + (PAGE_HEADER + PAGE_LAST_INSERT), 0); |
| 201 |
page_zip_write_header(page_zip, page + (PAGE_HEADER + PAGE_LAST_INSERT), 2, |
| 202 |
mtr); |
| 203 |
} else { |
| 204 |
mlog_write_ulint(page + (PAGE_HEADER + PAGE_LAST_INSERT), 0, MLOG_2BYTES, |
| 205 |
mtr); |
| 206 |
} |
| 207 |
} |
| 208 |
#endif /* !UNIV_HOTBACKUP */ |
| 209 |
|
| 210 |
/** Determine whether the page is in new-style compact format. |
| 211 |
@return nonzero if the page is in compact format, zero if it is in |
| 212 |
old-style format */ |
| 213 |
static inline bool page_is_comp(const page_t *page) /*!< in: index page */ |
| 214 |
{ |
| 215 |
return (page_header_get_field(page, PAGE_N_HEAP) & 0x8000) != 0; |
| 216 |
} |
| 217 |
|
| 218 |
/** true if the record is on a page in compact format. |
| 219 |
@return nonzero if in compact format */ |
| 220 |
static inline bool page_rec_is_comp(const rec_t *rec) /*!< in: record */ |
| 221 |
{ |
| 222 |
return (page_is_comp(page_align(rec))); |
| 223 |
} |
| 224 |
|
| 225 |
/** Returns the heap number of a record. |
| 226 |
@return heap number */ |
| 227 |
static inline ulint page_rec_get_heap_no( |
| 228 |
const rec_t *rec) /*!< in: the physical record */ |
| 229 |
{ |
| 230 |
if (page_rec_is_comp(rec)) { |
| 231 |
return (rec_get_heap_no_new(rec)); |
| 232 |
} else { |
| 233 |
return (rec_get_heap_no_old(rec)); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** Determine whether the page is a B-tree leaf. |
| 238 |
@return true if the page is a B-tree leaf (PAGE_LEVEL = 0) */ |
| 239 |
static inline bool page_is_leaf(const page_t *page) /*!< in: page */ |
| 240 |
{ |
| 241 |
return (!*(const uint16 *)(page + (PAGE_HEADER + PAGE_LEVEL))); |
| 242 |
} |
| 243 |
|
| 244 |
/** Determine whether the page is empty. |
| 245 |
@return true if the page is empty (PAGE_N_RECS = 0) */ |
| 246 |
static inline bool page_is_empty(const page_t *page) /*!< in: page */ |
| 247 |
{ |
| 248 |
return (!*(const uint16 *)(page + (PAGE_HEADER + PAGE_N_RECS))); |
| 249 |
} |
| 250 |
|
| 251 |
/** Determine whether a page is an index root page. |
| 252 |
@param[in] page page frame |
| 253 |
@return true if the page is a root page of an index */ |
| 254 |
static inline bool page_is_root(const page_t *page) { |
| 255 |
static_assert(FIL_PAGE_PREV % 8 == 0, "FIL_PAGE_PREV must be 64-bit aligned"); |
| 256 |
static_assert(FIL_PAGE_NEXT == FIL_PAGE_PREV + 4, |
| 257 |
"FIL_PAGE_NEXT must be adjacent to FIL_PAGE_PREV"); |
| 258 |
|
| 259 |
static_assert(FIL_NULL == 0xffffffff, "FIL_NULL != 0xffffffff"); |
| 260 |
|
| 261 |
/* Check that this is an index page and both the PREV and NEXT |
| 262 |
pointers are FIL_NULL, because the root page does not have any |
| 263 |
siblings. */ |
| 264 |
return (fil_page_index_page_check(page) && |
| 265 |
*reinterpret_cast<const uint64_t *>(page + FIL_PAGE_PREV) == |
| 266 |
std::numeric_limits<uint64_t>::max()); |
| 267 |
} |
| 268 |
|
| 269 |
/** Determine whether the page contains garbage. |
| 270 |
@return true if the page contains garbage (PAGE_GARBAGE is not 0) */ |
| 271 |
static inline bool page_has_garbage(const page_t *page) /*!< in: page */ |
| 272 |
{ |
| 273 |
return (!!*(const uint16 *)(page + (PAGE_HEADER + PAGE_GARBAGE))); |
| 274 |
} |
| 275 |
|
| 276 |
/** Gets the offset of the first record on the page. |
| 277 |
@return offset of the first record in record list, relative from page */ |
| 278 |
static inline ulint page_get_infimum_offset( |
| 279 |
const page_t *page) /*!< in: page which must have record(s) */ |
| 280 |
{ |
| 281 |
ut_ad(page); |
| 282 |
ut_ad(!page_offset(page)); |
| 283 |
|
| 284 |
if (page_is_comp(page)) { |
| 285 |
return (PAGE_NEW_INFIMUM); |
| 286 |
} else { |
| 287 |
return (PAGE_OLD_INFIMUM); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** Gets the offset of the last record on the page. |
| 292 |
@return offset of the last record in record list, relative from page */ |
| 293 |
static inline ulint page_get_supremum_offset( |
| 294 |
const page_t *page) /*!< in: page which must have record(s) */ |
| 295 |
{ |
| 296 |
ut_ad(page); |
| 297 |
ut_ad(!page_offset(page)); |
| 298 |
|
| 299 |
if (page_is_comp(page)) { |
| 300 |
return (PAGE_NEW_SUPREMUM); |
| 301 |
} else { |
| 302 |
return (PAGE_OLD_SUPREMUM); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/** true if the record is a user record on the page. |
| 307 |
@return true if a user record */ |
| 308 |
static inline bool page_rec_is_user_rec_low( |
| 309 |
ulint offset) /*!< in: record offset on page */ |
| 310 |
{ |
| 311 |
ut_ad(offset >= PAGE_NEW_INFIMUM); |
| 312 |
|
| 313 |
static_assert(PAGE_OLD_INFIMUM >= PAGE_NEW_INFIMUM, |
| 314 |
"PAGE_OLD_INFIMUM < PAGE_NEW_INFIMUM"); |
| 315 |
|
| 316 |
static_assert(PAGE_OLD_SUPREMUM >= PAGE_NEW_SUPREMUM, |
| 317 |
"PAGE_OLD_SUPREMUM < PAGE_NEW_SUPREMUM"); |
| 318 |
|
| 319 |
static_assert(PAGE_NEW_INFIMUM <= PAGE_OLD_SUPREMUM, |
| 320 |
"PAGE_NEW_INFIMUM > PAGE_OLD_SUPREMUM"); |
| 321 |
|
| 322 |
static_assert(PAGE_OLD_INFIMUM <= PAGE_NEW_SUPREMUM, |
| 323 |
"PAGE_OLD_INFIMUM > PAGE_NEW_SUPREMUM"); |
| 324 |
|
| 325 |
static_assert(PAGE_NEW_SUPREMUM <= PAGE_OLD_SUPREMUM_END, |
| 326 |
"PAGE_NEW_SUPREMUM > PAGE_OLD_SUPREMUM_END"); |
| 327 |
|
| 328 |
static_assert(PAGE_OLD_SUPREMUM <= PAGE_NEW_SUPREMUM_END, |
| 329 |
"PAGE_OLD_SUPREMUM > PAGE_NEW_SUPREMUM_END"); |
| 330 |
|
| 331 |
ut_ad(offset <= UNIV_PAGE_SIZE - PAGE_EMPTY_DIR_START); |
| 332 |
|
| 333 |
return (offset != PAGE_NEW_SUPREMUM && offset != PAGE_NEW_INFIMUM && |
| 334 |
offset != PAGE_OLD_INFIMUM && offset != PAGE_OLD_SUPREMUM); |
| 335 |
} |
| 336 |
|
| 337 |
/** true if the record is the supremum record on a page. |
| 338 |
@return true if the supremum record */ |
| 339 |
static inline bool page_rec_is_supremum_low( |
| 340 |
ulint offset) /*!< in: record offset on page */ |
| 341 |
{ |
| 342 |
ut_ad(offset >= PAGE_NEW_INFIMUM); |
| 343 |
ut_ad(offset <= UNIV_PAGE_SIZE - PAGE_EMPTY_DIR_START); |
| 344 |
|
| 345 |
return (offset == PAGE_NEW_SUPREMUM || offset == PAGE_OLD_SUPREMUM); |
| 346 |
} |
| 347 |
|
| 348 |
/** true if the record is the infimum record on a page. |
| 349 |
@return true if the infimum record */ |
| 350 |
static inline bool page_rec_is_infimum_low( |
| 351 |
ulint offset) /*!< in: record offset on page */ |
| 352 |
{ |
| 353 |
ut_ad(offset >= PAGE_NEW_INFIMUM); |
| 354 |
ut_ad(offset <= UNIV_PAGE_SIZE - PAGE_EMPTY_DIR_START); |
| 355 |
|
| 356 |
return (offset == PAGE_NEW_INFIMUM || offset == PAGE_OLD_INFIMUM); |
| 357 |
} |
| 358 |
|
| 359 |
/** true if the record is a user record on the page. |
| 360 |
@return true if a user record */ |
| 361 |
static inline bool page_rec_is_user_rec(const rec_t *rec) /*!< in: record */ |
| 362 |
{ |
| 363 |
ut_ad(page_rec_check(rec)); |
| 364 |
|
| 365 |
return (page_rec_is_user_rec_low(page_offset(rec))); |
| 366 |
} |
| 367 |
|
| 368 |
/** true if the record is the supremum record on a page. |
| 369 |
@return true if the supremum record */ |
| 370 |
static inline bool page_rec_is_supremum(const rec_t *rec) /*!< in: record */ |
| 371 |
{ |
| 372 |
ut_ad(page_rec_check(rec)); |
| 373 |
|
| 374 |
return (page_rec_is_supremum_low(page_offset(rec))); |
| 375 |
} |
| 376 |
|
| 377 |
/** true if the record is the infimum record on a page. |
| 378 |
@return true if the infimum record */ |
| 379 |
static inline bool page_rec_is_infimum(const rec_t *rec) /*!< in: record */ |
| 380 |
{ |
| 381 |
ut_ad(page_rec_check(rec)); |
| 382 |
|
| 383 |
return (page_rec_is_infimum_low(page_offset(rec))); |
| 384 |
} |
| 385 |
|
| 386 |
static inline bool page_rec_is_first(const rec_t *rec, const page_t *page) { |
| 387 |
ut_ad(page_get_n_recs(page) > 0); |
| 388 |
|
| 389 |
return (page_rec_get_next_const(page_get_infimum_rec(page)) == rec); |
| 390 |
} |
| 391 |
|
| 392 |
static inline bool page_rec_is_second(const rec_t *rec, const page_t *page) { |
| 393 |
ut_ad(page_get_n_recs(page) > 1); |
| 394 |
|
| 395 |
return (page_rec_get_next_const( |
| 396 |
page_rec_get_next_const(page_get_infimum_rec(page))) == rec); |
| 397 |
} |
| 398 |
|
| 399 |
static inline bool page_rec_is_last(const rec_t *rec, const page_t *page) { |
| 400 |
ut_ad(page_get_n_recs(page) > 0); |
| 401 |
|
| 402 |
return (page_rec_get_next_const(rec) == page_get_supremum_rec(page)); |
| 403 |
} |
| 404 |
|
| 405 |
static inline bool page_rec_distance_is_at_most(const rec_t *left_rec, |
| 406 |
const rec_t *right_rec, |
| 407 |
ulint val) { |
| 408 |
for (ulint i = 0; i <= val; i++) { |
| 409 |
if (left_rec == right_rec) { |
| 410 |
return (true); |
| 411 |
} |
| 412 |
left_rec = page_rec_get_next_const(left_rec); |
| 413 |
} |
| 414 |
return (false); |
| 415 |
} |
| 416 |
|
| 417 |
static inline bool page_rec_is_second_last(const rec_t *rec, |
| 418 |
const page_t *page) { |
| 419 |
ut_ad(page_get_n_recs(page) > 1); |
| 420 |
ut_ad(!page_rec_is_last(rec, page)); |
| 421 |
|
| 422 |
return (page_rec_get_next_const(page_rec_get_next_const(rec)) == |
| 423 |
page_get_supremum_rec(page)); |
| 424 |
} |
| 425 |
|
| 426 |
/** Returns the nth record of the record list. |
| 427 |
This is the inverse function of page_rec_get_n_recs_before(). |
| 428 |
@return nth record */ |
| 429 |
static inline rec_t *page_rec_get_nth(page_t *page, /*!< in: page */ |
| 430 |
ulint nth) /*!< in: nth record */ |
| 431 |
{ |
| 432 |
return ((rec_t *)page_rec_get_nth_const(page, nth)); |
| 433 |
} |
| 434 |
|
| 435 |
#ifndef UNIV_HOTBACKUP |
| 436 |
/** Returns the middle record of the records on the page. If there is an |
| 437 |
even number of records in the list, returns the first record of the |
| 438 |
upper half-list. |
| 439 |
@return middle record */ |
| 440 |
static inline rec_t *page_get_middle_rec(page_t *page) /*!< in: page */ |
| 441 |
{ |
| 442 |
ulint middle = (page_get_n_recs(page) + PAGE_HEAP_NO_USER_LOW) / 2; |
| 443 |
|
| 444 |
return (page_rec_get_nth(page, middle)); |
| 445 |
} |
| 446 |
#endif /* !UNIV_HOTBACKUP */ |
| 447 |
|
| 448 |
/** Gets the page number. |
| 449 |
@return page number */ |
| 450 |
static inline page_no_t page_get_page_no(const page_t *page) /*!< in: page */ |
| 451 |
{ |
| 452 |
ut_ad(page == page_align((page_t *)page)); |
| 453 |
return (mach_read_from_4(page + FIL_PAGE_OFFSET)); |
| 454 |
} |
| 455 |
|
| 456 |
/** Gets the tablespace identifier. |
| 457 |
@return space id */ |
| 458 |
static inline space_id_t page_get_space_id(const page_t *page) /*!< in: page */ |
| 459 |
{ |
| 460 |
ut_ad(page == page_align((page_t *)page)); |
| 461 |
return (mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID)); |
| 462 |
} |
| 463 |
|
| 464 |
static inline page_id_t page_get_page_id(const page_t *page) { |
| 465 |
return page_id_t{page_get_space_id(page), page_get_page_no(page)}; |
| 466 |
} |
| 467 |
|
| 468 |
/** Gets the number of user records on page (infimum and supremum records |
| 469 |
are not user records). |
| 470 |
@return number of user records */ |
| 471 |
static inline ulint page_get_n_recs(const page_t *page) /*!< in: index page */ |
| 472 |
{ |
| 473 |
return (page_header_get_field(page, PAGE_N_RECS)); |
| 474 |
} |
| 475 |
|
| 476 |
/** Gets the number of dir slots in directory. |
| 477 |
@return number of slots */ |
| 478 |
static inline ulint page_dir_get_n_slots( |
| 479 |
const page_t *page) /*!< in: index page */ |
| 480 |
{ |
| 481 |
return (page_header_get_field(page, PAGE_N_DIR_SLOTS)); |
| 482 |
} |
| 483 |
/** Sets the number of dir slots in directory. */ |
| 484 |
static inline void page_dir_set_n_slots( |
| 485 |
page_t *page, /*!< in/out: page */ |
| 486 |
page_zip_des_t *page_zip, /*!< in/out: compressed page whose |
| 487 |
uncompressed part will be updated, or NULL */ |
| 488 |
ulint n_slots) /*!< in: number of slots */ |
| 489 |
{ |
| 490 |
page_header_set_field(page, page_zip, PAGE_N_DIR_SLOTS, n_slots); |
| 491 |
} |
| 492 |
|
| 493 |
/** Sets the number of records in the heap. */ |
| 494 |
static inline void page_dir_set_n_heap( |
| 495 |
page_t *page, /*!< in/out: index page */ |
| 496 |
page_zip_des_t *page_zip, /*!< in/out: compressed page whose |
| 497 |
uncompressed part will be updated, or NULL. |
| 498 |
Note that the size of the dense page directory |
| 499 |
in the compressed page trailer is |
| 500 |
n_heap * PAGE_ZIP_DIR_SLOT_SIZE. */ |
| 501 |
ulint n_heap) /*!< in: number of records */ |
| 502 |
{ |
| 503 |
ut_ad(n_heap < 0x8000); |
| 504 |
ut_ad(!page_zip || |
| 505 |
n_heap == (page_header_get_field(page, PAGE_N_HEAP) & 0x7fffU) + 1); |
| 506 |
|
| 507 |
page_header_set_field( |
| 508 |
page, page_zip, PAGE_N_HEAP, |
| 509 |
n_heap | (0x8000 & page_header_get_field(page, PAGE_N_HEAP))); |
| 510 |
} |
| 511 |
|
| 512 |
#ifdef UNIV_DEBUG |
| 513 |
/** Gets pointer to nth directory slot. |
| 514 |
@return pointer to dir slot */ |
| 515 |
static inline page_dir_slot_t *page_dir_get_nth_slot( |
| 516 |
const page_t *page, /*!< in: index page */ |
| 517 |
ulint n) /*!< in: position */ |
| 518 |
{ |
| 519 |
ut_ad(page_dir_get_n_slots(page) > n); |
| 520 |
|
| 521 |
return ((page_dir_slot_t *)page + UNIV_PAGE_SIZE - PAGE_DIR - |
| 522 |
(n + 1) * PAGE_DIR_SLOT_SIZE); |
| 523 |
} |
| 524 |
#endif /* UNIV_DEBUG */ |
| 525 |
|
| 526 |
/** Used to check the consistency of a record on a page. |
| 527 |
@return true if succeed */ |
| 528 |
static inline bool page_rec_check(const rec_t *rec) /*!< in: record */ |
| 529 |
{ |
| 530 |
const page_t *page = page_align(rec); |
| 531 |
|
| 532 |
ut_a(rec); |
| 533 |
|
| 534 |
ut_a(page_offset(rec) <= page_header_get_field(page, PAGE_HEAP_TOP)); |
| 535 |
ut_a(page_offset(rec) >= PAGE_DATA); |
| 536 |
|
| 537 |
return true; |
| 538 |
} |
| 539 |
|
| 540 |
/** Gets the record pointed to by a directory slot. |
| 541 |
@return pointer to record */ |
| 542 |
static inline const rec_t *page_dir_slot_get_rec( |
| 543 |
const page_dir_slot_t *slot) /*!< in: directory slot */ |
| 544 |
{ |
| 545 |
return (page_align(slot) + mach_read_from_2(slot)); |
| 546 |
} |
| 547 |
|
| 548 |
/** This is used to set the record offset in a directory slot. */ |
| 549 |
static inline void page_dir_slot_set_rec( |
| 550 |
page_dir_slot_t *slot, /*!< in: directory slot */ |
| 551 |
rec_t *rec) /*!< in: record on the page */ |
| 552 |
{ |
| 553 |
ut_ad(page_rec_check(rec)); |
| 554 |
|
| 555 |
mach_write_to_2(slot, page_offset(rec)); |
| 556 |
} |
| 557 |
|
| 558 |
/** Gets the number of records owned by a directory slot. |
| 559 |
@return number of records */ |
| 560 |
static inline ulint page_dir_slot_get_n_owned( |
| 561 |
const page_dir_slot_t *slot) /*!< in: page directory slot */ |
| 562 |
{ |
| 563 |
const rec_t *rec = page_dir_slot_get_rec(slot); |
| 564 |
if (page_rec_is_comp(slot)) { |
| 565 |
return (rec_get_n_owned_new(rec)); |
| 566 |
} else { |
| 567 |
return (rec_get_n_owned_old(rec)); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
/** This is used to set the owned records field of a directory slot. */ |
| 572 |
static inline void page_dir_slot_set_n_owned( |
| 573 |
page_dir_slot_t *slot, /*!< in/out: directory slot */ |
| 574 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ |
| 575 |
ulint n) /*!< in: number of records owned by the slot */ |
| 576 |
{ |
| 577 |
rec_t *rec = (rec_t *)page_dir_slot_get_rec(slot); |
| 578 |
if (page_rec_is_comp(slot)) { |
| 579 |
rec_set_n_owned_new(rec, page_zip, n); |
| 580 |
} else { |
| 581 |
ut_ad(!page_zip); |
| 582 |
rec_set_n_owned_old(rec, n); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
/** Calculates the space reserved for directory slots of a given number of |
| 587 |
records. The exact value is a fraction number n * PAGE_DIR_SLOT_SIZE / |
| 588 |
PAGE_DIR_SLOT_MIN_N_OWNED, and it is rounded upwards to an integer. */ |
| 589 |
static inline ulint page_dir_calc_reserved_space( |
| 590 |
ulint n_recs) /*!< in: number of records */ |
| 591 |
{ |
| 592 |
return ((PAGE_DIR_SLOT_SIZE * n_recs + PAGE_DIR_SLOT_MIN_N_OWNED - 1) / |
| 593 |
PAGE_DIR_SLOT_MIN_N_OWNED); |
| 594 |
} |
| 595 |
|
| 596 |
/** Gets the pointer to the next record on the page. |
| 597 |
@return pointer to next record */ |
| 598 |
static inline const rec_t *page_rec_get_next_low( |
| 599 |
const rec_t *rec, /*!< in: pointer to record */ |
| 600 |
ulint comp) /*!< in: nonzero=compact page layout */ |
| 601 |
{ |
| 602 |
ulint offs; |
| 603 |
const page_t *page; |
| 604 |
|
| 605 |
ut_ad(page_rec_check(rec)); |
| 606 |
|
| 607 |
page = page_align(rec); |
| 608 |
|
| 609 |
offs = rec_get_next_offs(rec, comp); |
| 610 |
|
| 611 |
if (offs >= UNIV_PAGE_SIZE) { |
| 612 |
fprintf(stderr, |
| 613 |
"InnoDB: Next record offset is nonsensical %lu" |
| 614 |
" in record at offset %lu\n" |
| 615 |
"InnoDB: rec address %p, space id %lu, page %lu\n", |
| 616 |
(ulong)offs, (ulong)page_offset(rec), (void *)rec, |
| 617 |
(ulong)page_get_space_id(page), (ulong)page_get_page_no(page)); |
| 618 |
ut_error; |
| 619 |
} else if (offs == 0) { |
| 620 |
return (nullptr); |
| 621 |
} |
| 622 |
|
| 623 |
return (page + offs); |
| 624 |
} |
| 625 |
|
| 626 |
/** Gets the pointer to the next record on the page. |
| 627 |
@return pointer to next record */ |
| 628 |
static inline rec_t *page_rec_get_next(rec_t *rec) /*!< in: pointer to record */ |
| 629 |
{ |
| 630 |
return ((rec_t *)page_rec_get_next_low(rec, page_rec_is_comp(rec))); |
| 631 |
} |
| 632 |
|
| 633 |
/** Gets the pointer to the next record on the page. |
| 634 |
@return pointer to next record */ |
| 635 |
static inline const rec_t *page_rec_get_next_const( |
| 636 |
const rec_t *rec) /*!< in: pointer to record */ |
| 637 |
{ |
| 638 |
return (page_rec_get_next_low(rec, page_rec_is_comp(rec))); |
| 639 |
} |
| 640 |
|
| 641 |
/** Gets the pointer to the next non delete-marked record on the page. |
| 642 |
If all subsequent records are delete-marked, then this function |
| 643 |
will return the supremum record. |
| 644 |
@return pointer to next non delete-marked record or pointer to supremum */ |
| 645 |
static inline const rec_t *page_rec_get_next_non_del_marked( |
| 646 |
const rec_t *rec) /*!< in: pointer to record */ |
| 647 |
{ |
| 648 |
const rec_t *r; |
| 649 |
ulint page_is_compact = page_rec_is_comp(rec); |
| 650 |
|
| 651 |
for (r = page_rec_get_next_const(rec); |
| 652 |
!page_rec_is_supremum(r) && rec_get_deleted_flag(r, page_is_compact); |
| 653 |
r = page_rec_get_next_const(r)) { |
| 654 |
/* noop */ |
| 655 |
} |
| 656 |
|
| 657 |
return (r); |
| 658 |
} |
| 659 |
|
| 660 |
/** Sets the pointer to the next record on the page. */ |
| 661 |
static inline void page_rec_set_next( |
| 662 |
rec_t *rec, /*!< in: pointer to record, |
| 663 |
must not be page supremum */ |
| 664 |
const rec_t *next) /*!< in: pointer to next record, |
| 665 |
must not be page infimum */ |
| 666 |
{ |
| 667 |
ulint offs; |
| 668 |
|
| 669 |
ut_ad(page_rec_check(rec)); |
| 670 |
ut_ad(!page_rec_is_supremum(rec)); |
| 671 |
ut_ad(rec != next); |
| 672 |
|
| 673 |
ut_ad(!next || !page_rec_is_infimum(next)); |
| 674 |
ut_ad(!next || page_align(rec) == page_align(next)); |
| 675 |
|
| 676 |
offs = next != nullptr ? page_offset(next) : 0; |
| 677 |
|
| 678 |
if (page_rec_is_comp(rec)) { |
| 679 |
rec_set_next_offs_new(rec, offs); |
| 680 |
} else { |
| 681 |
rec_set_next_offs_old(rec, offs); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
/** Gets the pointer to the previous record. |
| 686 |
@return pointer to previous record */ |
| 687 |
static inline const rec_t *page_rec_get_prev_const( |
| 688 |
const rec_t *rec) /*!< in: pointer to record, must not be page |
| 689 |
infimum */ |
| 690 |
{ |
| 691 |
const page_dir_slot_t *slot; |
| 692 |
ulint slot_no; |
| 693 |
const rec_t *rec2; |
| 694 |
const rec_t *prev_rec = nullptr; |
| 695 |
const page_t *page; |
| 696 |
|
| 697 |
ut_ad(page_rec_check(rec)); |
| 698 |
|
| 699 |
page = page_align(rec); |
| 700 |
|
| 701 |
ut_ad(!page_rec_is_infimum(rec)); |
| 702 |
|
| 703 |
slot_no = page_dir_find_owner_slot(rec); |
| 704 |
|
| 705 |
ut_a(slot_no != 0); |
| 706 |
|
| 707 |
slot = page_dir_get_nth_slot(page, slot_no - 1); |
| 708 |
|
| 709 |
rec2 = page_dir_slot_get_rec(slot); |
| 710 |
|
| 711 |
if (page_is_comp(page)) { |
| 712 |
while (rec != rec2) { |
| 713 |
prev_rec = rec2; |
| 714 |
rec2 = page_rec_get_next_low(rec2, true); |
| 715 |
} |
| 716 |
} else { |
| 717 |
while (rec != rec2) { |
| 718 |
prev_rec = rec2; |
| 719 |
rec2 = page_rec_get_next_low(rec2, false); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
ut_a(prev_rec); |
| 724 |
|
| 725 |
return (prev_rec); |
| 726 |
} |
| 727 |
|
| 728 |
/** Gets the pointer to the previous record. |
| 729 |
@return pointer to previous record */ |
| 730 |
static inline rec_t *page_rec_get_prev(rec_t *rec) /*!< in: pointer to record, |
| 731 |
must not be page infimum */ |
| 732 |
{ |
| 733 |
return ((rec_t *)page_rec_get_prev_const(rec)); |
| 734 |
} |
| 735 |
|
| 736 |
/** Looks for the record which owns the given record. |
| 737 |
@return the owner record */ |
| 738 |
static inline rec_t *page_rec_find_owner_rec( |
| 739 |
rec_t *rec) /*!< in: the physical record */ |
| 740 |
{ |
| 741 |
ut_ad(page_rec_check(rec)); |
| 742 |
|
| 743 |
if (page_rec_is_comp(rec)) { |
| 744 |
while (rec_get_n_owned_new(rec) == 0) { |
| 745 |
rec = page_rec_get_next(rec); |
| 746 |
} |
| 747 |
} else { |
| 748 |
while (rec_get_n_owned_old(rec) == 0) { |
| 749 |
rec = page_rec_get_next(rec); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
return (rec); |
| 754 |
} |
| 755 |
|
| 756 |
/** Returns the base extra size of a physical record. This is the |
| 757 |
size of the fixed header, independent of the record size. |
| 758 |
@return REC_N_NEW_EXTRA_BYTES or REC_N_OLD_EXTRA_BYTES */ |
| 759 |
static inline ulint page_rec_get_base_extra_size( |
| 760 |
const rec_t *rec) /*!< in: physical record */ |
| 761 |
{ |
| 762 |
static_assert(REC_N_NEW_EXTRA_BYTES + 1 == REC_N_OLD_EXTRA_BYTES, |
| 763 |
"REC_N_NEW_EXTRA_BYTES + 1 != REC_N_OLD_EXTRA_BYTES"); |
| 764 |
return (REC_N_NEW_EXTRA_BYTES + (ulint)!page_rec_is_comp(rec)); |
| 765 |
} |
| 766 |
|
| 767 |
/** Returns the sum of the sizes of the records in the record list, excluding |
| 768 |
the infimum and supremum records. |
| 769 |
@return data in bytes */ |
| 770 |
static inline ulint page_get_data_size( |
| 771 |
const page_t *page) /*!< in: index page */ |
| 772 |
{ |
| 773 |
ulint ret; |
| 774 |
|
| 775 |
ret = (ulint)(page_header_get_field(page, PAGE_HEAP_TOP) - |
| 776 |
(page_is_comp(page) ? PAGE_NEW_SUPREMUM_END |
| 777 |
: PAGE_OLD_SUPREMUM_END) - |
| 778 |
page_header_get_field(page, PAGE_GARBAGE)); |
| 779 |
|
| 780 |
ut_ad(ret < UNIV_PAGE_SIZE); |
| 781 |
|
| 782 |
return (ret); |
| 783 |
} |
| 784 |
|
| 785 |
/** Allocates a block of memory from the free list of an index page. */ |
| 786 |
static inline void page_mem_alloc_free( |
| 787 |
page_t *page, /*!< in/out: index page */ |
| 788 |
page_zip_des_t *page_zip, /*!< in/out: compressed page with enough |
| 789 |
space available for inserting the record, |
| 790 |
or NULL */ |
| 791 |
rec_t *next_rec, /*!< in: pointer to the new head of the |
| 792 |
free record list */ |
| 793 |
ulint need) /*!< in: number of bytes allocated */ |
| 794 |
{ |
| 795 |
ulint garbage; |
| 796 |
|
| 797 |
#ifdef UNIV_DEBUG |
| 798 |
const rec_t *old_rec = page_header_get_ptr(page, PAGE_FREE); |
| 799 |
ulint next_offs; |
| 800 |
|
| 801 |
ut_ad(old_rec); |
| 802 |
next_offs = rec_get_next_offs(old_rec, page_is_comp(page)); |
| 803 |
ut_ad(next_rec == (next_offs ? page + next_offs : nullptr)); |
| 804 |
#endif |
| 805 |
|
| 806 |
page_header_set_ptr(page, page_zip, PAGE_FREE, next_rec); |
| 807 |
|
| 808 |
garbage = page_header_get_field(page, PAGE_GARBAGE); |
| 809 |
ut_ad(garbage >= need); |
| 810 |
|
| 811 |
page_header_set_field(page, page_zip, PAGE_GARBAGE, garbage - need); |
| 812 |
} |
| 813 |
|
| 814 |
/** Calculates free space if a page is emptied. |
| 815 |
@return free space */ |
| 816 |
static inline ulint page_get_free_space_of_empty( |
| 817 |
bool comp) /*!< in: nonzero=compact page layout */ |
| 818 |
{ |
| 819 |
if (comp) { |
| 820 |
return ((ulint)(UNIV_PAGE_SIZE - PAGE_NEW_SUPREMUM_END - PAGE_DIR - |
| 821 |
2 * PAGE_DIR_SLOT_SIZE)); |
| 822 |
} |
| 823 |
|
| 824 |
return ((ulint)(UNIV_PAGE_SIZE - PAGE_OLD_SUPREMUM_END - PAGE_DIR - |
| 825 |
2 * PAGE_DIR_SLOT_SIZE)); |
| 826 |
} |
| 827 |
|
| 828 |
/** Each user record on a page, and also the deleted user records in the heap |
| 829 |
takes its size plus the fraction of the dir cell size / |
| 830 |
PAGE_DIR_SLOT_MIN_N_OWNED bytes for it. If the sum of these exceeds the |
| 831 |
value of page_get_free_space_of_empty, the insert is impossible, otherwise |
| 832 |
it is allowed. This function returns the maximum combined size of records |
| 833 |
which can be inserted on top of the record heap. |
| 834 |
@return maximum combined size for inserted records */ |
| 835 |
static inline ulint page_get_max_insert_size( |
| 836 |
const page_t *page, /*!< in: index page */ |
| 837 |
ulint n_recs) /*!< in: number of records */ |
| 838 |
{ |
| 839 |
ulint occupied; |
| 840 |
ulint free_space; |
| 841 |
|
| 842 |
if (page_is_comp(page)) { |
| 843 |
occupied = |
| 844 |
page_header_get_field(page, PAGE_HEAP_TOP) - PAGE_NEW_SUPREMUM_END + |
| 845 |
page_dir_calc_reserved_space(n_recs + page_dir_get_n_heap(page) - 2); |
| 846 |
|
| 847 |
free_space = page_get_free_space_of_empty(true); |
| 848 |
} else { |
| 849 |
occupied = |
| 850 |
page_header_get_field(page, PAGE_HEAP_TOP) - PAGE_OLD_SUPREMUM_END + |
| 851 |
page_dir_calc_reserved_space(n_recs + page_dir_get_n_heap(page) - 2); |
| 852 |
|
| 853 |
free_space = page_get_free_space_of_empty(false); |
| 854 |
} |
| 855 |
|
| 856 |
/* Above the 'n_recs +' part reserves directory space for the new |
| 857 |
inserted records; the '- 2' excludes page infimum and supremum |
| 858 |
records */ |
| 859 |
|
| 860 |
if (occupied > free_space) { |
| 861 |
return (0); |
| 862 |
} |
| 863 |
|
| 864 |
return (free_space - occupied); |
| 865 |
} |
| 866 |
|
| 867 |
/** Returns the maximum combined size of records which can be inserted on top |
| 868 |
of the record heap if a page is first reorganized. |
| 869 |
@return maximum combined size for inserted records */ |
| 870 |
static inline ulint page_get_max_insert_size_after_reorganize( |
| 871 |
const page_t *page, /*!< in: index page */ |
| 872 |
ulint n_recs) /*!< in: number of records */ |
| 873 |
{ |
| 874 |
ulint occupied; |
| 875 |
ulint free_space; |
| 876 |
|
| 877 |
occupied = page_get_data_size(page) + |
| 878 |
page_dir_calc_reserved_space(n_recs + page_get_n_recs(page)); |
| 879 |
|
| 880 |
free_space = page_get_free_space_of_empty(page_is_comp(page)); |
| 881 |
|
| 882 |
if (occupied > free_space) { |
| 883 |
return (0); |
| 884 |
} |
| 885 |
|
| 886 |
return (free_space - occupied); |
| 887 |
} |
| 888 |
|
| 889 |
/** Puts a record to free list. */ |
| 890 |
static inline void page_mem_free( |
| 891 |
page_t *page, /*!< in/out: index page */ |
| 892 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, |
| 893 |
or NULL */ |
| 894 |
rec_t *rec, /*!< in: pointer to the |
| 895 |
(origin of) record */ |
| 896 |
const dict_index_t *index, /*!< in: index of rec */ |
| 897 |
const ulint *offsets) /*!< in: array returned by |
| 898 |
rec_get_offsets() */ |
| 899 |
{ |
| 900 |
rec_t *free; |
| 901 |
ulint garbage; |
| 902 |
|
| 903 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 904 |
free = page_header_get_ptr(page, PAGE_FREE); |
| 905 |
|
| 906 |
page_rec_set_next(rec, free); |
| 907 |
page_header_set_ptr(page, page_zip, PAGE_FREE, rec); |
| 908 |
|
| 909 |
garbage = page_header_get_field(page, PAGE_GARBAGE); |
| 910 |
|
| 911 |
page_header_set_field(page, page_zip, PAGE_GARBAGE, |
| 912 |
garbage + rec_offs_size(offsets)); |
| 913 |
|
| 914 |
if (page_zip) { |
| 915 |
page_zip_dir_delete(page_zip, rec, index, offsets, free); |
| 916 |
} else { |
| 917 |
page_header_set_field(page, page_zip, PAGE_N_RECS, |
| 918 |
page_get_n_recs(page) - 1); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/** Check that a page_size is correct for InnoDB. |
| 923 |
If correct, set the associated page_size_shift which is the power of 2 |
| 924 |
for this page size. |
| 925 |
@param[in] page_size Page Size to evaluate |
| 926 |
@return an associated page_size_shift if valid, 0 if invalid. */ |
| 927 |
inline ulong page_size_validate(ulong page_size) { |
| 928 |
for (ulong n = UNIV_PAGE_SIZE_SHIFT_MIN; n <= UNIV_PAGE_SIZE_SHIFT_MAX; n++) { |
| 929 |
if (page_size == static_cast<ulong>(1 << n)) { |
| 930 |
return (n); |
| 931 |
} |
| 932 |
} |
| 933 |
return (0); |
| 934 |
} |