| 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 include/page0zip.h |
| 30 |
Compressed page interface |
| 31 |
|
| 32 |
Created June 2005 by Marko Makela |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef page0zip_h |
| 36 |
#define page0zip_h |
| 37 |
|
| 38 |
#include <sys/types.h> |
| 39 |
#include <zlib.h> |
| 40 |
|
| 41 |
#include "buf0buf.h" |
| 42 |
#include "buf0checksum.h" |
| 43 |
#include "buf0types.h" |
| 44 |
#include "data0type.h" |
| 45 |
#include "dict0types.h" |
| 46 |
#include "mach0data.h" |
| 47 |
#include "mem0mem.h" |
| 48 |
#include "mtr0types.h" |
| 49 |
#include "page/zipdecompress.h" |
| 50 |
#include "page0types.h" |
| 51 |
#include "srv0srv.h" |
| 52 |
#include "trx0types.h" |
| 53 |
#include "univ.i" |
| 54 |
#include "ut0crc32.h" |
| 55 |
|
| 56 |
/* Compression level to be used by zlib. Settable by user. */ |
| 57 |
extern uint page_zip_level; |
| 58 |
|
| 59 |
/* Default compression level. */ |
| 60 |
constexpr uint32_t DEFAULT_COMPRESSION_LEVEL = 6; |
| 61 |
/** Start offset of the area that will be compressed */ |
| 62 |
#define PAGE_ZIP_START PAGE_NEW_SUPREMUM_END |
| 63 |
/** Predefine the sum of DIR_SLOT, TRX_ID & ROLL_PTR */ |
| 64 |
constexpr uint32_t PAGE_ZIP_CLUST_LEAF_SLOT_SIZE = |
| 65 |
PAGE_ZIP_DIR_SLOT_SIZE + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; |
| 66 |
/** Mask of record offsets */ |
| 67 |
constexpr uint32_t PAGE_ZIP_DIR_SLOT_MASK = 0x3fff; |
| 68 |
/** 'owned' flag */ |
| 69 |
constexpr uint32_t PAGE_ZIP_DIR_SLOT_OWNED = 0x4000; |
| 70 |
/** 'deleted' flag */ |
| 71 |
constexpr uint32_t PAGE_ZIP_DIR_SLOT_DEL = 0x8000; |
| 72 |
|
| 73 |
/* Whether or not to log compressed page images to avoid possible |
| 74 |
compression algorithm changes in zlib. */ |
| 75 |
extern bool page_zip_log_pages; |
| 76 |
|
| 77 |
/** Set the size of a compressed page in bytes. |
| 78 |
@param[in,out] page_zip compressed page |
| 79 |
@param[in] size size in bytes */ |
| 80 |
static inline void page_zip_set_size(page_zip_des_t *page_zip, ulint size); |
| 81 |
|
| 82 |
#ifndef UNIV_HOTBACKUP |
| 83 |
/** Determine if a record is so big that it needs to be stored externally. |
| 84 |
@param[in] rec_size length of the record in bytes |
| 85 |
@param[in] comp nonzero=compact format |
| 86 |
@param[in] n_fields number of fields in the record; ignored if |
| 87 |
tablespace is not compressed |
| 88 |
@param[in] page_size page size |
| 89 |
@return false if the entire record can be stored locally on the page */ |
| 90 |
[[nodiscard]] static inline bool page_zip_rec_needs_ext( |
| 91 |
ulint rec_size, ulint comp, ulint n_fields, const page_size_t &page_size); |
| 92 |
#endif /* !UNIV_HOTBACKUP */ |
| 93 |
|
| 94 |
/** Determine the guaranteed free space on an empty page. |
| 95 |
@param[in] n_fields number of columns in the index |
| 96 |
@param[in] zip_size compressed page size in bytes |
| 97 |
@return minimum payload size on the page */ |
| 98 |
ulint page_zip_empty_size(ulint n_fields, ulint zip_size); |
| 99 |
|
| 100 |
#ifndef UNIV_HOTBACKUP |
| 101 |
/** Check whether a tuple is too big for compressed table |
| 102 |
@param[in] index dict index object |
| 103 |
@param[in] entry entry for the index |
| 104 |
@return true if it's too big, otherwise false */ |
| 105 |
bool page_zip_is_too_big(const dict_index_t *index, const dtuple_t *entry); |
| 106 |
#endif /* !UNIV_HOTBACKUP */ |
| 107 |
|
| 108 |
/** Initialize a compressed page descriptor. */ |
| 109 |
static inline void page_zip_des_init( |
| 110 |
page_zip_des_t *page_zip); /*!< in/out: compressed page |
| 111 |
descriptor */ |
| 112 |
|
| 113 |
/** Configure the zlib allocator to use the given memory heap. |
| 114 |
@param[in,out] stream zlib stream |
| 115 |
@param[in] heap Memory heap to use */ |
| 116 |
void page_zip_set_alloc(void *stream, mem_heap_t *heap); |
| 117 |
|
| 118 |
/** Compress a page. |
| 119 |
@return true on success, false on failure; page_zip will be left |
| 120 |
intact on failure. */ |
| 121 |
bool page_zip_compress(page_zip_des_t *page_zip, /*!< in: size; out: data, |
| 122 |
n_blobs, m_start, m_end, |
| 123 |
m_nonempty */ |
| 124 |
const page_t *page, /*!< in: uncompressed page */ |
| 125 |
dict_index_t *index, /*!< in: index tree */ |
| 126 |
ulint level, /*!< in: compression level */ |
| 127 |
mtr_t *mtr); /*!< in/out: mini-transaction, |
| 128 |
or NULL */ |
| 129 |
|
| 130 |
/** Write the index information for the compressed page. |
| 131 |
@return used size of buf */ |
| 132 |
ulint page_zip_fields_encode( |
| 133 |
ulint n, /*!< in: number of fields |
| 134 |
to compress */ |
| 135 |
const dict_index_t *index, /*!< in: index comprising |
| 136 |
at least n fields */ |
| 137 |
ulint trx_id_pos, |
| 138 |
/*!< in: position of the trx_id column |
| 139 |
in the index, or ULINT_UNDEFINED if |
| 140 |
this is a non-leaf page */ |
| 141 |
byte *buf); /*!< out: buffer of (n + 1) * 2 bytes */ |
| 142 |
|
| 143 |
/** Decompress a page. This function should tolerate errors on the compressed |
| 144 |
page. Instead of letting assertions fail, it will return false if an |
| 145 |
inconsistency is detected. |
| 146 |
@return true on success, false on failure */ |
| 147 |
bool page_zip_decompress( |
| 148 |
page_zip_des_t *page_zip, /*!< in: data, ssize; |
| 149 |
out: m_start, m_end, m_nonempty, n_blobs */ |
| 150 |
page_t *page, /*!< out: uncompressed page, may be trashed */ |
| 151 |
bool all); /*!< in: true=decompress the whole page; |
| 152 |
false=verify but do not copy some |
| 153 |
page header fields that should not change |
| 154 |
after page creation */ |
| 155 |
|
| 156 |
#ifdef UNIV_ZIP_DEBUG |
| 157 |
/** Check that the compressed and decompressed pages match. |
| 158 |
@return true if valid, false if not */ |
| 159 |
bool page_zip_validate_low( |
| 160 |
const page_zip_des_t *page_zip, /*!< in: compressed page */ |
| 161 |
const page_t *page, /*!< in: uncompressed page */ |
| 162 |
const dict_index_t *index, /*!< in: index of the page, if known */ |
| 163 |
bool sloppy); /*!< in: false=strict, |
| 164 |
true=ignore the MIN_REC_FLAG */ |
| 165 |
/** Check that the compressed and decompressed pages match. */ |
| 166 |
bool page_zip_validate( |
| 167 |
const page_zip_des_t *page_zip, /*!< in: compressed page */ |
| 168 |
const page_t *page, /*!< in: uncompressed page */ |
| 169 |
const dict_index_t *index); /*!< in: index of the page, if known */ |
| 170 |
#endif /* UNIV_ZIP_DEBUG */ |
| 171 |
|
| 172 |
/** Determine how big record can be inserted without re-compressing the page. |
| 173 |
@param[in] page_zip Compressed page. |
| 174 |
@param[in] is_clust True if clustered index. |
| 175 |
@return a positive number indicating the maximum size of a record |
| 176 |
whose insertion is guaranteed to succeed, or zero or negative */ |
| 177 |
[[nodiscard]] static inline lint page_zip_max_ins_size( |
| 178 |
const page_zip_des_t *page_zip, bool is_clust); |
| 179 |
|
| 180 |
/** Determine if enough space is available in the modification log. |
| 181 |
@param[in] page_zip Compressed page. |
| 182 |
@param[in] is_clust True if clustered index. |
| 183 |
@param[in] length Combined size of the record. |
| 184 |
@param[in] create Nonzero=add the record to the heap. |
| 185 |
@return true if page_zip_write_rec() will succeed */ |
| 186 |
[[nodiscard]] static inline bool page_zip_available( |
| 187 |
const page_zip_des_t *page_zip, bool is_clust, ulint length, ulint create); |
| 188 |
|
| 189 |
/** Write data to the uncompressed header portion of a page. The data must |
| 190 |
already have been written to the uncompressed page. |
| 191 |
However, the data portion of the uncompressed page may differ from the |
| 192 |
compressed page when a record is being inserted in page_cur_insert_rec_low(). |
| 193 |
@param[in,out] page_zip Compressed page |
| 194 |
@param[in] str Address on the uncompressed page |
| 195 |
@param[in] length Length of the data |
| 196 |
@param[in] mtr Mini-transaction, or NULL */ |
| 197 |
static inline void page_zip_write_header(page_zip_des_t *page_zip, |
| 198 |
const byte *str, ulint length, |
| 199 |
mtr_t *mtr); |
| 200 |
|
| 201 |
/** Write an entire record on the compressed page. The data must already |
| 202 |
have been written to the uncompressed page. */ |
| 203 |
void page_zip_write_rec( |
| 204 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 205 |
const byte *rec, /*!< in: record being written */ |
| 206 |
const dict_index_t *index, /*!< in: the index the record belongs to */ |
| 207 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 208 |
ulint create); /*!< in: nonzero=insert, zero=update */ |
| 209 |
|
| 210 |
/** Parses a log record of writing a BLOB pointer of a record. |
| 211 |
@return end of log record or NULL */ |
| 212 |
byte *page_zip_parse_write_blob_ptr( |
| 213 |
byte *ptr, /*!< in: redo log buffer */ |
| 214 |
byte *end_ptr, /*!< in: redo log buffer end */ |
| 215 |
page_t *page, /*!< in/out: uncompressed page */ |
| 216 |
page_zip_des_t *page_zip); /*!< in/out: compressed page */ |
| 217 |
|
| 218 |
/** Write a BLOB pointer of a record on the leaf page of a clustered index. |
| 219 |
The information must already have been updated on the uncompressed page. */ |
| 220 |
void page_zip_write_blob_ptr( |
| 221 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 222 |
const byte *rec, /*!< in/out: record whose data is being |
| 223 |
written */ |
| 224 |
const dict_index_t *index, /*!< in: index of the page */ |
| 225 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 226 |
ulint n, /*!< in: column index */ |
| 227 |
mtr_t *mtr); /*!< in: mini-transaction handle, |
| 228 |
or NULL if no logging is needed */ |
| 229 |
|
| 230 |
/** Parses a log record of writing the node pointer of a record. |
| 231 |
@return end of log record or NULL */ |
| 232 |
byte *page_zip_parse_write_node_ptr( |
| 233 |
byte *ptr, /*!< in: redo log buffer */ |
| 234 |
byte *end_ptr, /*!< in: redo log buffer end */ |
| 235 |
page_t *page, /*!< in/out: uncompressed page */ |
| 236 |
page_zip_des_t *page_zip); /*!< in/out: compressed page */ |
| 237 |
|
| 238 |
/** Write the node pointer of a record on a non-leaf compressed page. |
| 239 |
@param[in,out] page_zip Compressed page |
| 240 |
@param[in,out] rec Record |
| 241 |
@param[in] size Data size of rec |
| 242 |
@param[in] ptr Node pointer |
| 243 |
@param[in] mtr Mini-transaction, or null */ |
| 244 |
void page_zip_write_node_ptr(page_zip_des_t *page_zip, byte *rec, ulint size, |
| 245 |
ulint ptr, mtr_t *mtr); |
| 246 |
|
| 247 |
/** Write the trx_id and roll_ptr of a record on a B-tree leaf node page. |
| 248 |
@param[in,out] page_zip Compressed page |
| 249 |
@param[in,out] rec Record |
| 250 |
@param[in] offsets Rec_get_offsets(rec, index) |
| 251 |
@param[in] trx_id_col Column number of trx_id in rec |
| 252 |
@param[in] trx_id Transaction identifier |
| 253 |
@param[in] roll_ptr Roll_ptr */ |
| 254 |
void page_zip_write_trx_id_and_roll_ptr(page_zip_des_t *page_zip, byte *rec, |
| 255 |
const ulint *offsets, ulint trx_id_col, |
| 256 |
trx_id_t trx_id, roll_ptr_t roll_ptr); |
| 257 |
|
| 258 |
/** Write the "deleted" flag of a record on a compressed page. The flag must |
| 259 |
already have been written on the uncompressed page. */ |
| 260 |
void page_zip_rec_set_deleted( |
| 261 |
page_zip_des_t *page_zip, /*!< in/out: compressed page */ |
| 262 |
const byte *rec, /*!< in: record on the uncompressed page */ |
| 263 |
bool flag); /*!< in: the deleted flag (nonzero=true) */ |
| 264 |
|
| 265 |
/** Write the "owned" flag of a record on a compressed page. The n_owned field |
| 266 |
must already have been written on the uncompressed page. |
| 267 |
@param[in,out] page_zip Compressed page |
| 268 |
@param[in] rec Record on the uncompressed page |
| 269 |
@param[in] flag The owned flag (nonzero=true) */ |
| 270 |
void page_zip_rec_set_owned(page_zip_des_t *page_zip, const byte *rec, |
| 271 |
ulint flag); |
| 272 |
|
| 273 |
/** Insert a record to the dense page directory. |
| 274 |
@param[in,out] page_zip Compressed page |
| 275 |
@param[in] prev_rec Record after which to insert |
| 276 |
@param[in] free_rec Record from which rec was allocated, or null |
| 277 |
@param[in] rec Record to insert */ |
| 278 |
void page_zip_dir_insert(page_zip_des_t *page_zip, const byte *prev_rec, |
| 279 |
const byte *free_rec, byte *rec); |
| 280 |
|
| 281 |
/** Shift the dense page directory and the array of BLOB pointers when a record |
| 282 |
is deleted. |
| 283 |
@param[in,out] page_zip compressed page |
| 284 |
@param[in] rec deleted record |
| 285 |
@param[in] index index of rec |
| 286 |
@param[in] offsets rec_get_offsets(rec) |
| 287 |
@param[in] free previous start of the free list */ |
| 288 |
void page_zip_dir_delete(page_zip_des_t *page_zip, byte *rec, |
| 289 |
const dict_index_t *index, const ulint *offsets, |
| 290 |
const byte *free); |
| 291 |
|
| 292 |
/** Add a slot to the dense page directory. |
| 293 |
@param[in,out] page_zip Compressed page |
| 294 |
@param[in] is_clustered Nonzero for clustered index, zero for others */ |
| 295 |
void page_zip_dir_add_slot(page_zip_des_t *page_zip, bool is_clustered); |
| 296 |
|
| 297 |
/** Parses a log record of writing to the header of a page. |
| 298 |
@return end of log record or NULL */ |
| 299 |
byte *page_zip_parse_write_header( |
| 300 |
byte *ptr, /*!< in: redo log buffer */ |
| 301 |
byte *end_ptr, /*!< in: redo log buffer end */ |
| 302 |
page_t *page, /*!< in/out: uncompressed page */ |
| 303 |
page_zip_des_t *page_zip); /*!< in/out: compressed page */ |
| 304 |
|
| 305 |
/** Reorganize and compress a page. This is a low-level operation for |
| 306 |
compressed pages, to be used when page_zip_compress() fails. |
| 307 |
On success, a redo log entry MLOG_ZIP_PAGE_COMPRESS will be written. |
| 308 |
The function btr_page_reorganize() should be preferred whenever possible. |
| 309 |
IMPORTANT: if page_zip_reorganize() is invoked on a leaf page of a |
| 310 |
non-clustered index, the caller must update the insert buffer free |
| 311 |
bits in the same mini-transaction in such a way that the modification |
| 312 |
will be redo-logged. |
| 313 |
@return true on success, false on failure; page_zip will be left |
| 314 |
intact on failure, but page will be overwritten. */ |
| 315 |
bool page_zip_reorganize( |
| 316 |
buf_block_t *block, /*!< in/out: page with compressed page; |
| 317 |
on the compressed page, in: size; |
| 318 |
out: data, n_blobs, |
| 319 |
m_start, m_end, m_nonempty */ |
| 320 |
dict_index_t *index, /*!< in: index of the B-tree node */ |
| 321 |
mtr_t *mtr); /*!< in: mini-transaction */ |
| 322 |
/** Copy the records of a page byte for byte. Do not copy the page header |
| 323 |
or trailer, except those B-tree header fields that are directly |
| 324 |
related to the storage of records. Also copy PAGE_MAX_TRX_ID. |
| 325 |
NOTE: The caller must update the lock table and the adaptive hash index. */ |
| 326 |
void page_zip_copy_recs( |
| 327 |
page_zip_des_t *page_zip, /*!< out: copy of src_zip |
| 328 |
(n_blobs, m_start, m_end, |
| 329 |
m_nonempty, data[0..size-1]) */ |
| 330 |
page_t *page, /*!< out: copy of src */ |
| 331 |
const page_zip_des_t *src_zip, /*!< in: compressed page */ |
| 332 |
const page_t *src, /*!< in: page */ |
| 333 |
dict_index_t *index, /*!< in: index of the B-tree */ |
| 334 |
mtr_t *mtr); /*!< in: mini-transaction */ |
| 335 |
#ifndef UNIV_HOTBACKUP |
| 336 |
#endif /* !UNIV_HOTBACKUP */ |
| 337 |
|
| 338 |
/** Parses a log record of compressing an index page. |
| 339 |
@return end of log record or NULL */ |
| 340 |
byte *page_zip_parse_compress( |
| 341 |
byte *ptr, /*!< in: buffer */ |
| 342 |
byte *end_ptr, /*!< in: buffer end */ |
| 343 |
page_t *page, /*!< out: uncompressed page */ |
| 344 |
page_zip_des_t *page_zip); /*!< out: compressed page */ |
| 345 |
|
| 346 |
/** Write a log record of compressing an index page without the data on the |
| 347 |
page. |
| 348 |
@param[in] level compression level |
| 349 |
@param[in] page page that is compressed |
| 350 |
@param[in] index index |
| 351 |
@param[in] mtr mtr */ |
| 352 |
static inline void page_zip_compress_write_log_no_data(ulint level, |
| 353 |
const page_t *page, |
| 354 |
dict_index_t *index, |
| 355 |
mtr_t *mtr); |
| 356 |
|
| 357 |
/** Parses a log record of compressing an index page without the data. |
| 358 |
@param[in] ptr buffer |
| 359 |
@param[in] end_ptr buffer end |
| 360 |
@param[in] page uncompressed page |
| 361 |
@param[out] page_zip compressed page |
| 362 |
@param[in] index index |
| 363 |
@return end of log record or NULL */ |
| 364 |
static inline byte *page_zip_parse_compress_no_data(byte *ptr, byte *end_ptr, |
| 365 |
page_t *page, |
| 366 |
page_zip_des_t *page_zip, |
| 367 |
dict_index_t *index); |
| 368 |
|
| 369 |
#ifndef UNIV_HOTBACKUP |
| 370 |
/** Reset the counters used for filling |
| 371 |
INFORMATION_SCHEMA.innodb_cmp_per_index. */ |
| 372 |
static inline void page_zip_reset_stat_per_index(); |
| 373 |
|
| 374 |
#endif /* !UNIV_HOTBACKUP */ |
| 375 |
|
| 376 |
#include "page0zip.ic" |
| 377 |
|
| 378 |
#endif /* page0zip_h */ |