| 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 page/page0cur.cc |
| 30 |
The page cursor |
| 31 |
|
| 32 |
Created 10/4/1994 Heikki Tuuri |
| 33 |
*************************************************************************/ |
| 34 |
|
| 35 |
#include "page0cur.h" |
| 36 |
|
| 37 |
#include "btr0btr.h" |
| 38 |
#include "ha_prototypes.h" |
| 39 |
#include "log0recv.h" |
| 40 |
#include "mtr0log.h" |
| 41 |
|
| 42 |
#include <algorithm> |
| 43 |
#include "page0zip.h" |
| 44 |
|
| 45 |
#ifndef UNIV_HOTBACKUP |
| 46 |
#include "gis0rtree.h" |
| 47 |
#include "rem0cmp.h" |
| 48 |
#endif /* !UNIV_HOTBACKUP */ |
| 49 |
|
| 50 |
#ifdef PAGE_CUR_ADAPT |
| 51 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 52 |
static ulint page_cur_short_succ = 0; |
| 53 |
#endif /* UNIV_SEARCH_PERF_STAT */ |
| 54 |
|
| 55 |
#ifndef UNIV_HOTBACKUP |
| 56 |
/** This is a linear congruential generator PRNG. Returns a pseudo random |
| 57 |
number between 0 and 2^64-1 inclusive. The formula and the constants |
| 58 |
being used are: |
| 59 |
X[n+1] = (a * X[n] + c) mod m |
| 60 |
where: |
| 61 |
X[0] = number based on std::chrono::steady_clock::now() |
| 62 |
a = 1103515245 (3^5 * 5 * 7 * 129749) |
| 63 |
c = 12345 (3 * 5 * 823) |
| 64 |
m = 18446744073709551616 (2^64) |
| 65 |
|
| 66 |
@return number between 0 and 2^64-1 */ |
| 67 |
static uint64_t page_cur_lcg_prng(void) { |
| 68 |
#define LCG_a 1103515245 |
| 69 |
#define LCG_c 12345 |
| 70 |
static uint64_t lcg_current = 0; |
| 71 |
static bool initialized = false; |
| 72 |
|
| 73 |
if (!initialized) { |
| 74 |
lcg_current = std::chrono::duration_cast<std::chrono::microseconds>( |
| 75 |
std::chrono::steady_clock::now() - |
| 76 |
std::chrono::steady_clock::time_point{}) |
| 77 |
.count(); |
| 78 |
initialized = true; |
| 79 |
} |
| 80 |
|
| 81 |
/* no need to "% 2^64" explicitly because lcg_current is |
| 82 |
64 bit and this will be done anyway */ |
| 83 |
lcg_current = LCG_a * lcg_current + LCG_c; |
| 84 |
|
| 85 |
return (lcg_current); |
| 86 |
} |
| 87 |
|
| 88 |
/** Try a search shortcut based on the last insert. |
| 89 |
@param[in] block index page |
| 90 |
@param[in] index index tree |
| 91 |
@param[in] tuple search key |
| 92 |
@param[in,out] iup_matched_fields already matched fields in the |
| 93 |
upper limit record |
| 94 |
@param[in,out] ilow_matched_fields already matched fields in the |
| 95 |
lower limit record |
| 96 |
@param[out] cursor page cursor |
| 97 |
@return true on success */ |
| 98 |
static inline bool page_cur_try_search_shortcut( |
| 99 |
const buf_block_t *block, const dict_index_t *index, const dtuple_t *tuple, |
| 100 |
ulint *iup_matched_fields, ulint *ilow_matched_fields, page_cur_t *cursor) { |
| 101 |
const rec_t *rec; |
| 102 |
const rec_t *next_rec; |
| 103 |
ulint low_match; |
| 104 |
ulint up_match; |
| 105 |
bool success = false; |
| 106 |
const page_t *page = buf_block_get_frame(block); |
| 107 |
mem_heap_t *heap = nullptr; |
| 108 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 109 |
ulint *offsets = offsets_; |
| 110 |
rec_offs_init(offsets_); |
| 111 |
|
| 112 |
ut_ad(dtuple_check_typed(tuple)); |
| 113 |
|
| 114 |
rec = page_header_get_ptr(page, PAGE_LAST_INSERT); |
| 115 |
offsets = rec_get_offsets(rec, index, offsets, dtuple_get_n_fields(tuple), |
| 116 |
UT_LOCATION_HERE, &heap); |
| 117 |
|
| 118 |
ut_ad(rec); |
| 119 |
ut_ad(page_rec_is_user_rec(rec)); |
| 120 |
|
| 121 |
low_match = up_match = std::min(*ilow_matched_fields, *iup_matched_fields); |
| 122 |
|
| 123 |
if (tuple->compare(rec, index, offsets, &low_match) < 0) { |
| 124 |
goto exit_func; |
| 125 |
} |
| 126 |
|
| 127 |
next_rec = page_rec_get_next_const(rec); |
| 128 |
if (!page_rec_is_supremum(next_rec)) { |
| 129 |
offsets = |
| 130 |
rec_get_offsets(next_rec, index, offsets, dtuple_get_n_fields(tuple), |
| 131 |
UT_LOCATION_HERE, &heap); |
| 132 |
|
| 133 |
if (tuple->compare(next_rec, index, offsets, &up_match) >= 0) { |
| 134 |
goto exit_func; |
| 135 |
} |
| 136 |
|
| 137 |
*iup_matched_fields = up_match; |
| 138 |
} |
| 139 |
|
| 140 |
page_cur_position(rec, block, cursor); |
| 141 |
|
| 142 |
*ilow_matched_fields = low_match; |
| 143 |
|
| 144 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 145 |
page_cur_short_succ++; |
| 146 |
#endif |
| 147 |
success = true; |
| 148 |
exit_func: |
| 149 |
if (UNIV_LIKELY_NULL(heap)) { |
| 150 |
mem_heap_free(heap); |
| 151 |
} |
| 152 |
return (success); |
| 153 |
} |
| 154 |
|
| 155 |
/** Try a search shortcut based on the last insert. |
| 156 |
@param[in] block index page |
| 157 |
@param[in] index index tree |
| 158 |
@param[in] tuple search key |
| 159 |
@param[in,out] iup_matched_fields already matched fields in the |
| 160 |
upper limit record |
| 161 |
@param[in,out] iup_matched_bytes already matched bytes in the |
| 162 |
first partially matched field in the upper limit record |
| 163 |
@param[in,out] ilow_matched_fields already matched fields in the |
| 164 |
lower limit record |
| 165 |
@param[in,out] ilow_matched_bytes already matched bytes in the |
| 166 |
first partially matched field in the lower limit record |
| 167 |
@param[out] cursor page cursor |
| 168 |
@return true on success */ |
| 169 |
static inline bool page_cur_try_search_shortcut_bytes( |
| 170 |
const buf_block_t *block, const dict_index_t *index, const dtuple_t *tuple, |
| 171 |
ulint *iup_matched_fields, ulint *iup_matched_bytes, |
| 172 |
ulint *ilow_matched_fields, ulint *ilow_matched_bytes, page_cur_t *cursor) { |
| 173 |
const rec_t *rec; |
| 174 |
const rec_t *next_rec; |
| 175 |
ulint low_match; |
| 176 |
ulint low_bytes; |
| 177 |
ulint up_match; |
| 178 |
ulint up_bytes; |
| 179 |
bool success = false; |
| 180 |
const page_t *page = buf_block_get_frame(block); |
| 181 |
mem_heap_t *heap = nullptr; |
| 182 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 183 |
ulint *offsets = offsets_; |
| 184 |
rec_offs_init(offsets_); |
| 185 |
|
| 186 |
ut_ad(dtuple_check_typed(tuple)); |
| 187 |
|
| 188 |
rec = page_header_get_ptr(page, PAGE_LAST_INSERT); |
| 189 |
offsets = rec_get_offsets(rec, index, offsets, dtuple_get_n_fields(tuple), |
| 190 |
UT_LOCATION_HERE, &heap); |
| 191 |
|
| 192 |
ut_ad(rec); |
| 193 |
ut_ad(page_rec_is_user_rec(rec)); |
| 194 |
if (ut_pair_cmp(*ilow_matched_fields, *ilow_matched_bytes, |
| 195 |
*iup_matched_fields, *iup_matched_bytes) < 0) { |
| 196 |
up_match = low_match = *ilow_matched_fields; |
| 197 |
up_bytes = low_bytes = *ilow_matched_bytes; |
| 198 |
} else { |
| 199 |
up_match = low_match = *iup_matched_fields; |
| 200 |
up_bytes = low_bytes = *iup_matched_bytes; |
| 201 |
} |
| 202 |
|
| 203 |
if (cmp_dtuple_rec_with_match_bytes(tuple, rec, index, offsets, &low_match, |
| 204 |
&low_bytes) < 0) { |
| 205 |
goto exit_func; |
| 206 |
} |
| 207 |
|
| 208 |
next_rec = page_rec_get_next_const(rec); |
| 209 |
if (!page_rec_is_supremum(next_rec)) { |
| 210 |
offsets = |
| 211 |
rec_get_offsets(next_rec, index, offsets, dtuple_get_n_fields(tuple), |
| 212 |
UT_LOCATION_HERE, &heap); |
| 213 |
|
| 214 |
if (cmp_dtuple_rec_with_match_bytes(tuple, next_rec, index, offsets, |
| 215 |
&up_match, &up_bytes) >= 0) { |
| 216 |
goto exit_func; |
| 217 |
} |
| 218 |
|
| 219 |
*iup_matched_fields = up_match; |
| 220 |
*iup_matched_bytes = up_bytes; |
| 221 |
} |
| 222 |
|
| 223 |
page_cur_position(rec, block, cursor); |
| 224 |
|
| 225 |
*ilow_matched_fields = low_match; |
| 226 |
*ilow_matched_bytes = low_bytes; |
| 227 |
|
| 228 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 229 |
page_cur_short_succ++; |
| 230 |
#endif |
| 231 |
success = true; |
| 232 |
exit_func: |
| 233 |
if (UNIV_LIKELY_NULL(heap)) { |
| 234 |
mem_heap_free(heap); |
| 235 |
} |
| 236 |
return (success); |
| 237 |
} |
| 238 |
#endif /* !UNIV_HOTBACKUP */ |
| 239 |
|
| 240 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 241 |
/** Checks if the nth field in a record is a character type field which extends |
| 242 |
the nth field in tuple, i.e., the field is longer or equal in length and has |
| 243 |
common first characters. |
| 244 |
@param[in] tuple data tuple |
| 245 |
@param[in] rec record |
| 246 |
@param[in] offsets array returned by rec_get_offsets() |
| 247 |
@param[in] n compare nth field |
| 248 |
@param[in] index index where the record resides |
| 249 |
@return true if rec field extends tuple field */ |
| 250 |
static bool page_cur_rec_field_extends(const dtuple_t *tuple, const rec_t *rec, |
| 251 |
const ulint *offsets, ulint n, |
| 252 |
const dict_index_t *index) { |
| 253 |
const dtype_t *type; |
| 254 |
const dfield_t *dfield; |
| 255 |
const byte *rec_f; |
| 256 |
ulint rec_f_len; |
| 257 |
|
| 258 |
ut_ad(rec_offs_validate(rec, NULL, offsets)); |
| 259 |
dfield = dtuple_get_nth_field(tuple, n); |
| 260 |
|
| 261 |
type = dfield_get_type(dfield); |
| 262 |
|
| 263 |
rec_f = rec_get_nth_field_instant(rec, offsets, n, index, &rec_f_len); |
| 264 |
|
| 265 |
if (type->mtype == DATA_VARCHAR || type->mtype == DATA_CHAR || |
| 266 |
type->mtype == DATA_FIXBINARY || type->mtype == DATA_BINARY || |
| 267 |
type->mtype == DATA_BLOB || DATA_GEOMETRY_MTYPE(type->mtype) || |
| 268 |
type->mtype == DATA_VARMYSQL || type->mtype == DATA_MYSQL) { |
| 269 |
if (dfield_get_len(dfield) != UNIV_SQL_NULL && rec_f_len != UNIV_SQL_NULL && |
| 270 |
rec_f_len >= dfield_get_len(dfield) |
| 271 |
/* is_ascending parameter in the below call is passed as a |
| 272 |
constant as we are only testing for equality and we are |
| 273 |
not interested in what the nonzero return value actually |
| 274 |
is. */ |
| 275 |
&& |
| 276 |
!cmp_data_data(type->mtype, type->prtype, true, dfield_get_data(dfield), |
| 277 |
dfield_get_len(dfield), rec_f, dfield_get_len(dfield))) { |
| 278 |
return true; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return false; |
| 283 |
} |
| 284 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 285 |
#endif /* PAGE_CUR_ADAPT */ |
| 286 |
|
| 287 |
#ifndef UNIV_HOTBACKUP |
| 288 |
/** Check if rec has at least one NULL value among the columns for which |
| 289 |
rec_cache.offsets was cached, which means rec_cache.offsets should not be used |
| 290 |
for this rec as it has a different layout of fields than the cached version. |
| 291 |
@param[in] rec B-Tree record |
| 292 |
@param[in] index The index from which the rec was taken |
| 293 |
@return true iff the rec has at least one relevant column with NULL */ |
| 294 |
static bool page_cur_has_null(const rec_t *rec, const dict_index_t *index) { |
| 295 |
ut_ad(index->rec_cache.offsets); |
| 296 |
const auto nullable_cols = index->rec_cache.nullable_cols; |
| 297 |
ut_ad(nullable_cols <= index->n_nullable); |
| 298 |
if (!nullable_cols) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
/* Check if this record has a NULL value. */ |
| 302 |
const byte *nulls = rec - (1 + REC_N_NEW_EXTRA_BYTES); |
| 303 |
size_t n_bytes_to_scan = UT_BITS_IN_BYTES(nullable_cols); |
| 304 |
byte null_mask = 0xff; |
| 305 |
size_t bits_examined = 0; |
| 306 |
|
| 307 |
for (size_t i = 0; i < n_bytes_to_scan - 1; i++) { |
| 308 |
if (*nulls & null_mask) { |
| 309 |
return true; |
| 310 |
} |
| 311 |
--nulls; |
| 312 |
bits_examined += 8; |
| 313 |
} |
| 314 |
|
| 315 |
null_mask >>= (8 - (nullable_cols - bits_examined)); |
| 316 |
return (*nulls & null_mask); |
| 317 |
} |
| 318 |
|
| 319 |
/** Searches the right position for a page cursor. |
| 320 |
@param[in] block Buffer block |
| 321 |
@param[in] index Record descriptor |
| 322 |
@param[in] tuple Data tuple |
| 323 |
@param[in] mode PAGE_CUR_L, PAGE_CUR_LE, PAGE_CUR_G, or PAGE_CUR_GE |
| 324 |
@param[in,out] iup_matched_fields Already matched fields in upper limit record |
| 325 |
@param[in,out] ilow_matched_fields Already matched fields in lower limit record |
| 326 |
@param[out] cursor Page cursor |
| 327 |
@param[in,out] rtr_info Rtree search stack */ |
| 328 |
void page_cur_search_with_match(const buf_block_t *block, |
| 329 |
const dict_index_t *index, |
| 330 |
const dtuple_t *tuple, page_cur_mode_t mode, |
| 331 |
ulint *iup_matched_fields, |
| 332 |
|
| 333 |
ulint *ilow_matched_fields, |
| 334 |
|
| 335 |
page_cur_t *cursor, rtr_info_t *rtr_info) { |
| 336 |
ulint up; |
| 337 |
ulint low; |
| 338 |
ulint mid; |
| 339 |
const page_t *page; |
| 340 |
const page_dir_slot_t *slot; |
| 341 |
const rec_t *up_rec; |
| 342 |
const rec_t *low_rec; |
| 343 |
const rec_t *mid_rec; |
| 344 |
ulint up_matched_fields; |
| 345 |
ulint low_matched_fields; |
| 346 |
ulint cur_matched_fields; |
| 347 |
int cmp = 0; |
| 348 |
#ifdef UNIV_ZIP_DEBUG |
| 349 |
const page_zip_des_t *page_zip = buf_block_get_page_zip(block); |
| 350 |
#endif /* UNIV_ZIP_DEBUG */ |
| 351 |
mem_heap_t *heap = nullptr; |
| 352 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 353 |
rec_offs_init(offsets_); |
| 354 |
|
| 355 |
ut_ad(dtuple_validate(tuple)); |
| 356 |
#ifdef UNIV_DEBUG |
| 357 |
#ifdef PAGE_CUR_DBG |
| 358 |
if (mode != PAGE_CUR_DBG) |
| 359 |
#endif /* PAGE_CUR_DBG */ |
| 360 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 361 |
if (mode != PAGE_CUR_LE_OR_EXTENDS) |
| 362 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 363 |
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE || mode == PAGE_CUR_G || |
| 364 |
mode == PAGE_CUR_GE || dict_index_is_spatial(index)); |
| 365 |
#endif /* UNIV_DEBUG */ |
| 366 |
page = buf_block_get_frame(block); |
| 367 |
#ifdef UNIV_ZIP_DEBUG |
| 368 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 369 |
#endif /* UNIV_ZIP_DEBUG */ |
| 370 |
|
| 371 |
ut_d(page_check_dir(page)); |
| 372 |
|
| 373 |
#ifdef PAGE_CUR_ADAPT |
| 374 |
if (page_is_leaf(page) && (mode == PAGE_CUR_LE) && |
| 375 |
!dict_index_is_spatial(index) && |
| 376 |
(page_header_get_field(page, PAGE_N_DIRECTION) > 3) && |
| 377 |
(page_header_get_ptr(page, PAGE_LAST_INSERT)) && |
| 378 |
(page_header_get_field(page, PAGE_DIRECTION) == PAGE_RIGHT)) { |
| 379 |
if (page_cur_try_search_shortcut(block, index, tuple, iup_matched_fields, |
| 380 |
ilow_matched_fields, cursor)) { |
| 381 |
return; |
| 382 |
} |
| 383 |
} |
| 384 |
#ifdef PAGE_CUR_DBG |
| 385 |
if (mode == PAGE_CUR_DBG) { |
| 386 |
mode = PAGE_CUR_LE; |
| 387 |
} |
| 388 |
#endif |
| 389 |
#endif |
| 390 |
|
| 391 |
/* If the mode is for R-tree indexes, use the special MBR |
| 392 |
related compare functions */ |
| 393 |
if (dict_index_is_spatial(index) && mode > PAGE_CUR_LE) { |
| 394 |
/* For leaf level insert, we still use the traditional |
| 395 |
compare function for now */ |
| 396 |
if (mode == PAGE_CUR_RTREE_INSERT && page_is_leaf(page)) { |
| 397 |
mode = PAGE_CUR_LE; |
| 398 |
} else { |
| 399 |
rtr_cur_search_with_match(block, (dict_index_t *)index, tuple, mode, |
| 400 |
cursor, rtr_info); |
| 401 |
return; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/* The following flag does not work for non-latin1 char sets because |
| 406 |
cmp_full_field does not tell how many bytes matched */ |
| 407 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 408 |
ut_a(mode != PAGE_CUR_LE_OR_EXTENDS); |
| 409 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 410 |
|
| 411 |
/* If mode PAGE_CUR_G is specified, we are trying to position the |
| 412 |
cursor to answer a query of the form "tuple < X", where tuple is |
| 413 |
the input parameter, and X denotes an arbitrary physical record on |
| 414 |
the page. We want to position the cursor on the first X which |
| 415 |
satisfies the condition. */ |
| 416 |
|
| 417 |
up_matched_fields = *iup_matched_fields; |
| 418 |
low_matched_fields = *ilow_matched_fields; |
| 419 |
|
| 420 |
/* Perform binary search. First the search is done through the page |
| 421 |
directory, after that as a linear search in the list of records |
| 422 |
owned by the upper limit directory slot. */ |
| 423 |
|
| 424 |
low = 0; |
| 425 |
up = page_dir_get_n_slots(page) - 1; |
| 426 |
|
| 427 |
const ulint *const cached_offsets{index->rec_cache.offsets}; |
| 428 |
#ifdef UNIV_DEBUG |
| 429 |
if (cached_offsets) { |
| 430 |
ut_ad(dict_table_is_comp(index->table)); |
| 431 |
ut_ad(!dict_index_has_virtual(index)); |
| 432 |
ut_ad(!index->table->has_instant_cols()); |
| 433 |
ut_ad(!index->table->has_row_versions()); |
| 434 |
ut_ad(!dict_index_is_spatial(index)); |
| 435 |
const size_t n = dtuple_get_n_fields_cmp(tuple); |
| 436 |
const size_t searchable = dict_index_get_n_unique_in_tree(index); |
| 437 |
// assert there was no change in number of columns since caching |
| 438 |
ut_ad(searchable == index->rec_cache.offsets[1]); |
| 439 |
// assert that the number of nullable columns hasn't changed since caching |
| 440 |
{ |
| 441 |
size_t nullable{0}; |
| 442 |
for (size_t i = 0; i < searchable; i++) { |
| 443 |
if (!(index->get_field(i)->col->prtype & DATA_NOT_NULL)) { |
| 444 |
++nullable; |
| 445 |
} |
| 446 |
} |
| 447 |
ut_ad(index->rec_cache.nullable_cols == nullable); |
| 448 |
} |
| 449 |
/* We never do a binary search on columns which are not part of internal |
| 450 |
nodes, nor on node_ptr field. If we ever start doing this, make sure to |
| 451 |
not use cached_offset offsets then, as it has only searchable columns. */ |
| 452 |
ut_ad(n <= searchable); |
| 453 |
} |
| 454 |
#endif |
| 455 |
auto get_mid_rec_offsets = [&]() -> const auto * { |
| 456 |
if (cached_offsets && !page_cur_has_null(mid_rec, index)) { |
| 457 |
#ifdef UNIV_DEBUG |
| 458 |
{ |
| 459 |
const size_t n = dtuple_get_n_fields_cmp(tuple); |
| 460 |
const auto *const offsets = rec_get_offsets(mid_rec, index, offsets_, n, |
| 461 |
UT_LOCATION_HERE, &heap); |
| 462 |
ut_a(n <= offsets[0]); |
| 463 |
ut_a(n <= offsets[1]); |
| 464 |
ut_a(n <= cached_offsets[0]); |
| 465 |
ut_a(n <= cached_offsets[1]); |
| 466 |
for (size_t i = 0; i < n; ++i) { |
| 467 |
ulint len; |
| 468 |
ulint len2; |
| 469 |
|
| 470 |
const auto off = rec_get_nth_field(index, mid_rec, offsets, i, &len); |
| 471 |
const auto off2 = |
| 472 |
rec_get_nth_field(index, mid_rec, cached_offsets, i, &len2); |
| 473 |
ut_a(off == off2); |
| 474 |
ut_a(len == len2); |
| 475 |
} |
| 476 |
} |
| 477 |
#endif |
| 478 |
return cached_offsets; |
| 479 |
} |
| 480 |
return rec_get_offsets(mid_rec, index, offsets_, |
| 481 |
dtuple_get_n_fields_cmp(tuple), UT_LOCATION_HERE, |
| 482 |
&heap); |
| 483 |
}; |
| 484 |
|
| 485 |
/* Perform binary search until the lower and upper limit directory |
| 486 |
slots come to the distance 1 of each other */ |
| 487 |
|
| 488 |
while (up - low > 1) { |
| 489 |
mid = (low + up) / 2; |
| 490 |
slot = page_dir_get_nth_slot(page, mid); |
| 491 |
mid_rec = page_dir_slot_get_rec(slot); |
| 492 |
|
| 493 |
cur_matched_fields = std::min(low_matched_fields, up_matched_fields); |
| 494 |
|
| 495 |
auto offsets = get_mid_rec_offsets(); |
| 496 |
|
| 497 |
cmp = tuple->compare(mid_rec, index, offsets, &cur_matched_fields); |
| 498 |
|
| 499 |
if (cmp > 0) { |
| 500 |
low_slot_match: |
| 501 |
low = mid; |
| 502 |
low_matched_fields = cur_matched_fields; |
| 503 |
|
| 504 |
} else if (cmp) { |
| 505 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 506 |
if (mode == PAGE_CUR_LE_OR_EXTENDS && |
| 507 |
page_cur_rec_field_extends(tuple, mid_rec, offsets, |
| 508 |
cur_matched_fields, index)) { |
| 509 |
goto low_slot_match; |
| 510 |
} |
| 511 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 512 |
up_slot_match: |
| 513 |
up = mid; |
| 514 |
up_matched_fields = cur_matched_fields; |
| 515 |
|
| 516 |
} else if (mode == PAGE_CUR_G || mode == PAGE_CUR_LE |
| 517 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 518 |
|| mode == PAGE_CUR_LE_OR_EXTENDS |
| 519 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 520 |
) { |
| 521 |
goto low_slot_match; |
| 522 |
} else { |
| 523 |
goto up_slot_match; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
slot = page_dir_get_nth_slot(page, low); |
| 528 |
low_rec = page_dir_slot_get_rec(slot); |
| 529 |
slot = page_dir_get_nth_slot(page, up); |
| 530 |
up_rec = page_dir_slot_get_rec(slot); |
| 531 |
|
| 532 |
/* Perform linear search until the upper and lower records come to |
| 533 |
distance 1 of each other. */ |
| 534 |
|
| 535 |
while (page_rec_get_next_const(low_rec) != up_rec) { |
| 536 |
mid_rec = page_rec_get_next_const(low_rec); |
| 537 |
|
| 538 |
cur_matched_fields = std::min(low_matched_fields, up_matched_fields); |
| 539 |
|
| 540 |
auto offsets = get_mid_rec_offsets(); |
| 541 |
|
| 542 |
cmp = tuple->compare(mid_rec, index, offsets, &cur_matched_fields); |
| 543 |
|
| 544 |
if (cmp > 0) { |
| 545 |
low_rec_match: |
| 546 |
low_rec = mid_rec; |
| 547 |
low_matched_fields = cur_matched_fields; |
| 548 |
|
| 549 |
} else if (cmp) { |
| 550 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 551 |
if (mode == PAGE_CUR_LE_OR_EXTENDS && |
| 552 |
page_cur_rec_field_extends(tuple, mid_rec, offsets, |
| 553 |
cur_matched_fields, index)) { |
| 554 |
goto low_rec_match; |
| 555 |
} |
| 556 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 557 |
up_rec_match: |
| 558 |
up_rec = mid_rec; |
| 559 |
up_matched_fields = cur_matched_fields; |
| 560 |
} else if (mode == PAGE_CUR_G || mode == PAGE_CUR_LE |
| 561 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 562 |
|| mode == PAGE_CUR_LE_OR_EXTENDS |
| 563 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 564 |
) { |
| 565 |
if (!cmp && !cur_matched_fields) { |
| 566 |
#ifdef UNIV_DEBUG |
| 567 |
mtr_t mtr; |
| 568 |
mtr_start(&mtr); |
| 569 |
|
| 570 |
/* We got a match, but cur_matched_fields is |
| 571 |
0, it must have REC_INFO_MIN_REC_FLAG */ |
| 572 |
ulint rec_info = rec_get_info_bits(mid_rec, rec_offs_comp(offsets)); |
| 573 |
ut_ad(rec_info & REC_INFO_MIN_REC_FLAG); |
| 574 |
ut_ad(btr_page_get_prev(page, &mtr) == FIL_NULL); |
| 575 |
mtr_commit(&mtr); |
| 576 |
#endif |
| 577 |
|
| 578 |
cur_matched_fields = dtuple_get_n_fields_cmp(tuple); |
| 579 |
} |
| 580 |
|
| 581 |
goto low_rec_match; |
| 582 |
} else { |
| 583 |
goto up_rec_match; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
if (mode <= PAGE_CUR_GE) { |
| 588 |
page_cur_position(up_rec, block, cursor); |
| 589 |
} else { |
| 590 |
page_cur_position(low_rec, block, cursor); |
| 591 |
} |
| 592 |
|
| 593 |
*iup_matched_fields = up_matched_fields; |
| 594 |
*ilow_matched_fields = low_matched_fields; |
| 595 |
if (UNIV_LIKELY_NULL(heap)) { |
| 596 |
mem_heap_free(heap); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
/** Search the right position for a page cursor. |
| 601 |
@param[in] block buffer block |
| 602 |
@param[in] index index tree |
| 603 |
@param[in] tuple key to be searched for |
| 604 |
@param[in] mode search mode |
| 605 |
@param[in,out] iup_matched_fields already matched fields in the |
| 606 |
upper limit record |
| 607 |
@param[in,out] iup_matched_bytes already matched bytes in the |
| 608 |
first partially matched field in the upper limit record |
| 609 |
@param[in,out] ilow_matched_fields already matched fields in the |
| 610 |
lower limit record |
| 611 |
@param[in,out] ilow_matched_bytes already matched bytes in the |
| 612 |
first partially matched field in the lower limit record |
| 613 |
@param[out] cursor page cursor */ |
| 614 |
void page_cur_search_with_match_bytes( |
| 615 |
const buf_block_t *block, const dict_index_t *index, const dtuple_t *tuple, |
| 616 |
page_cur_mode_t mode, ulint *iup_matched_fields, ulint *iup_matched_bytes, |
| 617 |
ulint *ilow_matched_fields, ulint *ilow_matched_bytes, page_cur_t *cursor) { |
| 618 |
ulint up; |
| 619 |
ulint low; |
| 620 |
ulint mid; |
| 621 |
const page_t *page; |
| 622 |
const page_dir_slot_t *slot; |
| 623 |
const rec_t *up_rec; |
| 624 |
const rec_t *low_rec; |
| 625 |
const rec_t *mid_rec; |
| 626 |
ulint up_matched_fields; |
| 627 |
ulint up_matched_bytes; |
| 628 |
ulint low_matched_fields; |
| 629 |
ulint low_matched_bytes; |
| 630 |
ulint cur_matched_fields; |
| 631 |
ulint cur_matched_bytes; |
| 632 |
int cmp; |
| 633 |
#ifdef UNIV_ZIP_DEBUG |
| 634 |
const page_zip_des_t *page_zip = buf_block_get_page_zip(block); |
| 635 |
#endif /* UNIV_ZIP_DEBUG */ |
| 636 |
mem_heap_t *heap = nullptr; |
| 637 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 638 |
ulint *offsets = offsets_; |
| 639 |
rec_offs_init(offsets_); |
| 640 |
|
| 641 |
ut_ad(dtuple_validate(tuple)); |
| 642 |
#ifdef UNIV_DEBUG |
| 643 |
#ifdef PAGE_CUR_DBG |
| 644 |
if (mode != PAGE_CUR_DBG) |
| 645 |
#endif /* PAGE_CUR_DBG */ |
| 646 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 647 |
if (mode != PAGE_CUR_LE_OR_EXTENDS) |
| 648 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 649 |
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE || mode == PAGE_CUR_G || |
| 650 |
mode == PAGE_CUR_GE); |
| 651 |
#endif /* UNIV_DEBUG */ |
| 652 |
page = buf_block_get_frame(block); |
| 653 |
#ifdef UNIV_ZIP_DEBUG |
| 654 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 655 |
#endif /* UNIV_ZIP_DEBUG */ |
| 656 |
|
| 657 |
ut_d(page_check_dir(page)); |
| 658 |
|
| 659 |
#ifdef PAGE_CUR_ADAPT |
| 660 |
if (page_is_leaf(page) && (mode == PAGE_CUR_LE) && |
| 661 |
(page_header_get_field(page, PAGE_N_DIRECTION) > 3) && |
| 662 |
(page_header_get_ptr(page, PAGE_LAST_INSERT)) && |
| 663 |
(page_header_get_field(page, PAGE_DIRECTION) == PAGE_RIGHT)) { |
| 664 |
if (page_cur_try_search_shortcut_bytes( |
| 665 |
block, index, tuple, iup_matched_fields, iup_matched_bytes, |
| 666 |
ilow_matched_fields, ilow_matched_bytes, cursor)) { |
| 667 |
return; |
| 668 |
} |
| 669 |
} |
| 670 |
#ifdef PAGE_CUR_DBG |
| 671 |
if (mode == PAGE_CUR_DBG) { |
| 672 |
mode = PAGE_CUR_LE; |
| 673 |
} |
| 674 |
#endif |
| 675 |
#endif |
| 676 |
|
| 677 |
/* The following flag does not work for non-latin1 char sets because |
| 678 |
cmp_full_field does not tell how many bytes matched */ |
| 679 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 680 |
ut_a(mode != PAGE_CUR_LE_OR_EXTENDS); |
| 681 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 682 |
|
| 683 |
/* If mode PAGE_CUR_G is specified, we are trying to position the |
| 684 |
cursor to answer a query of the form "tuple < X", where tuple is |
| 685 |
the input parameter, and X denotes an arbitrary physical record on |
| 686 |
the page. We want to position the cursor on the first X which |
| 687 |
satisfies the condition. */ |
| 688 |
|
| 689 |
up_matched_fields = *iup_matched_fields; |
| 690 |
up_matched_bytes = *iup_matched_bytes; |
| 691 |
low_matched_fields = *ilow_matched_fields; |
| 692 |
low_matched_bytes = *ilow_matched_bytes; |
| 693 |
|
| 694 |
/* Perform binary search. First the search is done through the page |
| 695 |
directory, after that as a linear search in the list of records |
| 696 |
owned by the upper limit directory slot. */ |
| 697 |
|
| 698 |
low = 0; |
| 699 |
up = page_dir_get_n_slots(page) - 1; |
| 700 |
|
| 701 |
/* Perform binary search until the lower and upper limit directory |
| 702 |
slots come to the distance 1 of each other */ |
| 703 |
|
| 704 |
while (up - low > 1) { |
| 705 |
mid = (low + up) / 2; |
| 706 |
slot = page_dir_get_nth_slot(page, mid); |
| 707 |
mid_rec = page_dir_slot_get_rec(slot); |
| 708 |
|
| 709 |
ut_pair_min(&cur_matched_fields, &cur_matched_bytes, low_matched_fields, |
| 710 |
low_matched_bytes, up_matched_fields, up_matched_bytes); |
| 711 |
|
| 712 |
offsets = rec_get_offsets(mid_rec, index, offsets_, |
| 713 |
dtuple_get_n_fields_cmp(tuple), UT_LOCATION_HERE, |
| 714 |
&heap); |
| 715 |
|
| 716 |
cmp = cmp_dtuple_rec_with_match_bytes(tuple, mid_rec, index, offsets, |
| 717 |
&cur_matched_fields, |
| 718 |
&cur_matched_bytes); |
| 719 |
|
| 720 |
if (cmp > 0) { |
| 721 |
low_slot_match: |
| 722 |
low = mid; |
| 723 |
low_matched_fields = cur_matched_fields; |
| 724 |
low_matched_bytes = cur_matched_bytes; |
| 725 |
|
| 726 |
} else if (cmp) { |
| 727 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 728 |
if (mode == PAGE_CUR_LE_OR_EXTENDS && |
| 729 |
page_cur_rec_field_extends(tuple, mid_rec, offsets, |
| 730 |
cur_matched_fields, index)) { |
| 731 |
goto low_slot_match; |
| 732 |
} |
| 733 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 734 |
up_slot_match: |
| 735 |
up = mid; |
| 736 |
up_matched_fields = cur_matched_fields; |
| 737 |
up_matched_bytes = cur_matched_bytes; |
| 738 |
|
| 739 |
} else if (mode == PAGE_CUR_G || mode == PAGE_CUR_LE |
| 740 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 741 |
|| mode == PAGE_CUR_LE_OR_EXTENDS |
| 742 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 743 |
) { |
| 744 |
goto low_slot_match; |
| 745 |
} else { |
| 746 |
goto up_slot_match; |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
slot = page_dir_get_nth_slot(page, low); |
| 751 |
low_rec = page_dir_slot_get_rec(slot); |
| 752 |
slot = page_dir_get_nth_slot(page, up); |
| 753 |
up_rec = page_dir_slot_get_rec(slot); |
| 754 |
|
| 755 |
/* Perform linear search until the upper and lower records come to |
| 756 |
distance 1 of each other. */ |
| 757 |
|
| 758 |
while (page_rec_get_next_const(low_rec) != up_rec) { |
| 759 |
mid_rec = page_rec_get_next_const(low_rec); |
| 760 |
|
| 761 |
ut_pair_min(&cur_matched_fields, &cur_matched_bytes, low_matched_fields, |
| 762 |
low_matched_bytes, up_matched_fields, up_matched_bytes); |
| 763 |
|
| 764 |
offsets = rec_get_offsets(mid_rec, index, offsets_, |
| 765 |
dtuple_get_n_fields_cmp(tuple), UT_LOCATION_HERE, |
| 766 |
&heap); |
| 767 |
|
| 768 |
cmp = cmp_dtuple_rec_with_match_bytes(tuple, mid_rec, index, offsets, |
| 769 |
&cur_matched_fields, |
| 770 |
&cur_matched_bytes); |
| 771 |
|
| 772 |
if (cmp > 0) { |
| 773 |
low_rec_match: |
| 774 |
low_rec = mid_rec; |
| 775 |
low_matched_fields = cur_matched_fields; |
| 776 |
low_matched_bytes = cur_matched_bytes; |
| 777 |
|
| 778 |
} else if (cmp) { |
| 779 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 780 |
if (mode == PAGE_CUR_LE_OR_EXTENDS && |
| 781 |
page_cur_rec_field_extends(tuple, mid_rec, offsets, |
| 782 |
cur_matched_fields, index)) { |
| 783 |
goto low_rec_match; |
| 784 |
} |
| 785 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 786 |
up_rec_match: |
| 787 |
up_rec = mid_rec; |
| 788 |
up_matched_fields = cur_matched_fields; |
| 789 |
up_matched_bytes = cur_matched_bytes; |
| 790 |
} else if (mode == PAGE_CUR_G || mode == PAGE_CUR_LE |
| 791 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 792 |
|| mode == PAGE_CUR_LE_OR_EXTENDS |
| 793 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 794 |
) { |
| 795 |
if (!cmp && !cur_matched_fields) { |
| 796 |
#ifdef UNIV_DEBUG |
| 797 |
mtr_t mtr; |
| 798 |
mtr_start(&mtr); |
| 799 |
|
| 800 |
/* We got a match, but cur_matched_fields is |
| 801 |
0, it must have REC_INFO_MIN_REC_FLAG */ |
| 802 |
ulint rec_info = rec_get_info_bits(mid_rec, rec_offs_comp(offsets)); |
| 803 |
ut_ad(rec_info & REC_INFO_MIN_REC_FLAG); |
| 804 |
ut_ad(btr_page_get_prev(page, &mtr) == FIL_NULL); |
| 805 |
mtr_commit(&mtr); |
| 806 |
#endif |
| 807 |
|
| 808 |
cur_matched_fields = dtuple_get_n_fields_cmp(tuple); |
| 809 |
} |
| 810 |
|
| 811 |
goto low_rec_match; |
| 812 |
} else { |
| 813 |
goto up_rec_match; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
if (mode <= PAGE_CUR_GE) { |
| 818 |
page_cur_position(up_rec, block, cursor); |
| 819 |
} else { |
| 820 |
page_cur_position(low_rec, block, cursor); |
| 821 |
} |
| 822 |
|
| 823 |
*iup_matched_fields = up_matched_fields; |
| 824 |
*iup_matched_bytes = up_matched_bytes; |
| 825 |
*ilow_matched_fields = low_matched_fields; |
| 826 |
*ilow_matched_bytes = low_matched_bytes; |
| 827 |
if (UNIV_LIKELY_NULL(heap)) { |
| 828 |
mem_heap_free(heap); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
/** Positions a page cursor on a randomly chosen user record on a page. If there |
| 833 |
are no user records, sets the cursor on the infimum record. */ |
| 834 |
void page_cur_open_on_rnd_user_rec(buf_block_t *block, /*!< in: page */ |
| 835 |
page_cur_t *cursor) /*!< out: page cursor */ |
| 836 |
{ |
| 837 |
ulint rnd; |
| 838 |
ulint n_recs = page_get_n_recs(buf_block_get_frame(block)); |
| 839 |
|
| 840 |
page_cur_set_before_first(block, cursor); |
| 841 |
|
| 842 |
if (UNIV_UNLIKELY(n_recs == 0)) { |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
rnd = (ulint)(page_cur_lcg_prng() % n_recs); |
| 847 |
|
| 848 |
do { |
| 849 |
page_cur_move_to_next(cursor); |
| 850 |
} while (rnd--); |
| 851 |
} |
| 852 |
|
| 853 |
/** Writes the log record of a record insert on a page. */ |
| 854 |
static void page_cur_insert_rec_write_log( |
| 855 |
rec_t *insert_rec, /*!< in: inserted physical record */ |
| 856 |
ulint rec_size, /*!< in: insert_rec size */ |
| 857 |
rec_t *cursor_rec, /*!< in: record the |
| 858 |
cursor is pointing to */ |
| 859 |
dict_index_t *index, /*!< in: record descriptor */ |
| 860 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 861 |
{ |
| 862 |
ulint cur_rec_size; |
| 863 |
ulint extra_size; |
| 864 |
ulint cur_extra_size; |
| 865 |
const byte *ins_ptr; |
| 866 |
const byte *log_end; |
| 867 |
ulint i; |
| 868 |
|
| 869 |
/* Avoid REDO logging to save on costly IO because |
| 870 |
temporary tables are not recovered during crash recovery. */ |
| 871 |
if (index->table->is_temporary()) { |
| 872 |
byte *log_ptr = nullptr; |
| 873 |
if (!mlog_open(mtr, 0, log_ptr)) { |
| 874 |
return; |
| 875 |
} |
| 876 |
mlog_close(mtr, log_ptr); |
| 877 |
log_ptr = nullptr; |
| 878 |
} |
| 879 |
|
| 880 |
ut_a(rec_size < UNIV_PAGE_SIZE); |
| 881 |
ut_ad(page_align(insert_rec) == page_align(cursor_rec)); |
| 882 |
ut_ad(!page_rec_is_comp(insert_rec) == !dict_table_is_comp(index->table)); |
| 883 |
|
| 884 |
{ |
| 885 |
mem_heap_t *heap = nullptr; |
| 886 |
ulint cur_offs_[REC_OFFS_NORMAL_SIZE]; |
| 887 |
ulint ins_offs_[REC_OFFS_NORMAL_SIZE]; |
| 888 |
|
| 889 |
ulint *cur_offs; |
| 890 |
ulint *ins_offs; |
| 891 |
|
| 892 |
rec_offs_init(cur_offs_); |
| 893 |
rec_offs_init(ins_offs_); |
| 894 |
|
| 895 |
cur_offs = rec_get_offsets(cursor_rec, index, cur_offs_, ULINT_UNDEFINED, |
| 896 |
UT_LOCATION_HERE, &heap); |
| 897 |
ins_offs = rec_get_offsets(insert_rec, index, ins_offs_, ULINT_UNDEFINED, |
| 898 |
UT_LOCATION_HERE, &heap); |
| 899 |
|
| 900 |
extra_size = rec_offs_extra_size(ins_offs); |
| 901 |
ut_ad(rec_size == rec_offs_size(ins_offs)); |
| 902 |
|
| 903 |
cur_rec_size = rec_offs_size(cur_offs); |
| 904 |
cur_extra_size = rec_offs_extra_size(cur_offs); |
| 905 |
|
| 906 |
if (heap != nullptr) { |
| 907 |
mem_heap_free(heap); |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
ins_ptr = insert_rec - extra_size; |
| 912 |
|
| 913 |
i = 0; |
| 914 |
|
| 915 |
uint8_t cur_version = 0; |
| 916 |
uint8_t ins_version = 0; |
| 917 |
if (index->has_row_versions()) { |
| 918 |
const bool is_cmp = page_rec_is_comp(insert_rec); |
| 919 |
|
| 920 |
auto has_version = [is_cmp](rec_t *rec) { |
| 921 |
return (is_cmp ? rec_new_is_versioned(rec) : rec_old_is_versioned(rec)); |
| 922 |
}; |
| 923 |
|
| 924 |
if (has_version(cursor_rec)) { |
| 925 |
cur_version = is_cmp ? rec_get_instant_row_version_new(cursor_rec) |
| 926 |
: rec_get_instant_row_version_old(cursor_rec); |
| 927 |
} |
| 928 |
|
| 929 |
/* New records always have the version except the case when records are |
| 930 |
being moved from one page to another (pessimistic, reorg) */ |
| 931 |
if (has_version(insert_rec)) { |
| 932 |
ins_version = is_cmp ? rec_get_instant_row_version_new(insert_rec) |
| 933 |
: rec_get_instant_row_version_old(insert_rec); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
/* If versions are different, then don't compare the records */ |
| 938 |
bool is_same_version = (cur_version == ins_version); |
| 939 |
DBUG_EXECUTE_IF("page_ins_simulate_differing_versions", |
| 940 |
is_same_version = false;); |
| 941 |
|
| 942 |
if (is_same_version && cur_extra_size == extra_size) { |
| 943 |
ulint min_rec_size = std::min(cur_rec_size, rec_size); |
| 944 |
|
| 945 |
const byte *cur_ptr = cursor_rec - cur_extra_size; |
| 946 |
|
| 947 |
/* Find out the first byte in insert_rec which differs from |
| 948 |
cursor_rec; skip the bytes in the record info */ |
| 949 |
|
| 950 |
do { |
| 951 |
if (*ins_ptr == *cur_ptr) { |
| 952 |
i++; |
| 953 |
ins_ptr++; |
| 954 |
cur_ptr++; |
| 955 |
} else if ((i < extra_size) && |
| 956 |
(i >= extra_size - page_rec_get_base_extra_size(insert_rec))) { |
| 957 |
i = extra_size; |
| 958 |
ins_ptr = insert_rec; |
| 959 |
cur_ptr = cursor_rec; |
| 960 |
} else { |
| 961 |
break; |
| 962 |
} |
| 963 |
} while (i < min_rec_size); |
| 964 |
} |
| 965 |
|
| 966 |
/* Length needed on REDO log : |
| 967 |
11 -> REDO_LOG_INITIAL_INFO_SIZE |
| 968 |
2 -> cursor rec offset |
| 969 |
5 -> record end segment length |
| 970 |
1 -> info bits |
| 971 |
5 -> record origin offset |
| 972 |
5 -> mismatch index */ |
| 973 |
byte *log_ptr = nullptr; |
| 974 |
|
| 975 |
if (mtr_get_log_mode(mtr) != MTR_LOG_SHORT_INSERTS) { |
| 976 |
if (!mlog_open_and_write_index(mtr, insert_rec, index, MLOG_REC_INSERT, |
| 977 |
2 + 5 + 1 + 5 + 5 + MLOG_BUF_MARGIN, |
| 978 |
log_ptr)) { |
| 979 |
/* Logging in mtr is switched off during crash recovery: in that case |
| 980 |
mlog_open returns NULL */ |
| 981 |
return; |
| 982 |
} |
| 983 |
|
| 984 |
log_end = &log_ptr[2 + 5 + 1 + 5 + 5 + MLOG_BUF_MARGIN]; |
| 985 |
/* Write the cursor rec offset as a 2-byte ulint */ |
| 986 |
mach_write_to_2(log_ptr, page_offset(cursor_rec)); |
| 987 |
log_ptr += 2; |
| 988 |
} else { |
| 989 |
if (!mlog_open(mtr, 5 + 1 + 5 + 5 + MLOG_BUF_MARGIN, log_ptr)) { |
| 990 |
/* Logging in mtr is switched off during crash |
| 991 |
recovery: in that case mlog_open returns NULL */ |
| 992 |
return; |
| 993 |
} |
| 994 |
log_end = &log_ptr[5 + 1 + 5 + 5 + MLOG_BUF_MARGIN]; |
| 995 |
} |
| 996 |
|
| 997 |
if (page_rec_is_comp(insert_rec)) { |
| 998 |
if (UNIV_UNLIKELY(rec_get_info_and_status_bits(insert_rec, true) != |
| 999 |
rec_get_info_and_status_bits(cursor_rec, true))) { |
| 1000 |
goto need_extra_info; |
| 1001 |
} |
| 1002 |
} else { |
| 1003 |
if (UNIV_UNLIKELY(rec_get_info_and_status_bits(insert_rec, false) != |
| 1004 |
rec_get_info_and_status_bits(cursor_rec, false))) { |
| 1005 |
goto need_extra_info; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
if (extra_size != cur_extra_size || rec_size != cur_rec_size || |
| 1010 |
cur_version != ins_version) { |
| 1011 |
need_extra_info: |
| 1012 |
/* Write the record end segment length |
| 1013 |
and the extra info storage flag */ |
| 1014 |
log_ptr += mach_write_compressed(log_ptr, 2 * (rec_size - i) + 1); |
| 1015 |
|
| 1016 |
/* Write the info bits */ |
| 1017 |
mach_write_to_1(log_ptr, rec_get_info_and_status_bits( |
| 1018 |
insert_rec, page_rec_is_comp(insert_rec))); |
| 1019 |
log_ptr++; |
| 1020 |
|
| 1021 |
/* Write the record origin offset */ |
| 1022 |
log_ptr += mach_write_compressed(log_ptr, extra_size); |
| 1023 |
|
| 1024 |
/* Write the mismatch index */ |
| 1025 |
log_ptr += mach_write_compressed(log_ptr, i); |
| 1026 |
|
| 1027 |
ut_a(i < UNIV_PAGE_SIZE); |
| 1028 |
ut_a(extra_size < UNIV_PAGE_SIZE); |
| 1029 |
} else { |
| 1030 |
/* Write the record end segment length |
| 1031 |
and the extra info storage flag */ |
| 1032 |
log_ptr += mach_write_compressed(log_ptr, 2 * (rec_size - i)); |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* Write to the log the inserted index record end segment which |
| 1036 |
differs from the cursor record */ |
| 1037 |
|
| 1038 |
rec_size -= i; |
| 1039 |
|
| 1040 |
if (log_ptr + rec_size <= log_end) { |
| 1041 |
memcpy(log_ptr, ins_ptr, rec_size); |
| 1042 |
mlog_close(mtr, log_ptr + rec_size); |
| 1043 |
} else { |
| 1044 |
mlog_close(mtr, log_ptr); |
| 1045 |
ut_a(rec_size < UNIV_PAGE_SIZE); |
| 1046 |
mlog_catenate_string(mtr, ins_ptr, rec_size); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
#else /* !UNIV_HOTBACKUP */ |
| 1050 |
#define page_cur_insert_rec_write_log(ins_rec, size, cur, index, mtr) ((void)0) |
| 1051 |
#endif /* !UNIV_HOTBACKUP */ |
| 1052 |
|
| 1053 |
/** Parses a log record of a record insert on a page. |
| 1054 |
@return end of log record or NULL */ |
| 1055 |
byte *page_cur_parse_insert_rec( |
| 1056 |
bool is_short, /*!< in: true if short inserts */ |
| 1057 |
const byte *ptr, /*!< in: buffer */ |
| 1058 |
const byte *end_ptr, /*!< in: buffer end */ |
| 1059 |
buf_block_t *block, /*!< in: page or NULL */ |
| 1060 |
dict_index_t *index, /*!< in: record descriptor */ |
| 1061 |
mtr_t *mtr) /*!< in: mtr or NULL */ |
| 1062 |
{ |
| 1063 |
ulint origin_offset = 0; /* remove warning */ |
| 1064 |
ulint end_seg_len; |
| 1065 |
ulint mismatch_index = 0; /* remove warning */ |
| 1066 |
page_t *page; |
| 1067 |
rec_t *cursor_rec{nullptr}; |
| 1068 |
byte buf1[1024]; |
| 1069 |
byte *buf; |
| 1070 |
const byte *ptr2 = ptr; |
| 1071 |
ulint info_and_status_bits = 0; /* remove warning */ |
| 1072 |
page_cur_t cursor; |
| 1073 |
mem_heap_t *heap = nullptr; |
| 1074 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1075 |
ulint *offsets = offsets_; |
| 1076 |
rec_offs_init(offsets_); |
| 1077 |
|
| 1078 |
page = block ? buf_block_get_frame(block) : nullptr; |
| 1079 |
|
| 1080 |
if (is_short) { |
| 1081 |
cursor_rec = page_rec_get_prev(page_get_supremum_rec(page)); |
| 1082 |
} else { |
| 1083 |
ulint offset; |
| 1084 |
|
| 1085 |
/* Read the cursor rec offset as a 2-byte ulint */ |
| 1086 |
|
| 1087 |
if (UNIV_UNLIKELY(end_ptr < ptr + 2)) { |
| 1088 |
return (nullptr); |
| 1089 |
} |
| 1090 |
|
| 1091 |
offset = mach_read_from_2(ptr); |
| 1092 |
ptr += 2; |
| 1093 |
|
| 1094 |
if (page != nullptr) cursor_rec = page + offset; |
| 1095 |
|
| 1096 |
if (offset >= UNIV_PAGE_SIZE) { |
| 1097 |
recv_sys->found_corrupt_log = true; |
| 1098 |
|
| 1099 |
return (nullptr); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
end_seg_len = mach_parse_compressed(&ptr, end_ptr); |
| 1104 |
|
| 1105 |
if (ptr == nullptr) { |
| 1106 |
return (nullptr); |
| 1107 |
} |
| 1108 |
|
| 1109 |
if (end_seg_len >= UNIV_PAGE_SIZE << 1) { |
| 1110 |
recv_sys->found_corrupt_log = true; |
| 1111 |
|
| 1112 |
return (nullptr); |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (end_seg_len & 0x1UL) { |
| 1116 |
/* Read the info bits */ |
| 1117 |
|
| 1118 |
if (end_ptr < ptr + 1) { |
| 1119 |
return (nullptr); |
| 1120 |
} |
| 1121 |
|
| 1122 |
info_and_status_bits = mach_read_from_1(ptr); |
| 1123 |
ptr++; |
| 1124 |
|
| 1125 |
origin_offset = mach_parse_compressed(&ptr, end_ptr); |
| 1126 |
|
| 1127 |
if (ptr == nullptr) { |
| 1128 |
return (nullptr); |
| 1129 |
} |
| 1130 |
|
| 1131 |
ut_a(origin_offset < UNIV_PAGE_SIZE); |
| 1132 |
|
| 1133 |
mismatch_index = mach_parse_compressed(&ptr, end_ptr); |
| 1134 |
|
| 1135 |
if (ptr == nullptr) { |
| 1136 |
return (nullptr); |
| 1137 |
} |
| 1138 |
|
| 1139 |
ut_a(mismatch_index < UNIV_PAGE_SIZE); |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (end_ptr < ptr + (end_seg_len >> 1)) { |
| 1143 |
return (nullptr); |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (!block) { |
| 1147 |
return (const_cast<byte *>(ptr + (end_seg_len >> 1))); |
| 1148 |
} |
| 1149 |
|
| 1150 |
ut_ad(page_is_comp(page) == dict_table_is_comp(index->table)); |
| 1151 |
ut_ad(!buf_block_get_page_zip(block) || page_is_comp(page)); |
| 1152 |
|
| 1153 |
/* Read from the log the inserted index record end segment which |
| 1154 |
differs from the cursor record */ |
| 1155 |
|
| 1156 |
if ((end_seg_len & 0x1UL) && mismatch_index == 0) { |
| 1157 |
/* This is a record has nothing common to cursor record. */ |
| 1158 |
} else { |
| 1159 |
offsets = rec_get_offsets(cursor_rec, index, offsets, ULINT_UNDEFINED, |
| 1160 |
UT_LOCATION_HERE, &heap); |
| 1161 |
|
| 1162 |
if (!(end_seg_len & 0x1UL)) { |
| 1163 |
info_and_status_bits = |
| 1164 |
rec_get_info_and_status_bits(cursor_rec, page_is_comp(page)); |
| 1165 |
origin_offset = rec_offs_extra_size(offsets); |
| 1166 |
mismatch_index = rec_offs_size(offsets) - (end_seg_len >> 1); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
end_seg_len >>= 1; |
| 1171 |
|
| 1172 |
if (mismatch_index + end_seg_len < sizeof buf1) { |
| 1173 |
buf = buf1; |
| 1174 |
} else { |
| 1175 |
buf = static_cast<byte *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 1176 |
mismatch_index + end_seg_len)); |
| 1177 |
} |
| 1178 |
|
| 1179 |
/* Build the inserted record to buf */ |
| 1180 |
|
| 1181 |
if (UNIV_UNLIKELY(mismatch_index >= UNIV_PAGE_SIZE)) { |
| 1182 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_859) |
| 1183 |
<< "is_short " << is_short << ", " |
| 1184 |
<< "info_and_status_bits " << info_and_status_bits << ", offset " |
| 1185 |
<< page_offset(cursor_rec) |
| 1186 |
<< "," |
| 1187 |
" o_offset " |
| 1188 |
<< origin_offset << ", mismatch index " << mismatch_index |
| 1189 |
<< ", end_seg_len " << end_seg_len << " parsed len " << (ptr - ptr2); |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (mismatch_index) { |
| 1193 |
ut_memcpy(buf, rec_get_start(cursor_rec, offsets), mismatch_index); |
| 1194 |
} |
| 1195 |
ut_memcpy(buf + mismatch_index, ptr, end_seg_len); |
| 1196 |
|
| 1197 |
if (page_is_comp(page)) { |
| 1198 |
rec_set_info_and_status_bits(buf + origin_offset, info_and_status_bits); |
| 1199 |
} else { |
| 1200 |
rec_set_info_bits_old(buf + origin_offset, info_and_status_bits); |
| 1201 |
} |
| 1202 |
|
| 1203 |
page_cur_position(cursor_rec, block, &cursor); |
| 1204 |
|
| 1205 |
offsets = rec_get_offsets(buf + origin_offset, index, offsets, |
| 1206 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 1207 |
if (UNIV_UNLIKELY(!page_cur_rec_insert(&cursor, buf + origin_offset, index, |
| 1208 |
offsets, mtr))) { |
| 1209 |
/* The redo log record should only have been written |
| 1210 |
after the write was successful. */ |
| 1211 |
ut_error; |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (buf != buf1) { |
| 1215 |
ut::free(buf); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1219 |
mem_heap_free(heap); |
| 1220 |
} |
| 1221 |
|
| 1222 |
return (const_cast<byte *>(ptr + end_seg_len)); |
| 1223 |
} |
| 1224 |
|
| 1225 |
/** Inserts a record next to page cursor on an uncompressed page. |
| 1226 |
Returns pointer to inserted record if succeed, i.e., enough |
| 1227 |
space available, NULL otherwise. The cursor stays at the same position. |
| 1228 |
@return pointer to record if succeed, NULL otherwise */ |
| 1229 |
rec_t *page_cur_insert_rec_low( |
| 1230 |
rec_t *current_rec, /*!< in: pointer to current record after |
| 1231 |
which the new record is inserted */ |
| 1232 |
dict_index_t *index, /*!< in: record descriptor */ |
| 1233 |
const rec_t *rec, /*!< in: pointer to a physical record */ |
| 1234 |
ulint *offsets, /*!< in/out: rec_get_offsets(rec, index) */ |
| 1235 |
mtr_t *mtr) /*!< in: mini-transaction handle, or NULL */ |
| 1236 |
{ |
| 1237 |
byte *insert_buf; |
| 1238 |
ulint rec_size; |
| 1239 |
page_t *page; /*!< the relevant page */ |
| 1240 |
rec_t *last_insert; /*!< cursor position at previous |
| 1241 |
insert */ |
| 1242 |
rec_t *free_rec; /*!< a free record that was reused, |
| 1243 |
or NULL */ |
| 1244 |
rec_t *insert_rec; /*!< inserted record */ |
| 1245 |
ulint heap_no; /*!< heap number of the inserted |
| 1246 |
record */ |
| 1247 |
|
| 1248 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 1249 |
|
| 1250 |
page = page_align(current_rec); |
| 1251 |
ut_ad(dict_table_is_comp(index->table) == page_is_comp(page)); |
| 1252 |
ut_ad(fil_page_index_page_check(page)); |
| 1253 |
ut_ad(mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID) == index->id || |
| 1254 |
recv_recovery_is_on() || |
| 1255 |
(mtr ? mtr->is_inside_ibuf() : dict_index_is_ibuf(index))); |
| 1256 |
|
| 1257 |
ut_ad(!page_rec_is_supremum(current_rec)); |
| 1258 |
|
| 1259 |
/* 1. Get the size of the physical record in the page */ |
| 1260 |
rec_size = rec_offs_size(offsets); |
| 1261 |
|
| 1262 |
#ifdef UNIV_DEBUG_VALGRIND |
| 1263 |
{ |
| 1264 |
const void *rec_start = rec - rec_offs_extra_size(offsets); |
| 1265 |
ulint extra_size = rec_offs_extra_size(offsets) - |
| 1266 |
(rec_offs_comp(offsets) ? REC_N_NEW_EXTRA_BYTES |
| 1267 |
: REC_N_OLD_EXTRA_BYTES); |
| 1268 |
|
| 1269 |
/* All data bytes of the record must be valid. */ |
| 1270 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 1271 |
/* The variable-length header must be valid. */ |
| 1272 |
UNIV_MEM_ASSERT_RW(rec_start, extra_size); |
| 1273 |
} |
| 1274 |
#endif /* UNIV_DEBUG_VALGRIND */ |
| 1275 |
|
| 1276 |
/* 2. Try to find suitable space from page memory management */ |
| 1277 |
|
| 1278 |
free_rec = page_header_get_ptr(page, PAGE_FREE); |
| 1279 |
if (UNIV_LIKELY_NULL(free_rec)) { |
| 1280 |
/* Try to allocate from the head of the free list. */ |
| 1281 |
ulint foffsets_[REC_OFFS_NORMAL_SIZE]; |
| 1282 |
ulint *foffsets = foffsets_; |
| 1283 |
mem_heap_t *heap = nullptr; |
| 1284 |
|
| 1285 |
rec_offs_init(foffsets_); |
| 1286 |
|
| 1287 |
foffsets = rec_get_offsets(free_rec, index, foffsets, ULINT_UNDEFINED, |
| 1288 |
UT_LOCATION_HERE, &heap); |
| 1289 |
|
| 1290 |
if (rec_offs_size(foffsets) < rec_size) { |
| 1291 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1292 |
mem_heap_free(heap); |
| 1293 |
} |
| 1294 |
|
| 1295 |
goto use_heap; |
| 1296 |
} |
| 1297 |
|
| 1298 |
insert_buf = free_rec - rec_offs_extra_size(foffsets); |
| 1299 |
|
| 1300 |
if (page_is_comp(page)) { |
| 1301 |
heap_no = rec_get_heap_no_new(free_rec); |
| 1302 |
page_mem_alloc_free(page, nullptr, rec_get_next_ptr(free_rec, true), |
| 1303 |
rec_size); |
| 1304 |
} else { |
| 1305 |
heap_no = rec_get_heap_no_old(free_rec); |
| 1306 |
page_mem_alloc_free(page, nullptr, rec_get_next_ptr(free_rec, false), |
| 1307 |
rec_size); |
| 1308 |
} |
| 1309 |
|
| 1310 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1311 |
mem_heap_free(heap); |
| 1312 |
} |
| 1313 |
} else { |
| 1314 |
use_heap: |
| 1315 |
free_rec = nullptr; |
| 1316 |
insert_buf = page_mem_alloc_heap(page, nullptr, rec_size, &heap_no); |
| 1317 |
|
| 1318 |
if (UNIV_UNLIKELY(insert_buf == nullptr)) { |
| 1319 |
return (nullptr); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* 3. Create the record */ |
| 1324 |
insert_rec = rec_copy(insert_buf, rec, offsets); |
| 1325 |
rec_offs_make_valid(insert_rec, index, offsets); |
| 1326 |
|
| 1327 |
/* 4. Insert the record in the linked list of records */ |
| 1328 |
ut_ad(current_rec != insert_rec); |
| 1329 |
|
| 1330 |
{ |
| 1331 |
/* next record after current before the insertion */ |
| 1332 |
rec_t *next_rec = page_rec_get_next(current_rec); |
| 1333 |
#ifdef UNIV_DEBUG |
| 1334 |
if (page_is_comp(page)) { |
| 1335 |
ut_ad(rec_get_status(current_rec) <= REC_STATUS_INFIMUM); |
| 1336 |
ut_ad(rec_get_status(insert_rec) < REC_STATUS_INFIMUM); |
| 1337 |
ut_ad(rec_get_status(next_rec) != REC_STATUS_INFIMUM); |
| 1338 |
} |
| 1339 |
#endif |
| 1340 |
page_rec_set_next(insert_rec, next_rec); |
| 1341 |
page_rec_set_next(current_rec, insert_rec); |
| 1342 |
} |
| 1343 |
|
| 1344 |
page_header_set_field(page, nullptr, PAGE_N_RECS, 1 + page_get_n_recs(page)); |
| 1345 |
|
| 1346 |
/* 5. Set the n_owned field in the inserted record to zero, |
| 1347 |
and set the heap_no field */ |
| 1348 |
if (page_is_comp(page)) { |
| 1349 |
rec_set_n_owned_new(insert_rec, nullptr, 0); |
| 1350 |
rec_set_heap_no_new(insert_rec, heap_no); |
| 1351 |
} else { |
| 1352 |
rec_set_n_owned_old(insert_rec, 0); |
| 1353 |
rec_set_heap_no_old(insert_rec, heap_no); |
| 1354 |
} |
| 1355 |
|
| 1356 |
UNIV_MEM_ASSERT_RW(rec_get_start(insert_rec, offsets), |
| 1357 |
rec_offs_size(offsets)); |
| 1358 |
/* 6. Update the last insertion info in page header */ |
| 1359 |
|
| 1360 |
last_insert = page_header_get_ptr(page, PAGE_LAST_INSERT); |
| 1361 |
ut_ad(!last_insert || !page_is_comp(page) || |
| 1362 |
rec_get_node_ptr_flag(last_insert) == |
| 1363 |
rec_get_node_ptr_flag(insert_rec)); |
| 1364 |
|
| 1365 |
if (!dict_index_is_spatial(index)) { |
| 1366 |
if (UNIV_UNLIKELY(last_insert == nullptr)) { |
| 1367 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 1368 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, 0); |
| 1369 |
|
| 1370 |
} else if ((last_insert == current_rec) && |
| 1371 |
(page_header_get_field(page, PAGE_DIRECTION) != PAGE_LEFT)) { |
| 1372 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_RIGHT); |
| 1373 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, |
| 1374 |
page_header_get_field(page, PAGE_N_DIRECTION) + 1); |
| 1375 |
|
| 1376 |
} else if ((page_rec_get_next(insert_rec) == last_insert) && |
| 1377 |
(page_header_get_field(page, PAGE_DIRECTION) != PAGE_RIGHT)) { |
| 1378 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_LEFT); |
| 1379 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, |
| 1380 |
page_header_get_field(page, PAGE_N_DIRECTION) + 1); |
| 1381 |
} else { |
| 1382 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 1383 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, 0); |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
page_header_set_ptr(page, nullptr, PAGE_LAST_INSERT, insert_rec); |
| 1388 |
|
| 1389 |
/* 7. It remains to update the owner record. */ |
| 1390 |
{ |
| 1391 |
rec_t *owner_rec = page_rec_find_owner_rec(insert_rec); |
| 1392 |
ulint n_owned; |
| 1393 |
if (page_is_comp(page)) { |
| 1394 |
n_owned = rec_get_n_owned_new(owner_rec); |
| 1395 |
rec_set_n_owned_new(owner_rec, nullptr, n_owned + 1); |
| 1396 |
} else { |
| 1397 |
n_owned = rec_get_n_owned_old(owner_rec); |
| 1398 |
rec_set_n_owned_old(owner_rec, n_owned + 1); |
| 1399 |
} |
| 1400 |
|
| 1401 |
/* 8. Now we have incremented the n_owned field of the owner |
| 1402 |
record. If the number exceeds PAGE_DIR_SLOT_MAX_N_OWNED, |
| 1403 |
we have to split the corresponding directory slot in two. */ |
| 1404 |
|
| 1405 |
if (UNIV_UNLIKELY(n_owned == PAGE_DIR_SLOT_MAX_N_OWNED)) { |
| 1406 |
page_dir_split_slot(page, nullptr, page_dir_find_owner_slot(owner_rec)); |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
/* 9. Write log record of the insert */ |
| 1411 |
if (UNIV_LIKELY(mtr != nullptr)) { |
| 1412 |
page_cur_insert_rec_write_log(insert_rec, rec_size, current_rec, index, |
| 1413 |
mtr); |
| 1414 |
} |
| 1415 |
|
| 1416 |
return (insert_rec); |
| 1417 |
} |
| 1418 |
|
| 1419 |
rec_t *page_cur_direct_insert_rec_low(rec_t *current_rec, dict_index_t *index, |
| 1420 |
const dtuple_t *tuple, mtr_t *mtr, |
| 1421 |
ulint rec_size) { |
| 1422 |
byte *insert_buf; |
| 1423 |
page_t *page; /*!< the relevant page */ |
| 1424 |
rec_t *last_insert; /*!< cursor position at previous |
| 1425 |
insert */ |
| 1426 |
rec_t *free_rec; /*!< a free record that was reused, |
| 1427 |
or NULL */ |
| 1428 |
rec_t *insert_rec; /*!< inserted record */ |
| 1429 |
ulint heap_no; /*!< heap number of the inserted |
| 1430 |
record */ |
| 1431 |
|
| 1432 |
page = page_align(current_rec); |
| 1433 |
|
| 1434 |
ut_ad(dict_table_is_comp(index->table) == page_is_comp(page)); |
| 1435 |
|
| 1436 |
ut_ad(fil_page_index_page_check(page)); |
| 1437 |
|
| 1438 |
ut_ad(mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID) == index->id); |
| 1439 |
|
| 1440 |
ut_ad(!page_rec_is_supremum(current_rec)); |
| 1441 |
|
| 1442 |
/* Try to find suitable space from page memory management */ |
| 1443 |
free_rec = page_header_get_ptr(page, PAGE_FREE); |
| 1444 |
if (free_rec) { |
| 1445 |
/* Try to allocate from the head of the free list. */ |
| 1446 |
ulint foffsets_[REC_OFFS_NORMAL_SIZE]; |
| 1447 |
ulint *foffsets = foffsets_; |
| 1448 |
mem_heap_t *heap = nullptr; |
| 1449 |
|
| 1450 |
rec_offs_init(foffsets_); |
| 1451 |
|
| 1452 |
foffsets = rec_get_offsets(free_rec, index, foffsets, ULINT_UNDEFINED, |
| 1453 |
UT_LOCATION_HERE, &heap); |
| 1454 |
if (rec_offs_size(foffsets) < rec_size) { |
| 1455 |
if (heap != nullptr) { |
| 1456 |
mem_heap_free(heap); |
| 1457 |
heap = nullptr; |
| 1458 |
} |
| 1459 |
|
| 1460 |
free_rec = nullptr; |
| 1461 |
insert_buf = page_mem_alloc_heap(page, nullptr, rec_size, &heap_no); |
| 1462 |
|
| 1463 |
if (insert_buf == nullptr) { |
| 1464 |
return (nullptr); |
| 1465 |
} |
| 1466 |
} else { |
| 1467 |
insert_buf = free_rec - rec_offs_extra_size(foffsets); |
| 1468 |
|
| 1469 |
if (page_is_comp(page)) { |
| 1470 |
heap_no = rec_get_heap_no_new(free_rec); |
| 1471 |
page_mem_alloc_free(page, nullptr, rec_get_next_ptr(free_rec, true), |
| 1472 |
rec_size); |
| 1473 |
} else { |
| 1474 |
heap_no = rec_get_heap_no_old(free_rec); |
| 1475 |
page_mem_alloc_free(page, nullptr, rec_get_next_ptr(free_rec, false), |
| 1476 |
rec_size); |
| 1477 |
} |
| 1478 |
|
| 1479 |
if (heap != nullptr) { |
| 1480 |
mem_heap_free(heap); |
| 1481 |
heap = nullptr; |
| 1482 |
} |
| 1483 |
} |
| 1484 |
} else { |
| 1485 |
free_rec = nullptr; |
| 1486 |
insert_buf = page_mem_alloc_heap(page, nullptr, rec_size, &heap_no); |
| 1487 |
|
| 1488 |
if (insert_buf == nullptr) { |
| 1489 |
return (nullptr); |
| 1490 |
} |
| 1491 |
} |
| 1492 |
|
| 1493 |
/* Create the record */ |
| 1494 |
insert_rec = rec_convert_dtuple_to_rec(insert_buf, index, tuple); |
| 1495 |
|
| 1496 |
/* Insert the record in the linked list of records */ |
| 1497 |
ut_ad(current_rec != insert_rec); |
| 1498 |
|
| 1499 |
{ |
| 1500 |
/* next record after current before the insertion */ |
| 1501 |
rec_t *next_rec = page_rec_get_next(current_rec); |
| 1502 |
#ifdef UNIV_DEBUG |
| 1503 |
if (page_is_comp(page)) { |
| 1504 |
ut_ad(rec_get_status(current_rec) <= REC_STATUS_INFIMUM); |
| 1505 |
ut_ad(rec_get_status(insert_rec) < REC_STATUS_INFIMUM); |
| 1506 |
ut_ad(rec_get_status(next_rec) != REC_STATUS_INFIMUM); |
| 1507 |
} |
| 1508 |
#endif |
| 1509 |
page_rec_set_next(insert_rec, next_rec); |
| 1510 |
page_rec_set_next(current_rec, insert_rec); |
| 1511 |
} |
| 1512 |
|
| 1513 |
page_header_set_field(page, nullptr, PAGE_N_RECS, 1 + page_get_n_recs(page)); |
| 1514 |
|
| 1515 |
/* Set the n_owned field in the inserted record to zero, and set the heap_no |
| 1516 |
field */ |
| 1517 |
if (page_is_comp(page)) { |
| 1518 |
rec_set_n_owned_new(insert_rec, nullptr, 0); |
| 1519 |
rec_set_heap_no_new(insert_rec, heap_no); |
| 1520 |
} else { |
| 1521 |
rec_set_n_owned_old(insert_rec, 0); |
| 1522 |
rec_set_heap_no_old(insert_rec, heap_no); |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* Update the last insertion info in page header */ |
| 1526 |
|
| 1527 |
last_insert = page_header_get_ptr(page, PAGE_LAST_INSERT); |
| 1528 |
ut_ad(!last_insert || !page_is_comp(page) || |
| 1529 |
rec_get_node_ptr_flag(last_insert) == |
| 1530 |
rec_get_node_ptr_flag(insert_rec)); |
| 1531 |
|
| 1532 |
if (last_insert == nullptr) { |
| 1533 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 1534 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, 0); |
| 1535 |
|
| 1536 |
} else if ((last_insert == current_rec) && |
| 1537 |
(page_header_get_field(page, PAGE_DIRECTION) != PAGE_LEFT)) { |
| 1538 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_RIGHT); |
| 1539 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, |
| 1540 |
page_header_get_field(page, PAGE_N_DIRECTION) + 1); |
| 1541 |
|
| 1542 |
} else if ((page_rec_get_next(insert_rec) == last_insert) && |
| 1543 |
(page_header_get_field(page, PAGE_DIRECTION) != PAGE_RIGHT)) { |
| 1544 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_LEFT); |
| 1545 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, |
| 1546 |
page_header_get_field(page, PAGE_N_DIRECTION) + 1); |
| 1547 |
} else { |
| 1548 |
page_header_set_field(page, nullptr, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 1549 |
page_header_set_field(page, nullptr, PAGE_N_DIRECTION, 0); |
| 1550 |
} |
| 1551 |
|
| 1552 |
page_header_set_ptr(page, nullptr, PAGE_LAST_INSERT, insert_rec); |
| 1553 |
|
| 1554 |
/* It remains to update the owner record. */ |
| 1555 |
{ |
| 1556 |
rec_t *owner_rec = page_rec_find_owner_rec(insert_rec); |
| 1557 |
ulint n_owned; |
| 1558 |
if (page_is_comp(page)) { |
| 1559 |
n_owned = rec_get_n_owned_new(owner_rec); |
| 1560 |
rec_set_n_owned_new(owner_rec, nullptr, n_owned + 1); |
| 1561 |
} else { |
| 1562 |
n_owned = rec_get_n_owned_old(owner_rec); |
| 1563 |
rec_set_n_owned_old(owner_rec, n_owned + 1); |
| 1564 |
} |
| 1565 |
|
| 1566 |
/* Now we have incremented the n_owned field of the owner |
| 1567 |
record. If the number exceeds PAGE_DIR_SLOT_MAX_N_OWNED, |
| 1568 |
we have to split the corresponding directory slot in two. */ |
| 1569 |
|
| 1570 |
if (n_owned == PAGE_DIR_SLOT_MAX_N_OWNED) { |
| 1571 |
page_dir_split_slot(page, nullptr, page_dir_find_owner_slot(owner_rec)); |
| 1572 |
} |
| 1573 |
} |
| 1574 |
|
| 1575 |
/* Open the mtr for name sake to set the modification flag |
| 1576 |
to true failing which no flush would be done. */ |
| 1577 |
byte *log_ptr = nullptr; |
| 1578 |
if (mlog_open(mtr, 0, log_ptr)) { |
| 1579 |
ut_d(ut_error); |
| 1580 |
ut_o(mlog_close(mtr, log_ptr)); |
| 1581 |
} |
| 1582 |
|
| 1583 |
return (insert_rec); |
| 1584 |
} |
| 1585 |
|
| 1586 |
/** Inserts a record next to page cursor on a compressed and uncompressed |
| 1587 |
page. Returns pointer to inserted record if succeed, i.e., |
| 1588 |
enough space available, NULL otherwise. |
| 1589 |
The cursor stays at the same position. |
| 1590 |
|
| 1591 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 1592 |
if this is a compressed leaf page in a secondary index. |
| 1593 |
This has to be done either within the same mini-transaction, |
| 1594 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 1595 |
|
| 1596 |
@return pointer to record if succeed, NULL otherwise */ |
| 1597 |
rec_t *page_cur_insert_rec_zip( |
| 1598 |
page_cur_t *cursor, /*!< in/out: page cursor */ |
| 1599 |
dict_index_t *index, /*!< in: record descriptor */ |
| 1600 |
const rec_t *rec, /*!< in: pointer to a physical record */ |
| 1601 |
ulint *offsets, /*!< in/out: rec_get_offsets(rec, index) */ |
| 1602 |
mtr_t *mtr) /*!< in: mini-transaction handle, or NULL */ |
| 1603 |
{ |
| 1604 |
byte *insert_buf; |
| 1605 |
ulint rec_size; |
| 1606 |
page_t *page; /*!< the relevant page */ |
| 1607 |
rec_t *last_insert; /*!< cursor position at previous |
| 1608 |
insert */ |
| 1609 |
rec_t *free_rec; /*!< a free record that was reused, |
| 1610 |
or NULL */ |
| 1611 |
rec_t *insert_rec; /*!< inserted record */ |
| 1612 |
ulint heap_no; /*!< heap number of the inserted |
| 1613 |
record */ |
| 1614 |
page_zip_des_t *page_zip; |
| 1615 |
|
| 1616 |
page_zip = page_cur_get_page_zip(cursor); |
| 1617 |
ut_ad(page_zip); |
| 1618 |
|
| 1619 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 1620 |
|
| 1621 |
page = page_cur_get_page(cursor); |
| 1622 |
ut_ad(dict_table_is_comp(index->table)); |
| 1623 |
ut_ad(page_is_comp(page)); |
| 1624 |
ut_ad(fil_page_index_page_check(page)); |
| 1625 |
ut_ad(mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID) == index->id || |
| 1626 |
(mtr ? mtr->is_inside_ibuf() : dict_index_is_ibuf(index)) || |
| 1627 |
recv_recovery_is_on()); |
| 1628 |
|
| 1629 |
ut_ad(!page_cur_is_after_last(cursor)); |
| 1630 |
#ifdef UNIV_ZIP_DEBUG |
| 1631 |
ut_a(page_zip_validate(page_zip, page, index)); |
| 1632 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1633 |
|
| 1634 |
/* 1. Get the size of the physical record in the page */ |
| 1635 |
rec_size = rec_offs_size(offsets); |
| 1636 |
|
| 1637 |
#ifdef UNIV_DEBUG_VALGRIND |
| 1638 |
{ |
| 1639 |
const void *rec_start = rec - rec_offs_extra_size(offsets); |
| 1640 |
ulint extra_size = rec_offs_extra_size(offsets) - |
| 1641 |
(rec_offs_comp(offsets) ? REC_N_NEW_EXTRA_BYTES |
| 1642 |
: REC_N_OLD_EXTRA_BYTES); |
| 1643 |
|
| 1644 |
/* All data bytes of the record must be valid. */ |
| 1645 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 1646 |
/* The variable-length header must be valid. */ |
| 1647 |
UNIV_MEM_ASSERT_RW(rec_start, extra_size); |
| 1648 |
} |
| 1649 |
#endif /* UNIV_DEBUG_VALGRIND */ |
| 1650 |
|
| 1651 |
const bool reorg_before_insert = |
| 1652 |
page_has_garbage(page) && rec_size > page_get_max_insert_size(page, 1) && |
| 1653 |
rec_size <= page_get_max_insert_size_after_reorganize(page, 1); |
| 1654 |
|
| 1655 |
/* 2. Try to find suitable space from page memory management */ |
| 1656 |
if (!page_zip_available(page_zip, index->is_clustered(), rec_size, 1) || |
| 1657 |
reorg_before_insert) { |
| 1658 |
/* The values can change dynamically. */ |
| 1659 |
bool log_compressed = page_zip_log_pages; |
| 1660 |
ulint level = page_zip_level; |
| 1661 |
#ifdef UNIV_DEBUG |
| 1662 |
rec_t *cursor_rec = page_cur_get_rec(cursor); |
| 1663 |
#endif /* UNIV_DEBUG */ |
| 1664 |
|
| 1665 |
/* If we are not writing compressed page images, we |
| 1666 |
must reorganize the page before attempting the |
| 1667 |
insert. */ |
| 1668 |
if (recv_recovery_is_on()) { |
| 1669 |
/* Insert into the uncompressed page only. |
| 1670 |
The page reorganization or creation that we |
| 1671 |
would attempt outside crash recovery would |
| 1672 |
have been covered by a previous redo log record. */ |
| 1673 |
} else if (page_is_empty(page)) { |
| 1674 |
ut_ad(page_cur_is_before_first(cursor)); |
| 1675 |
|
| 1676 |
/* This is an empty page. Recreate it to |
| 1677 |
get rid of the modification log. */ |
| 1678 |
page_create_zip(page_cur_get_block(cursor), index, |
| 1679 |
page_header_get_field(page, PAGE_LEVEL), 0, mtr, |
| 1680 |
fil_page_get_type(page)); |
| 1681 |
ut_ad(!page_header_get_ptr(page, PAGE_FREE)); |
| 1682 |
|
| 1683 |
if (page_zip_available(page_zip, index->is_clustered(), rec_size, 1)) { |
| 1684 |
goto use_heap; |
| 1685 |
} |
| 1686 |
|
| 1687 |
/* The cursor should remain on the page infimum. */ |
| 1688 |
return (nullptr); |
| 1689 |
} else if (!page_zip->m_nonempty && !page_has_garbage(page)) { |
| 1690 |
/* The page has been freshly compressed, so |
| 1691 |
reorganizing it will not help. */ |
| 1692 |
} else if (log_compressed && !reorg_before_insert) { |
| 1693 |
/* Insert into uncompressed page only, and |
| 1694 |
try page_zip_reorganize() afterwards. */ |
| 1695 |
} else if (btr_page_reorganize_low(recv_recovery_is_on(), level, cursor, |
| 1696 |
index, mtr)) { |
| 1697 |
ut_ad(!page_header_get_ptr(page, PAGE_FREE)); |
| 1698 |
|
| 1699 |
if (page_zip_available(page_zip, index->is_clustered(), rec_size, 1)) { |
| 1700 |
/* After reorganizing, there is space |
| 1701 |
available. */ |
| 1702 |
goto use_heap; |
| 1703 |
} |
| 1704 |
} else { |
| 1705 |
ut_ad(cursor->rec == cursor_rec); |
| 1706 |
return (nullptr); |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* Try compressing the whole page afterwards. */ |
| 1710 |
insert_rec = |
| 1711 |
page_cur_insert_rec_low(cursor->rec, index, rec, offsets, nullptr); |
| 1712 |
|
| 1713 |
/* If recovery is on, this implies that the compression |
| 1714 |
of the page was successful during runtime. Had that not |
| 1715 |
been the case or had the redo logging of compressed |
| 1716 |
pages been enabled during runtime then we'd have seen |
| 1717 |
a MLOG_ZIP_PAGE_COMPRESS redo record. Therefore, we |
| 1718 |
know that we don't need to reorganize the page. We, |
| 1719 |
however, do need to recompress the page. That will |
| 1720 |
happen when the next redo record is read which must |
| 1721 |
be of type MLOG_ZIP_PAGE_COMPRESS_NO_DATA and it must |
| 1722 |
contain a valid compression level value. |
| 1723 |
This implies that during recovery from this point till |
| 1724 |
the next redo is applied the uncompressed and |
| 1725 |
compressed versions are not identical and |
| 1726 |
page_zip_validate will fail but that is OK because |
| 1727 |
we call page_zip_validate only after processing |
| 1728 |
all changes to a page under a single mtr during |
| 1729 |
recovery. */ |
| 1730 |
if (insert_rec == nullptr) { |
| 1731 |
/* Out of space. |
| 1732 |
This should never occur during crash recovery, |
| 1733 |
because the MLOG_COMP_REC_INSERT should only |
| 1734 |
be logged after a successful operation. */ |
| 1735 |
ut_ad(!recv_recovery_is_on()); |
| 1736 |
} else if (recv_recovery_is_on()) { |
| 1737 |
/* This should be followed by |
| 1738 |
MLOG_ZIP_PAGE_COMPRESS_NO_DATA, |
| 1739 |
which should succeed. */ |
| 1740 |
rec_offs_make_valid(insert_rec, index, offsets); |
| 1741 |
} else { |
| 1742 |
ulint pos = page_rec_get_n_recs_before(insert_rec); |
| 1743 |
ut_ad(pos > 0); |
| 1744 |
|
| 1745 |
if (!log_compressed) { |
| 1746 |
if (page_zip_compress(page_zip, page, index, level, nullptr)) { |
| 1747 |
page_cur_insert_rec_write_log(insert_rec, rec_size, cursor->rec, |
| 1748 |
index, mtr); |
| 1749 |
page_zip_compress_write_log_no_data(level, page, index, mtr); |
| 1750 |
|
| 1751 |
rec_offs_make_valid(insert_rec, index, offsets); |
| 1752 |
return (insert_rec); |
| 1753 |
} |
| 1754 |
|
| 1755 |
ut_ad(cursor->rec == (pos > 1 ? page_rec_get_nth(page, pos - 1) |
| 1756 |
: page + PAGE_NEW_INFIMUM)); |
| 1757 |
} else { |
| 1758 |
/* We are writing entire page images |
| 1759 |
to the log. Reduce the redo log volume |
| 1760 |
by reorganizing the page at the same time. */ |
| 1761 |
if (page_zip_reorganize(cursor->block, index, mtr)) { |
| 1762 |
/* The page was reorganized: |
| 1763 |
Seek to pos. */ |
| 1764 |
if (pos > 1) { |
| 1765 |
cursor->rec = page_rec_get_nth(page, pos - 1); |
| 1766 |
} else { |
| 1767 |
cursor->rec = page + PAGE_NEW_INFIMUM; |
| 1768 |
} |
| 1769 |
|
| 1770 |
insert_rec = page + rec_get_next_offs(cursor->rec, true); |
| 1771 |
rec_offs_make_valid(insert_rec, index, offsets); |
| 1772 |
return (insert_rec); |
| 1773 |
} |
| 1774 |
|
| 1775 |
/* Theoretically, we could try one |
| 1776 |
last resort of btr_page_reorganize_low() |
| 1777 |
followed by page_zip_available(), but |
| 1778 |
that would be very unlikely to |
| 1779 |
succeed. (If the full reorganized page |
| 1780 |
failed to compress, why would it |
| 1781 |
succeed to compress the page, plus log |
| 1782 |
the insert of this record? */ |
| 1783 |
} |
| 1784 |
|
| 1785 |
/* Out of space: restore the page */ |
| 1786 |
if (!page_zip_decompress(page_zip, page, false)) { |
| 1787 |
ut_error; /* Memory corrupted? */ |
| 1788 |
} |
| 1789 |
ut_ad(page_validate(page, index)); |
| 1790 |
insert_rec = nullptr; |
| 1791 |
} |
| 1792 |
|
| 1793 |
return (insert_rec); |
| 1794 |
} |
| 1795 |
|
| 1796 |
free_rec = page_header_get_ptr(page, PAGE_FREE); |
| 1797 |
if (UNIV_LIKELY_NULL(free_rec)) { |
| 1798 |
/* Try to allocate from the head of the free list. */ |
| 1799 |
lint extra_size_diff; |
| 1800 |
ulint foffsets_[REC_OFFS_NORMAL_SIZE]; |
| 1801 |
ulint *foffsets = foffsets_; |
| 1802 |
mem_heap_t *heap = nullptr; |
| 1803 |
|
| 1804 |
rec_offs_init(foffsets_); |
| 1805 |
|
| 1806 |
foffsets = rec_get_offsets(free_rec, index, foffsets, ULINT_UNDEFINED, |
| 1807 |
UT_LOCATION_HERE, &heap); |
| 1808 |
if (rec_offs_size(foffsets) < rec_size) { |
| 1809 |
too_small: |
| 1810 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1811 |
mem_heap_free(heap); |
| 1812 |
} |
| 1813 |
|
| 1814 |
goto use_heap; |
| 1815 |
} |
| 1816 |
|
| 1817 |
insert_buf = free_rec - rec_offs_extra_size(foffsets); |
| 1818 |
|
| 1819 |
/* On compressed pages, do not relocate records from |
| 1820 |
the free list. If extra_size would grow, use the heap. */ |
| 1821 |
extra_size_diff = |
| 1822 |
rec_offs_extra_size(offsets) - rec_offs_extra_size(foffsets); |
| 1823 |
|
| 1824 |
if (UNIV_UNLIKELY(extra_size_diff < 0)) { |
| 1825 |
/* Add an offset to the extra_size. */ |
| 1826 |
if (rec_offs_size(foffsets) < rec_size - extra_size_diff) { |
| 1827 |
goto too_small; |
| 1828 |
} |
| 1829 |
|
| 1830 |
insert_buf -= extra_size_diff; |
| 1831 |
} else if (UNIV_UNLIKELY(extra_size_diff)) { |
| 1832 |
/* Do not allow extra_size to grow */ |
| 1833 |
|
| 1834 |
goto too_small; |
| 1835 |
} |
| 1836 |
|
| 1837 |
heap_no = rec_get_heap_no_new(free_rec); |
| 1838 |
page_mem_alloc_free(page, page_zip, rec_get_next_ptr(free_rec, true), |
| 1839 |
rec_size); |
| 1840 |
|
| 1841 |
if (!page_is_leaf(page)) { |
| 1842 |
/* Zero out the node pointer of free_rec, |
| 1843 |
in case it will not be overwritten by |
| 1844 |
insert_rec. */ |
| 1845 |
|
| 1846 |
ut_ad(rec_size > REC_NODE_PTR_SIZE); |
| 1847 |
|
| 1848 |
if (rec_offs_extra_size(foffsets) + rec_offs_data_size(foffsets) > |
| 1849 |
rec_size) { |
| 1850 |
memset(rec_get_end(free_rec, foffsets) - REC_NODE_PTR_SIZE, 0, |
| 1851 |
REC_NODE_PTR_SIZE); |
| 1852 |
} |
| 1853 |
} else if (index->is_clustered()) { |
| 1854 |
/* Zero out the DB_TRX_ID and DB_ROLL_PTR |
| 1855 |
columns of free_rec, in case it will not be |
| 1856 |
overwritten by insert_rec. */ |
| 1857 |
|
| 1858 |
ulint trx_id_col; |
| 1859 |
ulint trx_id_offs; |
| 1860 |
ulint len; |
| 1861 |
|
| 1862 |
trx_id_col = index->get_sys_col_pos(DATA_TRX_ID); |
| 1863 |
ut_ad(trx_id_col > 0); |
| 1864 |
ut_ad(trx_id_col != ULINT_UNDEFINED); |
| 1865 |
|
| 1866 |
trx_id_offs = rec_get_nth_field_offs(index, foffsets, trx_id_col, &len); |
| 1867 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 1868 |
|
| 1869 |
if (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN + trx_id_offs + |
| 1870 |
rec_offs_extra_size(foffsets) > |
| 1871 |
rec_size) { |
| 1872 |
/* We will have to zero out the |
| 1873 |
DB_TRX_ID and DB_ROLL_PTR, because |
| 1874 |
they will not be fully overwritten by |
| 1875 |
insert_rec. */ |
| 1876 |
|
| 1877 |
memset(free_rec + trx_id_offs, 0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 1878 |
} |
| 1879 |
|
| 1880 |
ut_ad(free_rec + trx_id_offs + DATA_TRX_ID_LEN == |
| 1881 |
rec_get_nth_field(index, free_rec, foffsets, trx_id_col + 1, &len)); |
| 1882 |
ut_ad(len == DATA_ROLL_PTR_LEN); |
| 1883 |
} |
| 1884 |
|
| 1885 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1886 |
mem_heap_free(heap); |
| 1887 |
} |
| 1888 |
} else { |
| 1889 |
use_heap: |
| 1890 |
free_rec = nullptr; |
| 1891 |
insert_buf = page_mem_alloc_heap(page, page_zip, rec_size, &heap_no); |
| 1892 |
|
| 1893 |
if (UNIV_UNLIKELY(insert_buf == nullptr)) { |
| 1894 |
return (nullptr); |
| 1895 |
} |
| 1896 |
|
| 1897 |
page_zip_dir_add_slot(page_zip, index->is_clustered()); |
| 1898 |
} |
| 1899 |
|
| 1900 |
/* 3. Create the record */ |
| 1901 |
insert_rec = rec_copy(insert_buf, rec, offsets); |
| 1902 |
rec_offs_make_valid(insert_rec, index, offsets); |
| 1903 |
|
| 1904 |
/* 4. Insert the record in the linked list of records */ |
| 1905 |
ut_ad(cursor->rec != insert_rec); |
| 1906 |
|
| 1907 |
{ |
| 1908 |
/* next record after current before the insertion */ |
| 1909 |
const rec_t *next_rec = page_rec_get_next_low(cursor->rec, true); |
| 1910 |
ut_ad(rec_get_status(cursor->rec) <= REC_STATUS_INFIMUM); |
| 1911 |
ut_ad(rec_get_status(insert_rec) < REC_STATUS_INFIMUM); |
| 1912 |
ut_ad(rec_get_status(next_rec) != REC_STATUS_INFIMUM); |
| 1913 |
|
| 1914 |
page_rec_set_next(insert_rec, next_rec); |
| 1915 |
page_rec_set_next(cursor->rec, insert_rec); |
| 1916 |
} |
| 1917 |
|
| 1918 |
page_header_set_field(page, page_zip, PAGE_N_RECS, 1 + page_get_n_recs(page)); |
| 1919 |
|
| 1920 |
/* 5. Set the n_owned field in the inserted record to zero, |
| 1921 |
and set the heap_no field */ |
| 1922 |
rec_set_n_owned_new(insert_rec, nullptr, 0); |
| 1923 |
rec_set_heap_no_new(insert_rec, heap_no); |
| 1924 |
|
| 1925 |
UNIV_MEM_ASSERT_RW(rec_get_start(insert_rec, offsets), |
| 1926 |
rec_offs_size(offsets)); |
| 1927 |
|
| 1928 |
page_zip_dir_insert(page_zip, cursor->rec, free_rec, insert_rec); |
| 1929 |
|
| 1930 |
/* 6. Update the last insertion info in page header */ |
| 1931 |
|
| 1932 |
last_insert = page_header_get_ptr(page, PAGE_LAST_INSERT); |
| 1933 |
ut_ad(!last_insert || rec_get_node_ptr_flag(last_insert) == |
| 1934 |
rec_get_node_ptr_flag(insert_rec)); |
| 1935 |
|
| 1936 |
if (!dict_index_is_spatial(index)) { |
| 1937 |
if (UNIV_UNLIKELY(last_insert == nullptr)) { |
| 1938 |
page_header_set_field(page, page_zip, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 1939 |
page_header_set_field(page, page_zip, PAGE_N_DIRECTION, 0); |
| 1940 |
|
| 1941 |
} else if ((last_insert == cursor->rec) && |
| 1942 |
(page_header_get_field(page, PAGE_DIRECTION) != PAGE_LEFT)) { |
| 1943 |
page_header_set_field(page, page_zip, PAGE_DIRECTION, PAGE_RIGHT); |
| 1944 |
page_header_set_field(page, page_zip, PAGE_N_DIRECTION, |
| 1945 |
page_header_get_field(page, PAGE_N_DIRECTION) + 1); |
| 1946 |
|
| 1947 |
} else if ((page_rec_get_next(insert_rec) == last_insert) && |
| 1948 |
(page_header_get_field(page, PAGE_DIRECTION) != PAGE_RIGHT)) { |
| 1949 |
page_header_set_field(page, page_zip, PAGE_DIRECTION, PAGE_LEFT); |
| 1950 |
page_header_set_field(page, page_zip, PAGE_N_DIRECTION, |
| 1951 |
page_header_get_field(page, PAGE_N_DIRECTION) + 1); |
| 1952 |
} else { |
| 1953 |
page_header_set_field(page, page_zip, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 1954 |
page_header_set_field(page, page_zip, PAGE_N_DIRECTION, 0); |
| 1955 |
} |
| 1956 |
} |
| 1957 |
|
| 1958 |
page_header_set_ptr(page, page_zip, PAGE_LAST_INSERT, insert_rec); |
| 1959 |
|
| 1960 |
/* 7. It remains to update the owner record. */ |
| 1961 |
{ |
| 1962 |
rec_t *owner_rec = page_rec_find_owner_rec(insert_rec); |
| 1963 |
ulint n_owned; |
| 1964 |
|
| 1965 |
n_owned = rec_get_n_owned_new(owner_rec); |
| 1966 |
rec_set_n_owned_new(owner_rec, page_zip, n_owned + 1); |
| 1967 |
|
| 1968 |
/* 8. Now we have incremented the n_owned field of the owner |
| 1969 |
record. If the number exceeds PAGE_DIR_SLOT_MAX_N_OWNED, |
| 1970 |
we have to split the corresponding directory slot in two. */ |
| 1971 |
|
| 1972 |
if (UNIV_UNLIKELY(n_owned == PAGE_DIR_SLOT_MAX_N_OWNED)) { |
| 1973 |
page_dir_split_slot(page, page_zip, page_dir_find_owner_slot(owner_rec)); |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
page_zip_write_rec(page_zip, insert_rec, index, offsets, 1); |
| 1978 |
|
| 1979 |
/* 9. Write log record of the insert */ |
| 1980 |
if (UNIV_LIKELY(mtr != nullptr)) { |
| 1981 |
page_cur_insert_rec_write_log(insert_rec, rec_size, cursor->rec, index, |
| 1982 |
mtr); |
| 1983 |
} |
| 1984 |
|
| 1985 |
return (insert_rec); |
| 1986 |
} |
| 1987 |
|
| 1988 |
#ifndef UNIV_HOTBACKUP |
| 1989 |
/** Writes a log record of copying a record list end to a new created page. |
| 1990 |
@param[in,out] page Index page |
| 1991 |
@param[in,out] index Record descriptor |
| 1992 |
@param[in,out] mtr Mini-transaction |
| 1993 |
@param[out] log_ptr 4-byte field where to write the log data length |
| 1994 |
@retval true if mtr log is opened successfully. |
| 1995 |
@retval false if mtr log is not opened. One case is when redo is disabled. */ |
| 1996 |
static inline bool page_copy_rec_list_to_created_page_write_log( |
| 1997 |
page_t *page, dict_index_t *index, mtr_t *mtr, byte *&log_ptr) { |
| 1998 |
ut_ad(page_is_comp(page) == dict_table_is_comp(index->table)); |
| 1999 |
|
| 2000 |
const bool opened = mlog_open_and_write_index( |
| 2001 |
mtr, page, index, MLOG_LIST_END_COPY_CREATED, 4, log_ptr); |
| 2002 |
|
| 2003 |
if (opened) { |
| 2004 |
mlog_close(mtr, log_ptr + 4); |
| 2005 |
} |
| 2006 |
|
| 2007 |
return (opened); |
| 2008 |
} |
| 2009 |
#endif /* !UNIV_HOTBACKUP */ |
| 2010 |
|
| 2011 |
/** Parses a log record of copying a record list end to a new created page. |
| 2012 |
@return end of log record or NULL */ |
| 2013 |
byte *page_parse_copy_rec_list_to_created_page( |
| 2014 |
byte *ptr, /*!< in: buffer */ |
| 2015 |
byte *end_ptr, /*!< in: buffer end */ |
| 2016 |
buf_block_t *block, /*!< in: page or NULL */ |
| 2017 |
dict_index_t *index, /*!< in: record descriptor */ |
| 2018 |
mtr_t *mtr) /*!< in: mtr or NULL */ |
| 2019 |
{ |
| 2020 |
byte *rec_end; |
| 2021 |
ulint log_data_len; |
| 2022 |
page_t *page; |
| 2023 |
page_zip_des_t *page_zip; |
| 2024 |
|
| 2025 |
if (ptr + 4 > end_ptr) { |
| 2026 |
return (nullptr); |
| 2027 |
} |
| 2028 |
|
| 2029 |
log_data_len = mach_read_from_4(ptr); |
| 2030 |
ptr += 4; |
| 2031 |
|
| 2032 |
rec_end = ptr + log_data_len; |
| 2033 |
|
| 2034 |
if (rec_end > end_ptr) { |
| 2035 |
return (nullptr); |
| 2036 |
} |
| 2037 |
|
| 2038 |
if (!block) { |
| 2039 |
return (rec_end); |
| 2040 |
} |
| 2041 |
|
| 2042 |
while (ptr < rec_end) { |
| 2043 |
ptr = page_cur_parse_insert_rec(true, ptr, end_ptr, block, index, mtr); |
| 2044 |
} |
| 2045 |
|
| 2046 |
ut_a(ptr == rec_end); |
| 2047 |
|
| 2048 |
page = buf_block_get_frame(block); |
| 2049 |
page_zip = buf_block_get_page_zip(block); |
| 2050 |
|
| 2051 |
page_header_set_ptr(page, page_zip, PAGE_LAST_INSERT, nullptr); |
| 2052 |
|
| 2053 |
if (!dict_index_is_spatial(index)) { |
| 2054 |
page_header_set_field(page, page_zip, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 2055 |
page_header_set_field(page, page_zip, PAGE_N_DIRECTION, 0); |
| 2056 |
} |
| 2057 |
|
| 2058 |
return (rec_end); |
| 2059 |
} |
| 2060 |
|
| 2061 |
#ifndef UNIV_HOTBACKUP |
| 2062 |
/** Copies records from page to a newly created page, from a given record |
| 2063 |
onward, including that record. Infimum and supremum records are not copied. |
| 2064 |
|
| 2065 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 2066 |
if this is a compressed leaf page in a secondary index. |
| 2067 |
This has to be done either within the same mini-transaction, |
| 2068 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). */ |
| 2069 |
void page_copy_rec_list_end_to_created_page( |
| 2070 |
page_t *new_page, /*!< in/out: index page to copy to */ |
| 2071 |
rec_t *rec, /*!< in: first record to copy */ |
| 2072 |
dict_index_t *index, /*!< in: record descriptor */ |
| 2073 |
mtr_t *mtr) /*!< in: mtr */ |
| 2074 |
{ |
| 2075 |
page_dir_slot_t *slot = nullptr; /* remove warning */ |
| 2076 |
byte *heap_top; |
| 2077 |
rec_t *insert_rec = nullptr; /* remove warning */ |
| 2078 |
rec_t *prev_rec; |
| 2079 |
ulint count; |
| 2080 |
ulint n_recs; |
| 2081 |
ulint slot_index; |
| 2082 |
ulint rec_size; |
| 2083 |
byte *log_ptr = nullptr; |
| 2084 |
ulint log_data_len; |
| 2085 |
mem_heap_t *heap = nullptr; |
| 2086 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 2087 |
ulint *offsets = offsets_; |
| 2088 |
rec_offs_init(offsets_); |
| 2089 |
|
| 2090 |
ut_ad(page_dir_get_n_heap(new_page) == PAGE_HEAP_NO_USER_LOW); |
| 2091 |
ut_ad(page_align(rec) != new_page); |
| 2092 |
ut_ad(page_rec_is_comp(rec) == page_is_comp(new_page)); |
| 2093 |
|
| 2094 |
if (page_rec_is_infimum(rec)) { |
| 2095 |
rec = page_rec_get_next(rec); |
| 2096 |
} |
| 2097 |
|
| 2098 |
if (page_rec_is_supremum(rec)) { |
| 2099 |
return; |
| 2100 |
} |
| 2101 |
|
| 2102 |
#ifdef UNIV_DEBUG |
| 2103 |
/* To pass the debug tests we have to set these dummy values |
| 2104 |
in the debug version */ |
| 2105 |
page_dir_set_n_slots(new_page, nullptr, UNIV_PAGE_SIZE / 2); |
| 2106 |
page_header_set_ptr(new_page, nullptr, PAGE_HEAP_TOP, |
| 2107 |
new_page + UNIV_PAGE_SIZE - 1); |
| 2108 |
#endif |
| 2109 |
|
| 2110 |
bool opened = page_copy_rec_list_to_created_page_write_log(new_page, index, |
| 2111 |
mtr, log_ptr); |
| 2112 |
|
| 2113 |
log_data_len = mtr->get_log()->size(); |
| 2114 |
|
| 2115 |
/* Individual inserts are logged in a shorter form */ |
| 2116 |
|
| 2117 |
mtr_log_t log_mode; |
| 2118 |
|
| 2119 |
if (index->table->is_temporary() || |
| 2120 |
index->table->ibd_file_missing /* IMPORT TABLESPACE */) { |
| 2121 |
log_mode = mtr_get_log_mode(mtr); |
| 2122 |
} else { |
| 2123 |
log_mode = mtr_set_log_mode(mtr, MTR_LOG_SHORT_INSERTS); |
| 2124 |
} |
| 2125 |
|
| 2126 |
prev_rec = page_get_infimum_rec(new_page); |
| 2127 |
if (page_is_comp(new_page)) { |
| 2128 |
heap_top = new_page + PAGE_NEW_SUPREMUM_END; |
| 2129 |
} else { |
| 2130 |
heap_top = new_page + PAGE_OLD_SUPREMUM_END; |
| 2131 |
} |
| 2132 |
count = 0; |
| 2133 |
slot_index = 0; |
| 2134 |
n_recs = 0; |
| 2135 |
|
| 2136 |
do { |
| 2137 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 2138 |
UT_LOCATION_HERE, &heap); |
| 2139 |
insert_rec = rec_copy(heap_top, rec, offsets); |
| 2140 |
|
| 2141 |
if (page_is_comp(new_page)) { |
| 2142 |
rec_set_next_offs_new(prev_rec, page_offset(insert_rec)); |
| 2143 |
|
| 2144 |
rec_set_n_owned_new(insert_rec, nullptr, 0); |
| 2145 |
rec_set_heap_no_new(insert_rec, PAGE_HEAP_NO_USER_LOW + n_recs); |
| 2146 |
} else { |
| 2147 |
rec_set_next_offs_old(prev_rec, page_offset(insert_rec)); |
| 2148 |
|
| 2149 |
rec_set_n_owned_old(insert_rec, 0); |
| 2150 |
rec_set_heap_no_old(insert_rec, PAGE_HEAP_NO_USER_LOW + n_recs); |
| 2151 |
} |
| 2152 |
|
| 2153 |
count++; |
| 2154 |
n_recs++; |
| 2155 |
|
| 2156 |
if (UNIV_UNLIKELY(count == (PAGE_DIR_SLOT_MAX_N_OWNED + 1) / 2)) { |
| 2157 |
slot_index++; |
| 2158 |
|
| 2159 |
slot = page_dir_get_nth_slot(new_page, slot_index); |
| 2160 |
|
| 2161 |
page_dir_slot_set_rec(slot, insert_rec); |
| 2162 |
page_dir_slot_set_n_owned(slot, nullptr, count); |
| 2163 |
|
| 2164 |
count = 0; |
| 2165 |
} |
| 2166 |
|
| 2167 |
rec_size = rec_offs_size(offsets); |
| 2168 |
|
| 2169 |
ut_ad(heap_top < new_page + UNIV_PAGE_SIZE); |
| 2170 |
|
| 2171 |
heap_top += rec_size; |
| 2172 |
|
| 2173 |
rec_offs_make_valid(insert_rec, index, offsets); |
| 2174 |
page_cur_insert_rec_write_log(insert_rec, rec_size, prev_rec, index, mtr); |
| 2175 |
prev_rec = insert_rec; |
| 2176 |
rec = page_rec_get_next(rec); |
| 2177 |
} while (!page_rec_is_supremum(rec)); |
| 2178 |
|
| 2179 |
if ((slot_index > 0) && (count + 1 + (PAGE_DIR_SLOT_MAX_N_OWNED + 1) / 2 <= |
| 2180 |
PAGE_DIR_SLOT_MAX_N_OWNED)) { |
| 2181 |
/* We can merge the two last dir slots. This operation is |
| 2182 |
here to make this function imitate exactly the equivalent |
| 2183 |
task made using page_cur_insert_rec, which we use in database |
| 2184 |
recovery to reproduce the task performed by this function. |
| 2185 |
To be able to check the correctness of recovery, it is good |
| 2186 |
that it imitates exactly. */ |
| 2187 |
|
| 2188 |
count += (PAGE_DIR_SLOT_MAX_N_OWNED + 1) / 2; |
| 2189 |
|
| 2190 |
page_dir_slot_set_n_owned(slot, nullptr, 0); |
| 2191 |
|
| 2192 |
slot_index--; |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (UNIV_LIKELY_NULL(heap)) { |
| 2196 |
mem_heap_free(heap); |
| 2197 |
} |
| 2198 |
|
| 2199 |
log_data_len = mtr->get_log()->size() - log_data_len; |
| 2200 |
|
| 2201 |
ut_a(log_data_len < 100 * UNIV_PAGE_SIZE); |
| 2202 |
|
| 2203 |
if (opened) { |
| 2204 |
mach_write_to_4(log_ptr, log_data_len); |
| 2205 |
} |
| 2206 |
|
| 2207 |
if (page_is_comp(new_page)) { |
| 2208 |
rec_set_next_offs_new(insert_rec, PAGE_NEW_SUPREMUM); |
| 2209 |
} else { |
| 2210 |
rec_set_next_offs_old(insert_rec, PAGE_OLD_SUPREMUM); |
| 2211 |
} |
| 2212 |
|
| 2213 |
slot = page_dir_get_nth_slot(new_page, 1 + slot_index); |
| 2214 |
|
| 2215 |
page_dir_slot_set_rec(slot, page_get_supremum_rec(new_page)); |
| 2216 |
page_dir_slot_set_n_owned(slot, nullptr, count + 1); |
| 2217 |
|
| 2218 |
page_dir_set_n_slots(new_page, nullptr, 2 + slot_index); |
| 2219 |
page_header_set_ptr(new_page, nullptr, PAGE_HEAP_TOP, heap_top); |
| 2220 |
page_dir_set_n_heap(new_page, nullptr, PAGE_HEAP_NO_USER_LOW + n_recs); |
| 2221 |
page_header_set_field(new_page, nullptr, PAGE_N_RECS, n_recs); |
| 2222 |
|
| 2223 |
page_header_set_ptr(new_page, nullptr, PAGE_LAST_INSERT, nullptr); |
| 2224 |
|
| 2225 |
page_header_set_field(new_page, nullptr, PAGE_DIRECTION, PAGE_NO_DIRECTION); |
| 2226 |
page_header_set_field(new_page, nullptr, PAGE_N_DIRECTION, 0); |
| 2227 |
|
| 2228 |
/* Restore the log mode */ |
| 2229 |
|
| 2230 |
mtr_set_log_mode(mtr, log_mode); |
| 2231 |
} |
| 2232 |
|
| 2233 |
/** Writes log record of a record delete on a page. */ |
| 2234 |
static inline void page_cur_delete_rec_write_log( |
| 2235 |
rec_t *rec, /*!< in: record to be deleted */ |
| 2236 |
const dict_index_t *index, /*!< in: record descriptor */ |
| 2237 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 2238 |
{ |
| 2239 |
byte *log_ptr = nullptr; |
| 2240 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table)); |
| 2241 |
|
| 2242 |
if (!mlog_open_and_write_index(mtr, rec, index, MLOG_REC_DELETE, 2, |
| 2243 |
log_ptr)) { |
| 2244 |
/* Logging in mtr is switched off during crash recovery: |
| 2245 |
in that case mlog_open returns NULL */ |
| 2246 |
return; |
| 2247 |
} |
| 2248 |
|
| 2249 |
/* Write the cursor rec offset as a 2-byte ulint */ |
| 2250 |
mach_write_to_2(log_ptr, page_offset(rec)); |
| 2251 |
|
| 2252 |
mlog_close(mtr, log_ptr + 2); |
| 2253 |
} |
| 2254 |
#else /* !UNIV_HOTBACKUP */ |
| 2255 |
#define page_cur_delete_rec_write_log(rec, index, mtr) ((void)0) |
| 2256 |
#endif /* !UNIV_HOTBACKUP */ |
| 2257 |
|
| 2258 |
/** Parses log record of a record delete on a page. |
| 2259 |
@return pointer to record end or NULL */ |
| 2260 |
byte *page_cur_parse_delete_rec( |
| 2261 |
byte *ptr, /*!< in: buffer */ |
| 2262 |
byte *end_ptr, /*!< in: buffer end */ |
| 2263 |
buf_block_t *block, /*!< in: page or NULL */ |
| 2264 |
dict_index_t *index, /*!< in: record descriptor */ |
| 2265 |
mtr_t *mtr) /*!< in: mtr or NULL */ |
| 2266 |
{ |
| 2267 |
ulint offset; |
| 2268 |
page_cur_t cursor; |
| 2269 |
|
| 2270 |
if (end_ptr < ptr + 2) { |
| 2271 |
return (nullptr); |
| 2272 |
} |
| 2273 |
|
| 2274 |
/* Read the cursor rec offset as a 2-byte ulint */ |
| 2275 |
offset = mach_read_from_2(ptr); |
| 2276 |
ptr += 2; |
| 2277 |
|
| 2278 |
ut_a(offset <= UNIV_PAGE_SIZE); |
| 2279 |
|
| 2280 |
if (block) { |
| 2281 |
page_t *page = buf_block_get_frame(block); |
| 2282 |
mem_heap_t *heap = nullptr; |
| 2283 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 2284 |
rec_t *rec = page + offset; |
| 2285 |
rec_offs_init(offsets_); |
| 2286 |
|
| 2287 |
page_cur_position(rec, block, &cursor); |
| 2288 |
#ifdef UNIV_HOTBACKUP |
| 2289 |
ib::trace_1() << "page_cur_parse_delete_rec: offset " << offset; |
| 2290 |
#endif /* UNIV_HOTBACKUP */ |
| 2291 |
ut_ad(!buf_block_get_page_zip(block) || page_is_comp(page)); |
| 2292 |
|
| 2293 |
page_cur_delete_rec(&cursor, index, |
| 2294 |
rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED, |
| 2295 |
UT_LOCATION_HERE, &heap), |
| 2296 |
mtr); |
| 2297 |
if (UNIV_LIKELY_NULL(heap)) { |
| 2298 |
mem_heap_free(heap); |
| 2299 |
} |
| 2300 |
} |
| 2301 |
|
| 2302 |
return (ptr); |
| 2303 |
} |
| 2304 |
|
| 2305 |
/** Deletes a record at the page cursor. The cursor is moved to the next |
| 2306 |
record after the deleted one. */ |
| 2307 |
void page_cur_delete_rec( |
| 2308 |
page_cur_t *cursor, /*!< in/out: a page cursor */ |
| 2309 |
const dict_index_t *index, /*!< in: record descriptor */ |
| 2310 |
const ulint *offsets, /*!< in: rec_get_offsets( |
| 2311 |
cursor->rec, index) */ |
| 2312 |
mtr_t *mtr) /*!< in: mini-transaction handle |
| 2313 |
or NULL */ |
| 2314 |
{ |
| 2315 |
page_dir_slot_t *cur_dir_slot; |
| 2316 |
page_dir_slot_t *prev_slot; |
| 2317 |
page_t *page; |
| 2318 |
page_zip_des_t *page_zip; |
| 2319 |
rec_t *current_rec; |
| 2320 |
rec_t *prev_rec = nullptr; |
| 2321 |
rec_t *next_rec; |
| 2322 |
ulint cur_slot_no; |
| 2323 |
ulint cur_n_owned; |
| 2324 |
rec_t *rec; |
| 2325 |
|
| 2326 |
page = page_cur_get_page(cursor); |
| 2327 |
page_zip = page_cur_get_page_zip(cursor); |
| 2328 |
|
| 2329 |
/* page_zip_validate() will fail here when |
| 2330 |
btr_cur_pessimistic_delete() invokes btr_set_min_rec_mark(). |
| 2331 |
Then, both "page_zip" and "page" would have the min-rec-mark |
| 2332 |
set on the smallest user record, but "page" would additionally |
| 2333 |
have it set on the smallest-but-one record. Because sloppy |
| 2334 |
page_zip_validate_low() only ignores min-rec-flag differences |
| 2335 |
in the smallest user record, it cannot be used here either. */ |
| 2336 |
|
| 2337 |
current_rec = cursor->rec; |
| 2338 |
ut_ad(rec_offs_validate(current_rec, index, offsets)); |
| 2339 |
ut_ad(page_is_comp(page) == dict_table_is_comp(index->table)); |
| 2340 |
ut_ad(fil_page_index_page_check(page)); |
| 2341 |
ut_ad(mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID) == index->id || |
| 2342 |
(mtr ? mtr->is_inside_ibuf() : dict_index_is_ibuf(index)) || |
| 2343 |
recv_recovery_is_on()); |
| 2344 |
|
| 2345 |
/* The record must not be the supremum or infimum record. */ |
| 2346 |
ut_ad(page_rec_is_user_rec(current_rec)); |
| 2347 |
|
| 2348 |
if (page_get_n_recs(page) == 1 && !recv_recovery_is_on()) { |
| 2349 |
/* Empty the page, unless we are applying the redo log |
| 2350 |
during crash recovery. During normal operation, the |
| 2351 |
page_create_empty() gets logged as one of MLOG_PAGE_CREATE, |
| 2352 |
MLOG_COMP_PAGE_CREATE, MLOG_ZIP_PAGE_COMPRESS. */ |
| 2353 |
ut_ad(page_is_leaf(page)); |
| 2354 |
/* Usually, this should be the root page, |
| 2355 |
and the whole index tree should become empty. |
| 2356 |
However, this could also be a call in |
| 2357 |
btr_cur_pessimistic_update() to delete the only |
| 2358 |
record in the page and to insert another one. */ |
| 2359 |
page_cur_move_to_next(cursor); |
| 2360 |
ut_ad(page_cur_is_after_last(cursor)); |
| 2361 |
page_create_empty(page_cur_get_block(cursor), |
| 2362 |
const_cast<dict_index_t *>(index), mtr); |
| 2363 |
return; |
| 2364 |
} |
| 2365 |
|
| 2366 |
/* Save to local variables some data associated with current_rec */ |
| 2367 |
cur_slot_no = page_dir_find_owner_slot(current_rec); |
| 2368 |
ut_ad(cur_slot_no > 0); |
| 2369 |
cur_dir_slot = page_dir_get_nth_slot(page, cur_slot_no); |
| 2370 |
cur_n_owned = page_dir_slot_get_n_owned(cur_dir_slot); |
| 2371 |
|
| 2372 |
/* 0. Write the log record */ |
| 2373 |
if (mtr != nullptr) { |
| 2374 |
page_cur_delete_rec_write_log(current_rec, index, mtr); |
| 2375 |
} |
| 2376 |
|
| 2377 |
/* 1. Reset the last insert info in the page header and increment |
| 2378 |
the modify clock for the frame */ |
| 2379 |
|
| 2380 |
page_header_set_ptr(page, page_zip, PAGE_LAST_INSERT, nullptr); |
| 2381 |
|
| 2382 |
/* The page gets invalid for optimistic searches: increment the |
| 2383 |
frame modify clock only if there is an mini-transaction covering |
| 2384 |
the change. During IMPORT we allocate local blocks that are not |
| 2385 |
part of the buffer pool. */ |
| 2386 |
|
| 2387 |
if (mtr != nullptr) { |
| 2388 |
buf_block_modify_clock_inc(page_cur_get_block(cursor)); |
| 2389 |
} |
| 2390 |
|
| 2391 |
/* 2. Find the next and the previous record. Note that the cursor is |
| 2392 |
left at the next record. */ |
| 2393 |
|
| 2394 |
ut_ad(cur_slot_no > 0); |
| 2395 |
prev_slot = page_dir_get_nth_slot(page, cur_slot_no - 1); |
| 2396 |
|
| 2397 |
rec = (rec_t *)page_dir_slot_get_rec(prev_slot); |
| 2398 |
|
| 2399 |
/* rec now points to the record of the previous directory slot. Look |
| 2400 |
for the immediate predecessor of current_rec in a loop. */ |
| 2401 |
|
| 2402 |
while (current_rec != rec) { |
| 2403 |
prev_rec = rec; |
| 2404 |
rec = page_rec_get_next(rec); |
| 2405 |
} |
| 2406 |
|
| 2407 |
page_cur_move_to_next(cursor); |
| 2408 |
next_rec = cursor->rec; |
| 2409 |
|
| 2410 |
/* 3. Remove the record from the linked list of records */ |
| 2411 |
|
| 2412 |
page_rec_set_next(prev_rec, next_rec); |
| 2413 |
|
| 2414 |
/* 4. If the deleted record is pointed to by a dir slot, update the |
| 2415 |
record pointer in slot. In the following if-clause we assume that |
| 2416 |
prev_rec is owned by the same slot, i.e., PAGE_DIR_SLOT_MIN_N_OWNED |
| 2417 |
>= 2. */ |
| 2418 |
|
| 2419 |
static_assert(PAGE_DIR_SLOT_MIN_N_OWNED >= 2, |
| 2420 |
"PAGE_DIR_SLOT_MIN_N_OWNED < 2"); |
| 2421 |
ut_ad(cur_n_owned > 1); |
| 2422 |
|
| 2423 |
if (current_rec == page_dir_slot_get_rec(cur_dir_slot)) { |
| 2424 |
page_dir_slot_set_rec(cur_dir_slot, prev_rec); |
| 2425 |
} |
| 2426 |
|
| 2427 |
/* 5. Update the number of owned records of the slot */ |
| 2428 |
|
| 2429 |
page_dir_slot_set_n_owned(cur_dir_slot, page_zip, cur_n_owned - 1); |
| 2430 |
|
| 2431 |
/* 6. Free the memory occupied by the record */ |
| 2432 |
page_mem_free(page, page_zip, current_rec, index, offsets); |
| 2433 |
|
| 2434 |
/* 7. Now we have decremented the number of owned records of the slot. |
| 2435 |
If the number drops below PAGE_DIR_SLOT_MIN_N_OWNED, we balance the |
| 2436 |
slots. */ |
| 2437 |
|
| 2438 |
if (cur_n_owned <= PAGE_DIR_SLOT_MIN_N_OWNED) { |
| 2439 |
page_dir_balance_slot(page, page_zip, cur_slot_no); |
| 2440 |
} |
| 2441 |
|
| 2442 |
#ifdef UNIV_ZIP_DEBUG |
| 2443 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 2444 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2445 |
} |
| 2446 |
|
| 2447 |
#ifndef UNIV_HOTBACKUP |
| 2448 |
#ifdef UNIV_COMPILE_TEST_FUNCS |
| 2449 |
|
| 2450 |
/** Print the first n numbers, generated by page_cur_lcg_prng() to make sure |
| 2451 |
(visually) that it works properly. */ |
| 2452 |
void test_page_cur_lcg_prng(int n) /*!< in: print first n numbers */ |
| 2453 |
{ |
| 2454 |
int i; |
| 2455 |
unsigned long long rnd; |
| 2456 |
|
| 2457 |
for (i = 0; i < n; i++) { |
| 2458 |
rnd = page_cur_lcg_prng(); |
| 2459 |
printf("%llu\t%%2=%llu %%3=%llu %%5=%llu %%7=%llu %%11=%llu\n", rnd, |
| 2460 |
rnd % 2, rnd % 3, rnd % 5, rnd % 7, rnd % 11); |
| 2461 |
} |
| 2462 |
} |
| 2463 |
|
| 2464 |
#endif /* UNIV_COMPILE_TEST_FUNCS */ |
| 2465 |
#endif /* !UNIV_HOTBACKUP */ |