Debian Codegraph @

storage/innobase/include/lob0index.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 lob0index_h
28 #define lob0index_h
29
30 #include "fut0lst.h"
31 #include "lob0util.h"
32 #include "trx0trx.h"
33 #include "univ.i"
34
35 namespace lob {
36
37 typedef std::map<page_no_t, buf_block_t *> BlockCache;
38 struct first_page_t;
39
40 /** An in-memory copy of an index_entry_t data */
41 struct index_entry_mem_t {
42 fil_addr_t m_self;
43 fil_addr_t m_prev;
44 fil_addr_t m_next;
45 flst_bnode_t m_versions;
46 trx_id_t m_trx_id;
47 trx_id_t m_trx_id_modifier;
48 undo_no_t m_undo_no;
49 undo_no_t m_undo_no_modifier;
50 page_no_t m_page_no;
51 ulint m_data_len;
52
53 index_entry_mem_t() { reset(); }
54
55 void reset();
56
57 bool is_null() { return (m_self.is_equal(fil_addr_null)); }
58
59 page_no_t get_page_no() const { return (m_page_no); }
60
61 /** Print this object into the given output stream.
62 @param[in] out the output stream.
63 @return the output stream. */
64 std::ostream &print(std::ostream &out) const;
65 };
66
67 /** List of index entry memory (iem) objects. */
68 using List_iem_t = std::list<index_entry_mem_t>;
69
70 /** Overloading the global output operator to print the index_entry_mem_t
71 object.
72 @param[in,out] out the output stream.
73 @param[in] obj an object of type index_entry_mem_t
74 @return the output stream. */
75 inline std::ostream &operator<<(std::ostream &out,
76 const index_entry_mem_t &obj) {
77 return (obj.print(out));
78 }
79
80 /** An index entry pointing to an LOB page. */
81 struct index_entry_t {
82 /** Index entry offsets within node. */
83 static const ulint OFFSET_PREV = 0;
84 static const ulint OFFSET_NEXT = OFFSET_PREV + FIL_ADDR_SIZE;
85
86 /** Points to base node of the list of versions. The size of base node is
87 16 bytes. */
88 static const ulint OFFSET_VERSIONS = OFFSET_NEXT + FIL_ADDR_SIZE;
89
90 /** The creator trx id. */
91 static const ulint OFFSET_TRXID = OFFSET_VERSIONS + FLST_BASE_NODE_SIZE;
92
93 /** The modifier trx id. */
94 static const ulint OFFSET_TRXID_MODIFIER = OFFSET_TRXID + 6;
95 static const ulint OFFSET_TRX_UNDO_NO = OFFSET_TRXID_MODIFIER + 6;
96
97 /** The undo number of the modifier trx. */
98 static const ulint OFFSET_TRX_UNDO_NO_MODIFIER = OFFSET_TRX_UNDO_NO + 4;
99 static const ulint OFFSET_PAGE_NO = OFFSET_TRX_UNDO_NO_MODIFIER + 4;
100 static const ulint OFFSET_DATA_LEN = OFFSET_PAGE_NO + 4;
101
102 /** The LOB version number. */
103 static const ulint OFFSET_LOB_VERSION = OFFSET_DATA_LEN + 4;
104
105 /** Total length of an index node. */
106 static const ulint SIZE = OFFSET_LOB_VERSION + 4;
107
108 /** Constructor.
109 @param[in] node the pointer where index entry is located. */
110 index_entry_t(flst_node_t *node)
111 : m_node(node), m_mtr(nullptr), m_index(nullptr), m_block(nullptr) {}
112
113 index_entry_t(flst_node_t *node, mtr_t *mtr)
114 : m_node(node), m_mtr(mtr), m_index(nullptr), m_block(nullptr) {}
115
116 index_entry_t(flst_node_t *node, mtr_t *mtr, dict_index_t *index)
117 : m_node(node), m_mtr(mtr), m_index(index), m_block(nullptr) {}
118
119 index_entry_t(mtr_t *mtr, const dict_index_t *index)
120 : m_node(nullptr), m_mtr(mtr), m_index(index), m_block(nullptr) {}
121
122 /* Move the node pointer to a different place within the same page.
123 @param[in] addr new location of node pointer. */
124 void reset(fil_addr_t &addr) {
125 ut_ad(m_block->page.id.page_no() == addr.page);
126
127 m_node = buf_block_get_frame(m_block) + addr.boffset;
128 }
129
130 /* Get the buffer block of the current index entry.
131 @return the buffer block of the current index entry.*/
132 buf_block_t *get_block() const { return (m_block); }
133
134 /* Reset the current object to point to a different node.
135 @param[in] node the new file list node. */
136 void reset(flst_node_t *node) { m_node = node; }
137
138 bool is_null() const {
139 const byte zero[SIZE] = {0x00};
140 return (m_node == nullptr || memcmp(m_node, zero, SIZE) == 0);
141 }
142
143 /** Initialize the object fully. */
144 void init() {
145 set_prev_null();
146 set_next_null();
147 set_versions_null();
148 set_trx_id(0);
149 set_trx_undo_no(0);
150 set_page_no(FIL_NULL);
151 set_data_len(0);
152 }
153
154 /** Get the location of the current index entry. */
155 fil_addr_t get_self() const;
156
157 /** The versions base node is set to NULL. */
158 void set_versions_null() {
159 ut_ad(m_mtr != nullptr);
160
161 byte *base_node = get_versions_ptr();
162 flst_init(base_node, m_mtr);
163 }
164
165 /** Determine if the current index entry be rolled back.
166 @param[in] trxid the transaction that is being purged.
167 @param[in] undo_no the undo number of trx.
168 @return true if this entry can be rolled back, false otherwise. */
169 bool can_rollback(trx_id_t trxid, undo_no_t undo_no) {
170 /* For rollback, make use of creator trx id. */
171 return ((trxid == get_trx_id()) && (get_trx_undo_no() >= undo_no));
172 }
173
174 /** Determine if the current index entry be purged.
175 @param[in] trxid the transaction that is being purged.
176 @param[in] undo_no the undo number of trx.
177 @return true if this entry can be purged, false otherwise. */
178 bool can_be_purged(trx_id_t trxid, undo_no_t undo_no) {
179 return ((trxid == get_trx_id_modifier()) &&
180 (get_trx_undo_no_modifier() == undo_no));
181 }
182
183 /* The given entry becomes the old version of the current entry.
184 Move the version base node from old entry to current entry.
185 @param[in] entry the old entry */
186 void set_old_version(index_entry_t &entry) {
187 flst_node_t *node = entry.get_node_ptr();
188 flst_base_node_t *version_list = get_versions_ptr();
189 ut_ad(flst_get_len(version_list) == 0);
190
191 entry.move_version_base_node(*this);
192 flst_add_first(version_list, node, m_mtr);
193 }
194
195 /** The current index entry points to a latest LOB page. It may or
196 may not have older versions. If older version is there, bring it
197 back to the index list from the versions list. Then remove the
198 current entry from the index list. Move the versions list from
199 current entry to older entry.
200 @param[in] index the clustered index containing the LOB.
201 @param[in] first_page The first lob page containing index
202 list and free list.
203 @return the location of next entry. */
204 fil_addr_t make_old_version_current(dict_index_t *index,
205 first_page_t &first_page);
206
207 /** Purge the current entry.
208 @param[in] index the clustered index containing the LOB.
209 @param[in] lst the base node of index list.
210 @param[in] free_list the base node of free list.
211 @return the location of the next entry. */
212 fil_addr_t purge_version(dict_index_t *index, flst_base_node_t *lst,
213 flst_base_node_t *free_list);
214
215 void add_version(index_entry_t &entry) const {
216 flst_node_t *node = entry.get_node_ptr();
217 flst_base_node_t *version_list = get_versions_ptr();
218 flst_add_first(version_list, node, m_mtr);
219 }
220
221 flst_base_node_t *get_versions_list() const { return (get_versions_ptr()); }
222
223 /** Add this node as the last node in the given list.
224 @param[in] bnode the base node of the file list. */
225 void push_back(flst_base_node_t *bnode) {
226 flst_add_last(bnode, m_node, m_mtr);
227 }
228
229 /** Get the base node of the list of versions. */
230 flst_bnode_t get_versions_mem() const {
231 flst_base_node_t *node = get_versions_list();
232 return (flst_bnode_t(node, m_mtr));
233 }
234
235 trx_id_t get_trx_id() const {
236 byte *ptr = get_trxid_ptr();
237 return (mach_read_from_6(ptr));
238 }
239
240 trx_id_t get_trx_id_modifier() const {
241 byte *ptr = get_trxid_modifier_ptr();
242 return (mach_read_from_6(ptr));
243 }
244
245 undo_no_t get_trx_undo_no() const {
246 byte *ptr = get_trx_undo_no_ptr();
247 return (mach_read_from_4(ptr));
248 }
249
250 uint32_t get_lob_version() const {
251 byte *ptr = get_lob_version_ptr();
252 return (mach_read_from_4(ptr));
253 }
254
255 /** Get the undo number of the modifier trx.
256 @return the undo number of the modifier trx. */
257 undo_no_t get_trx_undo_no_modifier() const {
258 byte *ptr = get_trx_undo_no_modifier_ptr();
259 return (mach_read_from_4(ptr));
260 }
261
262 fil_addr_t get_next() const {
263 ut_ad(m_node != nullptr);
264
265 return (flst_read_addr(m_node + OFFSET_NEXT, m_mtr));
266 }
267
268 /** Make the current index entry object to point to the next index
269 entry object.
270 @return the buffer block in which the next index entry is available.*/
271 buf_block_t *next() {
272 fil_addr_t node_loc = get_next();
273
274 if (node_loc.is_null()) {
275 return (nullptr);
276 }
277
278 if (m_block == nullptr || m_block->page.id.page_no() != node_loc.page) {
279 load_x(node_loc);
280 } else {
281 /* Next entry in the same page. */
282 reset(node_loc);
283 }
284
285 return (m_block);
286 }
287
288 /** Get the previous index entry.
289 @return The file address of previous index entry. */
290 fil_addr_t get_prev() const {
291 return (flst_read_addr(m_node + OFFSET_PREV, m_mtr));
292 }
293
294 /** Write the trx identifier to the index entry. No redo log
295 is generated for this modification. This is meant to be used
296 during tablespace import.
297 @param[in] id the trx identifier.*/
298 void set_trx_id_no_redo(trx_id_t id) {
299 byte *ptr = get_trxid_ptr();
300 mach_write_to_6(ptr, id);
301 }
302
303 /** Write the modifier trx identifier to the index entry. No redo log
304 is generated for this modification. This is meant to be used
305 during tablespace import.
306 @param[in] id the trx identifier.*/
307 void set_trx_id_modifier_no_redo(trx_id_t id) {
308 byte *ptr = get_trxid_modifier_ptr();
309 mach_write_to_6(ptr, id);
310 }
311
312 void set_trx_id(trx_id_t id) {
313 byte *ptr = get_trxid_ptr();
314 mach_write_to_6(ptr, id);
315 mlog_log_string(ptr, 6, m_mtr);
316 }
317
318 void set_trx_id_modifier(trx_id_t id) {
319 ut_ad(m_mtr != nullptr);
320
321 byte *ptr = get_trxid_modifier_ptr();
322 mach_write_to_6(ptr, id);
323 mlog_log_string(ptr, 6, m_mtr);
324 }
325
326 void set_trx_undo_no(undo_no_t undo_no) {
327 byte *ptr = get_trx_undo_no_ptr();
328 mlog_write_ulint(ptr, undo_no, MLOG_4BYTES, m_mtr);
329 }
330
331 /** Set the LOB version of this entry.
332 @param[in] version the LOB version number. */
333 void set_lob_version(uint32_t version) {
334 byte *ptr = get_lob_version_ptr();
335 mlog_write_ulint(ptr, version, MLOG_4BYTES, m_mtr);
336 }
337
338 void set_trx_undo_no_modifier(undo_no_t undo_no) {
339 ut_ad(m_mtr != nullptr);
340
341 byte *ptr = get_trx_undo_no_modifier_ptr();
342 mlog_write_ulint(ptr, undo_no, MLOG_4BYTES, m_mtr);
343 }
344
345 void set_page_no(page_no_t num) {
346 ut_ad(num > 0);
347 byte *ptr = get_pageno_ptr();
348 return (mlog_write_ulint(ptr, num, MLOG_4BYTES, m_mtr));
349 }
350
351 void set_prev_null() {
352 flst_write_addr(m_node + OFFSET_PREV, fil_addr_null, m_mtr);
353 }
354
355 void set_next_null() {
356 flst_write_addr(m_node + OFFSET_NEXT, fil_addr_null, m_mtr);
357 }
358
359 page_no_t get_page_no() const {
360 byte *ptr = get_pageno_ptr();
361 return (mach_read_from_4(ptr));
362 }
363
364 void set_data_len(ulint len) {
365 byte *ptr = get_datalen_ptr();
366 return (mlog_write_ulint(ptr, len, MLOG_2BYTES, m_mtr));
367 }
368
369 ulint get_data_len() const {
370 byte *ptr = get_datalen_ptr();
371 return (mach_read_from_2(ptr));
372 }
373
374 std::ostream &print(std::ostream &out) const;
375
376 bool is_same(const index_entry_t &that) { return (m_node == that.m_node); }
377
378 void read(index_entry_mem_t &entry_mem) const;
379
380 /** Load the index entry available in the given file address.
381 Take x-latch on the index page.
382 @param[in] addr the file address of the index entry.
383 @return the buffer block containing the index entry. */
384 buf_block_t *load_x(const fil_addr_t &addr);
385
386 /** Load the index entry available in the given file address.
387 Take s-latch on the index page.
388 @param[in] addr the file location of index entry.
389 @return the buffer block. */
390 buf_block_t *load_s(const fil_addr_t &addr);
391
392 void insert_after(flst_base_node_t *base, index_entry_t &entry) {
393 flst_insert_after(base, m_node, entry.get_node(), m_mtr);
394 }
395
396 void insert_before(flst_base_node_t *base, index_entry_t &entry) {
397 flst_insert_before(base, entry.get_node(), m_node, m_mtr);
398 }
399
400 void remove(flst_base_node_t *bnode) { flst_remove(bnode, m_node, m_mtr); }
401
402 /** Free the data page pointed to by this index entry. The data page is
403 available at offset OFFSET_PAGE_NO. After the page is freed, mark it as
404 FIL_NULL. No other fields are modified. If the index entry points to the
405 first page number of the LOB or if it is FIL_NULL, then this function is a
406 no-op.
407 @param[in] first_page_no the first page number of the LOB. */
408 void free_data_page(const page_no_t first_page_no);
409
410 private:
411 /** Move the version base node from current entry to the given entry.
412 @param[in] to_entry The index entry to which the version
413 base node is moved to.*/
414 void move_version_base_node(index_entry_t &to_entry);
415
416 /** Purge the current index entry. An index entry points to either a
417 FIRST page or DATA page. That LOB page will be freed if it is DATA
418 page. A FIRST page should not be freed. */
419 void purge(dict_index_t *index);
420
421 byte *get_versions_ptr() const { return (m_node + OFFSET_VERSIONS); }
422
423 byte *get_trxid_ptr() const { return (m_node + OFFSET_TRXID); }
424
425 byte *get_trxid_modifier_ptr() const {
426 return (m_node + OFFSET_TRXID_MODIFIER);
427 }
428
429 byte *get_trx_undo_no_ptr() const { return (m_node + OFFSET_TRX_UNDO_NO); }
430
431 byte *get_lob_version_ptr() const { return (m_node + OFFSET_LOB_VERSION); }
432
433 byte *get_trx_undo_no_modifier_ptr() const {
434 return (m_node + OFFSET_TRX_UNDO_NO_MODIFIER);
435 }
436
437 byte *get_pageno_ptr() const { return (m_node + OFFSET_PAGE_NO); }
438
439 byte *get_datalen_ptr() const { return (m_node + OFFSET_DATA_LEN); }
440
441 byte *get_node_ptr() const { return (m_node); }
442
443 byte *get_node() const { return (m_node); }
444
445 private:
446 byte *m_node;
447 mtr_t *m_mtr;
448 const dict_index_t *m_index;
449 buf_block_t *m_block;
450 };
451
452 /** Overloading the global output operator to easily print an index entry.
453 @param[in] out the output stream.
454 @param[in] obj the index entry.
455 @return the output stream. */
456 inline std::ostream &operator<<(std::ostream &out, const index_entry_t &obj) {
457 return (obj.print(out));
458 }
459
460 } /* namespace lob */
461
462 #endif /* lob0index_h */