Debian Codegraph @

storage/innobase/include/page0zip.ic

raw
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.ic
30 Compressed page interface
31
32 Created June 2005 by Marko Makela
33 *******************************************************/
34
35 #include "mtr0log.h"
36 #include "page0page.h"
37 #include "page0zip.h"
38 #include "srv0srv.h"
39
40 /* The format of compressed pages is as follows.
41
42 The header and trailer of the uncompressed pages, excluding the page
43 directory in the trailer, are copied as is to the header and trailer
44 of the compressed page.
45
46 At the end of the compressed page, there is a dense page directory
47 pointing to every user record contained on the page, including deleted
48 records on the free list. The dense directory is indexed in the
49 collation order, i.e., in the order in which the record list is
50 linked on the uncompressed page. The infimum and supremum records are
51 excluded. The two most significant bits of the entries are allocated
52 for the delete-mark and an n_owned flag indicating the last record in
53 a chain of records pointed to from the sparse page directory on the
54 uncompressed page.
55
56 The data between PAGE_ZIP_START and the last page directory entry will
57 be written in compressed format, starting at offset PAGE_DATA.
58 Infimum and supremum records are not stored. We exclude the
59 REC_N_NEW_EXTRA_BYTES in every record header. These can be recovered
60 from the dense page directory stored at the end of the compressed
61 page.
62
63 The fields node_ptr (in non-leaf B-tree nodes; level>0), trx_id and
64 roll_ptr (in leaf B-tree nodes; level=0), and BLOB pointers of
65 externally stored columns are stored separately, in ascending order of
66 heap_no and column index, starting backwards from the dense page
67 directory.
68
69 The compressed data stream may be followed by a modification log
70 covering the compressed portion of the page, as follows.
71
72 MODIFICATION LOG ENTRY FORMAT
73 - write record:
74 - (heap_no - 1) << 1 (1..2 bytes)
75 - extra bytes backwards
76 - data bytes
77 - clear record:
78 - (heap_no - 1) << 1 | 1 (1..2 bytes)
79
80 The integer values are stored in a variable-length format:
81 - 0xxxxxxx: 0..127
82 - 1xxxxxxx xxxxxxxx: 0..32767
83
84 The end of the modification log is marked by a 0 byte.
85
86 In summary, the compressed page looks like this:
87
88 (1) Uncompressed page header (PAGE_DATA bytes)
89 (2) Compressed index information
90 (3) Compressed page data
91 (4) Page modification log (page_zip->m_start..page_zip->m_end)
92 (5) Empty zero-filled space
93 (6) BLOB pointers (on leaf pages)
94 - BTR_EXTERN_FIELD_REF_SIZE for each externally stored column
95 - in descending collation order
96 (7) Uncompressed columns of user records, n_dense * uncompressed_size bytes,
97 - indexed by heap_no
98 - DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN for leaf pages of clustered indexes
99 - REC_NODE_PTR_SIZE for non-leaf pages
100 - 0 otherwise
101 (8) dense page directory, stored backwards
102 - n_dense = n_heap - 2
103 - existing records in ascending collation order
104 - deleted records (free list) in link order
105 */
106
107 /** Set the size of a compressed page in bytes. */
108 static inline void page_zip_set_size(
109 page_zip_des_t *page_zip, /*!< in/out: compressed page */
110 ulint size) /*!< in: size in bytes */
111 {
112 if (size) {
113 int ssize;
114
115 ut_ad(ut_is_2pow(size));
116
117 for (ssize = 1; size > (ulint)(512 << ssize); ssize++) {
118 }
119
120 page_zip->ssize = ssize;
121 } else {
122 page_zip->ssize = 0;
123 }
124
125 ut_ad(page_zip_get_size(page_zip) == size);
126 }
127
128 #ifndef UNIV_HOTBACKUP
129 /** Determine if a record is so big that it needs to be stored externally.
130 @param[in] rec_size length of the record in bytes
131 @param[in] comp nonzero=compact format
132 @param[in] n_fields number of fields in the record; ignored if
133 tablespace is not compressed
134 @param[in] page_size page size
135 @return false if the entire record can be stored locally on the page */
136 static inline bool page_zip_rec_needs_ext(ulint rec_size, ulint comp,
137 ulint n_fields,
138 const page_size_t &page_size) {
139 ut_ad(rec_size > (comp ? REC_N_NEW_EXTRA_BYTES : REC_N_OLD_EXTRA_BYTES));
140 ut_ad(comp || !page_size.is_compressed());
141
142 if (rec_size >= REC_MAX_DATA_SIZE) {
143 return true;
144 }
145
146 if (page_size.is_compressed()) {
147 ut_ad(comp);
148 /* On a compressed page, there is a two-byte entry in
149 the dense page directory for every record. But there
150 is no record header. There should be enough room for
151 one record on an empty leaf page. Subtract 1 byte for
152 the encoded heap number. Check also the available space
153 on the uncompressed page. */
154 return (rec_size - (REC_N_NEW_EXTRA_BYTES - 2 - 1) >=
155 page_zip_empty_size(n_fields, page_size.physical()) ||
156 rec_size >= page_get_free_space_of_empty(true) / 2);
157 }
158
159 return (rec_size >= page_get_free_space_of_empty(comp) / 2);
160 }
161 #endif /* !UNIV_HOTBACKUP */
162
163 /** Determine if the length of the page trailer.
164 @return length of the page trailer, in bytes, not including the
165 terminating zero byte of the modification log */
166 static inline ulint page_zip_get_trailer_len(
167 const page_zip_des_t *page_zip, /*!< in: compressed page */
168 bool is_clust) /*!< in: true if clustered index */
169 {
170 ulint uncompressed_size;
171
172 ut_ad(page_zip_simple_validate(page_zip));
173 UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
174
175 if (!page_is_leaf(page_zip->data)) {
176 uncompressed_size = PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE;
177 ut_ad(!page_zip->n_blobs);
178 } else if (is_clust) {
179 uncompressed_size =
180 PAGE_ZIP_DIR_SLOT_SIZE + DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
181 } else {
182 uncompressed_size = PAGE_ZIP_DIR_SLOT_SIZE;
183 ut_ad(!page_zip->n_blobs);
184 }
185
186 return ((page_dir_get_n_heap(page_zip->data) - 2) * uncompressed_size +
187 page_zip->n_blobs * BTR_EXTERN_FIELD_REF_SIZE);
188 }
189
190 static inline lint page_zip_max_ins_size(const page_zip_des_t *page_zip,
191 bool is_clust) {
192 ulint trailer_len;
193
194 trailer_len = page_zip_get_trailer_len(page_zip, is_clust);
195
196 /* When a record is created, a pointer may be added to
197 the dense directory.
198 Likewise, space for the columns that will not be
199 compressed will be allocated from the page trailer.
200 Also the BLOB pointers will be allocated from there, but
201 we may as well count them in the length of the record. */
202
203 trailer_len += PAGE_ZIP_DIR_SLOT_SIZE;
204
205 return ((lint)page_zip_get_size(page_zip) - trailer_len - page_zip->m_end -
206 (REC_N_NEW_EXTRA_BYTES - 2));
207 }
208
209 static inline bool page_zip_available(const page_zip_des_t *page_zip,
210 bool is_clust, ulint length,
211 ulint create) {
212 ulint trailer_len;
213
214 ut_ad(length > REC_N_NEW_EXTRA_BYTES);
215
216 trailer_len = page_zip_get_trailer_len(page_zip, is_clust);
217
218 /* Subtract the fixed extra bytes and add the maximum
219 space needed for identifying the record (encoded heap_no). */
220 length -= REC_N_NEW_EXTRA_BYTES - 2;
221
222 if (create > 0) {
223 /* When a record is created, a pointer may be added to
224 the dense directory.
225 Likewise, space for the columns that will not be
226 compressed will be allocated from the page trailer.
227 Also the BLOB pointers will be allocated from there, but
228 we may as well count them in the length of the record. */
229
230 trailer_len += PAGE_ZIP_DIR_SLOT_SIZE;
231 }
232
233 return (length + trailer_len + page_zip->m_end < page_zip_get_size(page_zip));
234 }
235
236 /** Initialize a compressed page descriptor. */
237 static inline void page_zip_des_init(
238 page_zip_des_t *page_zip) /*!< in/out: compressed page
239 descriptor */
240 {
241 memset(page_zip, 0, sizeof *page_zip);
242 }
243
244 /** Write a log record of writing to the uncompressed header portion of a page.
245 */
246 void page_zip_write_header_log(
247 const byte *data, /*!< in: data on the uncompressed page */
248 ulint length, /*!< in: length of the data */
249 mtr_t *mtr); /*!< in: mini-transaction */
250
251 /** Write data to the uncompressed header portion of a page. The data must
252 already have been written to the uncompressed page.
253 @param[in,out] page_zip Compressed page
254 @param[in] str Address on the uncompressed page
255 @param[in] length Length of the data
256 @param[in] mtr Mini-transaction, or NULL */
257 static inline void page_zip_write_header(page_zip_des_t *page_zip,
258 const byte *str, ulint length,
259 mtr_t *mtr) {
260 ulint pos;
261
262 ut_ad(page_zip_simple_validate(page_zip));
263 UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
264
265 pos = page_offset(str);
266
267 ut_ad(pos < PAGE_DATA);
268
269 memcpy(page_zip->data + pos, str, length);
270
271 /* The following would fail in page_cur_insert_rec_zip(). */
272 /* ut_ad(page_zip_validate(page_zip, str - pos)); */
273
274 if (mtr) {
275 #ifndef UNIV_HOTBACKUP
276 page_zip_write_header_log(str, length, mtr);
277 #endif /* !UNIV_HOTBACKUP */
278 }
279 }
280
281 /** Write a log record of compressing an index page without the data on the
282 page.
283 */
284 static inline void page_zip_compress_write_log_no_data(
285 ulint level, /*!< in: compression level */
286 const page_t *page, /*!< in: page that is compressed */
287 dict_index_t *index, /*!< in: index */
288 mtr_t *mtr) /*!< in: mtr */
289 {
290 #ifndef UNIV_HOTBACKUP
291
292 byte *log_ptr = nullptr;
293 if (!mlog_open_and_write_index(mtr, page, index,
294 MLOG_ZIP_PAGE_COMPRESS_NO_DATA, 1, log_ptr)) {
295 return;
296 }
297
298 mach_write_to_1(log_ptr, level);
299 mlog_close(mtr, log_ptr + 1);
300
301 #endif /* !UNIV_HOTBACKUP */
302 }
303
304 /** Parses a log record of compressing an index page without the data.
305 @return end of log record or NULL */
306 static inline byte *page_zip_parse_compress_no_data(
307 byte *ptr, /*!< in: buffer */
308 byte *end_ptr, /*!< in: buffer end */
309 page_t *page, /*!< in: uncompressed page */
310 page_zip_des_t *page_zip, /*!< out: compressed page */
311 dict_index_t *index) /*!< in: index */
312 {
313 ulint level;
314 if (end_ptr == ptr) {
315 return (nullptr);
316 }
317
318 level = mach_read_from_1(ptr);
319
320 /* If page compression fails then there must be something wrong
321 because a compress log record is logged only if the compression
322 was successful. Crash in this case. */
323
324 if (page && !page_zip_compress(page_zip, page, index, level, nullptr)) {
325 ut_error;
326 }
327
328 return (ptr + 1);
329 }
330
331 #ifndef UNIV_HOTBACKUP
332 /** Reset the counters used for filling
333 INFORMATION_SCHEMA.innodb_cmp_per_index. */
334 static inline void page_zip_reset_stat_per_index() {
335 mutex_enter(&page_zip_stat_per_index_mutex);
336
337 page_zip_stat_per_index.erase(page_zip_stat_per_index.begin(),
338 page_zip_stat_per_index.end());
339
340 mutex_exit(&page_zip_stat_per_index_mutex);
341 }
342 #endif /* !UNIV_HOTBACKUP */
343
344 /** Find the slot of the given record in the dense page directory.
345 @return dense directory slot, or NULL if record not found */
346 static inline byte *page_zip_dir_find_low(
347 byte *slot, /*!< in: start of records */
348 byte *end, /*!< in: end of records */
349 ulint offset) /*!< in: offset of user record */
350 {
351 ut_ad(slot <= end);
352
353 for (; slot < end; slot += PAGE_ZIP_DIR_SLOT_SIZE) {
354 if ((mach_read_from_2(slot) & PAGE_ZIP_DIR_SLOT_MASK) == offset) {
355 return (slot);
356 }
357 }
358
359 return (nullptr);
360 }
361
362 /** Gets the size of the compressed page trailer (the dense page directory),
363 only including user records (excluding the free list).
364 @return length of dense page directory comprising existing records, in bytes */
365 static inline ulint page_zip_dir_user_size(
366 const page_zip_des_t *page_zip) /*!< in: compressed page */
367 {
368 ulint size = PAGE_ZIP_DIR_SLOT_SIZE * page_get_n_recs(page_zip->data);
369 ut_ad(size <= page_zip_dir_size(page_zip));
370 return (size);
371 }
372
373 /** Find the slot of the given free record in the dense page directory.
374 @return dense directory slot, or NULL if record not found */
375 static inline byte *page_zip_dir_find_free(
376 page_zip_des_t *page_zip, /*!< in: compressed page */
377 ulint offset) /*!< in: offset of user record */
378 {
379 byte *end = page_zip->data + page_zip_get_size(page_zip);
380
381 ut_ad(page_zip_simple_validate(page_zip));
382
383 return (page_zip_dir_find_low(end - page_zip_dir_size(page_zip),
384 end - page_zip_dir_user_size(page_zip),
385 offset));
386 }
387
388 /** Gets an offset to the compressed page trailer (the dense page directory),
389 including deleted records (the free list).
390 @return offset of the dense page directory */
391 static inline ulint page_zip_dir_start_offs(
392 const page_zip_des_t *page_zip, /*!< in: compressed page */
393 ulint n_dense) /*!< in: directory size */
394 {
395 ut_ad(n_dense * PAGE_ZIP_DIR_SLOT_SIZE < page_zip_get_size(page_zip));
396
397 return (page_zip_get_size(page_zip) - n_dense * PAGE_ZIP_DIR_SLOT_SIZE);
398 }