| 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 |
#include <stddef.h> |
| 29 |
#include <sys/types.h> |
| 30 |
|
| 31 |
/** @file include/page0page.h |
| 32 |
Index page routines |
| 33 |
|
| 34 |
Created 2/2/1994 Heikki Tuuri |
| 35 |
*******************************************************/ |
| 36 |
|
| 37 |
#ifndef page0page_h |
| 38 |
#define page0page_h |
| 39 |
|
| 40 |
#include "univ.i" |
| 41 |
|
| 42 |
#include "buf0buf.h" |
| 43 |
#include "data0data.h" |
| 44 |
#include "dict0dict.h" |
| 45 |
#include "fil0fil.h" |
| 46 |
#include "fsp0fsp.h" |
| 47 |
#include "mtr0mtr.h" |
| 48 |
#include "page0types.h" |
| 49 |
#include "rem0rec.h" |
| 50 |
|
| 51 |
/* PAGE DIRECTORY |
| 52 |
============== |
| 53 |
*/ |
| 54 |
|
| 55 |
typedef byte page_dir_slot_t; |
| 56 |
typedef page_dir_slot_t page_dir_t; |
| 57 |
|
| 58 |
/* Offset of the directory start down from the page end. We call the |
| 59 |
slot with the highest file address directory start, as it points to |
| 60 |
the first record in the list of records. */ |
| 61 |
constexpr uint32_t PAGE_DIR = FIL_PAGE_DATA_END; |
| 62 |
|
| 63 |
/* We define a slot in the page directory as two bytes */ |
| 64 |
constexpr uint32_t PAGE_DIR_SLOT_SIZE = 2; |
| 65 |
|
| 66 |
/* The offset of the physically lower end of the directory, counted from |
| 67 |
page end, when the page is empty */ |
| 68 |
constexpr uint32_t PAGE_EMPTY_DIR_START = PAGE_DIR + 2 * PAGE_DIR_SLOT_SIZE; |
| 69 |
|
| 70 |
/* The maximum and minimum number of records owned by a directory slot. The |
| 71 |
number may drop below the minimum in the first and the last slot in the |
| 72 |
directory. */ |
| 73 |
constexpr uint32_t PAGE_DIR_SLOT_MAX_N_OWNED = 8; |
| 74 |
constexpr uint32_t PAGE_DIR_SLOT_MIN_N_OWNED = 4; |
| 75 |
|
| 76 |
/* The infimum and supremum records are omitted from the compressed page. |
| 77 |
On compress, we compare that the records are there, and on uncompress we |
| 78 |
restore the records. */ |
| 79 |
/** Extra bytes of an infimum record */ |
| 80 |
static const byte infimum_extra[] = { |
| 81 |
0x01, /* info_bits=0, n_owned=1 */ |
| 82 |
0x00, 0x02 /* heap_no=0, status=2 */ |
| 83 |
/* ?, ? */ /* next=(first user rec, or supremum) */ |
| 84 |
}; |
| 85 |
/** Data bytes of an infimum record */ |
| 86 |
static const byte infimum_data[] = { |
| 87 |
0x69, 0x6e, 0x66, 0x69, 0x6d, 0x75, 0x6d, 0x00 /* "infimum\0" */ |
| 88 |
}; |
| 89 |
/** Extra bytes and data bytes of a supremum record */ |
| 90 |
static const byte supremum_extra_data[] = { |
| 91 |
/* 0x0?, */ /* info_bits=0, n_owned=1..8 */ |
| 92 |
0x00, |
| 93 |
0x0b, /* heap_no=1, status=3 */ |
| 94 |
0x00, |
| 95 |
0x00, /* next=0 */ |
| 96 |
0x73, |
| 97 |
0x75, |
| 98 |
0x70, |
| 99 |
0x72, |
| 100 |
0x65, |
| 101 |
0x6d, |
| 102 |
0x75, |
| 103 |
0x6d /* "supremum" */ |
| 104 |
}; |
| 105 |
|
| 106 |
/** Gets the start of a page. |
| 107 |
@param[in] ptr pointer to page frame |
| 108 |
@return start of the page */ |
| 109 |
static inline page_t *page_align(const void *ptr); |
| 110 |
|
| 111 |
/** Gets the offset within a page. |
| 112 |
@param[in] ptr pointer to page frame |
| 113 |
@return offset from the start of the page */ |
| 114 |
static inline ulint page_offset(const void *ptr); |
| 115 |
|
| 116 |
/** Returns the max trx id field value. */ |
| 117 |
static inline trx_id_t page_get_max_trx_id(const page_t *page); /*!< in: page */ |
| 118 |
|
| 119 |
/** Sets the max trx id field value. |
| 120 |
@param[in,out] block Page |
| 121 |
@param[in,out] page_zip Compressed page, or NULL |
| 122 |
@param[in] trx_id Transaction id |
| 123 |
@param[in,out] mtr Mini-transaction, or NULL */ |
| 124 |
void page_set_max_trx_id(buf_block_t *block, page_zip_des_t *page_zip, |
| 125 |
trx_id_t trx_id, mtr_t *mtr); |
| 126 |
|
| 127 |
/** Sets the max trx id field value if trx_id is bigger than the previous |
| 128 |
value. |
| 129 |
@param[in,out] block Page |
| 130 |
@param[in,out] page_zip Compressed page whose uncompressed part will |
| 131 |
be updated, or NULL |
| 132 |
@param[in] trx_id Transaction id |
| 133 |
@param[in,out] mtr Mini-transaction */ |
| 134 |
static inline void page_update_max_trx_id(buf_block_t *block, |
| 135 |
page_zip_des_t *page_zip, |
| 136 |
trx_id_t trx_id, mtr_t *mtr); |
| 137 |
|
| 138 |
/** Returns the RTREE SPLIT SEQUENCE NUMBER (FIL_RTREE_SPLIT_SEQ_NUM). |
| 139 |
@param[in] page page |
| 140 |
@return SPLIT SEQUENCE NUMBER */ |
| 141 |
static inline node_seq_t page_get_ssn_id(const page_t *page); |
| 142 |
|
| 143 |
/** Sets the RTREE SPLIT SEQUENCE NUMBER field value |
| 144 |
@param[in,out] block Page |
| 145 |
@param[in,out] page_zip Compressed page whose uncompressed part will |
| 146 |
be updated, or NULL |
| 147 |
@param[in] ssn_id Split sequence id |
| 148 |
@param[in,out] mtr Mini-transaction */ |
| 149 |
static inline void page_set_ssn_id(buf_block_t *block, page_zip_des_t *page_zip, |
| 150 |
node_seq_t ssn_id, mtr_t *mtr); |
| 151 |
|
| 152 |
/** Reads the given header field. |
| 153 |
@param[in] page page |
| 154 |
@param[in] field PAGE_N_DIR_SLOTS, ... */ |
| 155 |
static inline uint16_t page_header_get_field(const page_t *page, ulint field); |
| 156 |
|
| 157 |
/** Sets the given header field. |
| 158 |
@param[in,out] page page |
| 159 |
@param[in,out] page_zip compressed page whose uncompressed part will |
| 160 |
be updated, or NULL |
| 161 |
@param[in] field PAGE_N_DIR_SLOTS, ... |
| 162 |
@param[in] val value */ |
| 163 |
static inline void page_header_set_field(page_t *page, page_zip_des_t *page_zip, |
| 164 |
ulint field, ulint val); |
| 165 |
|
| 166 |
/** Returns the offset stored in the given header field. |
| 167 |
@param[in] page Page. |
| 168 |
@param[in] field PAGE_FREE, ... |
| 169 |
@return offset from the start of the page, or 0 */ |
| 170 |
[[nodiscard]] static inline ulint page_header_get_offs(const page_t *page, |
| 171 |
ulint field); |
| 172 |
|
| 173 |
/** Returns the pointer stored in the given header field, or NULL. */ |
| 174 |
static inline byte *page_header_get_ptr(page_t *page, uint32_t field) { |
| 175 |
if (page_header_get_offs(page, field)) { |
| 176 |
return page + page_header_get_offs(page, field); |
| 177 |
} |
| 178 |
return nullptr; |
| 179 |
} |
| 180 |
|
| 181 |
static inline const byte *page_header_get_ptr(const page_t *page, |
| 182 |
uint32_t field) { |
| 183 |
if (page_header_get_offs(page, field)) { |
| 184 |
return page + page_header_get_offs(page, field); |
| 185 |
} |
| 186 |
return nullptr; |
| 187 |
} |
| 188 |
|
| 189 |
/** Sets the pointer stored in the given header field. |
| 190 |
@param[in,out] page page |
| 191 |
@param[in,out] page_zip compressed page whose uncompressed part will |
| 192 |
be updated, or NULL |
| 193 |
@param[in,out] field PAGE_FREE, ... |
| 194 |
@param[in] ptr pointer or NULL */ |
| 195 |
static inline void page_header_set_ptr(page_t *page, page_zip_des_t *page_zip, |
| 196 |
ulint field, const byte *ptr); |
| 197 |
#ifndef UNIV_HOTBACKUP |
| 198 |
|
| 199 |
/** Resets the last insert info field in the page header. Writes to mlog about |
| 200 |
this operation. |
| 201 |
@param[in] page page |
| 202 |
@param[in,out] page_zip compressed page whose uncompressed part will |
| 203 |
be updated, or NULL |
| 204 |
@param[in] mtr mtr */ |
| 205 |
static inline void page_header_reset_last_insert(page_t *page, |
| 206 |
page_zip_des_t *page_zip, |
| 207 |
mtr_t *mtr); |
| 208 |
#endif /* !UNIV_HOTBACKUP */ |
| 209 |
|
| 210 |
/** Gets the offset of the first record on the page. |
| 211 |
@return offset of the first record in record list, relative from page */ |
| 212 |
static inline ulint page_get_infimum_offset( |
| 213 |
const page_t *page); /*!< in: page which must have record(s) */ |
| 214 |
/** Gets the offset of the last record on the page. |
| 215 |
@return offset of the last record in record list, relative from page */ |
| 216 |
static inline ulint page_get_supremum_offset( |
| 217 |
const page_t *page); /*!< in: page which must have record(s) */ |
| 218 |
static inline byte *page_get_infimum_rec(byte *page) { |
| 219 |
return page + page_get_infimum_offset(page); |
| 220 |
} |
| 221 |
static inline const byte *page_get_infimum_rec(const byte *page) { |
| 222 |
return page + page_get_infimum_offset(page); |
| 223 |
} |
| 224 |
static inline byte *page_get_supremum_rec(byte *page) { |
| 225 |
return page + page_get_supremum_offset(page); |
| 226 |
} |
| 227 |
static inline const byte *page_get_supremum_rec(const byte *page) { |
| 228 |
return page + page_get_supremum_offset(page); |
| 229 |
} |
| 230 |
|
| 231 |
/** Returns the nth record of the record list. |
| 232 |
This is the inverse function of page_rec_get_n_recs_before(). |
| 233 |
@return nth record */ |
| 234 |
[[nodiscard]] const rec_t *page_rec_get_nth_const( |
| 235 |
const page_t *page, /*!< in: page */ |
| 236 |
ulint nth); /*!< in: nth record */ |
| 237 |
|
| 238 |
/** Returns the nth record of the record list. |
| 239 |
This is the inverse function of page_rec_get_n_recs_before(). |
| 240 |
@param[in] page page |
| 241 |
@param[in] nth nth record |
| 242 |
@return nth record */ |
| 243 |
[[nodiscard]] static inline rec_t *page_rec_get_nth(page_t *page, ulint nth); |
| 244 |
|
| 245 |
#ifndef UNIV_HOTBACKUP |
| 246 |
/** Returns the middle record of the records on the page. If there is an |
| 247 |
even number of records in the list, returns the first record of the |
| 248 |
upper half-list. |
| 249 |
@return middle record */ |
| 250 |
[[nodiscard]] static inline rec_t *page_get_middle_rec( |
| 251 |
page_t *page); /*!< in: page */ |
| 252 |
#endif /* !UNIV_HOTBACKUP */ |
| 253 |
/** Gets the page number. |
| 254 |
@return page number */ |
| 255 |
static inline page_no_t page_get_page_no(const page_t *page); /*!< in: page */ |
| 256 |
/** Gets the tablespace identifier. |
| 257 |
@return space id */ |
| 258 |
static inline space_id_t page_get_space_id(const page_t *page); /*!< in: page */ |
| 259 |
|
| 260 |
/** Gets the space id and page number identifying the page. |
| 261 |
@return page number */ |
| 262 |
static inline page_id_t page_get_page_id(const page_t *page); |
| 263 |
|
| 264 |
/** Gets the number of user records on page (the infimum and supremum records |
| 265 |
are not user records). |
| 266 |
@return number of user records */ |
| 267 |
static inline ulint page_get_n_recs(const page_t *page); /*!< in: index page */ |
| 268 |
/** Returns the number of records before the given record in chain. |
| 269 |
The number includes infimum and supremum records. |
| 270 |
This is the inverse function of page_rec_get_nth(). |
| 271 |
@return number of records */ |
| 272 |
ulint page_rec_get_n_recs_before( |
| 273 |
const rec_t *rec); /*!< in: the physical record */ |
| 274 |
/** Gets the number of records in the heap. |
| 275 |
@param[in] page index page |
| 276 |
@return number of user records */ |
| 277 |
static inline uint16_t page_dir_get_n_heap(const page_t *page); |
| 278 |
|
| 279 |
/** Sets the number of records in the heap. |
| 280 |
@param[in,out] page index page |
| 281 |
@param[in,out] page_zip compressed page whose uncompressed part will |
| 282 |
be updated, or NULL. Note that the size of the |
| 283 |
dense page directory in the compressed page |
| 284 |
trailer is n_heap * PAGE_ZIP_DIR_SLOT_SIZE. |
| 285 |
@param[in] n_heap number of records*/ |
| 286 |
static inline void page_dir_set_n_heap(page_t *page, page_zip_des_t *page_zip, |
| 287 |
ulint n_heap); |
| 288 |
|
| 289 |
/** Gets the number of dir slots in directory. |
| 290 |
@return number of slots */ |
| 291 |
static inline ulint page_dir_get_n_slots( |
| 292 |
const page_t *page); /*!< in: index page */ |
| 293 |
|
| 294 |
/** Sets the number of dir slots in directory. |
| 295 |
@param[in,out] page page |
| 296 |
@param[in,out] page_zip compressed page whose uncompressed part will |
| 297 |
be updated, or NULL |
| 298 |
@param[in] n_slots number of slots */ |
| 299 |
static inline void page_dir_set_n_slots(page_t *page, page_zip_des_t *page_zip, |
| 300 |
ulint n_slots); |
| 301 |
|
| 302 |
#ifdef UNIV_DEBUG |
| 303 |
/** Gets pointer to nth directory slot. |
| 304 |
@param[in] page index page |
| 305 |
@param[in] n position |
| 306 |
@return pointer to dir slot */ |
| 307 |
static inline page_dir_slot_t *page_dir_get_nth_slot(const page_t *page, |
| 308 |
ulint n); |
| 309 |
#else /* UNIV_DEBUG */ |
| 310 |
#define page_dir_get_nth_slot(page, n) \ |
| 311 |
((page) + (UNIV_PAGE_SIZE - PAGE_DIR - (n + 1) * PAGE_DIR_SLOT_SIZE)) |
| 312 |
#endif /* UNIV_DEBUG */ |
| 313 |
|
| 314 |
/** Used to check the consistency of a record on a page. |
| 315 |
@return true if succeed */ |
| 316 |
static inline bool page_rec_check(const rec_t *rec); /*!< in: record */ |
| 317 |
/** Gets the record pointed to by a directory slot. |
| 318 |
@return pointer to record */ |
| 319 |
static inline const rec_t *page_dir_slot_get_rec( |
| 320 |
const page_dir_slot_t *slot); /*!< in: directory slot */ |
| 321 |
|
| 322 |
/** This is used to set the record offset in a directory slot. |
| 323 |
@param[in] rec record on the page |
| 324 |
@param[in] slot directory slot */ |
| 325 |
static inline void page_dir_slot_set_rec(page_dir_slot_t *slot, rec_t *rec); |
| 326 |
|
| 327 |
/** Gets the number of records owned by a directory slot. |
| 328 |
@return number of records */ |
| 329 |
static inline ulint page_dir_slot_get_n_owned( |
| 330 |
const page_dir_slot_t *slot); /*!< in: page directory slot */ |
| 331 |
|
| 332 |
/** This is used to set the owned records field of a directory slot. |
| 333 |
@param[in,out] slot directory slot |
| 334 |
@param[in,out] page_zip compressed page, or NULL |
| 335 |
@param[in] n number of records owned by the slot */ |
| 336 |
static inline void page_dir_slot_set_n_owned(page_dir_slot_t *slot, |
| 337 |
page_zip_des_t *page_zip, ulint n); |
| 338 |
|
| 339 |
/** Calculates the space reserved for directory slots of a given |
| 340 |
number of records. The exact value is a fraction number |
| 341 |
n * PAGE_DIR_SLOT_SIZE / PAGE_DIR_SLOT_MIN_N_OWNED, and it is |
| 342 |
rounded upwards to an integer. */ |
| 343 |
static inline ulint page_dir_calc_reserved_space( |
| 344 |
ulint n_recs); /*!< in: number of records */ |
| 345 |
/** Looks for the directory slot which owns the given record. |
| 346 |
@return the directory slot number */ |
| 347 |
ulint page_dir_find_owner_slot( |
| 348 |
const rec_t *rec); /*!< in: the physical record */ |
| 349 |
/** Determine whether the page is in new-style compact format. |
| 350 |
@return nonzero if the page is in compact format, zero if it is in |
| 351 |
old-style format */ |
| 352 |
static inline bool page_is_comp(const page_t *page); /*!< in: index page */ |
| 353 |
/** true if the record is on a page in compact format. |
| 354 |
@return nonzero if in compact format */ |
| 355 |
static inline bool page_rec_is_comp(const rec_t *rec); /*!< in: record */ |
| 356 |
/** Returns the heap number of a record. |
| 357 |
@return heap number */ |
| 358 |
static inline ulint page_rec_get_heap_no( |
| 359 |
const rec_t *rec); /*!< in: the physical record */ |
| 360 |
/** Determine whether the page is a B-tree leaf. |
| 361 |
@return true if the page is a B-tree leaf (PAGE_LEVEL = 0) */ |
| 362 |
[[nodiscard]] static inline bool page_is_leaf( |
| 363 |
const page_t *page); /*!< in: page */ |
| 364 |
/** Determine whether the page is empty. |
| 365 |
@return true if the page is empty (PAGE_N_RECS = 0) */ |
| 366 |
[[nodiscard]] static inline bool page_is_empty( |
| 367 |
const page_t *page); /*!< in: page */ |
| 368 |
/** Determine whether a page is an index root page. |
| 369 |
@param[in] page page frame |
| 370 |
@return true if the page is a root page of an index */ |
| 371 |
[[nodiscard]] static inline bool page_is_root(const page_t *page); |
| 372 |
/** Determine whether the page contains garbage. |
| 373 |
@return true if the page contains garbage (PAGE_GARBAGE is not 0) */ |
| 374 |
[[nodiscard]] static inline bool page_has_garbage( |
| 375 |
const page_t *page); /*!< in: page */ |
| 376 |
|
| 377 |
/** Gets the pointer to the next record on the page. |
| 378 |
@param[in] rec pointer to record |
| 379 |
@param[in] comp nonzero=compact page layout |
| 380 |
@return pointer to next record */ |
| 381 |
static inline const rec_t *page_rec_get_next_low(const rec_t *rec, ulint comp); |
| 382 |
|
| 383 |
/** Gets the pointer to the next record on the page. |
| 384 |
@return pointer to next record */ |
| 385 |
static inline rec_t *page_rec_get_next( |
| 386 |
rec_t *rec); /*!< in: pointer to record */ |
| 387 |
/** Gets the pointer to the next record on the page. |
| 388 |
@return pointer to next record */ |
| 389 |
static inline const rec_t *page_rec_get_next_const( |
| 390 |
const rec_t *rec); /*!< in: pointer to record */ |
| 391 |
/** Gets the pointer to the next non delete-marked record on the page. |
| 392 |
If all subsequent records are delete-marked, then this function |
| 393 |
will return the supremum record. |
| 394 |
@return pointer to next non delete-marked record or pointer to supremum */ |
| 395 |
static inline const rec_t *page_rec_get_next_non_del_marked( |
| 396 |
const rec_t *rec); /*!< in: pointer to record */ |
| 397 |
|
| 398 |
/** Sets the pointer to the next record on the page. |
| 399 |
@param[in] rec pointer to record, must not be page supremum |
| 400 |
@param[in] next pointer to next record, must not be page infimum */ |
| 401 |
static inline void page_rec_set_next(rec_t *rec, const rec_t *next); |
| 402 |
|
| 403 |
/** Gets the pointer to the previous record. |
| 404 |
@return pointer to previous record */ |
| 405 |
static inline const rec_t *page_rec_get_prev_const( |
| 406 |
const rec_t *rec); /*!< in: pointer to record, must not be page |
| 407 |
infimum */ |
| 408 |
/** Gets the pointer to the previous record. |
| 409 |
@return pointer to previous record */ |
| 410 |
static inline rec_t *page_rec_get_prev(rec_t *rec); /*!< in: pointer to record, |
| 411 |
must not be page infimum */ |
| 412 |
/** true if the record is a user record on the page. |
| 413 |
@param[in] offset record offset on page |
| 414 |
@return true if a user record */ |
| 415 |
static inline bool page_rec_is_user_rec_low(ulint offset); |
| 416 |
|
| 417 |
/** true if the record is the supremum record on a page. |
| 418 |
@param[in] offset record offset on page |
| 419 |
@return true if the supremum record */ |
| 420 |
static inline bool page_rec_is_supremum_low(ulint offset); |
| 421 |
|
| 422 |
/** true if the record is the infimum record on a page. |
| 423 |
@param[in] offset record offset on page |
| 424 |
@return true if the infimum record */ |
| 425 |
static inline bool page_rec_is_infimum_low(ulint offset); |
| 426 |
|
| 427 |
/** true if the record is a user record on the page. |
| 428 |
@return true if a user record */ |
| 429 |
[[nodiscard]] static inline bool page_rec_is_user_rec( |
| 430 |
const rec_t *rec); /*!< in: record */ |
| 431 |
/** true if the record is the supremum record on a page. |
| 432 |
@return true if the supremum record */ |
| 433 |
[[nodiscard]] static inline bool page_rec_is_supremum( |
| 434 |
const rec_t *rec); /*!< in: record */ |
| 435 |
|
| 436 |
/** true if the record is the infimum record on a page. |
| 437 |
@return true if the infimum record */ |
| 438 |
[[nodiscard]] static inline bool page_rec_is_infimum( |
| 439 |
const rec_t *rec); /*!< in: record */ |
| 440 |
|
| 441 |
/** true if the record is the first user record on a page. |
| 442 |
@param[in] rec Record. |
| 443 |
@param[in] page Page. |
| 444 |
@return true if the first user record */ |
| 445 |
[[nodiscard]] static inline bool page_rec_is_first(const rec_t *rec, |
| 446 |
const page_t *page); |
| 447 |
|
| 448 |
/** true if the record is the second user record on a page. |
| 449 |
@param[in] rec Record. |
| 450 |
@param[in] page Page. |
| 451 |
@return true if the second user record */ |
| 452 |
[[nodiscard]] static inline bool page_rec_is_second(const rec_t *rec, |
| 453 |
const page_t *page); |
| 454 |
|
| 455 |
/** true if the record is the last user record on a page. |
| 456 |
@param[in] rec Record. |
| 457 |
@param[in] page Page. |
| 458 |
@return true if the last user record */ |
| 459 |
[[nodiscard]] static inline bool page_rec_is_last(const rec_t *rec, |
| 460 |
const page_t *page); |
| 461 |
|
| 462 |
/** true if distance between the records (measured in number of times we have to |
| 463 |
move to the next record) is at most the specified value |
| 464 |
@param[in] left_rec lefter record |
| 465 |
@param[in] right_rec righter record |
| 466 |
@param[in] val specified value to compare |
| 467 |
@return true if the distance is smaller than the value */ |
| 468 |
[[nodiscard]] static inline bool page_rec_distance_is_at_most( |
| 469 |
const rec_t *left_rec, const rec_t *right_rec, ulint val); |
| 470 |
|
| 471 |
/** true if the record is the second last user record on a page. |
| 472 |
@param[in] rec Record. |
| 473 |
@param[in] page Page. |
| 474 |
@return true if the second last user record */ |
| 475 |
[[nodiscard]] static inline bool page_rec_is_second_last(const rec_t *rec, |
| 476 |
const page_t *page); |
| 477 |
|
| 478 |
/** Looks for the record which owns the given record. |
| 479 |
@return the owner record */ |
| 480 |
static inline rec_t *page_rec_find_owner_rec( |
| 481 |
rec_t *rec); /*!< in: the physical record */ |
| 482 |
|
| 483 |
/** Returns the maximum combined size of records which can be inserted on top |
| 484 |
of record heap. |
| 485 |
@param[in] page index page |
| 486 |
@param[in] n_recs number of records |
| 487 |
@return maximum combined size for inserted records */ |
| 488 |
static inline ulint page_get_max_insert_size(const page_t *page, ulint n_recs); |
| 489 |
|
| 490 |
/** Returns the maximum combined size of records which can be inserted on top |
| 491 |
of record heap if page is first reorganized. |
| 492 |
@param[in] page index page |
| 493 |
@param[in] n_recs number of records |
| 494 |
@return maximum combined size for inserted records */ |
| 495 |
static inline ulint page_get_max_insert_size_after_reorganize( |
| 496 |
const page_t *page, ulint n_recs); |
| 497 |
|
| 498 |
/** Calculates free space if a page is emptied. |
| 499 |
@param[in] comp nonzero=compact page format |
| 500 |
@return free space */ |
| 501 |
static inline ulint page_get_free_space_of_empty(bool comp); |
| 502 |
|
| 503 |
/** Returns the base extra size of a physical record. This is the |
| 504 |
size of the fixed header, independent of the record size. |
| 505 |
@return REC_N_NEW_EXTRA_BYTES or REC_N_OLD_EXTRA_BYTES */ |
| 506 |
static inline ulint page_rec_get_base_extra_size( |
| 507 |
const rec_t *rec); /*!< in: physical record */ |
| 508 |
/** Returns the sum of the sizes of the records in the record list |
| 509 |
excluding the infimum and supremum records. |
| 510 |
@return data in bytes */ |
| 511 |
static inline ulint page_get_data_size( |
| 512 |
const page_t *page); /*!< in: index page */ |
| 513 |
|
| 514 |
/** Allocates a block of memory from the head of the free list of an index |
| 515 |
page. |
| 516 |
@param[in,out] page index page |
| 517 |
@param[in,out] page_zip compressed page with enough space available |
| 518 |
for inserting the record, or NULL |
| 519 |
@param[in] next_rec pointer to the new head of the free record |
| 520 |
list |
| 521 |
@param[in] need number of bytes allocated */ |
| 522 |
static inline void page_mem_alloc_free(page_t *page, page_zip_des_t *page_zip, |
| 523 |
rec_t *next_rec, ulint need); |
| 524 |
|
| 525 |
/** Allocates a block of memory from the heap of an index page. |
| 526 |
@return pointer to start of allocated buffer, or NULL if allocation fails */ |
| 527 |
byte *page_mem_alloc_heap( |
| 528 |
page_t *page, /*!< in/out: index page */ |
| 529 |
page_zip_des_t *page_zip, /*!< in/out: compressed page with enough |
| 530 |
space available for inserting the record, |
| 531 |
or NULL */ |
| 532 |
ulint need, /*!< in: total number of bytes needed */ |
| 533 |
ulint *heap_no); /*!< out: this contains the heap number |
| 534 |
of the allocated record |
| 535 |
if allocation succeeds */ |
| 536 |
|
| 537 |
/** Puts a record to free list. |
| 538 |
@param[in,out] page index page |
| 539 |
@param[in,out] page_zip compressed page, or NULL |
| 540 |
@param[in] rec pointer to the (origin of) record |
| 541 |
@param[in] index index of rec |
| 542 |
@param[in] offsets array returned by rec_get_offsets() */ |
| 543 |
static inline void page_mem_free(page_t *page, page_zip_des_t *page_zip, |
| 544 |
rec_t *rec, const dict_index_t *index, |
| 545 |
const ulint *offsets); |
| 546 |
|
| 547 |
/** Create an uncompressed B-tree or R-tree or SDI index page. |
| 548 |
@param[in] block A buffer block where the page is created |
| 549 |
@param[in] mtr Mini-transaction handle |
| 550 |
@param[in] comp nonzero=compact page format |
| 551 |
@param[in] page_type Page type |
| 552 |
@return pointer to the page */ |
| 553 |
page_t *page_create(buf_block_t *block, mtr_t *mtr, ulint comp, |
| 554 |
page_type_t page_type); |
| 555 |
|
| 556 |
/** Create a compressed B-tree index page. |
| 557 |
@param[in,out] block Buffer frame where the page is created |
| 558 |
@param[in] index Index of the page, or NULL when applying |
| 559 |
TRUNCATE log record during recovery |
| 560 |
@param[in] level The B-tree level of the page |
| 561 |
@param[in] max_trx_id PAGE_MAX_TRX_ID |
| 562 |
@param[in] mtr Mini-transaction handle |
| 563 |
@param[in] page_type Page type to be created. Only FIL_PAGE_INDEX, |
| 564 |
FIL_PAGE_RTREE, FIL_PAGE_SDI allowed |
| 565 |
@return pointer to the page */ |
| 566 |
page_t *page_create_zip(buf_block_t *block, dict_index_t *index, ulint level, |
| 567 |
trx_id_t max_trx_id, mtr_t *mtr, page_type_t page_type); |
| 568 |
|
| 569 |
/** Empty a previously created B-tree index page. |
| 570 |
@param[in,out] block B-tree block |
| 571 |
@param[in] index The index of the page |
| 572 |
@param[in,out] mtr Mini-transaction */ |
| 573 |
void page_create_empty(buf_block_t *block, dict_index_t *index, mtr_t *mtr); |
| 574 |
|
| 575 |
/** Differs from page_copy_rec_list_end, because this function does not |
| 576 |
touch the lock table and max trx id on page or compress the page. |
| 577 |
|
| 578 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 579 |
if new_block is a compressed leaf page in a secondary index. |
| 580 |
This has to be done either within the same mini-transaction, |
| 581 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). */ |
| 582 |
void page_copy_rec_list_end_no_locks( |
| 583 |
buf_block_t *new_block, /*!< in: index page to copy to */ |
| 584 |
buf_block_t *block, /*!< in: index page of rec */ |
| 585 |
rec_t *rec, /*!< in: record on page */ |
| 586 |
dict_index_t *index, /*!< in: record descriptor */ |
| 587 |
mtr_t *mtr); /*!< in: mtr */ |
| 588 |
/** Copies records from page to new_page, from the given record onward, |
| 589 |
including that record. Infimum and supremum records are not copied. |
| 590 |
The records are copied to the start of the record list on new_page. |
| 591 |
|
| 592 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 593 |
if new_block is a compressed leaf page in a secondary index. |
| 594 |
This has to be done either within the same mini-transaction, |
| 595 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 596 |
|
| 597 |
@return pointer to the original successor of the infimum record on |
| 598 |
new_page, or NULL on zip overflow (new_block will be decompressed) */ |
| 599 |
rec_t *page_copy_rec_list_end( |
| 600 |
buf_block_t *new_block, /*!< in/out: index page to copy to */ |
| 601 |
buf_block_t *block, /*!< in: index page containing rec */ |
| 602 |
rec_t *rec, /*!< in: record on page */ |
| 603 |
dict_index_t *index, /*!< in: record descriptor */ |
| 604 |
mtr_t *mtr); /*!< in: mtr */ |
| 605 |
/** Copies records from page to new_page, up to the given record, NOT |
| 606 |
including that record. Infimum and supremum records are not copied. |
| 607 |
The records are copied to the end of the record list on new_page. |
| 608 |
|
| 609 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 610 |
if new_block is a compressed leaf page in a secondary index. |
| 611 |
This has to be done either within the same mini-transaction, |
| 612 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 613 |
|
| 614 |
@return pointer to the original predecessor of the supremum record on |
| 615 |
new_page, or NULL on zip overflow (new_block will be decompressed) */ |
| 616 |
rec_t *page_copy_rec_list_start( |
| 617 |
buf_block_t *new_block, /*!< in/out: index page to copy to */ |
| 618 |
buf_block_t *block, /*!< in: index page containing rec */ |
| 619 |
rec_t *rec, /*!< in: record on page */ |
| 620 |
dict_index_t *index, /*!< in: record descriptor */ |
| 621 |
mtr_t *mtr); /*!< in: mtr */ |
| 622 |
/** Deletes records from a page from a given record onward, including that |
| 623 |
record. The infimum and supremum records are not deleted. */ |
| 624 |
void page_delete_rec_list_end( |
| 625 |
rec_t *rec, /*!< in: pointer to record on page */ |
| 626 |
buf_block_t *block, /*!< in: buffer block of the page */ |
| 627 |
dict_index_t *index, /*!< in: record descriptor */ |
| 628 |
ulint n_recs, /*!< in: number of records to delete, |
| 629 |
or ULINT_UNDEFINED if not known */ |
| 630 |
ulint size, /*!< in: the sum of the sizes of the |
| 631 |
records in the end of the chain to |
| 632 |
delete, or ULINT_UNDEFINED if not known */ |
| 633 |
mtr_t *mtr); /*!< in: mtr */ |
| 634 |
/** Deletes records from page, up to the given record, NOT including |
| 635 |
that record. Infimum and supremum records are not deleted. */ |
| 636 |
void page_delete_rec_list_start( |
| 637 |
rec_t *rec, /*!< in: record on page */ |
| 638 |
buf_block_t *block, /*!< in: buffer block of the page */ |
| 639 |
dict_index_t *index, /*!< in: record descriptor */ |
| 640 |
mtr_t *mtr); /*!< in: mtr */ |
| 641 |
/** Moves record list end to another page. Moved records include |
| 642 |
split_rec. |
| 643 |
|
| 644 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 645 |
if new_block is a compressed leaf page in a secondary index. |
| 646 |
This has to be done either within the same mini-transaction, |
| 647 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 648 |
|
| 649 |
@return true on success; false on compression failure (new_block will |
| 650 |
be decompressed) */ |
| 651 |
bool page_move_rec_list_end( |
| 652 |
buf_block_t *new_block, /*!< in/out: index page where to move */ |
| 653 |
buf_block_t *block, /*!< in: index page from where to move */ |
| 654 |
rec_t *split_rec, /*!< in: first record to move */ |
| 655 |
dict_index_t *index, /*!< in: record descriptor */ |
| 656 |
mtr_t *mtr); /*!< in: mtr */ |
| 657 |
/** Moves record list start to another page. Moved records do not include |
| 658 |
split_rec. |
| 659 |
|
| 660 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 661 |
if new_block is a compressed leaf page in a secondary index. |
| 662 |
This has to be done either within the same mini-transaction, |
| 663 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 664 |
|
| 665 |
@return true on success; false on compression failure */ |
| 666 |
bool page_move_rec_list_start( |
| 667 |
buf_block_t *new_block, /*!< in/out: index page where to move */ |
| 668 |
buf_block_t *block, /*!< in/out: page containing split_rec */ |
| 669 |
rec_t *split_rec, /*!< in: first record not to move */ |
| 670 |
dict_index_t *index, /*!< in: record descriptor */ |
| 671 |
mtr_t *mtr); /*!< in: mtr */ |
| 672 |
|
| 673 |
/** Splits a directory slot which owns too many records. |
| 674 |
@param[in,out] page Index page |
| 675 |
@param[in,out] page_zip Compressed page whose uncompressed part will be written, |
| 676 |
or null |
| 677 |
@param[in] slot_no The directory slot */ |
| 678 |
void page_dir_split_slot(page_t *page, page_zip_des_t *page_zip, ulint slot_no); |
| 679 |
|
| 680 |
/** Tries to balance the given directory slot with too few records with the |
| 681 |
upper neighbor, so that there are at least the minimum number of records |
| 682 |
owned by the slot; this may result in the merging of two slots. |
| 683 |
@param[in,out] page Index page |
| 684 |
@param[in,out] page_zip Compressed page, or null |
| 685 |
@param[in] slot_no The directory slot */ |
| 686 |
void page_dir_balance_slot(page_t *page, page_zip_des_t *page_zip, |
| 687 |
ulint slot_no); |
| 688 |
|
| 689 |
/** Parses a log record of a record list end or start deletion. |
| 690 |
@return end of log record or NULL */ |
| 691 |
byte *page_parse_delete_rec_list( |
| 692 |
mlog_id_t type, /*!< in: MLOG_LIST_END_DELETE, |
| 693 |
MLOG_LIST_START_DELETE, |
| 694 |
MLOG_COMP_LIST_END_DELETE or |
| 695 |
MLOG_COMP_LIST_START_DELETE */ |
| 696 |
byte *ptr, /*!< in: buffer */ |
| 697 |
byte *end_ptr, /*!< in: buffer end */ |
| 698 |
buf_block_t *block, /*!< in/out: buffer block or NULL */ |
| 699 |
dict_index_t *index, /*!< in: record descriptor */ |
| 700 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 701 |
|
| 702 |
/** Parses a redo log record of creating a page. |
| 703 |
@param[in,out] block buffer block, or NULL |
| 704 |
@param[in] comp nonzero=compact page format |
| 705 |
@param[in] page_type page type (FIL_PAGE_INDEX, FIL_PAGE_RTREE |
| 706 |
or FIL_PAGE_SDI) */ |
| 707 |
void page_parse_create(buf_block_t *block, ulint comp, page_type_t page_type); |
| 708 |
|
| 709 |
#ifndef UNIV_HOTBACKUP |
| 710 |
/** Prints record contents including the data relevant only in |
| 711 |
the index page context. |
| 712 |
@param[in] rec Physical record |
| 713 |
@param[in] offsets Record descriptor */ |
| 714 |
void page_rec_print(const rec_t *rec, const ulint *offsets); |
| 715 |
#ifdef UNIV_BTR_PRINT |
| 716 |
/** This is used to print the contents of the directory for |
| 717 |
debugging purposes. */ |
| 718 |
void page_dir_print(page_t *page, /*!< in: index page */ |
| 719 |
ulint pr_n); /*!< in: print n first and n last entries */ |
| 720 |
/** This is used to print the contents of the page record list for |
| 721 |
debugging purposes. */ |
| 722 |
void page_print_list( |
| 723 |
buf_block_t *block, /*!< in: index page */ |
| 724 |
dict_index_t *index, /*!< in: dictionary index of the page */ |
| 725 |
ulint pr_n); /*!< in: print n first and n last entries */ |
| 726 |
/** Prints the info in a page header. */ |
| 727 |
void page_header_print(const page_t *page); /*!< in: index page */ |
| 728 |
/** This is used to print the contents of the page for |
| 729 |
debugging purposes. */ |
| 730 |
void page_print(buf_block_t *block, /*!< in: index page */ |
| 731 |
dict_index_t *index, /*!< in: dictionary index of the page */ |
| 732 |
ulint dn, /*!< in: print dn first and last entries |
| 733 |
in directory */ |
| 734 |
ulint rn); /*!< in: print rn first and last records |
| 735 |
in directory */ |
| 736 |
#endif /* UNIV_BTR_PRINT */ |
| 737 |
#endif /* !UNIV_HOTBACKUP */ |
| 738 |
/** The following is used to validate a record on a page. This function |
| 739 |
differs from rec_validate as it can also check the n_owned field and |
| 740 |
the heap_no field. |
| 741 |
@return true if ok */ |
| 742 |
bool page_rec_validate( |
| 743 |
const rec_t *rec, /*!< in: physical record */ |
| 744 |
const ulint *offsets); /*!< in: array returned by rec_get_offsets() */ |
| 745 |
#ifdef UNIV_DEBUG |
| 746 |
/** Checks that the first directory slot points to the infimum record and |
| 747 |
the last to the supremum. This function is intended to track if the |
| 748 |
bug fixed in 4.0.14 has caused corruption to users' databases. */ |
| 749 |
void page_check_dir(const page_t *page); /*!< in: index page */ |
| 750 |
#endif /* UNIV_DEBUG */ |
| 751 |
/** This function checks the consistency of an index page when we do not |
| 752 |
know the index. This is also resilient so that this should never crash |
| 753 |
even if the page is total garbage. |
| 754 |
@return true if ok */ |
| 755 |
bool page_simple_validate_old( |
| 756 |
const page_t *page); /*!< in: index page in ROW_FORMAT=REDUNDANT */ |
| 757 |
/** This function checks the consistency of an index page when we do not |
| 758 |
know the index. This is also resilient so that this should never crash |
| 759 |
even if the page is total garbage. |
| 760 |
@return true if ok */ |
| 761 |
bool page_simple_validate_new( |
| 762 |
const page_t *page); /*!< in: index page in ROW_FORMAT!=REDUNDANT */ |
| 763 |
|
| 764 |
/** This function checks the consistency of an index page. |
| 765 |
@param[in] page index page |
| 766 |
@param[in] index data dictionary index containing the page record type |
| 767 |
definition |
| 768 |
@return true if ok */ |
| 769 |
bool page_validate(const page_t *page, dict_index_t *index); |
| 770 |
|
| 771 |
/** Looks in the page record list for a record with the given heap number. |
| 772 |
@return record, NULL if not found */ |
| 773 |
const rec_t *page_find_rec_with_heap_no( |
| 774 |
const page_t *page, /*!< in: index page */ |
| 775 |
ulint heap_no); /*!< in: heap number */ |
| 776 |
/** Get the last non-delete-marked record on a page. |
| 777 |
@param[in] page index tree leaf page |
| 778 |
@return the last record, not delete-marked |
| 779 |
@retval infimum record if all records are delete-marked */ |
| 780 |
const rec_t *page_find_rec_last_not_deleted(const page_t *page); |
| 781 |
|
| 782 |
/** Issue a warning when the checksum that is stored in the page is valid, |
| 783 |
but different than the global setting innodb_checksum_algorithm. |
| 784 |
@param[in] curr_algo current checksum algorithm |
| 785 |
@param[in] page_checksum page valid checksum |
| 786 |
@param[in] page_id page identifier */ |
| 787 |
void page_warn_strict_checksum(srv_checksum_algorithm_t curr_algo, |
| 788 |
srv_checksum_algorithm_t page_checksum, |
| 789 |
const page_id_t &page_id); |
| 790 |
|
| 791 |
/** Check that a page_size is correct for InnoDB. |
| 792 |
If correct, set the associated page_size_shift which is the power of 2 |
| 793 |
for this page size. |
| 794 |
@param[in] page_size Page Size to evaluate |
| 795 |
@return an associated page_size_shift if valid, 0 if invalid. */ |
| 796 |
inline ulong page_size_validate(ulong page_size); |
| 797 |
|
| 798 |
/** This function checks if the page in which record is present is a |
| 799 |
non-leaf node of a spatial index. |
| 800 |
param[in] rec Btree record |
| 801 |
param[in] index index |
| 802 |
@return true if ok */ |
| 803 |
bool page_is_spatial_non_leaf(const rec_t *rec, dict_index_t *index); |
| 804 |
|
| 805 |
#include "page0page.ic" |
| 806 |
|
| 807 |
#endif |