Debian Codegraph @

storage/innobase/include/zlob0index.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2016, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27 #ifndef zlob0index_h
28 #define zlob0index_h
29
30 #include "fil0fil.h"
31 #include "fut0lst.h"
32 #include "lob0impl.h"
33
34 namespace lob {
35
36 /** In-memory copy of the information from z_index_entry_t. */
37 struct z_index_entry_mem_t {
38 z_index_entry_mem_t()
39 : m_trx_id(0), m_z_page_no(FIL_NULL), m_z_frag_id(FRAG_ID_NULL) {}
40
41 /** The location of this index entry node. */
42 fil_addr_t m_self;
43
44 fil_addr_t m_prev;
45 fil_addr_t m_next;
46 flst_bnode_t m_versions;
47 trx_id_t m_trx_id;
48 trx_id_t m_trx_id_modifier;
49 undo_no_t m_trx_undo_no;
50 undo_no_t m_trx_undo_no_modifier;
51 page_no_t m_z_page_no;
52 frag_id_t m_z_frag_id;
53
54 /** Uncompressed data length. */
55 ulint m_data_len;
56
57 /** Compressed data length. */
58 ulint m_z_data_len;
59
60 std::ostream &print(std::ostream &out) const;
61
62 /** Initialize all the members. */
63 void reset() {
64 m_self = fil_addr_null;
65 m_prev = fil_addr_null;
66 m_next = fil_addr_null;
67 m_versions.reset();
68 m_trx_id = 0;
69 m_trx_id_modifier = 0;
70 m_trx_undo_no = 0;
71 m_trx_undo_no_modifier = 0;
72 m_z_page_no = FIL_NULL;
73 m_z_frag_id = 0;
74 m_data_len = 0;
75 m_z_data_len = 0;
76 }
77
78 bool is_null() { return (m_self.is_equal(fil_addr_null)); }
79 };
80
81 inline std::ostream &operator<<(std::ostream &out,
82 const z_index_entry_mem_t &obj) {
83 return (obj.print(out));
84 }
85
86 /** An index entry pointing to one zlib stream. */
87 struct z_index_entry_t {
88 /** Offset with index entry pointing to the prev index entry. */
89 static const ulint OFFSET_PREV = 0;
90
91 /** Offset with index entry pointing to the next index entry. */
92 static const ulint OFFSET_NEXT = OFFSET_PREV + FIL_ADDR_SIZE;
93
94 /** Offset within index entry pointing to base node of list of
95 versions.*/
96 static const ulint OFFSET_VERSIONS = OFFSET_NEXT + FIL_ADDR_SIZE;
97
98 /** Offset within index entry pointing to creator trxid.*/
99 static const ulint OFFSET_TRXID = OFFSET_VERSIONS + FLST_BASE_NODE_SIZE;
100
101 /** The modifier trx id. */
102 static const ulint OFFSET_TRXID_MODIFIER = OFFSET_TRXID + 6;
103
104 /** Offset within index entry pointing to trx undo no.*/
105 static const ulint OFFSET_TRX_UNDO_NO = OFFSET_TRXID_MODIFIER + 6;
106
107 /** Offset within index entry pointing to modifier trx undo no.*/
108 static const ulint OFFSET_TRX_UNDO_NO_MODIFIER = OFFSET_TRX_UNDO_NO + 4;
109
110 /** Offset within index entry pointing to page number where zlib
111 stream starts. This could be a data page or a fragment page. */
112 static const ulint OFFSET_Z_PAGE_NO = OFFSET_TRX_UNDO_NO_MODIFIER + 4;
113
114 /** Offset within index entry pointing to location of zlib stream.*/
115 static const ulint OFFSET_Z_FRAG_ID = OFFSET_Z_PAGE_NO + 4;
116
117 /** Offset within index entry pointing to uncompressed data
118 len (bytes).*/
119 static const ulint OFFSET_DATA_LEN = OFFSET_Z_FRAG_ID + 2;
120
121 /** Offset within index entry pointing to compressed data len
122 (bytes).*/
123 static const ulint OFFSET_ZDATA_LEN = OFFSET_DATA_LEN + 4;
124
125 /** LOB version */
126 static const ulint OFFSET_LOB_VERSION = OFFSET_ZDATA_LEN + 4;
127
128 /** Total size of one index entry. */
129 static const ulint SIZE = OFFSET_LOB_VERSION + 4;
130
131 /** Constructor. */
132 z_index_entry_t(flst_node_t *node, mtr_t *mtr) : m_node(node), m_mtr(mtr) {}
133
134 /** Constructor. */
135 z_index_entry_t(flst_node_t *node, mtr_t *mtr, dict_index_t *index)
136 : m_node(node), m_mtr(mtr), m_index(index) {}
137
138 /** Constructor
139 @param[in] mtr the mini-transaction
140 @param[in] index the clustered index to which LOB belongs. */
141 z_index_entry_t(mtr_t *mtr, dict_index_t *index)
142 : m_node(nullptr),
143 m_mtr(mtr),
144 m_index(index),
145 m_block(nullptr),
146 m_page_no(FIL_NULL) {}
147
148 /** Constructor
149 @param[in] node the location where index entry starts. */
150 z_index_entry_t(flst_node_t *node)
151 : m_node(node),
152 m_mtr(nullptr),
153 m_index(nullptr),
154 m_block(nullptr),
155 m_page_no(FIL_NULL) {}
156
157 /** Default constructor */
158 z_index_entry_t()
159 : m_node(nullptr),
160 m_mtr(nullptr),
161 m_index(nullptr),
162 m_block(nullptr),
163 m_page_no(FIL_NULL) {}
164
165 void set_index(dict_index_t *index) { m_index = index; }
166
167 /** Point to another index entry.
168 @param[in] node point to this file list node. */
169 void reset(flst_node_t *node) { m_node = node; }
170
171 /** Point to another index entry.
172 @param[in] entry another index entry.*/
173 void reset(const z_index_entry_t &entry) { m_node = entry.m_node; }
174
175 /** Initialize an index entry to some sane value. */
176 void init() {
177 ut_ad(m_mtr != nullptr);
178
179 set_prev_null();
180 set_next_null();
181 set_versions_null();
182 set_trx_id(0);
183 set_trx_undo_no(0);
184 set_z_page_no(FIL_NULL);
185 set_z_frag_id(FRAG_ID_NULL);
186 set_data_len(0);
187 set_zdata_len(0);
188 }
189
190 /** Determine if the current index entry be rolled back.
191 @param[in] trxid the transaction that is being rolled
192 back.
193 @param[in] undo_no the savepoint undo number of trx,
194 up to which rollback happens.
195 @return true if this entry can be rolled back, false otherwise. */
196 bool can_rollback(trx_id_t trxid, undo_no_t undo_no) {
197 /* For rollback, make use of creator trx id. */
198 return ((trxid == get_trx_id()) && (get_trx_undo_no() >= undo_no));
199 }
200
201 /** Determine if the current index entry be purged.
202 @param[in] trxid the transaction that is being purged.
203 @param[in] undo_no the undo number of trx.
204 @return true if this entry can be purged, false otherwise. */
205 bool can_be_purged(trx_id_t trxid, undo_no_t undo_no) {
206 return ((trxid == get_trx_id_modifier()) &&
207 (get_trx_undo_no_modifier() == undo_no));
208 }
209
210 /** Purge one index entry.
211 @param[in] index index to which LOB belongs.
212 @param[in] first first page of LOB.
213 @param[in,out] lst list from which this entry will be
214 removed.
215 @param[in,out] free_list list to which this entry will be
216 added.*/
217 fil_addr_t purge_version(dict_index_t *index, z_first_page_t &first,
218 flst_base_node_t *lst, flst_base_node_t *free_list);
219
220 /** Purge the current index entry. An index entry points to either a
221 FIRST page or DATA page. That LOB page will be freed if it is DATA
222 page. A FIRST page should not be freed. */
223 void purge(dict_index_t *index, z_first_page_t &first);
224
225 /** Remove this node from the given list.
226 @param[in] bnode the base node of the list from which to remove
227 current node. */
228 void remove(flst_base_node_t *bnode) {
229 ut_ad(m_mtr != nullptr);
230
231 flst_remove(bnode, m_node, m_mtr);
232 }
233
234 /** Insert the given index entry after the current index entry.
235 @param[in] base the base node of the file based list.
236 @param[in] entry the new node to be inserted.*/
237 void insert_after(flst_base_node_t *base, z_index_entry_t &entry) {
238 ut_ad(m_mtr != nullptr);
239
240 flst_insert_after(base, m_node, entry.get_node(), m_mtr);
241 }
242
243 void insert_before(flst_base_node_t *base, z_index_entry_t &entry) {
244 ut_ad(m_mtr != nullptr);
245
246 flst_insert_before(base, entry.get_node(), m_node, m_mtr);
247 }
248
249 /** Add this node as the last node in the given list.
250 @param[in] bnode the base node of the file list. */
251 void push_back(flst_base_node_t *bnode) {
252 ut_ad(m_mtr != nullptr);
253
254 flst_add_last(bnode, m_node, m_mtr);
255 }
256
257 /** Add this node as the last node in the given list.
258 @param[in] bnode the base node of the file list. */
259 void push_front(flst_base_node_t *bnode) {
260 ut_ad(m_mtr != nullptr);
261 flst_add_first(bnode, m_node, m_mtr);
262 }
263
264 /** Set the previous index entry as null. */
265 void set_prev_null() {
266 flst_write_addr(m_node + OFFSET_PREV, fil_addr_null, m_mtr);
267 }
268
269 /** Get the location of previous index entry. */
270 fil_addr_t get_prev() const {
271 return (flst_read_addr(m_node + OFFSET_PREV, m_mtr));
272 }
273
274 /** Get the location of next index entry.
275 @return the file address of the next index entry. */
276 fil_addr_t get_next() const {
277 return (flst_read_addr(m_node + OFFSET_NEXT, m_mtr));
278 }
279
280 /** Set the next index entry as null. */
281 void set_next_null() {
282 ut_ad(m_mtr != nullptr);
283 flst_write_addr(m_node + OFFSET_NEXT, fil_addr_null, m_mtr);
284 }
285
286 /** Set the versions list as null. */
287 void set_versions_null() {
288 flst_base_node_t *bnode = get_versions_list();
289 flst_init(bnode, m_mtr);
290 }
291
292 /** Get the base node of the list of versions. */
293 flst_base_node_t *get_versions_list() const {
294 return (m_node + OFFSET_VERSIONS);
295 }
296
297 /** Get the base node of the list of versions. */
298 flst_bnode_t get_versions_mem() const {
299 ut_ad(m_mtr != nullptr);
300 flst_base_node_t *node = get_versions_list();
301 return (flst_bnode_t(node, m_mtr));
302 }
303
304 trx_id_t get_trx_id() const {
305 return (mach_read_from_6(m_node + OFFSET_TRXID));
306 }
307
308 trx_id_t get_trx_id_modifier() const {
309 return (mach_read_from_6(m_node + OFFSET_TRXID_MODIFIER));
310 }
311
312 /** Get the undo number of the creator transaction. This is used
313 for rollback purposes.
314 @return the undo number of creator trx. */
315 undo_no_t get_trx_undo_no() const {
316 byte *ptr = m_node + OFFSET_TRX_UNDO_NO;
317 return (mach_read_from_4(ptr));
318 }
319
320 /** Get the undo number of the modifier transaction. This is used
321 for purging purposes.
322 @return the undo number of modifier trx. */
323 undo_no_t get_trx_undo_no_modifier() const {
324 byte *ptr = m_node + OFFSET_TRX_UNDO_NO_MODIFIER;
325 return (mach_read_from_4(ptr));
326 }
327
328 /** Set the trx identifier to given value, without generating redo
329 log records.
330 @param[in] id the given trx identifier.*/
331 void set_trx_id_no_redo(trx_id_t id) {
332 byte *ptr = m_node + OFFSET_TRXID;
333 mach_write_to_6(ptr, id);
334 }
335
336 /** Set the trx identifier to given value.
337 @param[in] id the given trx identifier.*/
338 void set_trx_id(trx_id_t id) {
339 ut_ad(m_mtr != nullptr);
340 byte *ptr = m_node + OFFSET_TRXID;
341 mach_write_to_6(ptr, id);
342 mlog_log_string(ptr, 6, m_mtr);
343 }
344
345 /** Set the modifier trxid to the given value.
346 @param[in] id the modifier trxid.*/
347 void set_trx_id_modifier(trx_id_t id) {
348 ut_ad(m_mtr != nullptr);
349
350 byte *ptr = m_node + OFFSET_TRXID_MODIFIER;
351 mach_write_to_6(ptr, id);
352 mlog_log_string(ptr, 6, m_mtr);
353 }
354
355 /** Set the modifier trxid to the given value, without generating
356 redo log records.
357 @param[in] id the modifier trxid.*/
358 void set_trx_id_modifier_no_redo(trx_id_t id) {
359 byte *ptr = m_node + OFFSET_TRXID_MODIFIER;
360 mach_write_to_6(ptr, id);
361 }
362
363 /** Set the undo number of the creator trx.
364 @param[in] undo_no the undo number value.*/
365 void set_trx_undo_no(undo_no_t undo_no) {
366 ut_ad(m_mtr != nullptr);
367 byte *ptr = m_node + OFFSET_TRX_UNDO_NO;
368 mlog_write_ulint(ptr, undo_no, MLOG_4BYTES, m_mtr);
369 }
370
371 /** Set the undo number of the modifier trx.
372 @param[in] undo_no the undo number value.*/
373 void set_trx_undo_no_modifier(undo_no_t undo_no) {
374 ut_ad(m_mtr != nullptr);
375 byte *ptr = m_node + OFFSET_TRX_UNDO_NO_MODIFIER;
376 mlog_write_ulint(ptr, undo_no, MLOG_4BYTES, m_mtr);
377 }
378
379 page_no_t get_z_page_no() const {
380 return (mach_read_from_4(m_node + OFFSET_Z_PAGE_NO));
381 }
382
383 /** Set the page number pointed to by this index entry to FIL_NULL.
384 @param[in] mtr The mini-transaction used for this modification. */
385 void set_z_page_no_null(mtr_t *mtr) {
386 mlog_write_ulint(m_node + OFFSET_Z_PAGE_NO, FIL_NULL, MLOG_4BYTES, mtr);
387 }
388
389 /** Free the data pages pointed to by this index entry.
390 @param[in] mtr the mini-transaction used to free the pages.
391 @return the number of pages freed. */
392 size_t free_data_pages(mtr_t *mtr);
393
394 /** Set the page number pointed to by this index entry to given value.
395 @param[in] page_no Page number to be put in index entry. */
396 void set_z_page_no(page_no_t page_no) {
397 ut_ad(m_mtr != nullptr);
398 mlog_write_ulint(m_node + OFFSET_Z_PAGE_NO, page_no, MLOG_4BYTES, m_mtr);
399 }
400
401 page_no_t get_z_frag_id() const {
402 return (mach_read_from_2(m_node + OFFSET_Z_FRAG_ID));
403 }
404
405 void set_z_frag_id(frag_id_t id) {
406 ut_ad(m_mtr != nullptr);
407 mlog_write_ulint(m_node + OFFSET_Z_FRAG_ID, id, MLOG_2BYTES, m_mtr);
408 }
409
410 /** Get the uncompressed data length in bytes. */
411 ulint get_data_len() const {
412 return (mach_read_from_4(m_node + OFFSET_DATA_LEN));
413 }
414
415 /** Set the uncompressed data length in bytes.
416 @param[in] len the uncompressed data length in bytes */
417 void set_data_len(ulint len) {
418 ut_ad(m_mtr != nullptr);
419 mlog_write_ulint(m_node + OFFSET_DATA_LEN, len, MLOG_4BYTES, m_mtr);
420 }
421
422 /** Get the compressed data length in bytes. */
423 ulint get_zdata_len() const {
424 return (mach_read_from_4(m_node + OFFSET_ZDATA_LEN));
425 }
426
427 /** Set the compressed data length in bytes.
428 @param[in] len the compressed data length in bytes */
429 void set_zdata_len(ulint len) {
430 ut_ad(m_mtr != nullptr);
431 mlog_write_ulint(m_node + OFFSET_ZDATA_LEN, len, MLOG_4BYTES, m_mtr);
432 }
433
434 /** Get the LOB version. */
435 uint32_t get_lob_version() const {
436 return (mach_read_from_4(m_node + OFFSET_LOB_VERSION));
437 }
438
439 /** Set the LOB version .
440 @param[in] version the lob version. */
441 void set_lob_version(ulint version) {
442 ut_ad(m_mtr != nullptr);
443 mlog_write_ulint(m_node + OFFSET_LOB_VERSION, version, MLOG_4BYTES, m_mtr);
444 }
445
446 /* The given entry becomes the old version of the current entry.
447 Move the version base node from old entry to current entry.
448 @param[in] entry the old entry */
449 void set_old_version(z_index_entry_t &entry);
450
451 /** The current index entry points to a latest LOB page. It may or
452 may not have older versions. If older version is there, bring it
453 back to the index list from the versions list. Then remove the
454 current entry from the index list. Move the versions list from
455 current entry to older entry.
456 @param[in] index the index in which LOB exists.
457 @param[in] first The first lob page containing index list and free
458 list. */
459 fil_addr_t make_old_version_current(dict_index_t *index,
460 z_first_page_t &first);
461
462 flst_node_t *get_node() { return (m_node); }
463 bool is_null() const { return (m_node == nullptr); }
464
465 std::ostream &print(std::ostream &out) const;
466 std::ostream &print_pages(std::ostream &out) const;
467
468 /** Load the page (in shared mode) whose number was cached.
469 @return the buffer block of the page loaded. */
470 buf_block_t *load_s() {
471 ut_ad(m_page_no != FIL_NULL);
472
473 page_id_t page_id(dict_index_get_space(m_index), m_page_no);
474 page_size_t page_size(dict_table_page_size(m_index->table));
475 m_block =
476 buf_page_get(page_id, page_size, RW_S_LATCH, UT_LOCATION_HERE, m_mtr);
477 return (m_block);
478 }
479
480 /** Load the given file address in s mode.
481 @param[in] addr the file address of the required node. */
482 void load_s(fil_addr_t &addr) {
483 space_id_t space = dict_index_get_space(m_index);
484 const page_size_t page_size = dict_table_page_size(m_index->table);
485 m_node = fut_get_ptr(space, page_size, addr, RW_S_LATCH, m_mtr, &m_block);
486 m_page_no = m_block->get_page_no();
487 }
488
489 /** Load the given file address in x mode.
490 @param[in] addr the file address of the required node. */
491 void load_x(fil_addr_t &addr) {
492 space_id_t space = dict_index_get_space(m_index);
493 const page_size_t page_size = dict_table_page_size(m_index->table);
494 m_node = fut_get_ptr(space, page_size, addr, RW_X_LATCH, m_mtr, &m_block);
495 m_page_no = m_block->get_page_no();
496 }
497
498 /** Read the given LOB index entry.
499 @param[in] entry_mem the LOB index entry. */
500 void read(z_index_entry_mem_t &entry_mem) const;
501
502 /** Read the given LOB index entry and then commit the mtr.
503 @param[in] entry_mem the LOB index entry. */
504 void read_and_commit(z_index_entry_mem_t &entry_mem) {
505 read(entry_mem);
506 mtr_commit(m_mtr);
507 m_node = nullptr;
508 }
509
510 /** Get the location of the current index entry. */
511 fil_addr_t get_self() const;
512
513 private:
514 /** Move the version base node from current entry to the given entry.
515 @param[in] entry The index entry to which the version base
516 node is moved to. */
517 void move_version_base_node(z_index_entry_t &entry);
518
519 /** The file list node in a db page. This node is persisted. */
520 flst_node_t *m_node;
521
522 /** A mini-transaction. */
523 mtr_t *m_mtr;
524
525 /** The index containing the LOB. */
526 dict_index_t *m_index;
527
528 /** The buffer block in which this entry exists. While reading data
529 from m_node, appropriate latches must be held on this block. */
530 buf_block_t *m_block;
531
532 /** The page number in which this entry is available. This
533 information will be cached and can be used to reload the page
534 conveniently. */
535 page_no_t m_page_no;
536 };
537
538 inline std::ostream &operator<<(std::ostream &out, const z_index_entry_t &obj) {
539 return (obj.print(out));
540 }
541
542 } /* namespace lob */
543
544 #endif /* zlob0index_h */