| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file mtr/mtr0log.cc |
| 29 |
Mini-transaction log routines |
| 30 |
|
| 31 |
Created 12/7/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "mtr0log.h" |
| 35 |
|
| 36 |
#ifndef UNIV_HOTBACKUP |
| 37 |
#include "buf0buf.h" |
| 38 |
#include "buf0dblwr.h" |
| 39 |
#include "dict0dict.h" |
| 40 |
#include "log0recv.h" |
| 41 |
#endif /* !UNIV_HOTBACKUP */ |
| 42 |
#include "page0page.h" |
| 43 |
|
| 44 |
#ifndef UNIV_HOTBACKUP |
| 45 |
#include "dict0boot.h" |
| 46 |
#endif /* !UNIV_HOTBACKUP */ |
| 47 |
|
| 48 |
/** Parses a log record written by mlog_open_and_write_index. |
| 49 |
@param[in] ptr buffer |
| 50 |
@param[in] end_ptr buffer end |
| 51 |
@param[out] index own: dummy index |
| 52 |
@return parsed record end, NULL if not a complete record */ |
| 53 |
[[nodiscard]] static byte *mlog_parse_index_v1(byte *ptr, const byte *end_ptr, |
| 54 |
dict_index_t **index); |
| 55 |
|
| 56 |
/** Catenates n bytes to the mtr log. |
| 57 |
@param[in] mtr Mini-transaction |
| 58 |
@param[in] str String to write |
| 59 |
@param[in] len String length */ |
| 60 |
void mlog_catenate_string(mtr_t *mtr, const byte *str, ulint len) { |
| 61 |
if (mtr_get_log_mode(mtr) == MTR_LOG_NONE) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
mtr->get_log()->push(str, uint32_t(len)); |
| 66 |
} |
| 67 |
|
| 68 |
#ifndef UNIV_HOTBACKUP |
| 69 |
/** Writes the initial part of a log record consisting of one-byte item |
| 70 |
type and four-byte space and page numbers. Also pushes info |
| 71 |
to the mtr memo that a buffer page has been modified. */ |
| 72 |
void mlog_write_initial_log_record( |
| 73 |
const byte *ptr, /*!< in: pointer to (inside) a buffer |
| 74 |
frame holding the file page where |
| 75 |
modification is made */ |
| 76 |
mlog_id_t type, /*!< in: log item type: MLOG_1BYTE, ... */ |
| 77 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 78 |
{ |
| 79 |
byte *log_ptr = nullptr; |
| 80 |
|
| 81 |
ut_ad(type <= MLOG_BIGGEST_TYPE); |
| 82 |
ut_ad(type > MLOG_8BYTES); |
| 83 |
|
| 84 |
/* If no logging is requested, we may return now */ |
| 85 |
if (!mlog_open(mtr, REDO_LOG_INITIAL_INFO_SIZE, log_ptr)) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
log_ptr = mlog_write_initial_log_record_fast(ptr, type, log_ptr, mtr); |
| 90 |
|
| 91 |
mlog_close(mtr, log_ptr); |
| 92 |
} |
| 93 |
#endif /* !UNIV_HOTBACKUP */ |
| 94 |
|
| 95 |
/** Parses an initial log record written by mlog_write_initial_dict_log_record. |
| 96 |
@param[in] ptr buffer |
| 97 |
@param[in] end_ptr buffer end |
| 98 |
@param[out] type log record type, should be |
| 99 |
MLOG_TABLE_DYNAMIC_META |
| 100 |
@param[out] id table id |
| 101 |
@param[out] version table dynamic metadata version |
| 102 |
@return parsed record end, NULL if not a complete record */ |
| 103 |
byte *mlog_parse_initial_dict_log_record(const byte *ptr, const byte *end_ptr, |
| 104 |
mlog_id_t *type, table_id_t *id, |
| 105 |
uint64_t *version) { |
| 106 |
if (end_ptr < ptr + 1) { |
| 107 |
return (nullptr); |
| 108 |
} |
| 109 |
|
| 110 |
*type = (mlog_id_t)((ulint)*ptr & ~MLOG_SINGLE_REC_FLAG); |
| 111 |
ut_ad(*type == MLOG_TABLE_DYNAMIC_META); |
| 112 |
|
| 113 |
ptr++; |
| 114 |
|
| 115 |
if (end_ptr < ptr + 1) { |
| 116 |
return (nullptr); |
| 117 |
} |
| 118 |
|
| 119 |
*id = mach_parse_u64_much_compressed(&ptr, end_ptr); |
| 120 |
|
| 121 |
if (ptr == nullptr || end_ptr < ptr + 1) { |
| 122 |
return (nullptr); |
| 123 |
} |
| 124 |
|
| 125 |
*version = mach_parse_u64_much_compressed(&ptr, end_ptr); |
| 126 |
|
| 127 |
return (const_cast<byte *>(ptr)); |
| 128 |
} |
| 129 |
|
| 130 |
/** Parses an initial log record written by mlog_write_initial_log_record. |
| 131 |
@return parsed record end, NULL if not a complete record */ |
| 132 |
byte *mlog_parse_initial_log_record( |
| 133 |
const byte *ptr, /*!< in: buffer */ |
| 134 |
const byte *end_ptr, /*!< in: buffer end */ |
| 135 |
mlog_id_t *type, /*!< out: log record type: MLOG_1BYTE, ... */ |
| 136 |
space_id_t *space, /*!< out: space id */ |
| 137 |
page_no_t *page_no) /*!< out: page number */ |
| 138 |
{ |
| 139 |
if (end_ptr < ptr + 1) { |
| 140 |
return (nullptr); |
| 141 |
} |
| 142 |
|
| 143 |
*type = (mlog_id_t)((ulint)*ptr & ~MLOG_SINGLE_REC_FLAG); |
| 144 |
ut_ad(*type <= MLOG_BIGGEST_TYPE); |
| 145 |
|
| 146 |
ptr++; |
| 147 |
|
| 148 |
if (end_ptr < ptr + 2) { |
| 149 |
return (nullptr); |
| 150 |
} |
| 151 |
|
| 152 |
*space = mach_parse_compressed(&ptr, end_ptr); |
| 153 |
|
| 154 |
if (ptr != nullptr) { |
| 155 |
*page_no = mach_parse_compressed(&ptr, end_ptr); |
| 156 |
} |
| 157 |
|
| 158 |
return (const_cast<byte *>(ptr)); |
| 159 |
} |
| 160 |
|
| 161 |
/** Parses a log record written by mlog_write_ulint or mlog_write_ull. |
| 162 |
@return parsed record end, NULL if not a complete record or a corrupt record */ |
| 163 |
byte *mlog_parse_nbytes( |
| 164 |
mlog_id_t type, /*!< in: log record type: MLOG_1BYTE, ... */ |
| 165 |
const byte *ptr, /*!< in: buffer */ |
| 166 |
const byte *end_ptr, /*!< in: buffer end */ |
| 167 |
byte *page, /*!< in: page where to apply the log |
| 168 |
record, or NULL */ |
| 169 |
void *page_zip) /*!< in/out: compressed page, or NULL */ |
| 170 |
{ |
| 171 |
ulint offset; |
| 172 |
ulint val; |
| 173 |
uint64_t dval; |
| 174 |
|
| 175 |
ut_a(type <= MLOG_8BYTES); |
| 176 |
ut_a(!page || !page_zip || !fil_page_index_page_check(page)); |
| 177 |
|
| 178 |
if (end_ptr < ptr + 2) { |
| 179 |
return (nullptr); |
| 180 |
} |
| 181 |
|
| 182 |
offset = mach_read_from_2(ptr); |
| 183 |
ptr += 2; |
| 184 |
|
| 185 |
if (offset >= UNIV_PAGE_SIZE) { |
| 186 |
recv_sys->found_corrupt_log = true; |
| 187 |
|
| 188 |
return (nullptr); |
| 189 |
} |
| 190 |
|
| 191 |
if (type == MLOG_8BYTES) { |
| 192 |
dval = mach_u64_parse_compressed(&ptr, end_ptr); |
| 193 |
|
| 194 |
if (ptr == nullptr) { |
| 195 |
return (nullptr); |
| 196 |
} |
| 197 |
|
| 198 |
if (page) { |
| 199 |
if (page_zip) { |
| 200 |
mach_write_to_8(((page_zip_des_t *)page_zip)->data + offset, dval); |
| 201 |
} |
| 202 |
mach_write_to_8(page + offset, dval); |
| 203 |
} |
| 204 |
|
| 205 |
return (const_cast<byte *>(ptr)); |
| 206 |
} |
| 207 |
|
| 208 |
val = mach_parse_compressed(&ptr, end_ptr); |
| 209 |
|
| 210 |
if (ptr == nullptr) { |
| 211 |
return (nullptr); |
| 212 |
} |
| 213 |
|
| 214 |
switch (type) { |
| 215 |
case MLOG_1BYTE: |
| 216 |
if (val > 0xFFUL) { |
| 217 |
goto corrupt; |
| 218 |
} |
| 219 |
if (page) { |
| 220 |
if (page_zip) { |
| 221 |
mach_write_to_1(((page_zip_des_t *)page_zip)->data + offset, val); |
| 222 |
} |
| 223 |
mach_write_to_1(page + offset, val); |
| 224 |
} |
| 225 |
break; |
| 226 |
case MLOG_2BYTES: |
| 227 |
if (val > 0xFFFFUL) { |
| 228 |
goto corrupt; |
| 229 |
} |
| 230 |
if (page) { |
| 231 |
if (page_zip) { |
| 232 |
mach_write_to_2(((page_zip_des_t *)page_zip)->data + offset, val); |
| 233 |
} |
| 234 |
mach_write_to_2(page + offset, val); |
| 235 |
} |
| 236 |
break; |
| 237 |
case MLOG_4BYTES: |
| 238 |
if (page) { |
| 239 |
if (page_zip) { |
| 240 |
mach_write_to_4(((page_zip_des_t *)page_zip)->data + offset, val); |
| 241 |
} |
| 242 |
mach_write_to_4(page + offset, val); |
| 243 |
} |
| 244 |
break; |
| 245 |
default: |
| 246 |
corrupt: |
| 247 |
recv_sys->found_corrupt_log = true; |
| 248 |
ptr = nullptr; |
| 249 |
} |
| 250 |
|
| 251 |
return (const_cast<byte *>(ptr)); |
| 252 |
} |
| 253 |
|
| 254 |
/** Writes 1, 2 or 4 bytes to a file page. Writes the corresponding log |
| 255 |
record to the mini-transaction log if mtr is not NULL. */ |
| 256 |
void mlog_write_ulint( |
| 257 |
byte *ptr, /*!< in: pointer where to write */ |
| 258 |
ulint val, /*!< in: value to write */ |
| 259 |
mlog_id_t type, /*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */ |
| 260 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 261 |
{ |
| 262 |
switch (type) { |
| 263 |
case MLOG_1BYTE: |
| 264 |
mach_write_to_1(ptr, val); |
| 265 |
break; |
| 266 |
case MLOG_2BYTES: |
| 267 |
mach_write_to_2(ptr, val); |
| 268 |
break; |
| 269 |
case MLOG_4BYTES: |
| 270 |
mach_write_to_4(ptr, val); |
| 271 |
break; |
| 272 |
default: |
| 273 |
ut_error; |
| 274 |
} |
| 275 |
|
| 276 |
if (mtr == nullptr) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
/* If no logging is requested, we may return now */ |
| 281 |
byte *log_ptr = nullptr; |
| 282 |
if (!mlog_open(mtr, REDO_LOG_INITIAL_INFO_SIZE + 2 + 5, log_ptr)) { |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
log_ptr = mlog_write_initial_log_record_fast(ptr, type, log_ptr, mtr); |
| 287 |
|
| 288 |
mach_write_to_2(log_ptr, page_offset(ptr)); |
| 289 |
log_ptr += 2; |
| 290 |
|
| 291 |
log_ptr += mach_write_compressed(log_ptr, val); |
| 292 |
|
| 293 |
mlog_close(mtr, log_ptr); |
| 294 |
} |
| 295 |
|
| 296 |
/** Writes 8 bytes to a file page. Writes the corresponding log |
| 297 |
record to the mini-transaction log, only if mtr is not NULL */ |
| 298 |
void mlog_write_ull(byte *ptr, /*!< in: pointer where to write */ |
| 299 |
uint64_t val, /*!< in: value to write */ |
| 300 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 301 |
{ |
| 302 |
mach_write_to_8(ptr, val); |
| 303 |
|
| 304 |
if (mtr == nullptr) { |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
/* If no logging is requested, we may return now */ |
| 309 |
byte *log_ptr = nullptr; |
| 310 |
if (!mlog_open(mtr, REDO_LOG_INITIAL_INFO_SIZE + 2 + 9, log_ptr)) { |
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
log_ptr = mlog_write_initial_log_record_fast(ptr, MLOG_8BYTES, log_ptr, mtr); |
| 315 |
|
| 316 |
mach_write_to_2(log_ptr, page_offset(ptr)); |
| 317 |
log_ptr += 2; |
| 318 |
|
| 319 |
log_ptr += mach_u64_write_compressed(log_ptr, val); |
| 320 |
|
| 321 |
mlog_close(mtr, log_ptr); |
| 322 |
} |
| 323 |
|
| 324 |
#ifndef UNIV_HOTBACKUP |
| 325 |
/** Writes a string to a file page buffered in the buffer pool. Writes the |
| 326 |
corresponding log record to the mini-transaction log. */ |
| 327 |
void mlog_write_string(byte *ptr, /*!< in: pointer where to write */ |
| 328 |
const byte *str, /*!< in: string to write */ |
| 329 |
ulint len, /*!< in: string length */ |
| 330 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 331 |
{ |
| 332 |
ut_ad(ptr && mtr); |
| 333 |
ut_a(len < UNIV_PAGE_SIZE); |
| 334 |
|
| 335 |
memcpy(ptr, str, len); |
| 336 |
|
| 337 |
mlog_log_string(ptr, len, mtr); |
| 338 |
} |
| 339 |
|
| 340 |
/** Logs a write of a string to a file page buffered in the buffer pool. |
| 341 |
Writes the corresponding log record to the mini-transaction log. */ |
| 342 |
void mlog_log_string(byte *ptr, /*!< in: pointer written to */ |
| 343 |
ulint len, /*!< in: string length */ |
| 344 |
mtr_t *mtr) /*!< in: mini-transaction handle */ |
| 345 |
{ |
| 346 |
byte *log_ptr = nullptr; |
| 347 |
|
| 348 |
ut_ad(ptr && mtr); |
| 349 |
ut_ad(len <= UNIV_PAGE_SIZE); |
| 350 |
|
| 351 |
/* If no logging is requested, we may return now */ |
| 352 |
if (!mlog_open(mtr, 30, log_ptr)) { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
log_ptr = |
| 357 |
mlog_write_initial_log_record_fast(ptr, MLOG_WRITE_STRING, log_ptr, mtr); |
| 358 |
mach_write_to_2(log_ptr, page_offset(ptr)); |
| 359 |
log_ptr += 2; |
| 360 |
|
| 361 |
mach_write_to_2(log_ptr, len); |
| 362 |
log_ptr += 2; |
| 363 |
|
| 364 |
mlog_close(mtr, log_ptr); |
| 365 |
|
| 366 |
mlog_catenate_string(mtr, ptr, len); |
| 367 |
} |
| 368 |
#endif /* !UNIV_HOTBACKUP */ |
| 369 |
|
| 370 |
/** Parses a log record written by mlog_write_string. |
| 371 |
@return parsed record end, NULL if not a complete record */ |
| 372 |
byte *mlog_parse_string( |
| 373 |
byte *ptr, /*!< in: buffer */ |
| 374 |
byte *end_ptr, /*!< in: buffer end */ |
| 375 |
byte *page, /*!< in: page where to apply the log record, or NULL */ |
| 376 |
void *page_zip) /*!< in/out: compressed page, or NULL */ |
| 377 |
{ |
| 378 |
ulint offset; |
| 379 |
ulint len; |
| 380 |
|
| 381 |
ut_a(!page || !page_zip || |
| 382 |
(fil_page_get_type(page) != FIL_PAGE_INDEX && |
| 383 |
fil_page_get_type(page) != FIL_PAGE_RTREE)); |
| 384 |
|
| 385 |
if (end_ptr < ptr + 4) { |
| 386 |
return (nullptr); |
| 387 |
} |
| 388 |
|
| 389 |
offset = mach_read_from_2(ptr); |
| 390 |
ptr += 2; |
| 391 |
len = mach_read_from_2(ptr); |
| 392 |
ptr += 2; |
| 393 |
|
| 394 |
if (offset >= UNIV_PAGE_SIZE || len + offset > UNIV_PAGE_SIZE) { |
| 395 |
recv_sys->found_corrupt_log = true; |
| 396 |
|
| 397 |
return (nullptr); |
| 398 |
} |
| 399 |
|
| 400 |
if (end_ptr < ptr + len) { |
| 401 |
return (nullptr); |
| 402 |
} |
| 403 |
|
| 404 |
if (page) { |
| 405 |
if (page_zip) { |
| 406 |
memcpy(((page_zip_des_t *)page_zip)->data + offset, ptr, len); |
| 407 |
} |
| 408 |
memcpy(page + offset, ptr, len); |
| 409 |
} |
| 410 |
|
| 411 |
return (ptr + len); |
| 412 |
} |
| 413 |
|
| 414 |
byte *mlog_parse_index_8027(byte *ptr, const byte *end_ptr, bool comp, |
| 415 |
dict_index_t **index) { |
| 416 |
ulint i; |
| 417 |
dict_table_t *table; |
| 418 |
dict_index_t *ind; |
| 419 |
bool instant = false; |
| 420 |
uint16_t n, n_uniq; |
| 421 |
uint16_t n_inst_cols = 0; |
| 422 |
|
| 423 |
if (comp) { |
| 424 |
if (end_ptr < ptr + 4) { |
| 425 |
return (nullptr); |
| 426 |
} |
| 427 |
n = mach_read_from_2(ptr); |
| 428 |
ptr += 2; |
| 429 |
if ((n & 0x8000) != 0) { |
| 430 |
/* This is instant fields, |
| 431 |
see also mlog_open_and_write_index() */ |
| 432 |
instant = true; |
| 433 |
n_inst_cols = n & ~0x8000; |
| 434 |
n = mach_read_from_2(ptr); |
| 435 |
ptr += 2; |
| 436 |
ut_ad((n & 0x8000) == 0); |
| 437 |
ut_ad(n_inst_cols <= n); |
| 438 |
|
| 439 |
if (end_ptr < ptr + 2) { |
| 440 |
return (nullptr); |
| 441 |
} |
| 442 |
} |
| 443 |
n_uniq = mach_read_from_2(ptr); |
| 444 |
ptr += 2; |
| 445 |
ut_ad(n_uniq <= n); |
| 446 |
if (end_ptr < ptr + n * 2) { |
| 447 |
return (nullptr); |
| 448 |
} |
| 449 |
} else { |
| 450 |
n = n_uniq = 1; |
| 451 |
} |
| 452 |
table = dict_mem_table_create("LOG_DUMMY", DICT_HDR_SPACE, n, 0, 0, |
| 453 |
comp ? DICT_TF_COMPACT : 0, 0); |
| 454 |
if (instant) { |
| 455 |
table->set_instant_cols(n_inst_cols); |
| 456 |
} |
| 457 |
|
| 458 |
ind = dict_mem_index_create("LOG_DUMMY", "LOG_DUMMY", DICT_HDR_SPACE, 0, n); |
| 459 |
ind->table = table; |
| 460 |
ind->n_uniq = (unsigned int)n_uniq; |
| 461 |
if (n_uniq != n) { |
| 462 |
ut_a(n_uniq + DATA_ROLL_PTR <= n); |
| 463 |
ind->type = DICT_CLUSTERED; |
| 464 |
} |
| 465 |
if (comp) { |
| 466 |
for (i = 0; i < n; i++) { |
| 467 |
ulint len = mach_read_from_2(ptr); |
| 468 |
ptr += 2; |
| 469 |
/* The high-order bit of len is the NOT NULL flag; |
| 470 |
the rest is 0 or 0x7fff for variable-length fields, |
| 471 |
and 1..0x7ffe for fixed-length fields. */ |
| 472 |
dict_mem_table_add_col( |
| 473 |
table, nullptr, nullptr, |
| 474 |
((len + 1) & 0x7fff) <= 1 ? DATA_BINARY : DATA_FIXBINARY, |
| 475 |
len & 0x8000 ? DATA_NOT_NULL : 0, len & 0x7fff, true); |
| 476 |
|
| 477 |
/* The is_ascending flag does not matter during |
| 478 |
redo log apply, because we do not compare for |
| 479 |
"less than" or "greater than". */ |
| 480 |
dict_index_add_col(ind, table, table->get_col(i), 0, true); |
| 481 |
} |
| 482 |
dict_table_add_system_columns(table, table->heap); |
| 483 |
if (n_uniq != n) { |
| 484 |
/* Identify DB_TRX_ID and DB_ROLL_PTR in the index. */ |
| 485 |
ut_a(DATA_TRX_ID_LEN == ind->get_col(DATA_TRX_ID - 1 + n_uniq)->len); |
| 486 |
ut_a(DATA_ROLL_PTR_LEN == ind->get_col(DATA_ROLL_PTR - 1 + n_uniq)->len); |
| 487 |
ind->fields[DATA_TRX_ID - 1 + n_uniq].col = &table->cols[n + DATA_TRX_ID]; |
| 488 |
ind->fields[DATA_ROLL_PTR - 1 + n_uniq].col = |
| 489 |
&table->cols[n + DATA_ROLL_PTR]; |
| 490 |
} |
| 491 |
|
| 492 |
if (ind->is_clustered() && ind->table->has_instant_cols()) { |
| 493 |
ind->instant_cols = true; |
| 494 |
ind->n_instant_nullable = |
| 495 |
ind->get_n_nullable_before(ind->get_instant_fields()); |
| 496 |
} else { |
| 497 |
ind->instant_cols = false; |
| 498 |
ind->n_instant_nullable = ind->n_nullable; |
| 499 |
} |
| 500 |
} |
| 501 |
/* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */ |
| 502 |
ind->cached = true; |
| 503 |
*index = ind; |
| 504 |
return (ptr); |
| 505 |
} |
| 506 |
|
| 507 |
#ifndef UNIV_HOTBACKUP |
| 508 |
/* logical_pos 2 bytes, phy_pos 2 bytes, v_added 1 byte, v_dropped 1 byte */ |
| 509 |
constexpr size_t inst_col_info_size = 6; |
| 510 |
|
| 511 |
/** Calculate total size needed to log index information. |
| 512 |
@param[in] index index |
| 513 |
@param[in] size size passed from caller |
| 514 |
@param[in] n number of fields in index |
| 515 |
@param[in] is_comp true if COMP |
| 516 |
@param[in] is_versioned if table has row versions |
| 517 |
@param[in] is_instant true if table has INSTANT cols |
| 518 |
@param[in] fields_with_changed_order bitmap to indicate fields with changed |
| 519 |
order |
| 520 |
@param[out] size_needed total size needed on REDO LOG */ |
| 521 |
static void log_index_get_size_needed(const dict_index_t *index, size_t size, |
| 522 |
uint16_t n, bool is_comp, |
| 523 |
bool is_versioned, bool is_instant, |
| 524 |
const bool *fields_with_changed_order, |
| 525 |
size_t &size_needed) { |
| 526 |
auto size_for_versioned_fields = [&](const dict_index_t *ind) { |
| 527 |
size_t _size = 0; |
| 528 |
/* 2 bytes for number of columns with version */ |
| 529 |
_size += 2; |
| 530 |
|
| 531 |
size_t n_versioned_fields = ind->table->get_n_instant_add_cols() + |
| 532 |
ind->table->get_n_instant_drop_cols(); |
| 533 |
ut_ad(n_versioned_fields != 0); |
| 534 |
|
| 535 |
_size += n_versioned_fields * inst_col_info_size; |
| 536 |
|
| 537 |
/* For fields with changed order */ |
| 538 |
size_t n_changed_order_fields = 0; |
| 539 |
for (size_t i = 0; i < n; i++) { |
| 540 |
if (fields_with_changed_order[i]) { |
| 541 |
n_changed_order_fields++; |
| 542 |
} |
| 543 |
} |
| 544 |
_size += n_changed_order_fields * inst_col_info_size; |
| 545 |
|
| 546 |
return (_size); |
| 547 |
}; |
| 548 |
|
| 549 |
ut_ad(size_needed == 0); |
| 550 |
|
| 551 |
size_needed += REDO_LOG_INITIAL_INFO_SIZE + size; |
| 552 |
|
| 553 |
/* 1 byte to log INDEX_LOG_VERSION */ |
| 554 |
size_needed += 1; |
| 555 |
|
| 556 |
/* 1 byte to log flag */ |
| 557 |
size_needed += 1; |
| 558 |
|
| 559 |
if (!is_versioned && !is_comp) { |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
/* 2 bytes to log n */ |
| 564 |
size_needed += 2; |
| 565 |
|
| 566 |
if (!is_comp) { |
| 567 |
ut_ad(is_versioned); |
| 568 |
size_needed += size_for_versioned_fields(index); |
| 569 |
return; |
| 570 |
} |
| 571 |
|
| 572 |
if (is_instant) { |
| 573 |
/* 2 bytes to log n_instant_cols */ |
| 574 |
size_needed += 2; |
| 575 |
} |
| 576 |
|
| 577 |
/* 2 bytes to log n_uniq */ |
| 578 |
size_needed += 2; |
| 579 |
|
| 580 |
/* 2 bytes needed for each (n) field to store it's len */ |
| 581 |
size_needed += n * 2; |
| 582 |
|
| 583 |
if (is_versioned) { |
| 584 |
size_needed += size_for_versioned_fields(index); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
/** Log the index log version. |
| 589 |
@param[in] version 1 byte index log version |
| 590 |
@param[in,out] log_ptr REDO LOG buffer pointer */ |
| 591 |
static void log_index_log_version(uint8_t version, byte *&log_ptr) { |
| 592 |
mach_write_to_1(log_ptr, version); |
| 593 |
log_ptr += 1; |
| 594 |
} |
| 595 |
|
| 596 |
/** Log the flag. |
| 597 |
@param[in] flag 1 byte flag to be logged |
| 598 |
@param[in,out] log_ptr REDO LOG buffer pointer */ |
| 599 |
static void log_index_flag(uint8_t flag, byte *&log_ptr) { |
| 600 |
mach_write_to_1(log_ptr, flag); |
| 601 |
log_ptr += 1; |
| 602 |
} |
| 603 |
|
| 604 |
/** Log the number of fields in index. |
| 605 |
@param[in] index index |
| 606 |
@param[in] n number of fields |
| 607 |
@param[in] rec index record |
| 608 |
@param[in] is_comp true if COMP |
| 609 |
@param[in] is_versioned true if table has row versions |
| 610 |
@param[in] is_instant true if table has INSTANT cols |
| 611 |
@param[in,out] log_ptr REDO LOG buffer pointer */ |
| 612 |
static void log_index_column_counts(const dict_index_t *index, uint16_t n, |
| 613 |
const byte *rec, bool is_comp, |
| 614 |
bool is_versioned, bool is_instant, |
| 615 |
byte *&log_ptr) { |
| 616 |
/* Only clustered index can have versions */ |
| 617 |
ut_ad(!is_versioned || index->is_clustered()); |
| 618 |
|
| 619 |
if (!is_versioned && !is_comp) { |
| 620 |
return; |
| 621 |
} |
| 622 |
|
| 623 |
/* log n */ |
| 624 |
mach_write_to_2(log_ptr, n); |
| 625 |
log_ptr += 2; |
| 626 |
|
| 627 |
if (!is_comp) { |
| 628 |
ut_ad(is_versioned); |
| 629 |
return; |
| 630 |
} |
| 631 |
|
| 632 |
if (is_instant) { |
| 633 |
mach_write_to_2(log_ptr, index->get_instant_fields()); |
| 634 |
log_ptr += 2; |
| 635 |
} |
| 636 |
|
| 637 |
/* log n_uniq */ |
| 638 |
const auto n_uniq = page_is_leaf(page_align(rec)) |
| 639 |
? dict_index_get_n_unique_in_tree(index) |
| 640 |
: dict_index_get_n_unique_in_tree_nonleaf(index); |
| 641 |
ut_ad(n_uniq <= n); |
| 642 |
mach_write_to_2(log_ptr, n_uniq); |
| 643 |
log_ptr += 2; |
| 644 |
} |
| 645 |
|
| 646 |
/** Close, allocate and reopen LOG pointer buffer. |
| 647 |
@param[in] log_ptr pointer to log buffer |
| 648 |
@param[in,out] log_start start of currently allocated buffer |
| 649 |
@param[in,out] log_end end of currently allocated buffer |
| 650 |
@param[in] mtr mini transaction |
| 651 |
@param[in,out] alloc size allocated as of now on log |
| 652 |
@param[in,out] total total size needed on log */ |
| 653 |
static bool close_and_reopen_log(byte *&log_ptr, const byte *&log_start, |
| 654 |
const byte *&log_end, mtr_t *&mtr, |
| 655 |
size_t &alloc, size_t &total) { |
| 656 |
mlog_close(mtr, log_ptr); |
| 657 |
ut_a(total > (ulint)(log_ptr - log_start)); |
| 658 |
total -= log_ptr - log_start; |
| 659 |
alloc = total; |
| 660 |
|
| 661 |
if (alloc > mtr_buf_t::MAX_DATA_SIZE) { |
| 662 |
alloc = mtr_buf_t::MAX_DATA_SIZE; |
| 663 |
} |
| 664 |
|
| 665 |
if (!mlog_open(mtr, alloc, log_ptr)) { |
| 666 |
/* logging is disabled */ |
| 667 |
return false; |
| 668 |
} |
| 669 |
log_start = log_ptr; |
| 670 |
log_end = log_ptr + alloc; |
| 671 |
|
| 672 |
return true; |
| 673 |
} |
| 674 |
|
| 675 |
template <typename F> |
| 676 |
/** Log index field len info. |
| 677 |
@param[in] index index |
| 678 |
@param[in] n number of fields |
| 679 |
@param[in] is_versioned true if table has row versions |
| 680 |
@param[in,out] f vector of fields with versions |
| 681 |
@param[in] changed_order array indicating fields changed position |
| 682 |
@param[in] log_ptr log buffer pointer |
| 683 |
@param[in] func callback to check size reopen log buffer */ |
| 684 |
static bool log_index_fields(const dict_index_t *index, uint16_t n, |
| 685 |
bool is_versioned, std::vector<dict_field_t *> &f, |
| 686 |
bool *changed_order, byte *&log_ptr, F &func) { |
| 687 |
/* Write metadata for each field. Log the fields in their logical order. */ |
| 688 |
for (size_t i = 0; i < n; i++) { |
| 689 |
dict_field_t *field = index->get_field(i); |
| 690 |
const dict_col_t *col = field->col; |
| 691 |
ulint len = field->fixed_len; |
| 692 |
ut_ad(len < 0x7fff); |
| 693 |
|
| 694 |
if (len == 0 && (DATA_BIG_COL(col))) { |
| 695 |
/* variable-length field with maximum length > 255 */ |
| 696 |
len = 0x7fff; |
| 697 |
} |
| 698 |
|
| 699 |
if (col->prtype & DATA_NOT_NULL) { |
| 700 |
len |= 0x8000; |
| 701 |
} |
| 702 |
|
| 703 |
if (!func(2)) { |
| 704 |
return false; |
| 705 |
} |
| 706 |
|
| 707 |
mach_write_to_2(log_ptr, len); |
| 708 |
log_ptr += 2; |
| 709 |
|
| 710 |
if (is_versioned) { |
| 711 |
if (col->is_instant_added() || col->is_instant_dropped() || |
| 712 |
changed_order[i]) { |
| 713 |
f.push_back(field); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
return true; |
| 719 |
} |
| 720 |
|
| 721 |
template <typename F> |
| 722 |
/** Log fields with version. |
| 723 |
@param[in] f vector of fields with versions |
| 724 |
@param[in] log_ptr log buffer pointer |
| 725 |
@param[in] func callback to check size reopen log buffer |
| 726 |
@param[in] index index to fetch field's logical position */ |
| 727 |
static bool log_index_versioned_fields(const std::vector<dict_field_t *> &f, |
| 728 |
byte *&log_ptr, F &func, |
| 729 |
const dict_index_t *index) { |
| 730 |
uint16_t n_inst = f.size(); |
| 731 |
ut_ad(n_inst > 0); |
| 732 |
|
| 733 |
if (!func(2)) { |
| 734 |
return false; |
| 735 |
} |
| 736 |
mach_write_to_2(log_ptr, n_inst); |
| 737 |
log_ptr += 2; |
| 738 |
|
| 739 |
for (auto field : f) { |
| 740 |
uint16_t logical_pos = index->get_logical_pos(field->get_phy_pos()); |
| 741 |
ut_a(logical_pos != UINT16_UNDEFINED); |
| 742 |
|
| 743 |
/* Maximum columns could be 1017. Which needs maximum 10 bits. So we can |
| 744 |
use MSB to indicate if version info follows. |
| 745 |
- - - - - -[----------] |
| 746 |
^ ^ ^ |
| 747 |
| | | |
| 748 |
| | | 10 bits phy pos |
| 749 |
| | |
| 750 |
| | 15th bit indicates drop version info follows. |
| 751 |
| 16th bit indicates add version info follows. */ |
| 752 |
uint16_t phy_pos = field->get_phy_pos(); |
| 753 |
|
| 754 |
/* It also might be accompanying column order change (!added&&!dropped) */ |
| 755 |
|
| 756 |
if (field->col->is_instant_added()) { |
| 757 |
/* Set 16th bit in phy_pos to indicate presence of version added */ |
| 758 |
phy_pos |= 0x8000; |
| 759 |
} |
| 760 |
|
| 761 |
if (field->col->is_instant_dropped()) { |
| 762 |
/* Set 15th bit in phy_pos to indicate presence of version added */ |
| 763 |
phy_pos |= 0x4000; |
| 764 |
} |
| 765 |
|
| 766 |
if (!func(6)) { |
| 767 |
return false; |
| 768 |
} |
| 769 |
|
| 770 |
mach_write_to_2(log_ptr, logical_pos); |
| 771 |
log_ptr += 2; |
| 772 |
|
| 773 |
mach_write_to_2(log_ptr, phy_pos); |
| 774 |
log_ptr += 2; |
| 775 |
|
| 776 |
if (field->col->is_instant_added()) { |
| 777 |
uint8_t v = field->col->get_version_added(); |
| 778 |
mach_write_to_1(log_ptr, v); |
| 779 |
log_ptr += 1; |
| 780 |
} |
| 781 |
|
| 782 |
if (field->col->is_instant_dropped()) { |
| 783 |
uint8_t v = field->col->get_version_dropped(); |
| 784 |
mach_write_to_1(log_ptr, v); |
| 785 |
log_ptr += 1; |
| 786 |
} |
| 787 |
} |
| 788 |
return true; |
| 789 |
} |
| 790 |
#endif |
| 791 |
|
| 792 |
bool mlog_open_and_write_index(mtr_t *mtr, const byte *rec, |
| 793 |
const dict_index_t *index, mlog_id_t type, |
| 794 |
size_t size, byte *&log_ptr) { |
| 795 |
#ifndef UNIV_HOTBACKUP |
| 796 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table)); |
| 797 |
const bool is_instant = index->has_instant_cols(); |
| 798 |
const bool is_versioned = index->has_row_versions(); |
| 799 |
const bool is_comp = dict_table_is_comp(index->table); |
| 800 |
|
| 801 |
const byte *log_start; |
| 802 |
const byte *log_end; |
| 803 |
|
| 804 |
uint16_t n = is_versioned ? index->get_n_total_fields() |
| 805 |
: dict_index_get_n_fields(index); |
| 806 |
/* For spatial index, on non-leaf page, we just keep |
| 807 |
2 fields, MBR and page no. */ |
| 808 |
if (dict_index_is_spatial(index) && !page_is_leaf(page_align(rec))) { |
| 809 |
n = DICT_INDEX_SPATIAL_NODEPTR_SIZE; |
| 810 |
} |
| 811 |
|
| 812 |
/* Ordinal position of an existing field can't be changed with INSTANT |
| 813 |
algorithm. But when it is combined with ADD/DROP COLUMN, ordinal position |
| 814 |
of a filed can be changed. This bool array of size #fields in index, |
| 815 |
represents if ordinal position of an existing filed is changed. */ |
| 816 |
bool *fields_with_changed_order = nullptr; |
| 817 |
if (is_versioned) { |
| 818 |
fields_with_changed_order = new bool[n]; |
| 819 |
memset(fields_with_changed_order, false, (sizeof(bool) * n)); |
| 820 |
|
| 821 |
uint16_t phy_pos = 0; |
| 822 |
for (size_t i = 0; i < n; i++) { |
| 823 |
dict_field_t *field = index->get_field(i); |
| 824 |
const dict_col_t *col = field->col; |
| 825 |
|
| 826 |
if (col->is_instant_added() || col->is_instant_dropped()) { |
| 827 |
continue; |
| 828 |
} else if (field->get_phy_pos() >= phy_pos) { |
| 829 |
phy_pos = field->get_phy_pos(); |
| 830 |
} else { |
| 831 |
fields_with_changed_order[i] = true; |
| 832 |
} |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
size_t size_needed = 0; |
| 837 |
log_index_get_size_needed(index, size, n, is_comp, is_versioned, is_instant, |
| 838 |
fields_with_changed_order, size_needed); |
| 839 |
size_t total = size_needed; |
| 840 |
size_t alloc = total; |
| 841 |
if (alloc > mtr_buf_t::MAX_DATA_SIZE) { |
| 842 |
alloc = mtr_buf_t::MAX_DATA_SIZE; |
| 843 |
} |
| 844 |
|
| 845 |
if (!mlog_open(mtr, alloc, log_ptr)) { |
| 846 |
if (is_versioned) { |
| 847 |
delete[] fields_with_changed_order; |
| 848 |
} |
| 849 |
/* logging is disabled */ |
| 850 |
return (false); |
| 851 |
} |
| 852 |
|
| 853 |
log_start = log_ptr; |
| 854 |
log_end = log_ptr + alloc; |
| 855 |
|
| 856 |
log_ptr = mlog_write_initial_log_record_fast(rec, type, log_ptr, mtr); |
| 857 |
|
| 858 |
uint8_t index_log_version = INDEX_LOG_VERSION_CURRENT; |
| 859 |
DBUG_EXECUTE_IF("invalid_index_log_version", |
| 860 |
index_log_version = INDEX_LOG_VERSION_MAX + 1;); |
| 861 |
log_index_log_version(index_log_version, log_ptr); |
| 862 |
|
| 863 |
uint8_t flag = 0; |
| 864 |
if (is_instant) SET_INSTANT(flag); |
| 865 |
if (is_versioned) SET_VERSIONED(flag); |
| 866 |
if (is_comp) SET_COMPACT(flag); |
| 867 |
log_index_flag(flag, log_ptr); |
| 868 |
|
| 869 |
log_index_column_counts(index, n, rec, is_comp, is_versioned, is_instant, |
| 870 |
log_ptr); |
| 871 |
|
| 872 |
/* List of INSTANT fields to be logged */ |
| 873 |
std::vector<dict_field_t *> instant_fields_to_log; |
| 874 |
|
| 875 |
/* To check _size is available on buffer. If not, close and reopen buffer */ |
| 876 |
auto f = [&](const size_t _size) { |
| 877 |
if (log_ptr + _size > log_end) { |
| 878 |
if (!close_and_reopen_log(log_ptr, log_start, log_end, mtr, alloc, |
| 879 |
total)) { |
| 880 |
return false; |
| 881 |
} |
| 882 |
} |
| 883 |
return true; |
| 884 |
}; |
| 885 |
|
| 886 |
if (is_comp) { |
| 887 |
/* Write fields info. */ |
| 888 |
if (!log_index_fields(index, n, is_versioned, instant_fields_to_log, |
| 889 |
fields_with_changed_order, log_ptr, f)) { |
| 890 |
if (is_versioned) { |
| 891 |
delete[] fields_with_changed_order; |
| 892 |
} |
| 893 |
return false; |
| 894 |
} |
| 895 |
} else if (is_versioned) { |
| 896 |
for (size_t i = 0; i < n; i++) { |
| 897 |
dict_field_t *field = index->get_field(i); |
| 898 |
const dict_col_t *col = field->col; |
| 899 |
if (col->is_instant_added() || col->is_instant_dropped() || |
| 900 |
fields_with_changed_order[i]) { |
| 901 |
instant_fields_to_log.push_back(field); |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
if (is_versioned) { |
| 907 |
delete[] fields_with_changed_order; |
| 908 |
} |
| 909 |
|
| 910 |
if (!instant_fields_to_log.empty()) { |
| 911 |
ut_ad(is_versioned); |
| 912 |
/* Log INSTANT ADD/DROP fields */ |
| 913 |
if (!log_index_versioned_fields(instant_fields_to_log, log_ptr, f, index)) { |
| 914 |
return false; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
if (size == 0) { |
| 919 |
mlog_close(mtr, log_ptr); |
| 920 |
log_ptr = nullptr; |
| 921 |
} else if (log_ptr + size > log_end) { |
| 922 |
mlog_close(mtr, log_ptr); |
| 923 |
bool success = mlog_open(mtr, size, log_ptr); |
| 924 |
ut_a(success); |
| 925 |
} |
| 926 |
|
| 927 |
return (log_ptr != nullptr); |
| 928 |
#else /* !UNIV_HOTBACKUP */ |
| 929 |
return (false); |
| 930 |
#endif /* !UNIV_HOTBACKUP */ |
| 931 |
} |
| 932 |
|
| 933 |
/** Read 2 bytes from log buffer. |
| 934 |
@param[in] ptr pointer to buffer |
| 935 |
@param[in] end_ptr pointer to end of buffer |
| 936 |
@param[out] val read 2 bytes value */ |
| 937 |
static byte *read_2_bytes(byte *ptr, const byte *end_ptr, uint16_t &val) { |
| 938 |
if (end_ptr < ptr + 2) { |
| 939 |
return (nullptr); |
| 940 |
} |
| 941 |
val = mach_read_from_2(ptr); |
| 942 |
ptr += 2; |
| 943 |
return ptr; |
| 944 |
} |
| 945 |
|
| 946 |
/** Read 1 bytes from log buffer. |
| 947 |
@param[in] ptr pointer to buffer |
| 948 |
@param[in] end_ptr pointer to end of buffer |
| 949 |
@param[out] val read 2 bytes value */ |
| 950 |
static byte *read_1_bytes(byte *ptr, const byte *end_ptr, uint8_t &val) { |
| 951 |
if (end_ptr < ptr + 1) { |
| 952 |
return (nullptr); |
| 953 |
} |
| 954 |
val = mach_read_from_1(ptr); |
| 955 |
ptr += 1; |
| 956 |
return ptr; |
| 957 |
} |
| 958 |
|
| 959 |
/** Read number of columns for index. |
| 960 |
@param[in] ptr pointer to buffer |
| 961 |
@param[in] end_ptr pointer to end of buffer |
| 962 |
@param[in] is_comp true if COMP |
| 963 |
@param[in] is_versioned true if table has row versions |
| 964 |
@param[in] is_instant true if table has INSTANT cols |
| 965 |
@param[out] n number of index fields |
| 966 |
@param[out] n_uniq n_uniq for index |
| 967 |
@param[out] inst_cols number of column before first instant add was done. |
| 968 |
@return pointer to buffer. */ |
| 969 |
static byte *parse_index_column_counts(byte *ptr, const byte *end_ptr, |
| 970 |
bool is_comp, bool is_versioned, |
| 971 |
bool is_instant, uint16_t &n, |
| 972 |
uint16_t &n_uniq, uint16_t &inst_cols) { |
| 973 |
if (!is_versioned && !is_comp) { |
| 974 |
n = n_uniq = 1; |
| 975 |
inst_cols = 0; |
| 976 |
return ptr; |
| 977 |
} |
| 978 |
|
| 979 |
/* Parse n */ |
| 980 |
ptr = read_2_bytes(ptr, end_ptr, n); |
| 981 |
if (ptr == nullptr) { |
| 982 |
return ptr; |
| 983 |
} |
| 984 |
|
| 985 |
if (!is_comp) { |
| 986 |
ut_ad(is_versioned); |
| 987 |
return ptr; |
| 988 |
} |
| 989 |
|
| 990 |
if (is_instant) { |
| 991 |
ptr = read_2_bytes(ptr, end_ptr, inst_cols); |
| 992 |
if (ptr == nullptr) { |
| 993 |
return ptr; |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
ptr = read_2_bytes(ptr, end_ptr, n_uniq); |
| 998 |
if (ptr == nullptr) { |
| 999 |
return ptr; |
| 1000 |
} |
| 1001 |
ut_ad(n_uniq <= n); |
| 1002 |
|
| 1003 |
return ptr; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** Parse index fields. |
| 1007 |
@param[in] ptr pointer to buffer |
| 1008 |
@param[in] end_ptr pointer to end of buffer |
| 1009 |
@param[in] n number of fields |
| 1010 |
@param[in] n_uniq n_uniq |
| 1011 |
@param[in] is_versioned true if table has row versions |
| 1012 |
@param[in,out] ind dummy index |
| 1013 |
@param[in,out] table dummy table |
| 1014 |
@return pointer to log buffer */ |
| 1015 |
static byte *parse_index_fields(byte *ptr, const byte *end_ptr, uint16_t n, |
| 1016 |
uint16_t n_uniq, bool is_versioned, |
| 1017 |
dict_index_t *&ind, dict_table_t *&table) { |
| 1018 |
for (size_t i = 0; i < n; i++) { |
| 1019 |
/* For redundant, col len metadata isn't needed for recovery as it is |
| 1020 |
part of record itself. */ |
| 1021 |
uint16_t len = 0; |
| 1022 |
ptr = read_2_bytes(ptr, end_ptr, len); |
| 1023 |
if (ptr == nullptr) { |
| 1024 |
return (nullptr); |
| 1025 |
} |
| 1026 |
|
| 1027 |
uint32_t phy_pos = UINT32_UNDEFINED; |
| 1028 |
uint8_t v_added = UINT8_UNDEFINED; |
| 1029 |
uint8_t v_dropped = UINT8_UNDEFINED; |
| 1030 |
|
| 1031 |
/* The high-order bit of len is the NOT NULL flag; |
| 1032 |
the rest is 0 or 0x7fff for variable-length fields, |
| 1033 |
and 1..0x7ffe for fixed-length fields. */ |
| 1034 |
dict_mem_table_add_col( |
| 1035 |
table, nullptr, nullptr, |
| 1036 |
((len + 1) & 0x7fff) <= 1 ? DATA_BINARY : DATA_FIXBINARY, |
| 1037 |
len & 0x8000 ? DATA_NOT_NULL : 0, len & 0x7fff, true, phy_pos, v_added, |
| 1038 |
v_dropped); |
| 1039 |
|
| 1040 |
/* The is_ascending flag does not matter during |
| 1041 |
redo log apply, because we do not compare for |
| 1042 |
"less than" or "greater than". */ |
| 1043 |
dict_index_add_col(ind, table, table->get_col(i), 0, true); |
| 1044 |
} |
| 1045 |
|
| 1046 |
dict_table_add_system_columns(table, table->heap); |
| 1047 |
|
| 1048 |
/* Identify DB_TRX_ID and DB_ROLL_PTR in the index. */ |
| 1049 |
if (is_versioned || (n_uniq != n)) { |
| 1050 |
size_t i = 0; |
| 1051 |
i = DATA_TRX_ID - 1 + n_uniq; |
| 1052 |
ut_a(DATA_TRX_ID_LEN == ind->get_col(i)->len); |
| 1053 |
ind->fields[i].col = &table->cols[n + DATA_TRX_ID]; |
| 1054 |
ind->fields[i].col->set_phy_pos(table->cols[i].get_phy_pos()); |
| 1055 |
|
| 1056 |
i = DATA_ROLL_PTR - 1 + n_uniq; |
| 1057 |
ut_a(DATA_ROLL_PTR_LEN == ind->get_col(i)->len); |
| 1058 |
ind->fields[i].col = &table->cols[n + DATA_ROLL_PTR]; |
| 1059 |
ind->fields[i].col->set_phy_pos(table->cols[i].get_phy_pos()); |
| 1060 |
} |
| 1061 |
|
| 1062 |
table->initial_col_count = table->current_col_count = table->total_col_count = |
| 1063 |
n; |
| 1064 |
return ptr; |
| 1065 |
} |
| 1066 |
|
| 1067 |
struct Field_instant_info { |
| 1068 |
uint16_t logical_pos{UINT16_UNDEFINED}; |
| 1069 |
uint16_t phy_pos{UINT16_UNDEFINED}; |
| 1070 |
uint8_t v_added{UINT8_UNDEFINED}; |
| 1071 |
uint8_t v_dropped{UINT8_UNDEFINED}; |
| 1072 |
}; |
| 1073 |
|
| 1074 |
using instant_fields_list_t = std::vector<Field_instant_info>; |
| 1075 |
|
| 1076 |
/** Parse the fields with versions. |
| 1077 |
@param[in] ptr pointer to buffer |
| 1078 |
@param[in] end_ptr pointer to end of buffer |
| 1079 |
@param[out] f vector of fields with versions |
| 1080 |
@param[out] crv current row version |
| 1081 |
@param[out] n_dropped number of dropped columns */ |
| 1082 |
static byte *parse_index_versioned_fields(byte *ptr, const byte *end_ptr, |
| 1083 |
instant_fields_list_t &f, |
| 1084 |
uint16_t &crv, size_t &n_dropped) { |
| 1085 |
uint16_t n_inst = 0; |
| 1086 |
ptr = read_2_bytes(ptr, end_ptr, n_inst); |
| 1087 |
if (ptr == nullptr) return (nullptr); |
| 1088 |
ut_ad(n_inst > 0); |
| 1089 |
|
| 1090 |
for (auto i = n_inst; i > 0; --i) { |
| 1091 |
Field_instant_info info; |
| 1092 |
|
| 1093 |
ptr = read_2_bytes(ptr, end_ptr, info.logical_pos); |
| 1094 |
if (ptr == nullptr) return (nullptr); |
| 1095 |
|
| 1096 |
ptr = read_2_bytes(ptr, end_ptr, info.phy_pos); |
| 1097 |
if (ptr == nullptr) return (nullptr); |
| 1098 |
|
| 1099 |
if ((info.phy_pos & 0x8000) != 0) { |
| 1100 |
info.phy_pos &= ~0x8000; |
| 1101 |
|
| 1102 |
/* Read v_added */ |
| 1103 |
ptr = read_1_bytes(ptr, end_ptr, info.v_added); |
| 1104 |
if (ptr == nullptr) return (nullptr); |
| 1105 |
ut_ad(info.v_added != UINT8_UNDEFINED); |
| 1106 |
crv = std::max(crv, (uint16_t)info.v_added); |
| 1107 |
} |
| 1108 |
|
| 1109 |
if ((info.phy_pos & 0x4000) != 0) { |
| 1110 |
info.phy_pos &= ~0x4000; |
| 1111 |
|
| 1112 |
/* Read v_dropped */ |
| 1113 |
ptr = read_1_bytes(ptr, end_ptr, info.v_dropped); |
| 1114 |
if (ptr == nullptr) return (nullptr); |
| 1115 |
ut_ad(info.v_dropped != UINT8_UNDEFINED); |
| 1116 |
crv = std::max(crv, (uint16_t)info.v_dropped); |
| 1117 |
n_dropped++; |
| 1118 |
} |
| 1119 |
|
| 1120 |
ut_ad((info.phy_pos & 0xC000) == 0); |
| 1121 |
|
| 1122 |
f.push_back(info); |
| 1123 |
} |
| 1124 |
|
| 1125 |
return (ptr); |
| 1126 |
} |
| 1127 |
|
| 1128 |
/** Update the version info for the columns. |
| 1129 |
NOTE : fields are logged in their physical order so with the help of phy_pos, |
| 1130 |
it's easy to locate them. |
| 1131 |
@param[in] f fields with versions |
| 1132 |
@param[in,out] index dummy index */ |
| 1133 |
static void update_instant_info(instant_fields_list_t f, dict_index_t *index) { |
| 1134 |
if (f.empty()) { |
| 1135 |
return; |
| 1136 |
} |
| 1137 |
|
| 1138 |
size_t n_added = 0; |
| 1139 |
size_t n_dropped = 0; |
| 1140 |
|
| 1141 |
for (auto field : f) { |
| 1142 |
bool is_added = field.v_added != UINT8_UNDEFINED; |
| 1143 |
bool is_dropped = field.v_dropped != UINT8_UNDEFINED; |
| 1144 |
|
| 1145 |
dict_col_t *col = index->fields[field.logical_pos].col; |
| 1146 |
|
| 1147 |
if (is_dropped) { |
| 1148 |
col->set_version_dropped(field.v_dropped); |
| 1149 |
n_dropped++; |
| 1150 |
if (col->is_nullable()) { |
| 1151 |
ut_a(index->n_nullable > 0); |
| 1152 |
--index->n_nullable; |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (is_added) { |
| 1157 |
col->set_version_added(field.v_added); |
| 1158 |
n_added++; |
| 1159 |
} |
| 1160 |
|
| 1161 |
col->set_phy_pos(field.phy_pos); |
| 1162 |
} |
| 1163 |
|
| 1164 |
index->table->initial_col_count -= n_added; |
| 1165 |
index->table->current_col_count -= n_dropped; |
| 1166 |
index->table->n_cols -= n_dropped; |
| 1167 |
} |
| 1168 |
|
| 1169 |
/** To populate dummy fields. Used only in case of REDUNDANT row format. |
| 1170 |
@param[in,out] index dummy index |
| 1171 |
@param[in,out] table dummy table |
| 1172 |
@param[in] n number of fields |
| 1173 |
@param[in] is_comp true if COMP |
| 1174 |
*/ |
| 1175 |
static void populate_dummy_fields(dict_index_t *index, dict_table_t *table, |
| 1176 |
size_t n IF_DEBUG(, bool is_comp)) { |
| 1177 |
ut_ad(!is_comp); |
| 1178 |
|
| 1179 |
uint32_t phy_pos = UINT32_UNDEFINED; |
| 1180 |
uint8_t v_added = UINT8_UNDEFINED; |
| 1181 |
uint8_t v_dropped = UINT8_UNDEFINED; |
| 1182 |
size_t dummy_len = 10; |
| 1183 |
|
| 1184 |
for (size_t i = 0; i < n; i++) { |
| 1185 |
dict_mem_table_add_col(table, nullptr, nullptr, DATA_BINARY, DATA_NOT_NULL, |
| 1186 |
dummy_len, true, phy_pos, v_added, v_dropped); |
| 1187 |
|
| 1188 |
dict_index_add_col(index, table, table->get_col(i), 0, true); |
| 1189 |
} |
| 1190 |
table->initial_col_count = table->current_col_count = table->total_col_count = |
| 1191 |
n; |
| 1192 |
} |
| 1193 |
|
| 1194 |
static byte *parse_index_log_version(byte *ptr, const byte *end_ptr, |
| 1195 |
uint8_t &version) { |
| 1196 |
ptr = read_1_bytes(ptr, end_ptr, version); |
| 1197 |
if (ptr == nullptr) return nullptr; |
| 1198 |
|
| 1199 |
return ptr; |
| 1200 |
} |
| 1201 |
static byte *parse_index_flag(byte *ptr, const byte *end_ptr, uint8_t &flag) { |
| 1202 |
ptr = read_1_bytes(ptr, end_ptr, flag); |
| 1203 |
if (ptr == nullptr) return nullptr; |
| 1204 |
|
| 1205 |
return ptr; |
| 1206 |
} |
| 1207 |
|
| 1208 |
byte *mlog_parse_index(byte *ptr, const byte *end_ptr, dict_index_t **index) { |
| 1209 |
/* Read the 1 byte for index log version */ |
| 1210 |
uint8_t index_log_version = 0; |
| 1211 |
ptr = parse_index_log_version(ptr, end_ptr, index_log_version); |
| 1212 |
if (ptr == nullptr) { |
| 1213 |
return nullptr; |
| 1214 |
} |
| 1215 |
|
| 1216 |
byte *ret = nullptr; |
| 1217 |
switch (index_log_version) { |
| 1218 |
case INDEX_LOG_VERSION_CURRENT: |
| 1219 |
ret = mlog_parse_index_v1(ptr, end_ptr, index); |
| 1220 |
break; |
| 1221 |
case INDEX_LOG_VERSION_0: |
| 1222 |
/* INDEX_LOG_VERSION_0 is used in 8.0.29 and in 8.0.30 REDO log format |
| 1223 |
has changed which expects REDOs from < 8.0.30 to be logically empty. Thus |
| 1224 |
we shall never reach here. */ |
| 1225 |
ut_error; |
| 1226 |
default: |
| 1227 |
ib::fatal(UT_LOCATION_HERE, ER_IB_INDEX_LOG_VERSION_MISMATCH, |
| 1228 |
(unsigned int)index_log_version, |
| 1229 |
(unsigned int)INDEX_LOG_VERSION_MAX); |
| 1230 |
} |
| 1231 |
|
| 1232 |
return ret; |
| 1233 |
} |
| 1234 |
|
| 1235 |
static byte *mlog_parse_index_v1(byte *ptr, const byte *end_ptr, |
| 1236 |
dict_index_t **index) { |
| 1237 |
/* Read the 1 byte flag */ |
| 1238 |
uint8_t flag = 0; |
| 1239 |
ptr = parse_index_flag(ptr, end_ptr, flag); |
| 1240 |
if (ptr == nullptr) { |
| 1241 |
return nullptr; |
| 1242 |
} |
| 1243 |
|
| 1244 |
const bool is_comp = IS_COMPACT(flag); |
| 1245 |
const bool is_versioned = IS_VERSIONED(flag); |
| 1246 |
const bool is_instant = IS_INSTANT(flag); |
| 1247 |
|
| 1248 |
/* Read n and n_uniq */ |
| 1249 |
uint16_t n = 0; |
| 1250 |
uint16_t n_uniq = 0; |
| 1251 |
uint16_t inst_cols = 0; |
| 1252 |
ptr = parse_index_column_counts(ptr, end_ptr, is_comp, is_versioned, |
| 1253 |
is_instant, n, n_uniq, inst_cols); |
| 1254 |
if (ptr == nullptr) { |
| 1255 |
return ptr; |
| 1256 |
} |
| 1257 |
ut_ad(inst_cols == 0 || is_instant); |
| 1258 |
|
| 1259 |
/* Create a dummy dict_table_t */ |
| 1260 |
dict_table_t *table = |
| 1261 |
dict_mem_table_create(RECOVERY_INDEX_TABLE_NAME, DICT_HDR_SPACE, n, 0, 0, |
| 1262 |
is_comp ? DICT_TF_COMPACT : 0, 0); |
| 1263 |
|
| 1264 |
if (inst_cols > 0) { |
| 1265 |
table->set_instant_cols(inst_cols); |
| 1266 |
} |
| 1267 |
|
| 1268 |
/* Create a dummy dict_index_t */ |
| 1269 |
dict_index_t *ind = |
| 1270 |
dict_mem_index_create(RECOVERY_INDEX_TABLE_NAME, |
| 1271 |
RECOVERY_INDEX_TABLE_NAME, DICT_HDR_SPACE, 0, n); |
| 1272 |
ind->table = table; |
| 1273 |
ind->n_uniq = (unsigned int)n_uniq; |
| 1274 |
if (n_uniq != n) { |
| 1275 |
ut_a(n_uniq + DATA_ROLL_PTR <= n); |
| 1276 |
ind->type = DICT_CLUSTERED; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (is_comp) { |
| 1280 |
/* Read each index field info */ |
| 1281 |
ptr = parse_index_fields(ptr, end_ptr, n, n_uniq, is_versioned, ind, table); |
| 1282 |
if (ptr == nullptr) { |
| 1283 |
*index = ind; |
| 1284 |
return ptr; |
| 1285 |
} |
| 1286 |
} else if (is_versioned) { |
| 1287 |
/* Populate dummy cols/fields and link them */ |
| 1288 |
populate_dummy_fields(ind, table, n IF_DEBUG(, is_comp)); |
| 1289 |
} |
| 1290 |
|
| 1291 |
size_t n_dropped = 0; |
| 1292 |
if (is_versioned) { |
| 1293 |
/* Read the fields with version added/dropped */ |
| 1294 |
instant_fields_list_t f; |
| 1295 |
uint16_t current_row_version = 0; |
| 1296 |
ptr = parse_index_versioned_fields(ptr, end_ptr, f, current_row_version, |
| 1297 |
n_dropped); |
| 1298 |
if (ptr == nullptr) { |
| 1299 |
*index = ind; |
| 1300 |
return (ptr); |
| 1301 |
} |
| 1302 |
ind->table->current_row_version = current_row_version; |
| 1303 |
|
| 1304 |
/* Update fields INSTANT info */ |
| 1305 |
update_instant_info(f, ind); |
| 1306 |
|
| 1307 |
bool *phy_pos_bitmap = new bool[ind->n_def]; |
| 1308 |
memset(phy_pos_bitmap, false, (sizeof(bool) * ind->n_def)); |
| 1309 |
for (auto field : f) { |
| 1310 |
phy_pos_bitmap[field.phy_pos] = true; |
| 1311 |
} |
| 1312 |
f.clear(); |
| 1313 |
|
| 1314 |
/* For the remaining columns, update physical pos */ |
| 1315 |
int shift_count = 0; |
| 1316 |
for (size_t i = 0; i < ind->n_def; i++) { |
| 1317 |
dict_field_t *field = ind->get_field(i); |
| 1318 |
if (field->col->get_phy_pos() == UINT32_UNDEFINED) { |
| 1319 |
uint16_t phy_pos = i + shift_count; |
| 1320 |
ut_ad(phy_pos < ind->n_def); |
| 1321 |
while (phy_pos_bitmap[phy_pos]) { |
| 1322 |
phy_pos++; |
| 1323 |
} |
| 1324 |
field->col->set_phy_pos(phy_pos); |
| 1325 |
phy_pos_bitmap[phy_pos] = true; |
| 1326 |
} else { |
| 1327 |
if (field->col->is_instant_added() && |
| 1328 |
!field->col->is_instant_dropped()) { |
| 1329 |
shift_count--; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
delete[] phy_pos_bitmap; |
| 1335 |
ind->row_versions = true; |
| 1336 |
} |
| 1337 |
|
| 1338 |
ind->n_fields = n - n_dropped; |
| 1339 |
ind->n_total_fields = n; |
| 1340 |
|
| 1341 |
/* For upgraded table from v1, set following */ |
| 1342 |
if (inst_cols > 0) { |
| 1343 |
ind->instant_cols = true; |
| 1344 |
const size_t n_instant_fields = ind->get_instant_fields(); |
| 1345 |
size_t new_n_nullable = ind->calculate_n_instant_nullable(n_instant_fields); |
| 1346 |
ind->set_instant_nullable(new_n_nullable); |
| 1347 |
} |
| 1348 |
|
| 1349 |
table->is_system_table = false; |
| 1350 |
|
| 1351 |
if (is_instant || is_versioned) { |
| 1352 |
if (is_versioned) { |
| 1353 |
ut_ad(ind->has_row_versions()); |
| 1354 |
ind->create_fields_array(); |
| 1355 |
} |
| 1356 |
if (is_instant) { |
| 1357 |
ind->table->set_upgraded_instant(); |
| 1358 |
} |
| 1359 |
ind->type = DICT_CLUSTERED; |
| 1360 |
ind->create_nullables(table->current_row_version); |
| 1361 |
} |
| 1362 |
|
| 1363 |
/* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */ |
| 1364 |
ind->cached = true; |
| 1365 |
*index = ind; |
| 1366 |
return (ptr); |
| 1367 |
} |