| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2005, 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/page0zip.cc |
| 30 |
Compressed page interface |
| 31 |
|
| 32 |
Created June 2005 by Marko Makela |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#include "page0zip.h" |
| 36 |
|
| 37 |
#include "page0size.h" |
| 38 |
|
| 39 |
/** A BLOB field reference full of zero, for use in assertions and tests. |
| 40 |
Initially, BLOB field references are set to zero, in |
| 41 |
dtuple_convert_big_rec(). */ |
| 42 |
const byte field_ref_zero[FIELD_REF_SIZE] = { |
| 43 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 44 |
}; |
| 45 |
|
| 46 |
#include <zlib.h> |
| 47 |
|
| 48 |
#include "btr0cur.h" |
| 49 |
#include "dict0dict.h" |
| 50 |
#include "log0recv.h" |
| 51 |
#include "mtr0log.h" |
| 52 |
#include "page0page.h" |
| 53 |
#include "page0types.h" |
| 54 |
#ifndef UNIV_HOTBACKUP |
| 55 |
#include "btr0sea.h" |
| 56 |
#include "buf0buf.h" |
| 57 |
#include "buf0lru.h" |
| 58 |
#include "dict0boot.h" |
| 59 |
#include "lock0lock.h" |
| 60 |
#include "srv0mon.h" |
| 61 |
#include "srv0srv.h" |
| 62 |
#endif /* !UNIV_HOTBACKUP */ |
| 63 |
#include "ut0crc32.h" |
| 64 |
#ifdef UNIV_HOTBACKUP |
| 65 |
#include "buf0checksum.h" |
| 66 |
|
| 67 |
#define lock_move_reorganize_page(block, temp_block) ((void)0) |
| 68 |
#define buf_LRU_stat_inc_unzip() ((void)0) |
| 69 |
#define MONITOR_INC(x) ((void)0) |
| 70 |
#endif /* !UNIV_HOTBACKUP */ |
| 71 |
|
| 72 |
#include <algorithm> |
| 73 |
#include <map> |
| 74 |
|
| 75 |
static_assert(DATA_TRX_ID_LEN == 6, "DATA_TRX_ID_LEN != 6"); |
| 76 |
static_assert(DATA_ROLL_PTR_LEN == 7, "DATA_ROLL_PTR_LEN != 7"); |
| 77 |
static_assert(DATA_TRX_ID + 1 == DATA_ROLL_PTR, "DATA_TRX_ID invalid value!"); |
| 78 |
|
| 79 |
#ifndef UNIV_HOTBACKUP |
| 80 |
/** Statistics on compression, indexed by page_zip_des_t::ssize - 1 */ |
| 81 |
page_zip_stat_t page_zip_stat[PAGE_ZIP_SSIZE_MAX]; |
| 82 |
/** Statistics on compression, indexed by index->id */ |
| 83 |
page_zip_stat_per_index_t page_zip_stat_per_index; |
| 84 |
#endif /* !UNIV_HOTBACKUP */ |
| 85 |
|
| 86 |
/* Compression level to be used by zlib. Settable by user. */ |
| 87 |
uint page_zip_level = DEFAULT_COMPRESSION_LEVEL; |
| 88 |
|
| 89 |
/* Whether or not to log compressed page images to avoid possible |
| 90 |
compression algorithm changes in zlib. */ |
| 91 |
bool page_zip_log_pages = true; |
| 92 |
|
| 93 |
/* Please refer to ../include/page0zip.ic for a description of the |
| 94 |
compressed page format. */ |
| 95 |
|
| 96 |
/** Assert that a block of memory is filled with zero bytes. |
| 97 |
Compare at most sizeof(field_ref_zero) bytes. |
| 98 |
@param b in: memory block |
| 99 |
@param s in: size of the memory block, in bytes */ |
| 100 |
#define ASSERT_ZERO(b, s) \ |
| 101 |
ut_ad(!memcmp(b, field_ref_zero, \ |
| 102 |
std::min(static_cast<size_t>(s), sizeof field_ref_zero))); |
| 103 |
/** Assert that a BLOB pointer is filled with zero bytes. |
| 104 |
@param b in: BLOB pointer */ |
| 105 |
#define ASSERT_ZERO_BLOB(b) \ |
| 106 |
ut_ad(!memcmp(b, field_ref_zero, sizeof field_ref_zero)) |
| 107 |
|
| 108 |
/** Determine the guaranteed free space on an empty page. |
| 109 |
@return minimum payload size on the page */ |
| 110 |
ulint page_zip_empty_size( |
| 111 |
ulint n_fields, /*!< in: number of columns in the index */ |
| 112 |
ulint zip_size) /*!< in: compressed page size in bytes */ |
| 113 |
{ |
| 114 |
lint size = zip_size |
| 115 |
/* subtract the page header and the longest |
| 116 |
uncompressed data needed for one record */ |
| 117 |
- (PAGE_DATA + PAGE_ZIP_CLUST_LEAF_SLOT_SIZE + |
| 118 |
1 /* encoded heap_no==2 in page_zip_write_rec() */ |
| 119 |
+ 1 /* end of modification log */ |
| 120 |
- REC_N_NEW_EXTRA_BYTES /* omitted bytes */) |
| 121 |
/* subtract the space for page_zip_fields_encode() */ |
| 122 |
- compressBound(static_cast<uLong>(2 * (n_fields + 1))); |
| 123 |
return (size > 0 ? (ulint)size : 0); |
| 124 |
} |
| 125 |
|
| 126 |
/** Check whether a tuple is too big for compressed table |
| 127 |
@param[in] index dict index object |
| 128 |
@param[in] entry entry for the index |
| 129 |
@return true if it's too big, otherwise false */ |
| 130 |
bool page_zip_is_too_big(const dict_index_t *index, const dtuple_t *entry) { |
| 131 |
const page_size_t &page_size = dict_table_page_size(index->table); |
| 132 |
|
| 133 |
/* Estimate the free space of an empty compressed page. |
| 134 |
Subtract one byte for the encoded heap_no in the |
| 135 |
modification log. */ |
| 136 |
ulint free_space_zip = |
| 137 |
page_zip_empty_size(index->n_fields, page_size.physical()); |
| 138 |
ulint n_uniq = dict_index_get_n_unique_in_tree(index); |
| 139 |
|
| 140 |
ut_ad(dict_table_is_comp(index->table)); |
| 141 |
ut_ad(page_size.is_compressed()); |
| 142 |
|
| 143 |
if (free_space_zip == 0) { |
| 144 |
return (true); |
| 145 |
} |
| 146 |
|
| 147 |
/* Subtract one byte for the encoded heap_no in the |
| 148 |
modification log. */ |
| 149 |
free_space_zip--; |
| 150 |
|
| 151 |
/* There should be enough room for two node pointer |
| 152 |
records on an empty non-leaf page. This prevents |
| 153 |
infinite page splits. */ |
| 154 |
|
| 155 |
if (entry->n_fields >= n_uniq && |
| 156 |
(REC_NODE_PTR_SIZE + |
| 157 |
rec_get_converted_size_comp_prefix(index, entry->fields, n_uniq, |
| 158 |
nullptr) |
| 159 |
/* On a compressed page, there is |
| 160 |
a two-byte entry in the dense |
| 161 |
page directory for every record. |
| 162 |
But there is no record header. */ |
| 163 |
- (REC_N_NEW_EXTRA_BYTES - 2) > |
| 164 |
free_space_zip / 2)) { |
| 165 |
return (true); |
| 166 |
} |
| 167 |
|
| 168 |
return (false); |
| 169 |
} |
| 170 |
|
| 171 |
/** Find the slot of the given non-free record in the dense page directory. |
| 172 |
@return dense directory slot, or NULL if record not found */ |
| 173 |
static inline byte *page_zip_dir_find( |
| 174 |
page_zip_des_t *page_zip, /*!< in: compressed page */ |
| 175 |
ulint offset) /*!< in: offset of user record */ |
| 176 |
{ |
| 177 |
byte *end = page_zip->data + page_zip_get_size(page_zip); |
| 178 |
|
| 179 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 180 |
|
| 181 |
return (page_zip_dir_find_low(end - page_zip_dir_user_size(page_zip), end, |
| 182 |
offset)); |
| 183 |
} |
| 184 |
|
| 185 |
#ifndef UNIV_HOTBACKUP |
| 186 |
/** Write a log record of compressing an index page. */ |
| 187 |
static void page_zip_compress_write_log( |
| 188 |
const page_zip_des_t *page_zip, /*!< in: compressed page */ |
| 189 |
const page_t *page, /*!< in: uncompressed page */ |
| 190 |
dict_index_t *index, /*!< in: index of the B-tree node */ |
| 191 |
mtr_t *mtr) /*!< in: mini-transaction */ |
| 192 |
{ |
| 193 |
byte *log_ptr = nullptr; |
| 194 |
ulint trailer_size; |
| 195 |
|
| 196 |
ut_ad(!dict_index_is_ibuf(index)); |
| 197 |
|
| 198 |
if (!mlog_open(mtr, 11 + 2 + 2, log_ptr)) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
/* Read the number of user records. */ |
| 203 |
trailer_size = page_dir_get_n_heap(page_zip->data) - PAGE_HEAP_NO_USER_LOW; |
| 204 |
/* Multiply by uncompressed of size stored per record */ |
| 205 |
if (!page_is_leaf(page)) { |
| 206 |
trailer_size *= PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE; |
| 207 |
} else if (index->is_clustered()) { |
| 208 |
trailer_size *= |
| 209 |
PAGE_ZIP_DIR_SLOT_SIZE + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; |
| 210 |
} else { |
| 211 |
trailer_size *= PAGE_ZIP_DIR_SLOT_SIZE; |
| 212 |
} |
| 213 |
/* Add the space occupied by BLOB pointers. */ |
| 214 |
trailer_size += page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE; |
| 215 |
ut_a(page_zip->m_end > PAGE_DATA); |
| 216 |
|
| 217 |
static_assert(FIL_PAGE_DATA <= PAGE_DATA, "FIL_PAGE_DATA > PAGE_DATA"); |
| 218 |
|
| 219 |
ut_a(page_zip->m_end + trailer_size <= page_zip_get_size(page_zip)); |
| 220 |
|
| 221 |
log_ptr = mlog_write_initial_log_record_fast( |
| 222 |
(page_t *)page, MLOG_ZIP_PAGE_COMPRESS, log_ptr, mtr); |
| 223 |
mach_write_to_2(log_ptr, page_zip->m_end - FIL_PAGE_TYPE); |
| 224 |
log_ptr += 2; |
| 225 |
mach_write_to_2(log_ptr, trailer_size); |
| 226 |
log_ptr += 2; |
| 227 |
mlog_close(mtr, log_ptr); |
| 228 |
|
| 229 |
/* Write FIL_PAGE_PREV and FIL_PAGE_NEXT */ |
| 230 |
mlog_catenate_string(mtr, page_zip->data + FIL_PAGE_PREV, 4); |
| 231 |
mlog_catenate_string(mtr, page_zip->data + FIL_PAGE_NEXT, 4); |
| 232 |
/* Write most of the page header, the compressed stream and |
| 233 |
the modification log. */ |
| 234 |
mlog_catenate_string(mtr, page_zip->data + FIL_PAGE_TYPE, |
| 235 |
page_zip->m_end - FIL_PAGE_TYPE); |
| 236 |
/* Write the uncompressed trailer of the compressed page. */ |
| 237 |
mlog_catenate_string( |
| 238 |
mtr, page_zip->data + page_zip_get_size(page_zip) - trailer_size, |
| 239 |
trailer_size); |
| 240 |
} |
| 241 |
#endif /* !UNIV_HOTBACKUP */ |
| 242 |
|
| 243 |
/** Determine how many externally stored columns are contained |
| 244 |
in existing records with smaller heap_no than rec. |
| 245 |
@param[in] page_zip Dense page directory on compressed page |
| 246 |
@param[in] rec Compact physical record on a b-tree leaf page |
| 247 |
@param[in] index Record descriptor */ |
| 248 |
static ulint page_zip_get_n_prev_extern(const page_zip_des_t *page_zip, |
| 249 |
const rec_t *rec, |
| 250 |
const dict_index_t *index) { |
| 251 |
const page_t *page = page_align(rec); |
| 252 |
ulint n_ext = 0; |
| 253 |
ulint i; |
| 254 |
ulint left; |
| 255 |
ulint heap_no; |
| 256 |
ulint n_recs = page_get_n_recs(page_zip->data); |
| 257 |
|
| 258 |
ut_ad(page_is_leaf(page)); |
| 259 |
ut_ad(page_is_comp(page)); |
| 260 |
ut_ad(dict_table_is_comp(index->table)); |
| 261 |
ut_ad(index->is_clustered()); |
| 262 |
ut_ad(!dict_index_is_ibuf(index)); |
| 263 |
|
| 264 |
heap_no = rec_get_heap_no_new(rec); |
| 265 |
ut_ad(heap_no >= PAGE_HEAP_NO_USER_LOW); |
| 266 |
left = heap_no - PAGE_HEAP_NO_USER_LOW; |
| 267 |
if (UNIV_UNLIKELY(!left)) { |
| 268 |
return (0); |
| 269 |
} |
| 270 |
|
| 271 |
for (i = 0; i < n_recs; i++) { |
| 272 |
const rec_t *r = |
| 273 |
page + (page_zip_dir_get(page_zip, i) & PAGE_ZIP_DIR_SLOT_MASK); |
| 274 |
|
| 275 |
if (rec_get_heap_no_new(r) < heap_no) { |
| 276 |
n_ext += rec_get_n_extern_new(r, index, ULINT_UNDEFINED); |
| 277 |
if (!--left) { |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return (n_ext); |
| 284 |
} |
| 285 |
|
| 286 |
/** Encode the length of a fixed-length column. |
| 287 |
@return buf + length of encoded val */ |
| 288 |
static byte *page_zip_fixed_field_encode( |
| 289 |
byte *buf, /*!< in: pointer to buffer where to write */ |
| 290 |
ulint val) /*!< in: value to write */ |
| 291 |
{ |
| 292 |
ut_ad(val >= 2); |
| 293 |
|
| 294 |
if (UNIV_LIKELY(val < 126)) { |
| 295 |
/* |
| 296 |
0 = nullable variable field of at most 255 bytes length; |
| 297 |
1 = not null variable field of at most 255 bytes length; |
| 298 |
126 = nullable variable field with maximum length >255; |
| 299 |
127 = not null variable field with maximum length >255 |
| 300 |
*/ |
| 301 |
*buf++ = (byte)val; |
| 302 |
} else { |
| 303 |
*buf++ = (byte)(0x80 | val >> 8); |
| 304 |
*buf++ = (byte)val; |
| 305 |
} |
| 306 |
|
| 307 |
return (buf); |
| 308 |
} |
| 309 |
|
| 310 |
/** Write the index information for the compressed page. |
| 311 |
@return used size of buf */ |
| 312 |
ulint page_zip_fields_encode( |
| 313 |
ulint n, /*!< in: number of fields |
| 314 |
to compress */ |
| 315 |
const dict_index_t *index, /*!< in: index comprising |
| 316 |
at least n fields */ |
| 317 |
ulint trx_id_pos, |
| 318 |
/*!< in: position of the trx_id column |
| 319 |
in the index, or ULINT_UNDEFINED if |
| 320 |
this is a non-leaf page */ |
| 321 |
byte *buf) /*!< out: buffer of (n + 1) * 2 bytes */ |
| 322 |
{ |
| 323 |
const byte *buf_start = buf; |
| 324 |
ulint i; |
| 325 |
ulint col; |
| 326 |
ulint trx_id_col = 0; |
| 327 |
/* sum of lengths of preceding non-nullable fixed fields, or 0 */ |
| 328 |
ulint fixed_sum = 0; |
| 329 |
|
| 330 |
ut_ad(trx_id_pos == ULINT_UNDEFINED || trx_id_pos < n); |
| 331 |
|
| 332 |
for (i = col = 0; i < n; i++) { |
| 333 |
dict_field_t *field = index->get_field(i); |
| 334 |
ulint val; |
| 335 |
|
| 336 |
if (field->col->prtype & DATA_NOT_NULL) { |
| 337 |
val = 1; /* set the "not nullable" flag */ |
| 338 |
} else { |
| 339 |
val = 0; /* nullable field */ |
| 340 |
} |
| 341 |
|
| 342 |
if (!field->fixed_len) { |
| 343 |
/* variable-length field */ |
| 344 |
const dict_col_t *column = field->col; |
| 345 |
|
| 346 |
if (DATA_BIG_COL(column)) { |
| 347 |
val |= 0x7e; /* max > 255 bytes */ |
| 348 |
} |
| 349 |
|
| 350 |
if (fixed_sum) { |
| 351 |
/* write out the length of any |
| 352 |
preceding non-nullable fields */ |
| 353 |
buf = page_zip_fixed_field_encode(buf, fixed_sum << 1 | 1); |
| 354 |
fixed_sum = 0; |
| 355 |
col++; |
| 356 |
} |
| 357 |
|
| 358 |
*buf++ = (byte)val; |
| 359 |
col++; |
| 360 |
} else if (val) { |
| 361 |
/* fixed-length non-nullable field */ |
| 362 |
|
| 363 |
if (fixed_sum && UNIV_UNLIKELY(fixed_sum + field->fixed_len > |
| 364 |
DICT_MAX_FIXED_COL_LEN)) { |
| 365 |
/* Write out the length of the |
| 366 |
preceding non-nullable fields, |
| 367 |
to avoid exceeding the maximum |
| 368 |
length of a fixed-length column. */ |
| 369 |
buf = page_zip_fixed_field_encode(buf, fixed_sum << 1 | 1); |
| 370 |
fixed_sum = 0; |
| 371 |
col++; |
| 372 |
} |
| 373 |
|
| 374 |
if (i && UNIV_UNLIKELY(i == trx_id_pos)) { |
| 375 |
if (fixed_sum) { |
| 376 |
/* Write out the length of any |
| 377 |
preceding non-nullable fields, |
| 378 |
and start a new trx_id column. */ |
| 379 |
buf = page_zip_fixed_field_encode(buf, fixed_sum << 1 | 1); |
| 380 |
col++; |
| 381 |
} |
| 382 |
|
| 383 |
trx_id_col = col; |
| 384 |
fixed_sum = field->fixed_len; |
| 385 |
} else { |
| 386 |
/* add to the sum */ |
| 387 |
fixed_sum += field->fixed_len; |
| 388 |
} |
| 389 |
} else { |
| 390 |
/* fixed-length nullable field */ |
| 391 |
|
| 392 |
if (fixed_sum) { |
| 393 |
/* write out the length of any |
| 394 |
preceding non-nullable fields */ |
| 395 |
buf = page_zip_fixed_field_encode(buf, fixed_sum << 1 | 1); |
| 396 |
fixed_sum = 0; |
| 397 |
col++; |
| 398 |
} |
| 399 |
|
| 400 |
buf = page_zip_fixed_field_encode(buf, field->fixed_len << 1); |
| 401 |
col++; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if (fixed_sum) { |
| 406 |
/* Write out the lengths of last fixed-length columns. */ |
| 407 |
buf = page_zip_fixed_field_encode(buf, fixed_sum << 1 | 1); |
| 408 |
} |
| 409 |
|
| 410 |
if (trx_id_pos != ULINT_UNDEFINED) { |
| 411 |
/* Write out the position of the trx_id column */ |
| 412 |
i = trx_id_col; |
| 413 |
} else { |
| 414 |
/* Write out the number of nullable fields */ |
| 415 |
i = index->n_nullable; |
| 416 |
} |
| 417 |
|
| 418 |
if (i < 128) { |
| 419 |
*buf++ = (byte)i; |
| 420 |
} else { |
| 421 |
*buf++ = (byte)(0x80 | i >> 8); |
| 422 |
*buf++ = (byte)i; |
| 423 |
} |
| 424 |
|
| 425 |
ut_ad((ulint)(buf - buf_start) <= (n + 2) * 2); |
| 426 |
return ((ulint)(buf - buf_start)); |
| 427 |
} |
| 428 |
|
| 429 |
/** Populate the dense page directory from the sparse directory. */ |
| 430 |
static void page_zip_dir_encode( |
| 431 |
const page_t *page, /*!< in: compact page */ |
| 432 |
byte *buf, /*!< in: pointer to dense page directory[-1]; |
| 433 |
out: dense directory on compressed page */ |
| 434 |
const rec_t **recs) /*!< in: pointer to an array of 0, or NULL; |
| 435 |
out: dense page directory sorted by ascending |
| 436 |
address (and heap_no) */ |
| 437 |
{ |
| 438 |
const byte *rec; |
| 439 |
ulint status; |
| 440 |
ulint min_mark; |
| 441 |
ulint heap_no; |
| 442 |
ulint i; |
| 443 |
ulint n_heap; |
| 444 |
ulint offs; |
| 445 |
|
| 446 |
min_mark = 0; |
| 447 |
|
| 448 |
if (page_is_leaf(page)) { |
| 449 |
status = REC_STATUS_ORDINARY; |
| 450 |
} else { |
| 451 |
status = REC_STATUS_NODE_PTR; |
| 452 |
if (UNIV_UNLIKELY(mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL)) { |
| 453 |
min_mark = REC_INFO_MIN_REC_FLAG; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
n_heap = page_dir_get_n_heap(page); |
| 458 |
|
| 459 |
/* Traverse the list of stored records in the collation order, |
| 460 |
starting from the first user record. */ |
| 461 |
|
| 462 |
rec = page + PAGE_NEW_INFIMUM; |
| 463 |
|
| 464 |
i = 0; |
| 465 |
|
| 466 |
for (;;) { |
| 467 |
ulint info_bits; |
| 468 |
offs = rec_get_next_offs(rec, true); |
| 469 |
if (UNIV_UNLIKELY(offs == PAGE_NEW_SUPREMUM)) { |
| 470 |
break; |
| 471 |
} |
| 472 |
rec = page + offs; |
| 473 |
heap_no = rec_get_heap_no_new(rec); |
| 474 |
ut_a(heap_no >= PAGE_HEAP_NO_USER_LOW); |
| 475 |
ut_a(heap_no < n_heap); |
| 476 |
ut_a(offs < UNIV_PAGE_SIZE - PAGE_DIR); |
| 477 |
ut_a(offs >= PAGE_ZIP_START); |
| 478 |
static_assert((PAGE_ZIP_DIR_SLOT_MASK & (PAGE_ZIP_DIR_SLOT_MASK + 1)) == 0, |
| 479 |
"PAGE_ZIP_DIR_SLOT_MASK is not 1 less than a power of 2"); |
| 480 |
static_assert(PAGE_ZIP_DIR_SLOT_MASK >= UNIV_ZIP_SIZE_MAX - 1, |
| 481 |
"PAGE_ZIP_DIR_SLOT_MASK < UNIV_ZIP_SIZE_MAX - 1"); |
| 482 |
if (UNIV_UNLIKELY(rec_get_n_owned_new(rec))) { |
| 483 |
offs |= PAGE_ZIP_DIR_SLOT_OWNED; |
| 484 |
} |
| 485 |
|
| 486 |
info_bits = rec_get_info_bits(rec, true); |
| 487 |
if (info_bits & REC_INFO_DELETED_FLAG) { |
| 488 |
info_bits &= ~REC_INFO_DELETED_FLAG; |
| 489 |
offs |= PAGE_ZIP_DIR_SLOT_DEL; |
| 490 |
} |
| 491 |
ut_a(info_bits == min_mark); |
| 492 |
/* Only the smallest user record can have |
| 493 |
REC_INFO_MIN_REC_FLAG set. */ |
| 494 |
min_mark = 0; |
| 495 |
|
| 496 |
mach_write_to_2(buf - PAGE_ZIP_DIR_SLOT_SIZE * ++i, offs); |
| 497 |
|
| 498 |
if (UNIV_LIKELY_NULL(recs)) { |
| 499 |
/* Ensure that each heap_no occurs at most once. */ |
| 500 |
ut_a(!recs[heap_no - PAGE_HEAP_NO_USER_LOW]); |
| 501 |
/* exclude infimum and supremum */ |
| 502 |
recs[heap_no - PAGE_HEAP_NO_USER_LOW] = rec; |
| 503 |
} |
| 504 |
|
| 505 |
ut_a(rec_get_status(rec) == status); |
| 506 |
} |
| 507 |
|
| 508 |
offs = page_header_get_field(page, PAGE_FREE); |
| 509 |
|
| 510 |
/* Traverse the free list (of deleted records). */ |
| 511 |
while (offs) { |
| 512 |
ut_ad(!(offs & ~PAGE_ZIP_DIR_SLOT_MASK)); |
| 513 |
rec = page + offs; |
| 514 |
|
| 515 |
heap_no = rec_get_heap_no_new(rec); |
| 516 |
ut_a(heap_no >= PAGE_HEAP_NO_USER_LOW); |
| 517 |
ut_a(heap_no < n_heap); |
| 518 |
|
| 519 |
ut_a(!rec[-REC_N_NEW_EXTRA_BYTES]); /* info_bits and n_owned */ |
| 520 |
ut_a(rec_get_status(rec) == status); |
| 521 |
|
| 522 |
mach_write_to_2(buf - PAGE_ZIP_DIR_SLOT_SIZE * ++i, offs); |
| 523 |
|
| 524 |
if (UNIV_LIKELY_NULL(recs)) { |
| 525 |
/* Ensure that each heap_no occurs at most once. */ |
| 526 |
ut_a(!recs[heap_no - PAGE_HEAP_NO_USER_LOW]); |
| 527 |
/* exclude infimum and supremum */ |
| 528 |
recs[heap_no - PAGE_HEAP_NO_USER_LOW] = rec; |
| 529 |
} |
| 530 |
|
| 531 |
offs = rec_get_next_offs(rec, true); |
| 532 |
} |
| 533 |
|
| 534 |
/* Ensure that each heap no occurs at least once. */ |
| 535 |
ut_a(i + PAGE_HEAP_NO_USER_LOW == n_heap); |
| 536 |
} |
| 537 |
|
| 538 |
#if 0 || defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG |
| 539 |
/** Symbol for enabling compression and decompression diagnostics */ |
| 540 |
#define PAGE_ZIP_COMPRESS_DBG |
| 541 |
#endif |
| 542 |
|
| 543 |
#ifdef PAGE_ZIP_COMPRESS_DBG |
| 544 |
/** Set this variable in a debugger to enable |
| 545 |
excessive logging in page_zip_compress(). */ |
| 546 |
static bool page_zip_compress_dbg; |
| 547 |
/** Set this variable in a debugger to enable |
| 548 |
binary logging of the data passed to deflate(). |
| 549 |
When this variable is nonzero, it will act |
| 550 |
as a log file name generator. */ |
| 551 |
static unsigned page_zip_compress_log; |
| 552 |
|
| 553 |
/** Wrapper for deflate(). Log the operation if page_zip_compress_dbg is set. |
| 554 |
@return deflate() status: Z_OK, Z_BUF_ERROR, ... */ |
| 555 |
static int page_zip_compress_deflate( |
| 556 |
FILE *logfile, /*!< in: log file, or NULL */ |
| 557 |
z_streamp strm, /*!< in/out: compressed stream for deflate() */ |
| 558 |
int flush) /*!< in: deflate() flushing method */ |
| 559 |
{ |
| 560 |
int status; |
| 561 |
if (UNIV_UNLIKELY(page_zip_compress_dbg)) { |
| 562 |
ut_print_buf(stderr, strm->next_in, strm->avail_in); |
| 563 |
} |
| 564 |
if (UNIV_LIKELY_NULL(logfile)) { |
| 565 |
if (fwrite(strm->next_in, 1, strm->avail_in, logfile) != strm->avail_in) { |
| 566 |
perror("fwrite"); |
| 567 |
} |
| 568 |
} |
| 569 |
status = deflate(strm, flush); |
| 570 |
if (UNIV_UNLIKELY(page_zip_compress_dbg)) { |
| 571 |
fprintf(stderr, " -> %d\n", status); |
| 572 |
} |
| 573 |
return (status); |
| 574 |
} |
| 575 |
|
| 576 |
/* Redefine deflate(). */ |
| 577 |
#undef deflate |
| 578 |
/** Debug wrapper for the zlib compression routine deflate(). |
| 579 |
Log the operation if page_zip_compress_dbg is set. |
| 580 |
@param strm in/out: compressed stream |
| 581 |
@param flush in: flushing method |
| 582 |
@return deflate() status: Z_OK, Z_BUF_ERROR, ... */ |
| 583 |
#define deflate(strm, flush) page_zip_compress_deflate(logfile, strm, flush) |
| 584 |
/** Declaration of the logfile parameter */ |
| 585 |
#define FILE_LOGFILE FILE *logfile, |
| 586 |
/** The logfile parameter */ |
| 587 |
#define LOGFILE logfile, |
| 588 |
#else /* PAGE_ZIP_COMPRESS_DBG */ |
| 589 |
/** Empty declaration of the logfile parameter */ |
| 590 |
#define FILE_LOGFILE |
| 591 |
/** Missing logfile parameter */ |
| 592 |
#define LOGFILE |
| 593 |
#endif /* PAGE_ZIP_COMPRESS_DBG */ |
| 594 |
|
| 595 |
/** Compress the records of a node pointer page. |
| 596 |
@return Z_OK, or a zlib error code */ |
| 597 |
static int page_zip_compress_node_ptrs( |
| 598 |
FILE_LOGFILE z_stream *c_stream, /*!< in/out: compressed page stream */ |
| 599 |
const rec_t **recs, /*!< in: dense page directory |
| 600 |
sorted by address */ |
| 601 |
ulint n_dense, /*!< in: size of recs[] */ |
| 602 |
dict_index_t *index, /*!< in: the index of the page */ |
| 603 |
byte *storage, /*!< in: end of dense page directory */ |
| 604 |
mem_heap_t *heap) /*!< in: temporary memory heap */ |
| 605 |
{ |
| 606 |
int err = Z_OK; |
| 607 |
ulint *offsets = nullptr; |
| 608 |
|
| 609 |
do { |
| 610 |
const rec_t *rec = *recs++; |
| 611 |
|
| 612 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 613 |
UT_LOCATION_HERE, &heap); |
| 614 |
/* Only leaf nodes may contain externally stored columns. */ |
| 615 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 616 |
|
| 617 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 618 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 619 |
rec_offs_extra_size(offsets)); |
| 620 |
|
| 621 |
/* Compress the extra bytes. */ |
| 622 |
c_stream->avail_in = |
| 623 |
static_cast<uInt>(rec - REC_N_NEW_EXTRA_BYTES - c_stream->next_in); |
| 624 |
|
| 625 |
if (c_stream->avail_in) { |
| 626 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 627 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 628 |
break; |
| 629 |
} |
| 630 |
} |
| 631 |
ut_ad(!c_stream->avail_in); |
| 632 |
|
| 633 |
/* Compress the data bytes, except node_ptr. */ |
| 634 |
c_stream->next_in = (byte *)rec; |
| 635 |
c_stream->avail_in = |
| 636 |
static_cast<uInt>(rec_offs_data_size(offsets) - REC_NODE_PTR_SIZE); |
| 637 |
|
| 638 |
if (c_stream->avail_in) { |
| 639 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 640 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 641 |
break; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
ut_ad(!c_stream->avail_in); |
| 646 |
|
| 647 |
memcpy(storage - REC_NODE_PTR_SIZE * (rec_get_heap_no_new(rec) - 1), |
| 648 |
c_stream->next_in, REC_NODE_PTR_SIZE); |
| 649 |
c_stream->next_in += REC_NODE_PTR_SIZE; |
| 650 |
} while (--n_dense); |
| 651 |
|
| 652 |
return (err); |
| 653 |
} |
| 654 |
|
| 655 |
/** Compress the records of a leaf node of a secondary index. |
| 656 |
@return Z_OK, or a zlib error code */ |
| 657 |
static int page_zip_compress_sec( |
| 658 |
FILE_LOGFILE z_stream *c_stream, /*!< in/out: compressed page stream */ |
| 659 |
const rec_t **recs, /*!< in: dense page directory |
| 660 |
sorted by address */ |
| 661 |
ulint n_dense) /*!< in: size of recs[] */ |
| 662 |
{ |
| 663 |
int err = Z_OK; |
| 664 |
|
| 665 |
ut_ad(n_dense > 0); |
| 666 |
|
| 667 |
do { |
| 668 |
const rec_t *rec = *recs++; |
| 669 |
|
| 670 |
/* Compress everything up to this record. */ |
| 671 |
c_stream->avail_in = |
| 672 |
static_cast<uInt>(rec - REC_N_NEW_EXTRA_BYTES - c_stream->next_in); |
| 673 |
|
| 674 |
if (UNIV_LIKELY(c_stream->avail_in)) { |
| 675 |
UNIV_MEM_ASSERT_RW(c_stream->next_in, c_stream->avail_in); |
| 676 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 677 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 678 |
break; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
ut_ad(!c_stream->avail_in); |
| 683 |
ut_ad(c_stream->next_in == rec - REC_N_NEW_EXTRA_BYTES); |
| 684 |
|
| 685 |
/* Skip the REC_N_NEW_EXTRA_BYTES. */ |
| 686 |
|
| 687 |
c_stream->next_in = (byte *)rec; |
| 688 |
} while (--n_dense); |
| 689 |
|
| 690 |
return (err); |
| 691 |
} |
| 692 |
|
| 693 |
/** Compress a record of a leaf node of a clustered index that contains |
| 694 |
externally stored columns. |
| 695 |
@param[in,out] c_stream compressed page stream |
| 696 |
@param[in] index index |
| 697 |
@param[in] rec record |
| 698 |
@param[in] offsets rec_get_offsets(rec) |
| 699 |
@param[in] trx_id_col position of of DB_TRX_ID |
| 700 |
@param[in] deleted dense directory entry pointing to the head of the free list |
| 701 |
@param[in] storage pointer to the next available BLOB pointer |
| 702 |
@param[in,out] externs pointer to the next available BLOB pointer |
| 703 |
@param[in,out] n_blobs number of externally stored columns |
| 704 |
@return Z_OK, or a zlib error code */ |
| 705 |
static int page_zip_compress_clust_ext(FILE_LOGFILE z_stream *c_stream, |
| 706 |
const dict_index_t *index, |
| 707 |
const rec_t *rec, const ulint *offsets, |
| 708 |
ulint trx_id_col, byte *deleted, |
| 709 |
byte *storage, byte **externs, |
| 710 |
ulint *n_blobs) { |
| 711 |
int err; |
| 712 |
ulint i; |
| 713 |
|
| 714 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 715 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 716 |
rec_offs_extra_size(offsets)); |
| 717 |
|
| 718 |
for (i = 0; i < rec_offs_n_fields(offsets); i++) { |
| 719 |
ulint len; |
| 720 |
const byte *src; |
| 721 |
|
| 722 |
if (UNIV_UNLIKELY(i == trx_id_col)) { |
| 723 |
ut_ad(!rec_offs_nth_extern(index, offsets, i)); |
| 724 |
/* Store trx_id and roll_ptr |
| 725 |
in uncompressed form. */ |
| 726 |
src = rec_get_nth_field(index, rec, offsets, i, &len); |
| 727 |
ut_ad(src + DATA_TRX_ID_LEN == |
| 728 |
rec_get_nth_field(index, rec, offsets, i + 1, &len)); |
| 729 |
ut_ad(len == DATA_ROLL_PTR_LEN); |
| 730 |
|
| 731 |
/* Compress any preceding bytes. */ |
| 732 |
c_stream->avail_in = static_cast<uInt>(src - c_stream->next_in); |
| 733 |
|
| 734 |
if (c_stream->avail_in) { |
| 735 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 736 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 737 |
return (err); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
ut_ad(!c_stream->avail_in); |
| 742 |
ut_ad(c_stream->next_in == src); |
| 743 |
|
| 744 |
memcpy(storage - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN) * |
| 745 |
(rec_get_heap_no_new(rec) - 1), |
| 746 |
c_stream->next_in, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 747 |
|
| 748 |
c_stream->next_in += DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; |
| 749 |
|
| 750 |
/* Skip also roll_ptr */ |
| 751 |
i++; |
| 752 |
} else if (rec_offs_nth_extern(index, offsets, i)) { |
| 753 |
src = rec_get_nth_field(index, rec, offsets, i, &len); |
| 754 |
ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE); |
| 755 |
src += len - BTR_EXTERN_FIELD_REF_SIZE; |
| 756 |
|
| 757 |
c_stream->avail_in = static_cast<uInt>(src - c_stream->next_in); |
| 758 |
if (UNIV_LIKELY(c_stream->avail_in)) { |
| 759 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 760 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 761 |
return (err); |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
ut_ad(!c_stream->avail_in); |
| 766 |
ut_ad(c_stream->next_in == src); |
| 767 |
|
| 768 |
/* Reserve space for the data at |
| 769 |
the end of the space reserved for |
| 770 |
the compressed data and the page |
| 771 |
modification log. */ |
| 772 |
|
| 773 |
if (UNIV_UNLIKELY(c_stream->avail_out <= BTR_EXTERN_FIELD_REF_SIZE)) { |
| 774 |
/* out of space */ |
| 775 |
return (Z_BUF_ERROR); |
| 776 |
} |
| 777 |
|
| 778 |
ut_ad(*externs == c_stream->next_out + c_stream->avail_out + |
| 779 |
1 /* end of modif. log */); |
| 780 |
|
| 781 |
c_stream->next_in += BTR_EXTERN_FIELD_REF_SIZE; |
| 782 |
|
| 783 |
/* Skip deleted records. */ |
| 784 |
if (UNIV_LIKELY_NULL( |
| 785 |
page_zip_dir_find_low(storage, deleted, page_offset(rec)))) { |
| 786 |
continue; |
| 787 |
} |
| 788 |
|
| 789 |
(*n_blobs)++; |
| 790 |
c_stream->avail_out -= BTR_EXTERN_FIELD_REF_SIZE; |
| 791 |
*externs -= BTR_EXTERN_FIELD_REF_SIZE; |
| 792 |
|
| 793 |
/* Copy the BLOB pointer */ |
| 794 |
memcpy(*externs, c_stream->next_in - BTR_EXTERN_FIELD_REF_SIZE, |
| 795 |
BTR_EXTERN_FIELD_REF_SIZE); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
return (Z_OK); |
| 800 |
} |
| 801 |
|
| 802 |
/** Compress the records of a leaf node of a clustered index. |
| 803 |
@return Z_OK, or a zlib error code */ |
| 804 |
static int page_zip_compress_clust( |
| 805 |
FILE_LOGFILE z_stream *c_stream, /*!< in/out: compressed page stream */ |
| 806 |
const rec_t **recs, /*!< in: dense page directory |
| 807 |
sorted by address */ |
| 808 |
ulint n_dense, /*!< in: size of recs[] */ |
| 809 |
dict_index_t *index, /*!< in: the index of the page */ |
| 810 |
ulint *n_blobs, /*!< in: 0; out: number of |
| 811 |
externally stored columns */ |
| 812 |
ulint trx_id_col, /*!< index of the trx_id column */ |
| 813 |
byte *deleted, /*!< in: dense directory entry pointing |
| 814 |
to the head of the free list */ |
| 815 |
byte *storage, /*!< in: end of dense page directory */ |
| 816 |
mem_heap_t *heap) /*!< in: temporary memory heap */ |
| 817 |
{ |
| 818 |
int err = Z_OK; |
| 819 |
ulint *offsets = nullptr; |
| 820 |
/* BTR_EXTERN_FIELD_REF storage */ |
| 821 |
byte *externs = storage - n_dense * (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 822 |
|
| 823 |
ut_ad(*n_blobs == 0); |
| 824 |
|
| 825 |
do { |
| 826 |
const rec_t *rec = *recs++; |
| 827 |
|
| 828 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 829 |
UT_LOCATION_HERE, &heap); |
| 830 |
ut_ad(rec_offs_n_fields(offsets) == dict_index_get_n_fields(index)); |
| 831 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 832 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 833 |
rec_offs_extra_size(offsets)); |
| 834 |
|
| 835 |
/* Compress the extra bytes. */ |
| 836 |
c_stream->avail_in = |
| 837 |
static_cast<uInt>(rec - REC_N_NEW_EXTRA_BYTES - c_stream->next_in); |
| 838 |
|
| 839 |
if (c_stream->avail_in) { |
| 840 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 841 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 842 |
goto func_exit; |
| 843 |
} |
| 844 |
} |
| 845 |
ut_ad(!c_stream->avail_in); |
| 846 |
ut_ad(c_stream->next_in == rec - REC_N_NEW_EXTRA_BYTES); |
| 847 |
|
| 848 |
/* Compress the data bytes. */ |
| 849 |
|
| 850 |
c_stream->next_in = (byte *)rec; |
| 851 |
|
| 852 |
/* Check if there are any externally stored columns. |
| 853 |
For each externally stored column, store the |
| 854 |
BTR_EXTERN_FIELD_REF separately. */ |
| 855 |
if (rec_offs_any_extern(offsets)) { |
| 856 |
ut_ad(index->is_clustered()); |
| 857 |
|
| 858 |
err = page_zip_compress_clust_ext(LOGFILE c_stream, index, rec, offsets, |
| 859 |
trx_id_col, deleted, storage, &externs, |
| 860 |
n_blobs); |
| 861 |
|
| 862 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 863 |
goto func_exit; |
| 864 |
} |
| 865 |
} else { |
| 866 |
ulint len; |
| 867 |
const byte *src; |
| 868 |
|
| 869 |
/* Store trx_id and roll_ptr in uncompressed form. */ |
| 870 |
src = rec_get_nth_field(index, rec, offsets, trx_id_col, &len); |
| 871 |
ut_ad(src + DATA_TRX_ID_LEN == |
| 872 |
rec_get_nth_field(index, rec, offsets, trx_id_col + 1, &len)); |
| 873 |
ut_ad(len == DATA_ROLL_PTR_LEN); |
| 874 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 875 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 876 |
rec_offs_extra_size(offsets)); |
| 877 |
|
| 878 |
/* Compress any preceding bytes. */ |
| 879 |
c_stream->avail_in = static_cast<uInt>(src - c_stream->next_in); |
| 880 |
|
| 881 |
if (c_stream->avail_in) { |
| 882 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 883 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 884 |
return (err); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
ut_ad(!c_stream->avail_in); |
| 889 |
ut_ad(c_stream->next_in == src); |
| 890 |
|
| 891 |
memcpy(storage - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN) * |
| 892 |
(rec_get_heap_no_new(rec) - 1), |
| 893 |
c_stream->next_in, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 894 |
|
| 895 |
c_stream->next_in += DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; |
| 896 |
|
| 897 |
/* Skip also roll_ptr */ |
| 898 |
ut_ad(trx_id_col + 1 < rec_offs_n_fields(offsets)); |
| 899 |
} |
| 900 |
|
| 901 |
/* Compress the last bytes of the record. */ |
| 902 |
c_stream->avail_in = static_cast<uInt>(rec + rec_offs_data_size(offsets) - |
| 903 |
c_stream->next_in); |
| 904 |
|
| 905 |
if (c_stream->avail_in) { |
| 906 |
err = deflate(c_stream, Z_NO_FLUSH); |
| 907 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 908 |
goto func_exit; |
| 909 |
} |
| 910 |
} |
| 911 |
ut_ad(!c_stream->avail_in); |
| 912 |
} while (--n_dense); |
| 913 |
|
| 914 |
func_exit: |
| 915 |
return (err); |
| 916 |
} |
| 917 |
|
| 918 |
/** Compress a page. |
| 919 |
@return true on success, false on failure; page_zip will be left |
| 920 |
intact on failure. */ |
| 921 |
bool page_zip_compress(page_zip_des_t *page_zip, /*!< in: size; out: data, |
| 922 |
n_blobs, m_start, m_end, |
| 923 |
m_nonempty */ |
| 924 |
const page_t *page, /*!< in: uncompressed page */ |
| 925 |
dict_index_t *index, /*!< in: index tree */ |
| 926 |
ulint level, /*!< in: compression level */ |
| 927 |
mtr_t *mtr) /*!< in/out: mini-transaction, |
| 928 |
or NULL */ |
| 929 |
{ |
| 930 |
z_stream c_stream; |
| 931 |
int err; |
| 932 |
ulint n_fields; /* number of index fields |
| 933 |
needed */ |
| 934 |
byte *fields; /*!< index field information */ |
| 935 |
byte *buf; /*!< compressed payload of the |
| 936 |
page */ |
| 937 |
byte *buf_end; /* end of buf */ |
| 938 |
ulint n_dense; |
| 939 |
ulint slot_size; /* amount of uncompressed bytes |
| 940 |
per record */ |
| 941 |
const rec_t **recs; /*!< dense page directory, |
| 942 |
sorted by address */ |
| 943 |
mem_heap_t *heap; |
| 944 |
ulint trx_id_col = ULINT_UNDEFINED; |
| 945 |
ulint n_blobs = 0; |
| 946 |
byte *storage; /* storage of uncompressed |
| 947 |
columns */ |
| 948 |
#ifndef UNIV_HOTBACKUP |
| 949 |
const auto start_time = std::chrono::steady_clock::now(); |
| 950 |
#endif /* !UNIV_HOTBACKUP */ |
| 951 |
#ifdef PAGE_ZIP_COMPRESS_DBG |
| 952 |
FILE *logfile = nullptr; |
| 953 |
#endif |
| 954 |
#ifndef UNIV_HOTBACKUP |
| 955 |
/* A local copy of srv_cmp_per_index_enabled to avoid reading that |
| 956 |
variable multiple times in this function since it can be changed at |
| 957 |
anytime. */ |
| 958 |
bool cmp_per_index_enabled; |
| 959 |
cmp_per_index_enabled = srv_cmp_per_index_enabled; |
| 960 |
#endif /* !UNIV_HOTBACKUP */ |
| 961 |
|
| 962 |
ut_a(page_is_comp(page)); |
| 963 |
ut_a(fil_page_index_page_check(page)); |
| 964 |
ut_ad(page_simple_validate_new((page_t *)page)); |
| 965 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 966 |
ut_ad(dict_table_is_comp(index->table)); |
| 967 |
ut_ad(!dict_index_is_ibuf(index)); |
| 968 |
|
| 969 |
UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE); |
| 970 |
|
| 971 |
/* Check the data that will be omitted. */ |
| 972 |
ut_a(!memcmp(page + (PAGE_NEW_INFIMUM - REC_N_NEW_EXTRA_BYTES), infimum_extra, |
| 973 |
sizeof infimum_extra)); |
| 974 |
ut_a(!memcmp(page + PAGE_NEW_INFIMUM, infimum_data, sizeof infimum_data)); |
| 975 |
ut_a(page[PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES] |
| 976 |
/* info_bits == 0, n_owned <= max */ |
| 977 |
<= PAGE_DIR_SLOT_MAX_N_OWNED); |
| 978 |
ut_a(!memcmp(page + (PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES + 1), |
| 979 |
supremum_extra_data, sizeof supremum_extra_data)); |
| 980 |
|
| 981 |
if (page_is_empty(page)) { |
| 982 |
ut_a(rec_get_next_offs(page + PAGE_NEW_INFIMUM, true) == PAGE_NEW_SUPREMUM); |
| 983 |
} |
| 984 |
|
| 985 |
if (page_is_leaf(page)) { |
| 986 |
n_fields = dict_index_get_n_fields(index); |
| 987 |
} else { |
| 988 |
n_fields = dict_index_get_n_unique_in_tree_nonleaf(index); |
| 989 |
} |
| 990 |
|
| 991 |
index_id_t ind_id(index->space, index->id); |
| 992 |
|
| 993 |
/* The dense directory excludes the infimum and supremum records. */ |
| 994 |
n_dense = page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW; |
| 995 |
#ifdef PAGE_ZIP_COMPRESS_DBG |
| 996 |
if (UNIV_UNLIKELY(page_zip_compress_dbg)) { |
| 997 |
ib::info(ER_IB_MSG_915) |
| 998 |
<< "compress " << static_cast<void *>(page_zip) << " " |
| 999 |
<< static_cast<const void *>(page) << " " << page_is_leaf(page) << " " |
| 1000 |
<< n_fields << " " << n_dense; |
| 1001 |
} |
| 1002 |
|
| 1003 |
if (UNIV_UNLIKELY(page_zip_compress_log)) { |
| 1004 |
/* Create a log file for every compression attempt. */ |
| 1005 |
char logfilename[9]; |
| 1006 |
snprintf(logfilename, sizeof logfilename, "%08x", page_zip_compress_log++); |
| 1007 |
logfile = fopen(logfilename, "wb"); |
| 1008 |
|
| 1009 |
if (logfile) { |
| 1010 |
/* Write the uncompressed page to the log. */ |
| 1011 |
if (fwrite(page, 1, UNIV_PAGE_SIZE, logfile) != UNIV_PAGE_SIZE) { |
| 1012 |
perror("fwrite"); |
| 1013 |
} |
| 1014 |
/* Record the compressed size as zero. |
| 1015 |
This will be overwritten at successful exit. */ |
| 1016 |
putc(0, logfile); |
| 1017 |
putc(0, logfile); |
| 1018 |
putc(0, logfile); |
| 1019 |
putc(0, logfile); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
#endif /* PAGE_ZIP_COMPRESS_DBG */ |
| 1023 |
#ifndef UNIV_HOTBACKUP |
| 1024 |
page_zip_stat[page_zip->ssize - 1].compressed++; |
| 1025 |
|
| 1026 |
if (cmp_per_index_enabled) { |
| 1027 |
mutex_enter(&page_zip_stat_per_index_mutex); |
| 1028 |
page_zip_stat_per_index[ind_id].compressed++; |
| 1029 |
mutex_exit(&page_zip_stat_per_index_mutex); |
| 1030 |
} |
| 1031 |
#endif /* !UNIV_HOTBACKUP */ |
| 1032 |
|
| 1033 |
if (UNIV_UNLIKELY(n_dense * PAGE_ZIP_DIR_SLOT_SIZE >= |
| 1034 |
page_zip_get_size(page_zip))) { |
| 1035 |
goto err_exit; |
| 1036 |
} |
| 1037 |
|
| 1038 |
MONITOR_INC(MONITOR_PAGE_COMPRESS); |
| 1039 |
|
| 1040 |
heap = mem_heap_create( |
| 1041 |
page_zip_get_size(page_zip) + n_fields * (2 + sizeof(ulint)) + |
| 1042 |
REC_OFFS_HEADER_SIZE + |
| 1043 |
n_dense * ((sizeof *recs) - PAGE_ZIP_DIR_SLOT_SIZE) + |
| 1044 |
UNIV_PAGE_SIZE * 4 + (512 << MAX_MEM_LEVEL), |
| 1045 |
UT_LOCATION_HERE); |
| 1046 |
|
| 1047 |
recs = static_cast<const rec_t **>( |
| 1048 |
mem_heap_zalloc(heap, n_dense * sizeof *recs)); |
| 1049 |
|
| 1050 |
fields = static_cast<byte *>(mem_heap_alloc(heap, (n_fields + 1) * 2)); |
| 1051 |
|
| 1052 |
buf = static_cast<byte *>( |
| 1053 |
mem_heap_alloc(heap, page_zip_get_size(page_zip) - PAGE_DATA)); |
| 1054 |
|
| 1055 |
buf_end = buf + page_zip_get_size(page_zip) - PAGE_DATA; |
| 1056 |
|
| 1057 |
/* Compress the data payload. */ |
| 1058 |
page_zip_set_alloc(&c_stream, heap); |
| 1059 |
|
| 1060 |
err = deflateInit2(&c_stream, static_cast<int>(level), Z_DEFLATED, |
| 1061 |
UNIV_PAGE_SIZE_SHIFT, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); |
| 1062 |
ut_a(err == Z_OK); |
| 1063 |
|
| 1064 |
c_stream.next_out = buf; |
| 1065 |
|
| 1066 |
/* Subtract the space reserved for uncompressed data. */ |
| 1067 |
/* Page header and the end marker of the modification log */ |
| 1068 |
c_stream.avail_out = static_cast<uInt>(buf_end - buf - 1); |
| 1069 |
|
| 1070 |
/* Dense page directory and uncompressed columns, if any */ |
| 1071 |
if (page_is_leaf(page)) { |
| 1072 |
if (index->is_clustered()) { |
| 1073 |
trx_id_col = index->get_sys_col_pos(DATA_TRX_ID); |
| 1074 |
ut_ad(trx_id_col > 0); |
| 1075 |
ut_ad(trx_id_col != ULINT_UNDEFINED); |
| 1076 |
|
| 1077 |
slot_size = PAGE_ZIP_DIR_SLOT_SIZE + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; |
| 1078 |
|
| 1079 |
} else { |
| 1080 |
/* Signal the absence of trx_id |
| 1081 |
in page_zip_fields_encode() */ |
| 1082 |
ut_ad(index->get_sys_col_pos(DATA_TRX_ID) == ULINT_UNDEFINED); |
| 1083 |
trx_id_col = 0; |
| 1084 |
slot_size = PAGE_ZIP_DIR_SLOT_SIZE; |
| 1085 |
} |
| 1086 |
} else { |
| 1087 |
slot_size = PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE; |
| 1088 |
trx_id_col = ULINT_UNDEFINED; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (UNIV_UNLIKELY(c_stream.avail_out <= |
| 1092 |
n_dense * slot_size + |
| 1093 |
6 /* sizeof(zlib header and footer) */)) { |
| 1094 |
goto zlib_error; |
| 1095 |
} |
| 1096 |
|
| 1097 |
c_stream.avail_out -= static_cast<uInt>(n_dense * slot_size); |
| 1098 |
c_stream.avail_in = static_cast<uInt>( |
| 1099 |
page_zip_fields_encode(n_fields, index, trx_id_col, fields)); |
| 1100 |
c_stream.next_in = fields; |
| 1101 |
|
| 1102 |
if (UNIV_LIKELY(!trx_id_col)) { |
| 1103 |
trx_id_col = ULINT_UNDEFINED; |
| 1104 |
} |
| 1105 |
|
| 1106 |
UNIV_MEM_ASSERT_RW(c_stream.next_in, c_stream.avail_in); |
| 1107 |
err = deflate(&c_stream, Z_FULL_FLUSH); |
| 1108 |
if (err != Z_OK) { |
| 1109 |
goto zlib_error; |
| 1110 |
} |
| 1111 |
|
| 1112 |
ut_ad(!c_stream.avail_in); |
| 1113 |
|
| 1114 |
page_zip_dir_encode(page, buf_end, recs); |
| 1115 |
|
| 1116 |
c_stream.next_in = (byte *)page + PAGE_ZIP_START; |
| 1117 |
|
| 1118 |
storage = buf_end - n_dense * PAGE_ZIP_DIR_SLOT_SIZE; |
| 1119 |
|
| 1120 |
/* Compress the records in heap_no order. */ |
| 1121 |
if (UNIV_UNLIKELY(!n_dense)) { |
| 1122 |
} else if (!page_is_leaf(page)) { |
| 1123 |
/* This is a node pointer page. */ |
| 1124 |
err = page_zip_compress_node_ptrs(LOGFILE & c_stream, recs, n_dense, index, |
| 1125 |
storage, heap); |
| 1126 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 1127 |
goto zlib_error; |
| 1128 |
} |
| 1129 |
} else if (UNIV_LIKELY(trx_id_col == ULINT_UNDEFINED)) { |
| 1130 |
/* This is a leaf page in a secondary index. */ |
| 1131 |
err = page_zip_compress_sec(LOGFILE & c_stream, recs, n_dense); |
| 1132 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 1133 |
goto zlib_error; |
| 1134 |
} |
| 1135 |
} else { |
| 1136 |
/* This is a leaf page in a clustered index. */ |
| 1137 |
err = page_zip_compress_clust( |
| 1138 |
LOGFILE & c_stream, recs, n_dense, index, &n_blobs, trx_id_col, |
| 1139 |
buf_end - PAGE_ZIP_DIR_SLOT_SIZE * page_get_n_recs(page), storage, |
| 1140 |
heap); |
| 1141 |
if (UNIV_UNLIKELY(err != Z_OK)) { |
| 1142 |
goto zlib_error; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* Finish the compression. */ |
| 1147 |
ut_ad(!c_stream.avail_in); |
| 1148 |
/* Compress any trailing garbage, in case the last record was |
| 1149 |
allocated from an originally longer space on the free list, |
| 1150 |
or the data of the last record from page_zip_compress_sec(). */ |
| 1151 |
c_stream.avail_in = static_cast<uInt>( |
| 1152 |
page_header_get_field(page, PAGE_HEAP_TOP) - (c_stream.next_in - page)); |
| 1153 |
ut_a(c_stream.avail_in <= UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR); |
| 1154 |
|
| 1155 |
UNIV_MEM_ASSERT_RW(c_stream.next_in, c_stream.avail_in); |
| 1156 |
err = deflate(&c_stream, Z_FINISH); |
| 1157 |
|
| 1158 |
if (UNIV_UNLIKELY(err != Z_STREAM_END)) { |
| 1159 |
zlib_error: |
| 1160 |
deflateEnd(&c_stream); |
| 1161 |
mem_heap_free(heap); |
| 1162 |
err_exit: |
| 1163 |
#ifdef PAGE_ZIP_COMPRESS_DBG |
| 1164 |
if (logfile) { |
| 1165 |
fclose(logfile); |
| 1166 |
} |
| 1167 |
#endif /* PAGE_ZIP_COMPRESS_DBG */ |
| 1168 |
#ifndef UNIV_HOTBACKUP |
| 1169 |
if (page_is_leaf(page) && index) { |
| 1170 |
dict_index_zip_failure(index); |
| 1171 |
} |
| 1172 |
|
| 1173 |
const auto time_diff = |
| 1174 |
std::chrono::duration_cast<std::chrono::microseconds>( |
| 1175 |
std::chrono::steady_clock::now() - start_time); |
| 1176 |
page_zip_stat[page_zip->ssize - 1].compress_time += time_diff; |
| 1177 |
if (cmp_per_index_enabled) { |
| 1178 |
mutex_enter(&page_zip_stat_per_index_mutex); |
| 1179 |
page_zip_stat_per_index[ind_id].compress_time += time_diff; |
| 1180 |
mutex_exit(&page_zip_stat_per_index_mutex); |
| 1181 |
} |
| 1182 |
#endif /* !UNIV_HOTBACKUP */ |
| 1183 |
return false; |
| 1184 |
} |
| 1185 |
|
| 1186 |
err = deflateEnd(&c_stream); |
| 1187 |
ut_a(err == Z_OK); |
| 1188 |
|
| 1189 |
ut_ad(buf + c_stream.total_out == c_stream.next_out); |
| 1190 |
ut_ad((ulint)(storage - c_stream.next_out) >= c_stream.avail_out); |
| 1191 |
|
| 1192 |
/* Valgrind believes that zlib does not initialize some bits |
| 1193 |
in the last 7 or 8 bytes of the stream. Make Valgrind happy. */ |
| 1194 |
UNIV_MEM_VALID(buf, c_stream.total_out); |
| 1195 |
|
| 1196 |
/* Zero out the area reserved for the modification log. |
| 1197 |
Space for the end marker of the modification log is not |
| 1198 |
included in avail_out. */ |
| 1199 |
memset(c_stream.next_out, 0, c_stream.avail_out + 1 /* end marker */); |
| 1200 |
|
| 1201 |
#ifdef UNIV_DEBUG |
| 1202 |
page_zip->m_start = |
| 1203 |
#endif /* UNIV_DEBUG */ |
| 1204 |
page_zip->m_end = PAGE_DATA + c_stream.total_out; |
| 1205 |
page_zip->m_nonempty = false; |
| 1206 |
page_zip->n_blobs = n_blobs; |
| 1207 |
/* Copy those header fields that will not be written |
| 1208 |
in buf_flush_init_for_writing() */ |
| 1209 |
memcpy(page_zip->data + FIL_PAGE_PREV, page + FIL_PAGE_PREV, |
| 1210 |
FIL_PAGE_LSN - FIL_PAGE_PREV); |
| 1211 |
memcpy(page_zip->data + FIL_PAGE_TYPE, page + FIL_PAGE_TYPE, 2); |
| 1212 |
memcpy(page_zip->data + FIL_PAGE_DATA, page + FIL_PAGE_DATA, |
| 1213 |
PAGE_DATA - FIL_PAGE_DATA); |
| 1214 |
/* Copy the rest of the compressed page */ |
| 1215 |
memcpy(page_zip->data + PAGE_DATA, buf, |
| 1216 |
page_zip_get_size(page_zip) - PAGE_DATA); |
| 1217 |
mem_heap_free(heap); |
| 1218 |
#ifdef UNIV_ZIP_DEBUG |
| 1219 |
ut_a(page_zip_validate(page_zip, page, index)); |
| 1220 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1221 |
|
| 1222 |
if (mtr) { |
| 1223 |
#ifndef UNIV_HOTBACKUP |
| 1224 |
page_zip_compress_write_log(page_zip, page, index, mtr); |
| 1225 |
#endif /* !UNIV_HOTBACKUP */ |
| 1226 |
} |
| 1227 |
|
| 1228 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 1229 |
|
| 1230 |
#ifdef PAGE_ZIP_COMPRESS_DBG |
| 1231 |
if (logfile) { |
| 1232 |
/* Record the compressed size of the block. */ |
| 1233 |
byte sz[4]; |
| 1234 |
mach_write_to_4(sz, c_stream.total_out); |
| 1235 |
fseek(logfile, UNIV_PAGE_SIZE, SEEK_SET); |
| 1236 |
if (fwrite(sz, 1, sizeof sz, logfile) != sizeof sz) { |
| 1237 |
perror("fwrite"); |
| 1238 |
} |
| 1239 |
fclose(logfile); |
| 1240 |
} |
| 1241 |
#endif /* PAGE_ZIP_COMPRESS_DBG */ |
| 1242 |
#ifndef UNIV_HOTBACKUP |
| 1243 |
const auto time_diff = std::chrono::duration_cast<std::chrono::microseconds>( |
| 1244 |
std::chrono::steady_clock::now() - start_time); |
| 1245 |
page_zip_stat[page_zip->ssize - 1].compressed_ok++; |
| 1246 |
page_zip_stat[page_zip->ssize - 1].compress_time += time_diff; |
| 1247 |
if (cmp_per_index_enabled) { |
| 1248 |
mutex_enter(&page_zip_stat_per_index_mutex); |
| 1249 |
page_zip_stat_per_index[ind_id].compressed_ok++; |
| 1250 |
page_zip_stat_per_index[ind_id].compress_time += time_diff; |
| 1251 |
mutex_exit(&page_zip_stat_per_index_mutex); |
| 1252 |
} |
| 1253 |
|
| 1254 |
if (page_is_leaf(page)) { |
| 1255 |
dict_index_zip_success(index); |
| 1256 |
} |
| 1257 |
#endif /* !UNIV_HOTBACKUP */ |
| 1258 |
|
| 1259 |
return true; |
| 1260 |
} |
| 1261 |
|
| 1262 |
/** Decompress a page. This function should tolerate errors on the compressed |
| 1263 |
page. Instead of letting assertions fail, it will return false if an |
| 1264 |
inconsistency is detected. |
| 1265 |
@return true on success, false on failure */ |
| 1266 |
bool page_zip_decompress( |
| 1267 |
page_zip_des_t *page_zip, /*!< in: data, ssize; |
| 1268 |
out: m_start, m_end, m_nonempty, n_blobs */ |
| 1269 |
page_t *page, /*!< out: uncompressed page, may be trashed */ |
| 1270 |
bool all) /*!< in: true=decompress the whole page; |
| 1271 |
false=verify but do not copy some |
| 1272 |
page header fields that should not change |
| 1273 |
after page creation */ |
| 1274 |
{ |
| 1275 |
#ifndef UNIV_HOTBACKUP |
| 1276 |
const auto start_time = std::chrono::steady_clock::now(); |
| 1277 |
#endif /* !UNIV_HOTBACKUP */ |
| 1278 |
|
| 1279 |
if (!page_zip_decompress_low(page_zip, page, all)) { |
| 1280 |
return false; |
| 1281 |
} |
| 1282 |
|
| 1283 |
#ifndef UNIV_HOTBACKUP |
| 1284 |
const auto time_diff = std::chrono::duration_cast<std::chrono::microseconds>( |
| 1285 |
std::chrono::steady_clock::now() - start_time); |
| 1286 |
page_zip_stat[page_zip->ssize - 1].decompressed++; |
| 1287 |
page_zip_stat[page_zip->ssize - 1].decompress_time += time_diff; |
| 1288 |
|
| 1289 |
if (srv_cmp_per_index_enabled) { |
| 1290 |
index_id_t index_id(page_get_space_id(page), btr_page_get_index_id(page)); |
| 1291 |
|
| 1292 |
mutex_enter(&page_zip_stat_per_index_mutex); |
| 1293 |
page_zip_stat_per_index[index_id].decompressed++; |
| 1294 |
page_zip_stat_per_index[index_id].decompress_time += time_diff; |
| 1295 |
mutex_exit(&page_zip_stat_per_index_mutex); |
| 1296 |
} |
| 1297 |
#endif /* !UNIV_HOTBACKUP */ |
| 1298 |
|
| 1299 |
/* Update the stat counter for LRU policy. */ |
| 1300 |
buf_LRU_stat_inc_unzip(); |
| 1301 |
|
| 1302 |
MONITOR_INC(MONITOR_PAGE_DECOMPRESS); |
| 1303 |
|
| 1304 |
return true; |
| 1305 |
} |
| 1306 |
|
| 1307 |
#ifdef UNIV_ZIP_DEBUG |
| 1308 |
/** Dump a block of memory on the standard error stream. */ |
| 1309 |
static void page_zip_hexdump_func( |
| 1310 |
const char *name, /*!< in: name of the data structure */ |
| 1311 |
const void *buf, /*!< in: data */ |
| 1312 |
ulint size) /*!< in: length of the data, in bytes */ |
| 1313 |
{ |
| 1314 |
const byte *s = static_cast<const byte *>(buf); |
| 1315 |
ulint addr; |
| 1316 |
const ulint width = 32; /* bytes per line */ |
| 1317 |
|
| 1318 |
fprintf(stderr, "%s:\n", name); |
| 1319 |
|
| 1320 |
for (addr = 0; addr < size; addr += width) { |
| 1321 |
ulint i; |
| 1322 |
|
| 1323 |
fprintf(stderr, "%04lx ", (ulong)addr); |
| 1324 |
|
| 1325 |
i = std::min(width, size - addr); |
| 1326 |
|
| 1327 |
while (i--) { |
| 1328 |
fprintf(stderr, "%02x", *s++); |
| 1329 |
} |
| 1330 |
|
| 1331 |
putc('\n', stderr); |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
/** Dump a block of memory on the standard error stream. |
| 1336 |
@param buf in: data |
| 1337 |
@param size in: length of the data, in bytes */ |
| 1338 |
#define page_zip_hexdump(buf, size) page_zip_hexdump_func(#buf, buf, size) |
| 1339 |
|
| 1340 |
/** Flag: make page_zip_validate() compare page headers only */ |
| 1341 |
bool page_zip_validate_header_only = false; |
| 1342 |
|
| 1343 |
#define page_zip_fail(fmt_args) page_zip_fail_func fmt_args |
| 1344 |
int page_zip_fail_func(const char *fmt, ...); |
| 1345 |
|
| 1346 |
/** Check that the compressed and decompressed pages match. |
| 1347 |
@return true if valid, false if not */ |
| 1348 |
bool page_zip_validate_low( |
| 1349 |
const page_zip_des_t *page_zip, /*!< in: compressed page */ |
| 1350 |
const page_t *page, /*!< in: uncompressed page */ |
| 1351 |
const dict_index_t *index, /*!< in: index of the page, if known */ |
| 1352 |
bool sloppy) /*!< in: false=strict, |
| 1353 |
true=ignore the MIN_REC_FLAG */ |
| 1354 |
{ |
| 1355 |
page_zip_des_t temp_page_zip; |
| 1356 |
page_t *temp_page; |
| 1357 |
|
| 1358 |
if (memcmp(page_zip->data + FIL_PAGE_PREV, page + FIL_PAGE_PREV, |
| 1359 |
FIL_PAGE_LSN - FIL_PAGE_PREV) || |
| 1360 |
memcmp(page_zip->data + FIL_PAGE_TYPE, page + FIL_PAGE_TYPE, 2) || |
| 1361 |
memcmp(page_zip->data + FIL_PAGE_DATA, page + FIL_PAGE_DATA, |
| 1362 |
PAGE_DATA - FIL_PAGE_DATA)) { |
| 1363 |
page_zip_fail(("page_zip_validate: page header\n")); |
| 1364 |
page_zip_hexdump(page_zip, sizeof *page_zip); |
| 1365 |
page_zip_hexdump(page_zip->data, page_zip_get_size(page_zip)); |
| 1366 |
page_zip_hexdump(page, UNIV_PAGE_SIZE); |
| 1367 |
return false; |
| 1368 |
} |
| 1369 |
|
| 1370 |
ut_a(page_is_comp(page)); |
| 1371 |
|
| 1372 |
if (page_zip_validate_header_only) { |
| 1373 |
return true; |
| 1374 |
} |
| 1375 |
|
| 1376 |
/* page_zip_decompress() expects the uncompressed page to be |
| 1377 |
UNIV_PAGE_SIZE aligned. */ |
| 1378 |
temp_page = |
| 1379 |
static_cast<byte *>(ut::aligned_alloc(UNIV_PAGE_SIZE, UNIV_PAGE_SIZE)); |
| 1380 |
|
| 1381 |
UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE); |
| 1382 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 1383 |
|
| 1384 |
temp_page_zip = *page_zip; |
| 1385 |
auto valid = page_zip_decompress_low(&temp_page_zip, temp_page, true); |
| 1386 |
if (!valid) { |
| 1387 |
fputs("page_zip_validate(): failed to decompress\n", stderr); |
| 1388 |
goto func_exit; |
| 1389 |
} |
| 1390 |
if (page_zip->n_blobs != temp_page_zip.n_blobs) { |
| 1391 |
page_zip_fail(("page_zip_validate: n_blobs: %u!=%u\n", page_zip->n_blobs, |
| 1392 |
temp_page_zip.n_blobs)); |
| 1393 |
valid = false; |
| 1394 |
} |
| 1395 |
#ifdef UNIV_DEBUG |
| 1396 |
if (page_zip->m_start != temp_page_zip.m_start) { |
| 1397 |
page_zip_fail(("page_zip_validate: m_start: %u!=%u\n", page_zip->m_start, |
| 1398 |
temp_page_zip.m_start)); |
| 1399 |
valid = false; |
| 1400 |
} |
| 1401 |
#endif /* UNIV_DEBUG */ |
| 1402 |
if (page_zip->m_end != temp_page_zip.m_end) { |
| 1403 |
page_zip_fail(("page_zip_validate: m_end: %u!=%u\n", page_zip->m_end, |
| 1404 |
temp_page_zip.m_end)); |
| 1405 |
valid = false; |
| 1406 |
} |
| 1407 |
if (page_zip->m_nonempty != temp_page_zip.m_nonempty) { |
| 1408 |
page_zip_fail(("page_zip_validate(): m_nonempty: %u!=%u\n", |
| 1409 |
page_zip->m_nonempty, temp_page_zip.m_nonempty)); |
| 1410 |
valid = false; |
| 1411 |
} |
| 1412 |
if (memcmp(page + PAGE_HEADER, temp_page + PAGE_HEADER, |
| 1413 |
UNIV_PAGE_SIZE - PAGE_HEADER - FIL_PAGE_DATA_END)) { |
| 1414 |
/* In crash recovery, the "minimum record" flag may be |
| 1415 |
set incorrectly until the mini-transaction is |
| 1416 |
committed. Let us tolerate that difference when we |
| 1417 |
are performing a sloppy validation. */ |
| 1418 |
|
| 1419 |
ulint *offsets; |
| 1420 |
mem_heap_t *heap; |
| 1421 |
const rec_t *rec; |
| 1422 |
const rec_t *trec; |
| 1423 |
byte info_bits_diff; |
| 1424 |
ulint offset = rec_get_next_offs(page + PAGE_NEW_INFIMUM, true); |
| 1425 |
ut_a(offset >= PAGE_NEW_SUPREMUM); |
| 1426 |
offset -= 5 /*REC_NEW_INFO_BITS*/; |
| 1427 |
|
| 1428 |
info_bits_diff = page[offset] ^ temp_page[offset]; |
| 1429 |
|
| 1430 |
if (info_bits_diff == REC_INFO_MIN_REC_FLAG) { |
| 1431 |
temp_page[offset] = page[offset]; |
| 1432 |
|
| 1433 |
if (!memcmp(page + PAGE_HEADER, temp_page + PAGE_HEADER, |
| 1434 |
UNIV_PAGE_SIZE - PAGE_HEADER - FIL_PAGE_DATA_END)) { |
| 1435 |
/* Only the minimum record flag |
| 1436 |
differed. Let us ignore it. */ |
| 1437 |
page_zip_fail( |
| 1438 |
("page_zip_validate:" |
| 1439 |
" min_rec_flag" |
| 1440 |
" (%s%lu,%lu,0x%02lx)\n", |
| 1441 |
sloppy ? "ignored, " : "", page_get_space_id(page), |
| 1442 |
page_get_page_no(page), (ulong)page[offset])); |
| 1443 |
/* We don't check for spatial index, since |
| 1444 |
the "minimum record" could be deleted when |
| 1445 |
doing rtr_update_mbr_field. |
| 1446 |
GIS_FIXME: need to validate why |
| 1447 |
rtr_update_mbr_field.() could affect this */ |
| 1448 |
if (index && dict_index_is_spatial(index)) { |
| 1449 |
valid = true; |
| 1450 |
} else { |
| 1451 |
valid = sloppy; |
| 1452 |
} |
| 1453 |
goto func_exit; |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
/* Compare the pointers in the PAGE_FREE list. */ |
| 1458 |
rec = page_header_get_ptr(page, PAGE_FREE); |
| 1459 |
trec = page_header_get_ptr(temp_page, PAGE_FREE); |
| 1460 |
|
| 1461 |
while (rec || trec) { |
| 1462 |
if (page_offset(rec) != page_offset(trec)) { |
| 1463 |
page_zip_fail( |
| 1464 |
("page_zip_validate:" |
| 1465 |
" PAGE_FREE list: %u!=%u\n", |
| 1466 |
(unsigned)page_offset(rec), (unsigned)page_offset(trec))); |
| 1467 |
valid = false; |
| 1468 |
goto func_exit; |
| 1469 |
} |
| 1470 |
|
| 1471 |
rec = page_rec_get_next_low(rec, true); |
| 1472 |
trec = page_rec_get_next_low(trec, true); |
| 1473 |
} |
| 1474 |
|
| 1475 |
/* Compare the records. */ |
| 1476 |
heap = nullptr; |
| 1477 |
offsets = nullptr; |
| 1478 |
rec = page_rec_get_next_low(page + PAGE_NEW_INFIMUM, true); |
| 1479 |
trec = page_rec_get_next_low(temp_page + PAGE_NEW_INFIMUM, true); |
| 1480 |
|
| 1481 |
do { |
| 1482 |
if (page_offset(rec) != page_offset(trec)) { |
| 1483 |
page_zip_fail( |
| 1484 |
("page_zip_validate:" |
| 1485 |
" record list: 0x%02x!=0x%02x\n", |
| 1486 |
(unsigned)page_offset(rec), (unsigned)page_offset(trec))); |
| 1487 |
valid = false; |
| 1488 |
break; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (index) { |
| 1492 |
/* Compare the data. */ |
| 1493 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1494 |
UT_LOCATION_HERE, &heap); |
| 1495 |
|
| 1496 |
if (memcmp(rec - rec_offs_extra_size(offsets), |
| 1497 |
trec - rec_offs_extra_size(offsets), |
| 1498 |
rec_offs_size(offsets))) { |
| 1499 |
page_zip_fail( |
| 1500 |
("page_zip_validate:" |
| 1501 |
" record content: 0x%02x", |
| 1502 |
(unsigned)page_offset(rec))); |
| 1503 |
valid = false; |
| 1504 |
break; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
rec = page_rec_get_next_low(rec, true); |
| 1509 |
trec = page_rec_get_next_low(trec, true); |
| 1510 |
} while (rec || trec); |
| 1511 |
|
| 1512 |
if (heap) { |
| 1513 |
mem_heap_free(heap); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
func_exit: |
| 1518 |
if (!valid) { |
| 1519 |
page_zip_hexdump(page_zip, sizeof *page_zip); |
| 1520 |
page_zip_hexdump(page_zip->data, page_zip_get_size(page_zip)); |
| 1521 |
page_zip_hexdump(page, UNIV_PAGE_SIZE); |
| 1522 |
page_zip_hexdump(temp_page, UNIV_PAGE_SIZE); |
| 1523 |
} |
| 1524 |
ut::aligned_free(temp_page); |
| 1525 |
return (valid); |
| 1526 |
} |
| 1527 |
|
| 1528 |
/** Check that the compressed and decompressed pages match. |
| 1529 |
@return true if valid, false if not */ |
| 1530 |
bool page_zip_validate( |
| 1531 |
const page_zip_des_t *page_zip, /*!< in: compressed page */ |
| 1532 |
const page_t *page, /*!< in: uncompressed page */ |
| 1533 |
const dict_index_t *index) /*!< in: index of the page, if known */ |
| 1534 |
{ |
| 1535 |
return (page_zip_validate_low(page_zip, page, index, recv_recovery_is_on())); |
| 1536 |
} |
| 1537 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1538 |
|
| 1539 |
#ifdef UNIV_DEBUG |
| 1540 |
/** Assert that the compressed and decompressed page headers match. |
| 1541 |
@param[in] page_zip compressed page |
| 1542 |
@param[in] page uncompressed page */ |
| 1543 |
static void page_zip_header_cmp(const page_zip_des_t *page_zip, |
| 1544 |
const byte *page) { |
| 1545 |
ut_ad(!memcmp(page_zip->data + FIL_PAGE_PREV, page + FIL_PAGE_PREV, |
| 1546 |
FIL_PAGE_LSN - FIL_PAGE_PREV)); |
| 1547 |
ut_ad(!memcmp(page_zip->data + FIL_PAGE_TYPE, page + FIL_PAGE_TYPE, 2)); |
| 1548 |
ut_ad(!memcmp(page_zip->data + FIL_PAGE_DATA, page + FIL_PAGE_DATA, |
| 1549 |
PAGE_DATA - FIL_PAGE_DATA)); |
| 1550 |
} |
| 1551 |
#endif /* UNIV_DEBUG */ |
| 1552 |
|
| 1553 |
/** Write a record on the compressed page that contains externally stored |
| 1554 |
columns. The data must already have been written to the uncompressed page. |
| 1555 |
@return end of modification log */ |
| 1556 |
static byte *page_zip_write_rec_ext( |
| 1557 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 1558 |
const page_t *page, /*!< in: page containing rec */ |
| 1559 |
const byte *rec, /*!< in: record being written */ |
| 1560 |
const dict_index_t *index, /*!< in: record descriptor */ |
| 1561 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 1562 |
ulint create, /*!< in: nonzero=insert, zero=update */ |
| 1563 |
ulint trx_id_col, /*!< in: position of DB_TRX_ID */ |
| 1564 |
ulint heap_no, /*!< in: heap number of rec */ |
| 1565 |
byte *storage, /*!< in: end of dense page directory */ |
| 1566 |
byte *data) /*!< in: end of modification log */ |
| 1567 |
{ |
| 1568 |
const byte *start = rec; |
| 1569 |
ulint i; |
| 1570 |
ulint len; |
| 1571 |
byte *externs = storage; |
| 1572 |
ulint n_ext = rec_offs_n_extern(index, offsets); |
| 1573 |
|
| 1574 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 1575 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 1576 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 1577 |
rec_offs_extra_size(offsets)); |
| 1578 |
|
| 1579 |
externs -= (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN) * |
| 1580 |
(page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW); |
| 1581 |
|
| 1582 |
/* Note that this will not take into account |
| 1583 |
the BLOB columns of rec if create==true. */ |
| 1584 |
ut_ad(data + rec_offs_data_size(offsets) - |
| 1585 |
(DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN) - |
| 1586 |
n_ext * BTR_EXTERN_FIELD_REF_SIZE < |
| 1587 |
externs - BTR_EXTERN_FIELD_REF_SIZE * page_zip->n_blobs); |
| 1588 |
|
| 1589 |
{ |
| 1590 |
ulint blob_no = page_zip_get_n_prev_extern(page_zip, rec, index); |
| 1591 |
byte *ext_end = externs - page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE; |
| 1592 |
ut_ad(blob_no <= page_zip->n_blobs); |
| 1593 |
externs -= blob_no * BTR_EXTERN_FIELD_REF_SIZE; |
| 1594 |
|
| 1595 |
if (create) { |
| 1596 |
page_zip->n_blobs += static_cast<unsigned>(n_ext); |
| 1597 |
ASSERT_ZERO_BLOB(ext_end - n_ext * BTR_EXTERN_FIELD_REF_SIZE); |
| 1598 |
memmove(ext_end - n_ext * BTR_EXTERN_FIELD_REF_SIZE, ext_end, |
| 1599 |
externs - ext_end); |
| 1600 |
} |
| 1601 |
|
| 1602 |
ut_a(blob_no + n_ext <= page_zip->n_blobs); |
| 1603 |
} |
| 1604 |
|
| 1605 |
for (i = 0; i < rec_offs_n_fields(offsets); i++) { |
| 1606 |
const byte *src; |
| 1607 |
|
| 1608 |
if (UNIV_UNLIKELY(i == trx_id_col)) { |
| 1609 |
ut_ad(!rec_offs_nth_extern(index, offsets, i)); |
| 1610 |
ut_ad(!rec_offs_nth_extern(index, offsets, i + 1)); |
| 1611 |
/* Locate trx_id and roll_ptr. */ |
| 1612 |
src = rec_get_nth_field(index, rec, offsets, i, &len); |
| 1613 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 1614 |
ut_ad(src + DATA_TRX_ID_LEN == |
| 1615 |
rec_get_nth_field(index, rec, offsets, i + 1, &len)); |
| 1616 |
ut_ad(len == DATA_ROLL_PTR_LEN); |
| 1617 |
|
| 1618 |
/* Log the preceding fields. */ |
| 1619 |
ASSERT_ZERO(data, src - start); |
| 1620 |
memcpy(data, start, src - start); |
| 1621 |
data += src - start; |
| 1622 |
start = src + (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 1623 |
|
| 1624 |
/* Store trx_id and roll_ptr. */ |
| 1625 |
memcpy(storage - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN) * (heap_no - 1), |
| 1626 |
src, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 1627 |
i++; /* skip also roll_ptr */ |
| 1628 |
} else if (rec_offs_nth_extern(index, offsets, i)) { |
| 1629 |
src = rec_get_nth_field(index, rec, offsets, i, &len); |
| 1630 |
|
| 1631 |
ut_ad(index->is_clustered()); |
| 1632 |
ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE); |
| 1633 |
src += len - BTR_EXTERN_FIELD_REF_SIZE; |
| 1634 |
|
| 1635 |
ASSERT_ZERO(data, src - start); |
| 1636 |
memcpy(data, start, src - start); |
| 1637 |
data += src - start; |
| 1638 |
start = src + BTR_EXTERN_FIELD_REF_SIZE; |
| 1639 |
|
| 1640 |
/* Store the BLOB pointer. */ |
| 1641 |
externs -= BTR_EXTERN_FIELD_REF_SIZE; |
| 1642 |
ut_ad(data < externs); |
| 1643 |
memcpy(externs, src, BTR_EXTERN_FIELD_REF_SIZE); |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
/* Log the last bytes of the record. */ |
| 1648 |
len = rec_offs_data_size(offsets) - (start - rec); |
| 1649 |
|
| 1650 |
ASSERT_ZERO(data, len); |
| 1651 |
memcpy(data, start, len); |
| 1652 |
data += len; |
| 1653 |
|
| 1654 |
return (data); |
| 1655 |
} |
| 1656 |
|
| 1657 |
/** Write an entire record on the compressed page. The data must already |
| 1658 |
have been written to the uncompressed page. */ |
| 1659 |
void page_zip_write_rec( |
| 1660 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 1661 |
const byte *rec, /*!< in: record being written */ |
| 1662 |
const dict_index_t *index, /*!< in: the index the record belongs to */ |
| 1663 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 1664 |
ulint create) /*!< in: nonzero=insert, zero=update */ |
| 1665 |
{ |
| 1666 |
const page_t *page; |
| 1667 |
byte *data; |
| 1668 |
byte *storage; |
| 1669 |
ulint heap_no; |
| 1670 |
byte *slot; |
| 1671 |
|
| 1672 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 1673 |
ut_ad(page_zip_get_size(page_zip) > PAGE_DATA + page_zip_dir_size(page_zip)); |
| 1674 |
ut_ad(rec_offs_comp(offsets)); |
| 1675 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 1676 |
|
| 1677 |
ut_ad(page_zip->m_start >= PAGE_DATA); |
| 1678 |
|
| 1679 |
page = page_align(rec); |
| 1680 |
|
| 1681 |
ut_d(page_zip_header_cmp(page_zip, page)); |
| 1682 |
ut_ad(page_simple_validate_new((page_t *)page)); |
| 1683 |
|
| 1684 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 1685 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 1686 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 1687 |
rec_offs_extra_size(offsets)); |
| 1688 |
|
| 1689 |
slot = page_zip_dir_find(page_zip, page_offset(rec)); |
| 1690 |
ut_a(slot); |
| 1691 |
/* Copy the delete mark. */ |
| 1692 |
if (rec_get_deleted_flag(rec, true)) { |
| 1693 |
*slot |= PAGE_ZIP_DIR_SLOT_DEL >> 8; |
| 1694 |
} else { |
| 1695 |
*slot &= ~(PAGE_ZIP_DIR_SLOT_DEL >> 8); |
| 1696 |
} |
| 1697 |
|
| 1698 |
ut_ad(rec_get_start((rec_t *)rec, offsets) >= page + PAGE_ZIP_START); |
| 1699 |
ut_ad(rec_get_end((rec_t *)rec, offsets) <= |
| 1700 |
page + UNIV_PAGE_SIZE - PAGE_DIR - |
| 1701 |
PAGE_DIR_SLOT_SIZE * page_dir_get_n_slots(page)); |
| 1702 |
|
| 1703 |
heap_no = rec_get_heap_no_new(rec); |
| 1704 |
ut_ad(heap_no >= PAGE_HEAP_NO_USER_LOW); /* not infimum or supremum */ |
| 1705 |
ut_ad(heap_no < page_dir_get_n_heap(page)); |
| 1706 |
|
| 1707 |
/* Append to the modification log. */ |
| 1708 |
data = page_zip->data + page_zip->m_end; |
| 1709 |
ut_ad(!*data); |
| 1710 |
|
| 1711 |
/* Identify the record by writing its heap number - 1. |
| 1712 |
0 is reserved to indicate the end of the modification log. */ |
| 1713 |
|
| 1714 |
if (UNIV_UNLIKELY(heap_no - 1 >= 64)) { |
| 1715 |
*data++ = (byte)(0x80 | (heap_no - 1) >> 7); |
| 1716 |
ut_ad(!*data); |
| 1717 |
} |
| 1718 |
*data++ = (byte)((heap_no - 1) << 1); |
| 1719 |
ut_ad(!*data); |
| 1720 |
|
| 1721 |
{ |
| 1722 |
const byte *start = rec - rec_offs_extra_size(offsets); |
| 1723 |
const byte *b = rec - REC_N_NEW_EXTRA_BYTES; |
| 1724 |
|
| 1725 |
/* Write the extra bytes backwards, so that |
| 1726 |
rec_offs_extra_size() can be easily computed in |
| 1727 |
page_zip_apply_log() by invoking |
| 1728 |
rec_get_offsets_reverse(). */ |
| 1729 |
|
| 1730 |
while (b != start) { |
| 1731 |
*data++ = *--b; |
| 1732 |
ut_ad(!*data); |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
/* Write the data bytes. Store the uncompressed bytes separately. */ |
| 1737 |
storage = page_zip_dir_start(page_zip); |
| 1738 |
|
| 1739 |
if (page_is_leaf(page)) { |
| 1740 |
ulint len; |
| 1741 |
|
| 1742 |
if (index->is_clustered()) { |
| 1743 |
ulint trx_id_col; |
| 1744 |
|
| 1745 |
trx_id_col = index->get_sys_col_pos(DATA_TRX_ID); |
| 1746 |
ut_ad(trx_id_col != ULINT_UNDEFINED); |
| 1747 |
|
| 1748 |
/* Store separately trx_id, roll_ptr and |
| 1749 |
the BTR_EXTERN_FIELD_REF of each BLOB column. */ |
| 1750 |
if (rec_offs_any_extern(offsets)) { |
| 1751 |
data = |
| 1752 |
page_zip_write_rec_ext(page_zip, page, rec, index, offsets, create, |
| 1753 |
trx_id_col, heap_no, storage, data); |
| 1754 |
} else { |
| 1755 |
/* Locate trx_id and roll_ptr. */ |
| 1756 |
const byte *src = |
| 1757 |
rec_get_nth_field(index, rec, offsets, trx_id_col, &len); |
| 1758 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 1759 |
ut_ad(src + DATA_TRX_ID_LEN == |
| 1760 |
rec_get_nth_field(index, rec, offsets, trx_id_col + 1, &len)); |
| 1761 |
ut_ad(len == DATA_ROLL_PTR_LEN); |
| 1762 |
|
| 1763 |
/* Log the preceding fields. */ |
| 1764 |
ASSERT_ZERO(data, src - rec); |
| 1765 |
memcpy(data, rec, src - rec); |
| 1766 |
data += src - rec; |
| 1767 |
|
| 1768 |
/* Store trx_id and roll_ptr. */ |
| 1769 |
memcpy(storage - (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN) * (heap_no - 1), |
| 1770 |
src, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 1771 |
|
| 1772 |
src += DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; |
| 1773 |
|
| 1774 |
/* Log the last bytes of the record. */ |
| 1775 |
len = rec_offs_data_size(offsets) - (src - rec); |
| 1776 |
|
| 1777 |
ASSERT_ZERO(data, len); |
| 1778 |
memcpy(data, src, len); |
| 1779 |
data += len; |
| 1780 |
} |
| 1781 |
} else { |
| 1782 |
/* Leaf page of a secondary index: |
| 1783 |
no externally stored columns */ |
| 1784 |
ut_ad(index->get_sys_col_pos(DATA_TRX_ID) == ULINT_UNDEFINED); |
| 1785 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 1786 |
|
| 1787 |
/* Log the entire record. */ |
| 1788 |
len = rec_offs_data_size(offsets); |
| 1789 |
|
| 1790 |
ASSERT_ZERO(data, len); |
| 1791 |
memcpy(data, rec, len); |
| 1792 |
data += len; |
| 1793 |
} |
| 1794 |
} else { |
| 1795 |
/* This is a node pointer page. */ |
| 1796 |
ulint len; |
| 1797 |
|
| 1798 |
/* Non-leaf nodes should not have any externally |
| 1799 |
stored columns. */ |
| 1800 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 1801 |
|
| 1802 |
/* Copy the data bytes, except node_ptr. */ |
| 1803 |
len = rec_offs_data_size(offsets) - REC_NODE_PTR_SIZE; |
| 1804 |
ut_ad(data + len < |
| 1805 |
storage - REC_NODE_PTR_SIZE * |
| 1806 |
(page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW)); |
| 1807 |
ASSERT_ZERO(data, len); |
| 1808 |
memcpy(data, rec, len); |
| 1809 |
data += len; |
| 1810 |
|
| 1811 |
/* Copy the node pointer to the uncompressed area. */ |
| 1812 |
memcpy(storage - REC_NODE_PTR_SIZE * (heap_no - 1), rec + len, |
| 1813 |
REC_NODE_PTR_SIZE); |
| 1814 |
} |
| 1815 |
|
| 1816 |
ut_a(!*data); |
| 1817 |
ut_ad((ulint)(data - page_zip->data) < page_zip_get_size(page_zip)); |
| 1818 |
page_zip->m_end = data - page_zip->data; |
| 1819 |
page_zip->m_nonempty = true; |
| 1820 |
|
| 1821 |
#ifdef UNIV_ZIP_DEBUG |
| 1822 |
ut_a(page_zip_validate(page_zip, page_align(rec), index)); |
| 1823 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1824 |
} |
| 1825 |
|
| 1826 |
/** Parses a log record of writing a BLOB pointer of a record. |
| 1827 |
@return end of log record or NULL */ |
| 1828 |
byte *page_zip_parse_write_blob_ptr( |
| 1829 |
byte *ptr, /*!< in: redo log buffer */ |
| 1830 |
byte *end_ptr, /*!< in: redo log buffer end */ |
| 1831 |
page_t *page, /*!< in/out: uncompressed page */ |
| 1832 |
page_zip_des_t *page_zip) /*!< in/out: compressed page */ |
| 1833 |
{ |
| 1834 |
ulint offset; |
| 1835 |
ulint z_offset; |
| 1836 |
|
| 1837 |
ut_ad(ptr != nullptr); |
| 1838 |
ut_ad(end_ptr != nullptr); |
| 1839 |
ut_ad(!page == !page_zip); |
| 1840 |
|
| 1841 |
if (UNIV_UNLIKELY(end_ptr < ptr + (2 + 2 + BTR_EXTERN_FIELD_REF_SIZE))) { |
| 1842 |
return (nullptr); |
| 1843 |
} |
| 1844 |
|
| 1845 |
offset = mach_read_from_2(ptr); |
| 1846 |
z_offset = mach_read_from_2(ptr + 2); |
| 1847 |
|
| 1848 |
if (offset < PAGE_ZIP_START || offset >= UNIV_PAGE_SIZE || |
| 1849 |
z_offset >= UNIV_PAGE_SIZE) { |
| 1850 |
corrupt: |
| 1851 |
recv_sys->found_corrupt_log = true; |
| 1852 |
|
| 1853 |
return (nullptr); |
| 1854 |
} |
| 1855 |
|
| 1856 |
if (page) { |
| 1857 |
if (!page_zip || !page_is_leaf(page)) { |
| 1858 |
goto corrupt; |
| 1859 |
} |
| 1860 |
|
| 1861 |
#ifdef UNIV_ZIP_DEBUG |
| 1862 |
ut_a(page_zip_validate(page_zip, page, nullptr)); |
| 1863 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1864 |
|
| 1865 |
memcpy(page + offset, ptr + 4, BTR_EXTERN_FIELD_REF_SIZE); |
| 1866 |
memcpy(page_zip->data + z_offset, ptr + 4, BTR_EXTERN_FIELD_REF_SIZE); |
| 1867 |
|
| 1868 |
#ifdef UNIV_ZIP_DEBUG |
| 1869 |
ut_a(page_zip_validate(page_zip, page, nullptr)); |
| 1870 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1871 |
} |
| 1872 |
|
| 1873 |
return (ptr + (2 + 2 + BTR_EXTERN_FIELD_REF_SIZE)); |
| 1874 |
} |
| 1875 |
|
| 1876 |
/** Write a BLOB pointer of a record on the leaf page of a clustered index. |
| 1877 |
The information must already have been updated on the uncompressed page. */ |
| 1878 |
void page_zip_write_blob_ptr( |
| 1879 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 1880 |
const byte *rec, /*!< in/out: record whose data is being |
| 1881 |
written */ |
| 1882 |
const dict_index_t *index, /*!< in: index of the page */ |
| 1883 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 1884 |
ulint n, /*!< in: column index */ |
| 1885 |
mtr_t *mtr) /*!< in: mini-transaction handle, |
| 1886 |
or NULL if no logging is needed */ |
| 1887 |
{ |
| 1888 |
const byte *field; |
| 1889 |
byte *externs; |
| 1890 |
const page_t *page = page_align(rec); |
| 1891 |
ulint blob_no; |
| 1892 |
ulint len; |
| 1893 |
|
| 1894 |
ut_ad(page_zip != nullptr); |
| 1895 |
ut_ad(rec != nullptr); |
| 1896 |
ut_ad(index != nullptr); |
| 1897 |
ut_ad(offsets != nullptr); |
| 1898 |
ut_ad(page_simple_validate_new((page_t *)page)); |
| 1899 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 1900 |
ut_ad(page_zip_get_size(page_zip) > PAGE_DATA + page_zip_dir_size(page_zip)); |
| 1901 |
ut_ad(rec_offs_comp(offsets)); |
| 1902 |
ut_ad(rec_offs_validate(rec, nullptr, offsets)); |
| 1903 |
ut_ad(rec_offs_any_extern(offsets)); |
| 1904 |
ut_ad(rec_offs_nth_extern(index, offsets, n)); |
| 1905 |
|
| 1906 |
ut_ad(page_zip->m_start >= PAGE_DATA); |
| 1907 |
ut_d(page_zip_header_cmp(page_zip, page)); |
| 1908 |
|
| 1909 |
ut_ad(page_is_leaf(page)); |
| 1910 |
ut_ad(index->is_clustered()); |
| 1911 |
|
| 1912 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 1913 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 1914 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 1915 |
rec_offs_extra_size(offsets)); |
| 1916 |
|
| 1917 |
blob_no = page_zip_get_n_prev_extern(page_zip, rec, index) + |
| 1918 |
rec_get_n_extern_new(rec, index, n); |
| 1919 |
ut_a(blob_no < page_zip->n_blobs); |
| 1920 |
|
| 1921 |
externs = page_zip->data + page_zip_get_size(page_zip) - |
| 1922 |
(page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW) * |
| 1923 |
PAGE_ZIP_CLUST_LEAF_SLOT_SIZE; |
| 1924 |
|
| 1925 |
field = rec_get_nth_field(index, rec, offsets, n, &len); |
| 1926 |
|
| 1927 |
externs -= (blob_no + 1) * BTR_EXTERN_FIELD_REF_SIZE; |
| 1928 |
field += len - BTR_EXTERN_FIELD_REF_SIZE; |
| 1929 |
|
| 1930 |
memcpy(externs, field, BTR_EXTERN_FIELD_REF_SIZE); |
| 1931 |
|
| 1932 |
#ifdef UNIV_ZIP_DEBUG |
| 1933 |
ut_a(page_zip_validate(page_zip, page, index)); |
| 1934 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1935 |
|
| 1936 |
if (mtr == nullptr) { |
| 1937 |
return; |
| 1938 |
} |
| 1939 |
#ifndef UNIV_HOTBACKUP |
| 1940 |
byte *log_ptr = nullptr; |
| 1941 |
if (!mlog_open(mtr, 11 + 2 + 2 + BTR_EXTERN_FIELD_REF_SIZE, log_ptr)) { |
| 1942 |
return; |
| 1943 |
} |
| 1944 |
|
| 1945 |
log_ptr = mlog_write_initial_log_record_fast( |
| 1946 |
(byte *)field, MLOG_ZIP_WRITE_BLOB_PTR, log_ptr, mtr); |
| 1947 |
mach_write_to_2(log_ptr, page_offset(field)); |
| 1948 |
log_ptr += 2; |
| 1949 |
mach_write_to_2(log_ptr, externs - page_zip->data); |
| 1950 |
log_ptr += 2; |
| 1951 |
memcpy(log_ptr, externs, BTR_EXTERN_FIELD_REF_SIZE); |
| 1952 |
log_ptr += BTR_EXTERN_FIELD_REF_SIZE; |
| 1953 |
mlog_close(mtr, log_ptr); |
| 1954 |
#endif /* !UNIV_HOTBACKUP */ |
| 1955 |
} |
| 1956 |
|
| 1957 |
/** Parses a log record of writing the node pointer of a record. |
| 1958 |
@return end of log record or NULL */ |
| 1959 |
byte *page_zip_parse_write_node_ptr( |
| 1960 |
byte *ptr, /*!< in: redo log buffer */ |
| 1961 |
byte *end_ptr, /*!< in: redo log buffer end */ |
| 1962 |
page_t *page, /*!< in/out: uncompressed page */ |
| 1963 |
page_zip_des_t *page_zip) /*!< in/out: compressed page */ |
| 1964 |
{ |
| 1965 |
ulint offset; |
| 1966 |
ulint z_offset; |
| 1967 |
|
| 1968 |
ut_ad(ptr != nullptr); |
| 1969 |
ut_ad(end_ptr != nullptr); |
| 1970 |
ut_ad(!page == !page_zip); |
| 1971 |
|
| 1972 |
if (UNIV_UNLIKELY(end_ptr < ptr + (2 + 2 + REC_NODE_PTR_SIZE))) { |
| 1973 |
return (nullptr); |
| 1974 |
} |
| 1975 |
|
| 1976 |
offset = mach_read_from_2(ptr); |
| 1977 |
z_offset = mach_read_from_2(ptr + 2); |
| 1978 |
|
| 1979 |
if (offset < PAGE_ZIP_START || offset >= UNIV_PAGE_SIZE || |
| 1980 |
z_offset >= UNIV_PAGE_SIZE) { |
| 1981 |
corrupt: |
| 1982 |
recv_sys->found_corrupt_log = true; |
| 1983 |
|
| 1984 |
return (nullptr); |
| 1985 |
} |
| 1986 |
|
| 1987 |
if (page) { |
| 1988 |
byte *storage_end; |
| 1989 |
byte *field; |
| 1990 |
byte *storage; |
| 1991 |
ulint heap_no; |
| 1992 |
|
| 1993 |
if (!page_zip || page_is_leaf(page)) { |
| 1994 |
goto corrupt; |
| 1995 |
} |
| 1996 |
|
| 1997 |
#ifdef UNIV_ZIP_DEBUG |
| 1998 |
ut_a(page_zip_validate(page_zip, page, nullptr)); |
| 1999 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2000 |
|
| 2001 |
field = page + offset; |
| 2002 |
storage = page_zip->data + z_offset; |
| 2003 |
|
| 2004 |
storage_end = page_zip_dir_start(page_zip); |
| 2005 |
|
| 2006 |
heap_no = 1 + (storage_end - storage) / REC_NODE_PTR_SIZE; |
| 2007 |
|
| 2008 |
if (UNIV_UNLIKELY((storage_end - storage) % REC_NODE_PTR_SIZE) || |
| 2009 |
UNIV_UNLIKELY(heap_no < PAGE_HEAP_NO_USER_LOW) || |
| 2010 |
UNIV_UNLIKELY(heap_no >= page_dir_get_n_heap(page))) { |
| 2011 |
goto corrupt; |
| 2012 |
} |
| 2013 |
|
| 2014 |
memcpy(field, ptr + 4, REC_NODE_PTR_SIZE); |
| 2015 |
memcpy(storage, ptr + 4, REC_NODE_PTR_SIZE); |
| 2016 |
|
| 2017 |
#ifdef UNIV_ZIP_DEBUG |
| 2018 |
ut_a(page_zip_validate(page_zip, page, nullptr)); |
| 2019 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2020 |
} |
| 2021 |
|
| 2022 |
return (ptr + (2 + 2 + REC_NODE_PTR_SIZE)); |
| 2023 |
} |
| 2024 |
|
| 2025 |
/** Write the node pointer of a record on a non-leaf compressed page. |
| 2026 |
@param[in,out] page_zip Compressed page |
| 2027 |
@param[in,out] rec Record |
| 2028 |
@param[in] size Data size of rec |
| 2029 |
@param[in] ptr Node pointer |
| 2030 |
@param[in] mtr Mini-transaction, or null */ |
| 2031 |
void page_zip_write_node_ptr(page_zip_des_t *page_zip, byte *rec, ulint size, |
| 2032 |
ulint ptr, mtr_t *mtr) { |
| 2033 |
byte *field; |
| 2034 |
byte *storage; |
| 2035 |
#ifdef UNIV_DEBUG |
| 2036 |
page_t *page = page_align(rec); |
| 2037 |
#endif /* UNIV_DEBUG */ |
| 2038 |
|
| 2039 |
ut_ad(page_simple_validate_new(page)); |
| 2040 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 2041 |
ut_ad(page_zip_get_size(page_zip) > PAGE_DATA + page_zip_dir_size(page_zip)); |
| 2042 |
ut_ad(page_rec_is_comp(rec)); |
| 2043 |
|
| 2044 |
ut_ad(page_zip->m_start >= PAGE_DATA); |
| 2045 |
ut_d(page_zip_header_cmp(page_zip, page)); |
| 2046 |
|
| 2047 |
ut_ad(!page_is_leaf(page)); |
| 2048 |
|
| 2049 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2050 |
UNIV_MEM_ASSERT_RW(rec, size); |
| 2051 |
|
| 2052 |
storage = page_zip_dir_start(page_zip) - |
| 2053 |
(rec_get_heap_no_new(rec) - 1) * REC_NODE_PTR_SIZE; |
| 2054 |
field = rec + size - REC_NODE_PTR_SIZE; |
| 2055 |
|
| 2056 |
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG |
| 2057 |
ut_a(!memcmp(storage, field, REC_NODE_PTR_SIZE)); |
| 2058 |
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */ |
| 2059 |
static_assert(REC_NODE_PTR_SIZE == 4, "REC_NODE_PTR_SIZE != 4"); |
| 2060 |
mach_write_to_4(field, ptr); |
| 2061 |
memcpy(storage, field, REC_NODE_PTR_SIZE); |
| 2062 |
|
| 2063 |
if (mtr == nullptr) { |
| 2064 |
return; |
| 2065 |
} |
| 2066 |
|
| 2067 |
#ifndef UNIV_HOTBACKUP |
| 2068 |
byte *log_ptr = nullptr; |
| 2069 |
|
| 2070 |
if (!mlog_open(mtr, 11 + 2 + 2 + REC_NODE_PTR_SIZE, log_ptr)) { |
| 2071 |
return; |
| 2072 |
} |
| 2073 |
|
| 2074 |
log_ptr = mlog_write_initial_log_record_fast(field, MLOG_ZIP_WRITE_NODE_PTR, |
| 2075 |
log_ptr, mtr); |
| 2076 |
mach_write_to_2(log_ptr, page_offset(field)); |
| 2077 |
log_ptr += 2; |
| 2078 |
mach_write_to_2(log_ptr, storage - page_zip->data); |
| 2079 |
log_ptr += 2; |
| 2080 |
memcpy(log_ptr, field, REC_NODE_PTR_SIZE); |
| 2081 |
log_ptr += REC_NODE_PTR_SIZE; |
| 2082 |
mlog_close(mtr, log_ptr); |
| 2083 |
#endif /* !UNIV_HOTBACKUP */ |
| 2084 |
} |
| 2085 |
|
| 2086 |
/** Write the trx_id and roll_ptr of a record on a B-tree leaf node page. |
| 2087 |
@param[in,out] page_zip Compressed page |
| 2088 |
@param[in,out] rec Record |
| 2089 |
@param[in] offsets Rec_get_offsets(rec, index) |
| 2090 |
@param[in] trx_id_col Column number of trx_id in rec |
| 2091 |
@param[in] trx_id Transaction identifier |
| 2092 |
@param[in] roll_ptr Roll_ptr */ |
| 2093 |
void page_zip_write_trx_id_and_roll_ptr(page_zip_des_t *page_zip, byte *rec, |
| 2094 |
const ulint *offsets, ulint trx_id_col, |
| 2095 |
trx_id_t trx_id, roll_ptr_t roll_ptr) { |
| 2096 |
byte *field; |
| 2097 |
byte *storage; |
| 2098 |
#ifdef UNIV_DEBUG |
| 2099 |
page_t *page = page_align(rec); |
| 2100 |
#endif /* UNIV_DEBUG */ |
| 2101 |
ulint len; |
| 2102 |
|
| 2103 |
ut_ad(page_simple_validate_new(page)); |
| 2104 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 2105 |
ut_ad(page_zip_get_size(page_zip) > PAGE_DATA + page_zip_dir_size(page_zip)); |
| 2106 |
ut_ad(rec_offs_validate(rec, nullptr, offsets)); |
| 2107 |
ut_ad(rec_offs_comp(offsets)); |
| 2108 |
|
| 2109 |
ut_ad(page_zip->m_start >= PAGE_DATA); |
| 2110 |
ut_d(page_zip_header_cmp(page_zip, page)); |
| 2111 |
|
| 2112 |
ut_ad(page_is_leaf(page)); |
| 2113 |
|
| 2114 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2115 |
|
| 2116 |
storage = |
| 2117 |
page_zip_dir_start(page_zip) - |
| 2118 |
(rec_get_heap_no_new(rec) - 1) * (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 2119 |
|
| 2120 |
field = rec_get_nth_field(nullptr, rec, offsets, trx_id_col, &len); |
| 2121 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 2122 |
ut_ad(field + DATA_TRX_ID_LEN == |
| 2123 |
rec_get_nth_field(nullptr, rec, offsets, trx_id_col + 1, &len)); |
| 2124 |
ut_ad(len == DATA_ROLL_PTR_LEN); |
| 2125 |
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG |
| 2126 |
ut_a(!memcmp(storage, field, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)); |
| 2127 |
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */ |
| 2128 |
mach_write_to_6(field, trx_id); |
| 2129 |
mach_write_to_7(field + DATA_TRX_ID_LEN, roll_ptr); |
| 2130 |
memcpy(storage, field, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 2131 |
|
| 2132 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 2133 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 2134 |
rec_offs_extra_size(offsets)); |
| 2135 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2136 |
} |
| 2137 |
|
| 2138 |
/** Clear an area on the uncompressed and compressed page. |
| 2139 |
Do not clear the data payload, as that would grow the modification log. */ |
| 2140 |
static void page_zip_clear_rec( |
| 2141 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 2142 |
byte *rec, /*!< in: record to clear */ |
| 2143 |
const dict_index_t *index, /*!< in: index of rec */ |
| 2144 |
const ulint *offsets) /*!< in: rec_get_offsets(rec, index) */ |
| 2145 |
{ |
| 2146 |
ulint heap_no; |
| 2147 |
page_t *page = page_align(rec); |
| 2148 |
byte *storage; |
| 2149 |
byte *field; |
| 2150 |
ulint len; |
| 2151 |
/* page_zip_validate() would fail here if a record |
| 2152 |
containing externally stored columns is being deleted. */ |
| 2153 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 2154 |
ut_ad(!page_zip_dir_find(page_zip, page_offset(rec))); |
| 2155 |
ut_ad(page_zip_dir_find_free(page_zip, page_offset(rec))); |
| 2156 |
ut_d(page_zip_header_cmp(page_zip, page)); |
| 2157 |
|
| 2158 |
heap_no = rec_get_heap_no_new(rec); |
| 2159 |
ut_ad(heap_no >= PAGE_HEAP_NO_USER_LOW); |
| 2160 |
|
| 2161 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2162 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 2163 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 2164 |
rec_offs_extra_size(offsets)); |
| 2165 |
|
| 2166 |
if (!page_is_leaf(page)) { |
| 2167 |
/* Clear node_ptr. On the compressed page, |
| 2168 |
there is an array of node_ptr immediately before the |
| 2169 |
dense page directory, at the very end of the page. */ |
| 2170 |
storage = page_zip_dir_start(page_zip); |
| 2171 |
ut_ad(dict_index_get_n_unique_in_tree_nonleaf(index) == |
| 2172 |
rec_offs_n_fields(offsets) - 1); |
| 2173 |
field = rec_get_nth_field(nullptr, rec, offsets, |
| 2174 |
rec_offs_n_fields(offsets) - 1, &len); |
| 2175 |
ut_ad(len == REC_NODE_PTR_SIZE); |
| 2176 |
|
| 2177 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 2178 |
memset(field, 0, REC_NODE_PTR_SIZE); |
| 2179 |
memset(storage - (heap_no - 1) * REC_NODE_PTR_SIZE, 0, REC_NODE_PTR_SIZE); |
| 2180 |
} else if (index->is_clustered()) { |
| 2181 |
/* Clear trx_id and roll_ptr. On the compressed page, |
| 2182 |
there is an array of these fields immediately before the |
| 2183 |
dense page directory, at the very end of the page. */ |
| 2184 |
const ulint trx_id_pos = |
| 2185 |
dict_col_get_clust_pos(index->table->get_sys_col(DATA_TRX_ID), index); |
| 2186 |
storage = page_zip_dir_start(page_zip); |
| 2187 |
field = rec_get_nth_field(index, rec, offsets, trx_id_pos, &len); |
| 2188 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 2189 |
|
| 2190 |
memset(field, 0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 2191 |
memset(storage - (heap_no - 1) * (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN), 0, |
| 2192 |
DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 2193 |
|
| 2194 |
if (rec_offs_any_extern(offsets)) { |
| 2195 |
ulint i; |
| 2196 |
|
| 2197 |
for (i = rec_offs_n_fields(offsets); i--;) { |
| 2198 |
/* Clear all BLOB pointers in order to make |
| 2199 |
page_zip_validate() pass. */ |
| 2200 |
if (rec_offs_nth_extern(index, offsets, i)) { |
| 2201 |
field = rec_get_nth_field(index, rec, offsets, i, &len); |
| 2202 |
ut_ad(len == BTR_EXTERN_FIELD_REF_SIZE); |
| 2203 |
memset(field + len - BTR_EXTERN_FIELD_REF_SIZE, 0, |
| 2204 |
BTR_EXTERN_FIELD_REF_SIZE); |
| 2205 |
} |
| 2206 |
} |
| 2207 |
} |
| 2208 |
} else { |
| 2209 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 2210 |
} |
| 2211 |
|
| 2212 |
#ifdef UNIV_ZIP_DEBUG |
| 2213 |
ut_a(page_zip_validate(page_zip, page, index)); |
| 2214 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2215 |
} |
| 2216 |
|
| 2217 |
/** Write the "deleted" flag of a record on a compressed page. The flag must |
| 2218 |
already have been written on the uncompressed page. */ |
| 2219 |
void page_zip_rec_set_deleted( |
| 2220 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 2221 |
const byte *rec, /*!< in: record on the uncompressed page */ |
| 2222 |
bool flag) /*!< in: the deleted flag (nonzero=true) */ |
| 2223 |
{ |
| 2224 |
byte *slot = page_zip_dir_find(page_zip, page_offset(rec)); |
| 2225 |
ut_a(slot); |
| 2226 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2227 |
if (flag) { |
| 2228 |
*slot |= (PAGE_ZIP_DIR_SLOT_DEL >> 8); |
| 2229 |
} else { |
| 2230 |
*slot &= ~(PAGE_ZIP_DIR_SLOT_DEL >> 8); |
| 2231 |
} |
| 2232 |
#ifdef UNIV_ZIP_DEBUG |
| 2233 |
ut_a(page_zip_validate(page_zip, page_align(rec), nullptr)); |
| 2234 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2235 |
} |
| 2236 |
|
| 2237 |
/** Write the "owned" flag of a record on a compressed page. The n_owned field |
| 2238 |
must already have been written on the uncompressed page. |
| 2239 |
@param[in,out] page_zip Compressed page |
| 2240 |
@param[in] rec Record on the uncompressed page |
| 2241 |
@param[in] flag The owned flag (nonzero=true) */ |
| 2242 |
void page_zip_rec_set_owned(page_zip_des_t *page_zip, const byte *rec, |
| 2243 |
ulint flag) { |
| 2244 |
byte *slot = page_zip_dir_find(page_zip, page_offset(rec)); |
| 2245 |
ut_a(slot); |
| 2246 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2247 |
if (flag) { |
| 2248 |
*slot |= (PAGE_ZIP_DIR_SLOT_OWNED >> 8); |
| 2249 |
} else { |
| 2250 |
*slot &= ~(PAGE_ZIP_DIR_SLOT_OWNED >> 8); |
| 2251 |
} |
| 2252 |
} |
| 2253 |
|
| 2254 |
/** Insert a record to the dense page directory. |
| 2255 |
@param[in,out] page_zip Compressed page |
| 2256 |
@param[in] prev_rec Record after which to insert |
| 2257 |
@param[in] free_rec Record from which rec was allocated, or null |
| 2258 |
@param[in] rec Record to insert */ |
| 2259 |
void page_zip_dir_insert(page_zip_des_t *page_zip, const byte *prev_rec, |
| 2260 |
const byte *free_rec, byte *rec) { |
| 2261 |
ulint n_dense; |
| 2262 |
byte *slot_rec; |
| 2263 |
byte *slot_free; |
| 2264 |
|
| 2265 |
ut_ad(prev_rec != rec); |
| 2266 |
ut_ad(page_rec_get_next((rec_t *)prev_rec) == rec); |
| 2267 |
ut_ad(page_zip_simple_validate(page_zip)); |
| 2268 |
|
| 2269 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2270 |
|
| 2271 |
if (page_rec_is_infimum(prev_rec)) { |
| 2272 |
/* Use the first slot. */ |
| 2273 |
slot_rec = page_zip->data + page_zip_get_size(page_zip); |
| 2274 |
} else { |
| 2275 |
byte *end = page_zip->data + page_zip_get_size(page_zip); |
| 2276 |
byte *start = end - page_zip_dir_user_size(page_zip); |
| 2277 |
|
| 2278 |
if (UNIV_LIKELY(!free_rec)) { |
| 2279 |
/* PAGE_N_RECS was already incremented |
| 2280 |
in page_cur_insert_rec_zip(), but the |
| 2281 |
dense directory slot at that position |
| 2282 |
contains garbage. Skip it. */ |
| 2283 |
start += PAGE_ZIP_DIR_SLOT_SIZE; |
| 2284 |
} |
| 2285 |
|
| 2286 |
slot_rec = page_zip_dir_find_low(start, end, page_offset(prev_rec)); |
| 2287 |
ut_a(slot_rec); |
| 2288 |
} |
| 2289 |
|
| 2290 |
/* Read the old n_dense (n_heap may have been incremented). */ |
| 2291 |
n_dense = page_dir_get_n_heap(page_zip->data) - (PAGE_HEAP_NO_USER_LOW + 1); |
| 2292 |
|
| 2293 |
if (UNIV_LIKELY_NULL(free_rec)) { |
| 2294 |
/* The record was allocated from the free list. |
| 2295 |
Shift the dense directory only up to that slot. |
| 2296 |
Note that in this case, n_dense is actually |
| 2297 |
off by one, because page_cur_insert_rec_zip() |
| 2298 |
did not increment n_heap. */ |
| 2299 |
ut_ad(rec_get_heap_no_new(rec) < n_dense + 1 + PAGE_HEAP_NO_USER_LOW); |
| 2300 |
ut_ad(rec >= free_rec); |
| 2301 |
slot_free = page_zip_dir_find(page_zip, page_offset(free_rec)); |
| 2302 |
ut_ad(slot_free); |
| 2303 |
slot_free += PAGE_ZIP_DIR_SLOT_SIZE; |
| 2304 |
} else { |
| 2305 |
/* The record was allocated from the heap. |
| 2306 |
Shift the entire dense directory. */ |
| 2307 |
ut_ad(rec_get_heap_no_new(rec) == n_dense + PAGE_HEAP_NO_USER_LOW); |
| 2308 |
|
| 2309 |
/* Shift to the end of the dense page directory. */ |
| 2310 |
slot_free = page_zip->data + page_zip_get_size(page_zip) - |
| 2311 |
PAGE_ZIP_DIR_SLOT_SIZE * n_dense; |
| 2312 |
} |
| 2313 |
|
| 2314 |
/* Shift the dense directory to allocate place for rec. */ |
| 2315 |
memmove(slot_free - PAGE_ZIP_DIR_SLOT_SIZE, slot_free, slot_rec - slot_free); |
| 2316 |
|
| 2317 |
/* Write the entry for the inserted record. |
| 2318 |
The "owned" and "deleted" flags must be zero. */ |
| 2319 |
mach_write_to_2(slot_rec - PAGE_ZIP_DIR_SLOT_SIZE, page_offset(rec)); |
| 2320 |
} |
| 2321 |
|
| 2322 |
/** Shift the dense page directory and the array of BLOB pointers when a record |
| 2323 |
is deleted. |
| 2324 |
@param[in,out] page_zip compressed page |
| 2325 |
@param[in] rec deleted record |
| 2326 |
@param[in] index index of rec |
| 2327 |
@param[in] offsets rec_get_offsets(rec) |
| 2328 |
@param[in] free previous start of the free list */ |
| 2329 |
void page_zip_dir_delete(page_zip_des_t *page_zip, byte *rec, |
| 2330 |
const dict_index_t *index, const ulint *offsets, |
| 2331 |
const byte *free) { |
| 2332 |
byte *slot_rec; |
| 2333 |
byte *slot_free; |
| 2334 |
ulint n_ext; |
| 2335 |
page_t *page = page_align(rec); |
| 2336 |
|
| 2337 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 2338 |
ut_ad(rec_offs_comp(offsets)); |
| 2339 |
|
| 2340 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2341 |
UNIV_MEM_ASSERT_RW(rec, rec_offs_data_size(offsets)); |
| 2342 |
UNIV_MEM_ASSERT_RW(rec - rec_offs_extra_size(offsets), |
| 2343 |
rec_offs_extra_size(offsets)); |
| 2344 |
|
| 2345 |
slot_rec = page_zip_dir_find(page_zip, page_offset(rec)); |
| 2346 |
|
| 2347 |
ut_a(slot_rec); |
| 2348 |
|
| 2349 |
/* This could not be done before page_zip_dir_find(). */ |
| 2350 |
page_header_set_field(page, page_zip, PAGE_N_RECS, |
| 2351 |
(ulint)(page_get_n_recs(page) - 1)); |
| 2352 |
|
| 2353 |
if (UNIV_UNLIKELY(!free)) { |
| 2354 |
/* Make the last slot the start of the free list. */ |
| 2355 |
slot_free = page_zip->data + page_zip_get_size(page_zip) - |
| 2356 |
PAGE_ZIP_DIR_SLOT_SIZE * (page_dir_get_n_heap(page_zip->data) - |
| 2357 |
PAGE_HEAP_NO_USER_LOW); |
| 2358 |
} else { |
| 2359 |
slot_free = page_zip_dir_find_free(page_zip, page_offset(free)); |
| 2360 |
ut_a(slot_free < slot_rec); |
| 2361 |
/* Grow the free list by one slot by moving the start. */ |
| 2362 |
slot_free += PAGE_ZIP_DIR_SLOT_SIZE; |
| 2363 |
} |
| 2364 |
|
| 2365 |
if (UNIV_LIKELY(slot_rec > slot_free)) { |
| 2366 |
memmove(slot_free + PAGE_ZIP_DIR_SLOT_SIZE, slot_free, |
| 2367 |
slot_rec - slot_free); |
| 2368 |
} |
| 2369 |
|
| 2370 |
/* Write the entry for the deleted record. |
| 2371 |
The "owned" and "deleted" flags will be cleared. */ |
| 2372 |
mach_write_to_2(slot_free, page_offset(rec)); |
| 2373 |
|
| 2374 |
if (!page_is_leaf(page) || !index->is_clustered()) { |
| 2375 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 2376 |
goto skip_blobs; |
| 2377 |
} |
| 2378 |
|
| 2379 |
n_ext = rec_offs_n_extern(index, offsets); |
| 2380 |
if (UNIV_UNLIKELY(n_ext)) { |
| 2381 |
/* Shift and zero fill the array of BLOB pointers. */ |
| 2382 |
ulint blob_no; |
| 2383 |
byte *externs; |
| 2384 |
byte *ext_end; |
| 2385 |
|
| 2386 |
blob_no = page_zip_get_n_prev_extern(page_zip, rec, index); |
| 2387 |
ut_a(blob_no + n_ext <= page_zip->n_blobs); |
| 2388 |
|
| 2389 |
externs = page_zip->data + page_zip_get_size(page_zip) - |
| 2390 |
(page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW) * |
| 2391 |
PAGE_ZIP_CLUST_LEAF_SLOT_SIZE; |
| 2392 |
|
| 2393 |
ext_end = externs - page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE; |
| 2394 |
externs -= blob_no * BTR_EXTERN_FIELD_REF_SIZE; |
| 2395 |
|
| 2396 |
page_zip->n_blobs -= static_cast<unsigned>(n_ext); |
| 2397 |
/* Shift and zero fill the array. */ |
| 2398 |
memmove(ext_end + n_ext * BTR_EXTERN_FIELD_REF_SIZE, ext_end, |
| 2399 |
(page_zip->n_blobs - blob_no) * BTR_EXTERN_FIELD_REF_SIZE); |
| 2400 |
memset(ext_end, 0, n_ext * BTR_EXTERN_FIELD_REF_SIZE); |
| 2401 |
} |
| 2402 |
|
| 2403 |
skip_blobs: |
| 2404 |
/* The compression algorithm expects info_bits and n_owned |
| 2405 |
to be 0 for deleted records. */ |
| 2406 |
rec[-REC_N_NEW_EXTRA_BYTES] = 0; /* info_bits and n_owned */ |
| 2407 |
|
| 2408 |
page_zip_clear_rec(page_zip, rec, index, offsets); |
| 2409 |
} |
| 2410 |
|
| 2411 |
/** Add a slot to the dense page directory. |
| 2412 |
@param[in,out] page_zip Compressed page |
| 2413 |
@param[in] is_clustered Nonzero for clustered index, zero for others */ |
| 2414 |
void page_zip_dir_add_slot(page_zip_des_t *page_zip, bool is_clustered) { |
| 2415 |
ulint n_dense; |
| 2416 |
byte *dir; |
| 2417 |
byte *stored; |
| 2418 |
|
| 2419 |
ut_ad(page_is_comp(page_zip->data)); |
| 2420 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2421 |
|
| 2422 |
/* Read the old n_dense (n_heap has already been incremented). */ |
| 2423 |
n_dense = page_dir_get_n_heap(page_zip->data) - (PAGE_HEAP_NO_USER_LOW + 1); |
| 2424 |
|
| 2425 |
dir = page_zip->data + page_zip_get_size(page_zip) - |
| 2426 |
PAGE_ZIP_DIR_SLOT_SIZE * n_dense; |
| 2427 |
|
| 2428 |
if (!page_is_leaf(page_zip->data)) { |
| 2429 |
ut_ad(!page_zip->n_blobs); |
| 2430 |
stored = dir - n_dense * REC_NODE_PTR_SIZE; |
| 2431 |
} else if (is_clustered) { |
| 2432 |
/* Move the BLOB pointer array backwards to make space for the |
| 2433 |
roll_ptr and trx_id columns and the dense directory slot. */ |
| 2434 |
byte *externs; |
| 2435 |
|
| 2436 |
stored = dir - n_dense * (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN); |
| 2437 |
externs = stored - page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE; |
| 2438 |
ASSERT_ZERO(externs - PAGE_ZIP_CLUST_LEAF_SLOT_SIZE, |
| 2439 |
PAGE_ZIP_CLUST_LEAF_SLOT_SIZE); |
| 2440 |
memmove(externs - PAGE_ZIP_CLUST_LEAF_SLOT_SIZE, externs, stored - externs); |
| 2441 |
} else { |
| 2442 |
stored = dir - page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE; |
| 2443 |
ASSERT_ZERO(stored - PAGE_ZIP_DIR_SLOT_SIZE, |
| 2444 |
static_cast<size_t>(PAGE_ZIP_DIR_SLOT_SIZE)); |
| 2445 |
} |
| 2446 |
|
| 2447 |
/* Move the uncompressed area backwards to make space |
| 2448 |
for one directory slot. */ |
| 2449 |
memmove(stored - PAGE_ZIP_DIR_SLOT_SIZE, stored, dir - stored); |
| 2450 |
} |
| 2451 |
|
| 2452 |
/** Parses a log record of writing to the header of a page. |
| 2453 |
@return end of log record or NULL */ |
| 2454 |
byte *page_zip_parse_write_header( |
| 2455 |
byte *ptr, /*!< in: redo log buffer */ |
| 2456 |
byte *end_ptr, /*!< in: redo log buffer end */ |
| 2457 |
page_t *page, /*!< in/out: uncompressed page */ |
| 2458 |
page_zip_des_t *page_zip) /*!< in/out: compressed page */ |
| 2459 |
{ |
| 2460 |
ulint offset; |
| 2461 |
ulint len; |
| 2462 |
|
| 2463 |
ut_ad(ptr != nullptr); |
| 2464 |
ut_ad(end_ptr != nullptr); |
| 2465 |
ut_ad(!page == !page_zip); |
| 2466 |
|
| 2467 |
if (UNIV_UNLIKELY(end_ptr < ptr + (1 + 1))) { |
| 2468 |
return (nullptr); |
| 2469 |
} |
| 2470 |
|
| 2471 |
offset = (ulint)*ptr++; |
| 2472 |
len = (ulint)*ptr++; |
| 2473 |
|
| 2474 |
if (len == 0 || offset + len >= PAGE_DATA) { |
| 2475 |
corrupt: |
| 2476 |
recv_sys->found_corrupt_log = true; |
| 2477 |
|
| 2478 |
return (nullptr); |
| 2479 |
} |
| 2480 |
|
| 2481 |
if (end_ptr < ptr + len) { |
| 2482 |
return (nullptr); |
| 2483 |
} |
| 2484 |
|
| 2485 |
if (page) { |
| 2486 |
if (!page_zip) { |
| 2487 |
goto corrupt; |
| 2488 |
} |
| 2489 |
#ifdef UNIV_ZIP_DEBUG |
| 2490 |
ut_a(page_zip_validate(page_zip, page, nullptr)); |
| 2491 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2492 |
|
| 2493 |
memcpy(page + offset, ptr, len); |
| 2494 |
memcpy(page_zip->data + offset, ptr, len); |
| 2495 |
|
| 2496 |
#ifdef UNIV_ZIP_DEBUG |
| 2497 |
ut_a(page_zip_validate(page_zip, page, nullptr)); |
| 2498 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2499 |
} |
| 2500 |
|
| 2501 |
return (ptr + len); |
| 2502 |
} |
| 2503 |
|
| 2504 |
#ifndef UNIV_HOTBACKUP |
| 2505 |
/** Write a log record of writing to the uncompressed header portion of a page. |
| 2506 |
*/ |
| 2507 |
void page_zip_write_header_log( |
| 2508 |
const byte *data, /*!< in: data on the uncompressed page */ |
| 2509 |
ulint length, /*!< in: length of the data */ |
| 2510 |
mtr_t *mtr) /*!< in: mini-transaction */ |
| 2511 |
{ |
| 2512 |
ulint offset = page_offset(data); |
| 2513 |
|
| 2514 |
ut_ad(offset < PAGE_DATA); |
| 2515 |
ut_ad(offset + length < PAGE_DATA); |
| 2516 |
|
| 2517 |
static_assert(PAGE_DATA <= 255, "PAGE_DATA > 255"); |
| 2518 |
|
| 2519 |
ut_ad(length < 256); |
| 2520 |
|
| 2521 |
byte *log_ptr = nullptr; |
| 2522 |
/* If no logging is requested, we may return now */ |
| 2523 |
if (!mlog_open(mtr, 11 + 1 + 1, log_ptr)) { |
| 2524 |
return; |
| 2525 |
} |
| 2526 |
|
| 2527 |
log_ptr = mlog_write_initial_log_record_fast( |
| 2528 |
(byte *)data, MLOG_ZIP_WRITE_HEADER, log_ptr, mtr); |
| 2529 |
*log_ptr++ = (byte)offset; |
| 2530 |
*log_ptr++ = (byte)length; |
| 2531 |
mlog_close(mtr, log_ptr); |
| 2532 |
|
| 2533 |
mlog_catenate_string(mtr, data, length); |
| 2534 |
} |
| 2535 |
#endif /* !UNIV_HOTBACKUP */ |
| 2536 |
|
| 2537 |
/** Reorganize and compress a page. This is a low-level operation for |
| 2538 |
compressed pages, to be used when page_zip_compress() fails. |
| 2539 |
On success, a redo log entry MLOG_ZIP_PAGE_COMPRESS will be written. |
| 2540 |
The function btr_page_reorganize() should be preferred whenever possible. |
| 2541 |
IMPORTANT: if page_zip_reorganize() is invoked on a leaf page of a |
| 2542 |
non-clustered index, the caller must update the insert buffer free |
| 2543 |
bits in the same mini-transaction in such a way that the modification |
| 2544 |
will be redo-logged. |
| 2545 |
@return true on success, false on failure; page_zip will be left |
| 2546 |
intact on failure, but page will be overwritten. */ |
| 2547 |
bool page_zip_reorganize( |
| 2548 |
buf_block_t *block, /*!< in/out: page with compressed page; |
| 2549 |
on the compressed page, in: size; |
| 2550 |
out: data, n_blobs, |
| 2551 |
m_start, m_end, m_nonempty */ |
| 2552 |
dict_index_t *index, /*!< in: index of the B-tree node */ |
| 2553 |
mtr_t *mtr) /*!< in: mini-transaction */ |
| 2554 |
{ |
| 2555 |
#ifndef UNIV_HOTBACKUP |
| 2556 |
buf_pool_t *buf_pool = buf_pool_from_block(block); |
| 2557 |
#endif /* !UNIV_HOTBACKUP */ |
| 2558 |
page_zip_des_t *page_zip = buf_block_get_page_zip(block); |
| 2559 |
page_t *page = buf_block_get_frame(block); |
| 2560 |
buf_block_t *temp_block; |
| 2561 |
page_t *temp_page; |
| 2562 |
|
| 2563 |
#ifndef UNIV_HOTBACKUP |
| 2564 |
ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX)); |
| 2565 |
#endif /* !UNIV_HOTBACKUP */ |
| 2566 |
ut_ad(page_is_comp(page)); |
| 2567 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2568 |
ut_ad(!index->table->is_temporary()); |
| 2569 |
/* Note that page_zip_validate(page_zip, page, index) may fail here. */ |
| 2570 |
UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE); |
| 2571 |
UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip)); |
| 2572 |
|
| 2573 |
/* Disable logging */ |
| 2574 |
mtr_log_t log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE); |
| 2575 |
|
| 2576 |
#ifndef UNIV_HOTBACKUP |
| 2577 |
temp_block = buf_block_alloc(buf_pool); |
| 2578 |
btr_search_drop_page_hash_index(block); |
| 2579 |
#else /* !UNIV_HOTBACKUP */ |
| 2580 |
temp_block = back_block2; |
| 2581 |
#endif /* !UNIV_HOTBACKUP */ |
| 2582 |
temp_page = temp_block->frame; |
| 2583 |
|
| 2584 |
/* Copy the old page to temporary space */ |
| 2585 |
buf_frame_copy(temp_page, page); |
| 2586 |
|
| 2587 |
/* Recreate the page: note that global data on page (possible |
| 2588 |
segment headers, next page-field, etc.) is preserved intact */ |
| 2589 |
|
| 2590 |
page_create(block, mtr, true, fil_page_get_type(page)); |
| 2591 |
|
| 2592 |
/* Copy the records from the temporary space to the recreated page; |
| 2593 |
do not copy the lock bits yet */ |
| 2594 |
|
| 2595 |
page_copy_rec_list_end_no_locks(block, temp_block, |
| 2596 |
page_get_infimum_rec(temp_page), index, mtr); |
| 2597 |
|
| 2598 |
/* Temp-Tables are not shared across connection and so we avoid |
| 2599 |
locking of temp-tables as there would be no 2 trx trying to |
| 2600 |
operate on same temp-table in parallel. |
| 2601 |
max_trx_id is use to track which all trxs wrote to the page |
| 2602 |
in parallel but in case of temp-table this can is not needed. */ |
| 2603 |
if (!index->is_clustered() && page_is_leaf(temp_page)) { |
| 2604 |
/* Copy max trx id to recreated page */ |
| 2605 |
trx_id_t max_trx_id = page_get_max_trx_id(temp_page); |
| 2606 |
page_set_max_trx_id(block, nullptr, max_trx_id, nullptr); |
| 2607 |
ut_ad(max_trx_id != 0); |
| 2608 |
} |
| 2609 |
|
| 2610 |
/* Restore logging. */ |
| 2611 |
mtr_set_log_mode(mtr, log_mode); |
| 2612 |
|
| 2613 |
if (!page_zip_compress(page_zip, page, index, page_zip_level, mtr)) { |
| 2614 |
#ifndef UNIV_HOTBACKUP |
| 2615 |
buf_block_free(temp_block); |
| 2616 |
#endif /* !UNIV_HOTBACKUP */ |
| 2617 |
return false; |
| 2618 |
} |
| 2619 |
|
| 2620 |
lock_move_reorganize_page(block, temp_block); |
| 2621 |
|
| 2622 |
#ifndef UNIV_HOTBACKUP |
| 2623 |
buf_block_free(temp_block); |
| 2624 |
#endif /* !UNIV_HOTBACKUP */ |
| 2625 |
return true; |
| 2626 |
} |
| 2627 |
|
| 2628 |
#ifndef UNIV_HOTBACKUP |
| 2629 |
/** Copy the records of a page byte for byte. Do not copy the page header |
| 2630 |
or trailer, except those B-tree header fields that are directly |
| 2631 |
related to the storage of records. Also copy PAGE_MAX_TRX_ID. |
| 2632 |
NOTE: The caller must update the lock table and the adaptive hash index. */ |
| 2633 |
void page_zip_copy_recs( |
| 2634 |
page_zip_des_t *page_zip, /*!< out: copy of src_zip |
| 2635 |
(n_blobs, m_start, m_end, |
| 2636 |
m_nonempty, data[0..size-1]) */ |
| 2637 |
page_t *page, /*!< out: copy of src */ |
| 2638 |
const page_zip_des_t *src_zip, /*!< in: compressed page */ |
| 2639 |
const page_t *src, /*!< in: page */ |
| 2640 |
dict_index_t *index, /*!< in: index of the B-tree */ |
| 2641 |
mtr_t *mtr) /*!< in: mini-transaction */ |
| 2642 |
{ |
| 2643 |
ut_ad(!index->table->is_temporary()); |
| 2644 |
ut_ad(mtr_memo_contains_page(mtr, page, MTR_MEMO_PAGE_X_FIX)); |
| 2645 |
ut_ad(mtr_memo_contains_page(mtr, src, MTR_MEMO_PAGE_X_FIX)); |
| 2646 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2647 |
#ifdef UNIV_ZIP_DEBUG |
| 2648 |
/* The B-tree operations that call this function may set |
| 2649 |
FIL_PAGE_PREV or PAGE_LEVEL, causing a temporary min_rec_flag |
| 2650 |
mismatch. A strict page_zip_validate() will be executed later |
| 2651 |
during the B-tree operations. */ |
| 2652 |
ut_a(page_zip_validate_low(src_zip, src, index, true)); |
| 2653 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2654 |
ut_a(page_zip_get_size(page_zip) == page_zip_get_size(src_zip)); |
| 2655 |
if (UNIV_UNLIKELY(src_zip->n_blobs)) { |
| 2656 |
ut_a(page_is_leaf(src)); |
| 2657 |
ut_a(index->is_clustered()); |
| 2658 |
} |
| 2659 |
|
| 2660 |
/* The PAGE_MAX_TRX_ID must be set on leaf pages of secondary |
| 2661 |
indexes. It does not matter on other pages. */ |
| 2662 |
ut_a(index->is_clustered() || !page_is_leaf(src) || page_get_max_trx_id(src)); |
| 2663 |
|
| 2664 |
UNIV_MEM_ASSERT_W(page, UNIV_PAGE_SIZE); |
| 2665 |
UNIV_MEM_ASSERT_W(page_zip->data, page_zip_get_size(page_zip)); |
| 2666 |
UNIV_MEM_ASSERT_RW(src, UNIV_PAGE_SIZE); |
| 2667 |
UNIV_MEM_ASSERT_RW(src_zip->data, page_zip_get_size(page_zip)); |
| 2668 |
|
| 2669 |
/* Copy those B-tree page header fields that are related to |
| 2670 |
the records stored in the page. Also copy the field |
| 2671 |
PAGE_MAX_TRX_ID. Skip the rest of the page header and |
| 2672 |
trailer. On the compressed page, there is no trailer. */ |
| 2673 |
static_assert(PAGE_MAX_TRX_ID + 8 == PAGE_HEADER_PRIV_END, |
| 2674 |
"PAGE_MAX_TRX_ID + 8 != PAGE_HEADER_PRIV_END"); |
| 2675 |
memcpy(PAGE_HEADER + page, PAGE_HEADER + src, PAGE_HEADER_PRIV_END); |
| 2676 |
memcpy(PAGE_DATA + page, PAGE_DATA + src, |
| 2677 |
UNIV_PAGE_SIZE - PAGE_DATA - FIL_PAGE_DATA_END); |
| 2678 |
memcpy(PAGE_HEADER + page_zip->data, PAGE_HEADER + src_zip->data, |
| 2679 |
PAGE_HEADER_PRIV_END); |
| 2680 |
memcpy(PAGE_DATA + page_zip->data, PAGE_DATA + src_zip->data, |
| 2681 |
page_zip_get_size(page_zip) - PAGE_DATA); |
| 2682 |
|
| 2683 |
/* Copy all fields of src_zip to page_zip, except the pointer |
| 2684 |
to the compressed data page. */ |
| 2685 |
{ |
| 2686 |
page_zip_t *data = page_zip->data; |
| 2687 |
memcpy(page_zip, src_zip, sizeof *page_zip); |
| 2688 |
page_zip->data = data; |
| 2689 |
} |
| 2690 |
ut_ad(page_zip_get_trailer_len(page_zip, index->is_clustered()) + |
| 2691 |
page_zip->m_end < |
| 2692 |
page_zip_get_size(page_zip)); |
| 2693 |
|
| 2694 |
if (!page_is_leaf(src) && |
| 2695 |
UNIV_UNLIKELY(mach_read_from_4(src + FIL_PAGE_PREV) == FIL_NULL) && |
| 2696 |
UNIV_LIKELY(mach_read_from_4(page + FIL_PAGE_PREV) != FIL_NULL)) { |
| 2697 |
/* Clear the REC_INFO_MIN_REC_FLAG of the first user record. */ |
| 2698 |
ulint offs = rec_get_next_offs(page + PAGE_NEW_INFIMUM, true); |
| 2699 |
if (UNIV_LIKELY(offs != PAGE_NEW_SUPREMUM)) { |
| 2700 |
rec_t *rec = page + offs; |
| 2701 |
ut_a(rec[-REC_N_NEW_EXTRA_BYTES] & REC_INFO_MIN_REC_FLAG); |
| 2702 |
rec[-REC_N_NEW_EXTRA_BYTES] &= ~REC_INFO_MIN_REC_FLAG; |
| 2703 |
} |
| 2704 |
} |
| 2705 |
|
| 2706 |
#ifdef UNIV_ZIP_DEBUG |
| 2707 |
ut_a(page_zip_validate(page_zip, page, index)); |
| 2708 |
#endif /* UNIV_ZIP_DEBUG */ |
| 2709 |
page_zip_compress_write_log(page_zip, page, index, mtr); |
| 2710 |
} |
| 2711 |
#endif /* !UNIV_HOTBACKUP */ |
| 2712 |
|
| 2713 |
/** Parses a log record of compressing an index page. |
| 2714 |
@return end of log record or NULL */ |
| 2715 |
byte *page_zip_parse_compress( |
| 2716 |
byte *ptr, /*!< in: buffer */ |
| 2717 |
byte *end_ptr, /*!< in: buffer end */ |
| 2718 |
page_t *page, /*!< out: uncompressed page */ |
| 2719 |
page_zip_des_t *page_zip) /*!< out: compressed page */ |
| 2720 |
{ |
| 2721 |
ulint size; |
| 2722 |
ulint trailer_size; |
| 2723 |
|
| 2724 |
ut_ad(ptr != nullptr); |
| 2725 |
ut_ad(end_ptr != nullptr); |
| 2726 |
ut_ad(!page == !page_zip); |
| 2727 |
|
| 2728 |
if (UNIV_UNLIKELY(ptr + (2 + 2) > end_ptr)) { |
| 2729 |
return (nullptr); |
| 2730 |
} |
| 2731 |
|
| 2732 |
size = mach_read_from_2(ptr); |
| 2733 |
ptr += 2; |
| 2734 |
trailer_size = mach_read_from_2(ptr); |
| 2735 |
ptr += 2; |
| 2736 |
|
| 2737 |
if (UNIV_UNLIKELY(ptr + 8 + size + trailer_size > end_ptr)) { |
| 2738 |
return (nullptr); |
| 2739 |
} |
| 2740 |
|
| 2741 |
if (page) { |
| 2742 |
if (!page_zip || page_zip_get_size(page_zip) < size) { |
| 2743 |
corrupt: |
| 2744 |
recv_sys->found_corrupt_log = true; |
| 2745 |
|
| 2746 |
return (nullptr); |
| 2747 |
} |
| 2748 |
|
| 2749 |
memcpy(page_zip->data + FIL_PAGE_PREV, ptr, 4); |
| 2750 |
memcpy(page_zip->data + FIL_PAGE_NEXT, ptr + 4, 4); |
| 2751 |
memcpy(page_zip->data + FIL_PAGE_TYPE, ptr + 8, size); |
| 2752 |
memset(page_zip->data + FIL_PAGE_TYPE + size, 0, |
| 2753 |
page_zip_get_size(page_zip) - trailer_size - (FIL_PAGE_TYPE + size)); |
| 2754 |
memcpy(page_zip->data + page_zip_get_size(page_zip) - trailer_size, |
| 2755 |
ptr + 8 + size, trailer_size); |
| 2756 |
|
| 2757 |
if (UNIV_UNLIKELY(!page_zip_decompress(page_zip, page, true))) { |
| 2758 |
goto corrupt; |
| 2759 |
} |
| 2760 |
} |
| 2761 |
|
| 2762 |
return (ptr + 8 + size + trailer_size); |
| 2763 |
} |