Debian Codegraph @

storage/innobase/include/lob0first.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
28 #ifndef lob0first_h
29 #define lob0first_h
30
31 #include "btr0btr.h"
32 #include "buf0buf.h"
33 #include "dict0dict.h"
34 #include "fut0lst.h"
35 #include "lob0index.h"
36 #include "lob0lob.h"
37 #include "lob0util.h"
38 #include "mtr0log.h"
39
40 namespace lob {
41
42 /** The first page of an uncompressed LOB. */
43 struct first_page_t : public basic_page_t {
44 /** Version information. One byte. */
45 static const ulint OFFSET_VERSION = FIL_PAGE_DATA;
46
47 /** One byte of flag bits. Currently only one bit (the least
48 significant bit) is used, other 7 bits are available for future use.*/
49 static const ulint OFFSET_FLAGS = FIL_PAGE_DATA + 1;
50
51 /** LOB version. 4 bytes.*/
52 static const uint32_t OFFSET_LOB_VERSION = OFFSET_FLAGS + 1;
53
54 /** The latest transaction that modified this LOB. */
55 static const ulint OFFSET_LAST_TRX_ID = OFFSET_LOB_VERSION + 4;
56
57 /** The latest transaction undo_no that modified this LOB. */
58 static const ulint OFFSET_LAST_UNDO_NO = OFFSET_LAST_TRX_ID + 6;
59
60 /** Length of data stored in this page. 4 bytes. */
61 static const ulint OFFSET_DATA_LEN = OFFSET_LAST_UNDO_NO + 4;
62
63 /** The trx that created the data stored in this page. */
64 static const ulint OFFSET_TRX_ID = OFFSET_DATA_LEN + 4;
65
66 /** The offset where the list base node is located. This is the list
67 of LOB pages. */
68 static const ulint OFFSET_INDEX_LIST = OFFSET_TRX_ID + 6;
69
70 /** The offset where the list base node is located. This is the list
71 of free nodes. */
72 static const ulint OFFSET_INDEX_FREE_NODES =
73 OFFSET_INDEX_LIST + FLST_BASE_NODE_SIZE;
74
75 /** The offset where the contents of the first page begins. */
76 static const ulint LOB_PAGE_DATA =
77 OFFSET_INDEX_FREE_NODES + FLST_BASE_NODE_SIZE;
78
79 static const ulint LOB_PAGE_TRAILER_LEN = FIL_PAGE_DATA_END;
80
81 /** The default constructor. */
82 first_page_t() = default;
83
84 /** Constructor.
85 @param[in] block Buffer block of the first page.
86 @param[in] mtr Mini-transaction context. */
87 first_page_t(buf_block_t *block, mtr_t *mtr) : basic_page_t(block, mtr) {}
88
89 /** Constructor.
90 @param[in] block the buffer block of the first page.*/
91 first_page_t(buf_block_t *block) : basic_page_t(block, nullptr) {}
92
93 /** Constructor.
94 @param[in] block Buffer block of the first page.
95 @param[in] mtr Mini-transaction context.
96 @param[in] index Clustered index containing the LOB. */
97 first_page_t(buf_block_t *block, mtr_t *mtr, dict_index_t *index)
98 : basic_page_t(block, mtr, index) {}
99
100 /** Constructor.
101 @param[in] mtr Mini-transaction context.
102 @param[in] index Clustered index containing the LOB. */
103 first_page_t(mtr_t *mtr, dict_index_t *index)
104 : basic_page_t(nullptr, mtr, index) {}
105
106 /** Set the LOB format version number to 0. */
107 void set_version_0() {
108 mlog_write_ulint(frame() + OFFSET_VERSION, 0, MLOG_1BYTE, m_mtr);
109 }
110
111 /** Obtain the flags value. This has 8 bits of which only the first
112 bit is used.
113 @return one byte flag */
114 uint8_t get_flags() { return (mach_read_from_1(frame() + OFFSET_FLAGS)); }
115
116 /** When the bit is set, the LOB is not partially updatable anymore.
117 @return true, if partially updatable.
118 @return false, if partially NOT updatable. */
119 bool can_be_partially_updated() {
120 uint8_t flags = get_flags();
121 return (!(flags & 0x01));
122 }
123
124 /** Do tablespace import. */
125 void import(trx_id_t trx_id);
126
127 /** When the bit is set, the LOB is not partially updatable anymore.
128 Enable the bit.
129 @param[in] trx the current transaction. */
130 void mark_cannot_be_partially_updated(trx_t *trx);
131
132 /** Allocate the first page for uncompressed LOB.
133 @param[in,out] alloc_mtr the allocation mtr.
134 @param[in] is_bulk true if it is bulk operation.
135 (OPCODE_INSERT_BULK)
136 return the allocated buffer block.*/
137 buf_block_t *alloc(mtr_t *alloc_mtr, bool is_bulk);
138
139 /** Free all the index pages. The list of index pages can be accessed
140 by traversing via the FIL_PAGE_NEXT field.*/
141 void free_all_index_pages();
142
143 /** Free all the data pages. The data pages can be accessed through
144 index entry. */
145 void free_all_data_pages();
146
147 /** Load the first page of LOB with s-latch.
148 @param[in] page_id the page identifier of the first page.
149 @param[in] page_size the page size information.
150 @return the buffer block of the first page. */
151 buf_block_t *load_s(page_id_t page_id, page_size_t page_size) {
152 m_block =
153 buf_page_get(page_id, page_size, RW_S_LATCH, UT_LOCATION_HERE, m_mtr);
154 return (m_block);
155 }
156
157 /** Load the first page of LOB with x-latch.
158 @param[in] page_id Page identifier of the first page.
159 @param[in] page_size Page size information.
160 @param[in] mtr Mini-transaction context for latch.
161 @return the buffer block of the first page. */
162 buf_block_t *load_x(const page_id_t &page_id, const page_size_t &page_size,
163 mtr_t *mtr);
164
165 /** Load the first page of LOB with x-latch in the given mtr context.
166 The first page must already be x-latched by the m_mtr.
167 @param[in] mtr Mini-transaction context for latch.
168 @return the buffer block of the first page. */
169 buf_block_t *load_x(mtr_t *mtr) const {
170 ut_ad(mtr_memo_contains(m_mtr, m_block, MTR_MEMO_PAGE_X_FIX));
171 buf_block_t *tmp = buf_page_get(m_block->page.id, m_index->get_page_size(),
172 RW_X_LATCH, UT_LOCATION_HERE, mtr);
173 ut_ad(tmp == m_block);
174 return (tmp);
175 }
176
177 /** Load the first page of LOB with x-latch.
178 @param[in] page_id the page identifier of the first page.
179 @param[in] page_size the page size information.
180 @return the buffer block of the first page. */
181 buf_block_t *load_x(const page_id_t &page_id, const page_size_t &page_size) {
182 return (load_x(page_id, page_size, m_mtr));
183 }
184
185 /** Get the buffer block of the LOB first page.
186 @return the buffer block. */
187 buf_block_t *get_block() { return (m_block); }
188
189 /** Load the file list node from the given location. An x-latch is taken
190 on the page containing the file list node.
191 @param[in] addr Location of file list node.
192 @param[in] mtr Mini-transaction context to be used.
193 @return the file list node.*/
194 flst_node_t *addr2ptr_x(const fil_addr_t &addr, mtr_t *mtr) const;
195
196 /** Load the file list node from the given location. An x-latch is taken
197 on the page containing the file list node.
198 @param[in] addr the location of file list node.
199 @return the file list node.*/
200 flst_node_t *addr2ptr_x(fil_addr_t &addr) const {
201 return (addr2ptr_x(addr, m_mtr));
202 }
203
204 /** Load the file list node from the given location, assuming that it
205 exists in the first page itself.
206 @param[in] addr the location of file list node.
207 @return the file list node.*/
208 flst_node_t *addr2ptr(const fil_addr_t &addr) {
209 ut_ad(m_block->page.id.page_no() == addr.page);
210 return (buf_block_get_frame(m_block) + addr.boffset);
211 }
212
213 /** Load the file list node from the given location. An s-latch is taken
214 on the page containing the file list node.
215 @param[in] addr the location of file list node.
216 @return the file list node.*/
217 flst_node_t *addr2ptr_s(fil_addr_t &addr) {
218 space_id_t space = dict_index_get_space(m_index);
219 const page_size_t page_size = dict_table_page_size(m_index->table);
220 return (fut_get_ptr(space, page_size, addr, RW_S_LATCH, m_mtr));
221 }
222
223 /** Load the file list node from the given location. An s-latch is taken
224 on the page containing the file list node. The given cache is checked to
225 see if the page is already loaded.
226 @param[in] cache cache of loaded buffer blocks.
227 @param[in] addr the location of file list node.
228 @return the file list node.*/
229 flst_node_t *addr2ptr_s_cache(std::map<page_no_t, buf_block_t *> &cache,
230 fil_addr_t &addr) const {
231 byte *result;
232 space_id_t space = dict_index_get_space(m_index);
233 const page_size_t page_size = dict_table_page_size(m_index->table);
234
235 auto iter = cache.find(addr.page);
236
237 if (iter == cache.end()) {
238 /* Not there in cached blocks. Add the loaded block to cache. */
239 buf_block_t *block = nullptr;
240 result = fut_get_ptr(space, page_size, addr, RW_S_LATCH, m_mtr, &block);
241 cache.insert(std::make_pair(addr.page, block));
242 } else {
243 buf_block_t *block = iter->second;
244 ut_ad(block->page.id.page_no() == addr.page);
245 result = buf_block_get_frame(block) + addr.boffset;
246 }
247 return (result);
248 }
249
250 /** Free the first page. This is done when all other LOB pages have
251 been freed. */
252 void dealloc();
253
254 /** Free all the pages associated with this LOB. */
255 void destroy();
256
257 /** Free all the pages associated with this LOB, except the first page. */
258 void make_empty();
259
260 /** Check if the index list is empty or not.
261 @return true if empty, false otherwise. */
262 bool is_empty() const {
263 flst_base_node_t *base = index_list();
264 ut_ad(base != nullptr);
265 return (flst_get_len(base) == 0);
266 }
267
268 /** Allocate one index entry. If required an index page (of type
269 FIL_PAGE_TYPE_LOB_INDEX) will be allocated.
270 @param[in] bulk true if it is a bulk operation
271 (OPCODE_INSERT_BULK), false otherwise.
272 @return the file list node of the index entry. */
273 flst_node_t *alloc_index_entry(bool bulk);
274
275 /** Get a pointer to the beginning of the index entry nodes in the
276 first part of the page.
277 @return the first index entry node. */
278 byte *nodes_begin() const { return (frame() + LOB_PAGE_DATA); }
279
280 /** Calculate and return the payload.
281 @return the payload possible in this page. */
282 static ulint payload() {
283 return (UNIV_PAGE_SIZE - LOB_PAGE_DATA - LOB_PAGE_TRAILER_LEN);
284 }
285
286 /** Set the transaction identifier in the first page header without
287 generating redo logs.
288 @param[in] id the transaction identifier. */
289 void set_trx_id_no_redo(trx_id_t id) {
290 byte *ptr = frame() + OFFSET_TRX_ID;
291 mach_write_to_6(ptr, id);
292 }
293
294 /** Set the transaction identifier in the first page header.
295 @param[in] id the transaction identifier. */
296 void set_trx_id(trx_id_t id) {
297 byte *ptr = frame() + OFFSET_TRX_ID;
298 mach_write_to_6(ptr, id);
299 mlog_log_string(ptr, 6, m_mtr);
300 }
301
302 /** Initialize the LOB version to 1. */
303 void init_lob_version() {
304 ut_ad(m_mtr != nullptr);
305
306 mlog_write_ulint(frame() + OFFSET_LOB_VERSION, 1, MLOG_4BYTES, m_mtr);
307 }
308
309 /** Get the lob version number.
310 @return the lob version. */
311 uint32_t get_lob_version() {
312 return (mach_read_from_4(frame() + OFFSET_LOB_VERSION));
313 }
314
315 /** Increment the lob version by 1. */
316 uint32_t incr_lob_version();
317
318 /** Set the last transaction identifier, without generating redo log
319 records.
320 @param[in] id the trx identifier. */
321 void set_last_trx_id_no_redo(trx_id_t id) {
322 byte *ptr = frame() + OFFSET_LAST_TRX_ID;
323 mach_write_to_6(ptr, id);
324 }
325
326 /** Set the last transaction identifier.
327 @param[in] id the trx identifier. */
328 void set_last_trx_id(trx_id_t id) {
329 byte *ptr = frame() + OFFSET_LAST_TRX_ID;
330 mach_write_to_6(ptr, id);
331 mlog_log_string(ptr, 6, m_mtr);
332 }
333
334 /** Set the last transaction undo number.
335 @param[in] undo_no the trx undo number. */
336 void set_last_trx_undo_no(undo_no_t undo_no) {
337 ut_ad(m_mtr != nullptr);
338
339 byte *ptr = frame() + OFFSET_LAST_UNDO_NO;
340 mlog_write_ulint(ptr, undo_no, MLOG_4BYTES, m_mtr);
341 }
342
343 /** Get the last transaction identifier.
344 @return the transaction identifier. */
345 trx_id_t get_last_trx_id() const {
346 byte *ptr = frame() + OFFSET_LAST_TRX_ID;
347 return (mach_read_from_6(ptr));
348 }
349
350 /** Get the last transaction undo number.
351 @return the transaction undo number. */
352 undo_no_t get_last_trx_undo_no() const {
353 byte *ptr = frame() + OFFSET_LAST_UNDO_NO;
354 return (mach_read_from_4(ptr));
355 }
356
357 /** Set the length of data stored in bytes.
358 @param[in] len amount of data stored in bytes. */
359 void set_data_len(ulint len) {
360 ut_ad(m_mtr != nullptr);
361
362 mlog_write_ulint(frame() + OFFSET_DATA_LEN, len, MLOG_4BYTES, m_mtr);
363 }
364
365 /** Write as much as possible of the given data into the page.
366 @param[in] trxid the current transaction.
367 @param[in] data the data to be written.
368 @param[in] len the length of the given data.
369 @return number of bytes actually written. */
370 ulint write(trx_id_t trxid, const byte *&data, ulint &len);
371
372 /** Replace data in the page by making a copy-on-write.
373 @param[in] trx Current transaction.
374 @param[in] offset Location where replace operation starts.
375 @param[in,out] ptr Buffer containing new data. after the call it will
376 point to remaining data.
377 @param[in,out] want Requested amount of data to be replaced. After the
378 call it will contain amount of data yet to be replaced.
379 @param[in] mtr Mini-transaction context.
380 @return the newly allocated buffer block.
381 @return nullptr if new page could not be allocated
382 (DB_OUT_OF_FILE_SPACE). */
383 buf_block_t *replace(trx_t *trx, ulint offset, const byte *&ptr, ulint &want,
384 mtr_t *mtr);
385
386 /** Replace data in the page inline.
387 @param[in] offset Location where replace operation starts.
388 @param[in,out] ptr Buffer containing new data. after the
389 call it will point to remaining data.
390 @param[in,out] want Requested amount of data to be replaced.
391 after the call it will contain amount of
392 data yet to be replaced.
393 @param[in] mtr Mini-transaction context.*/
394 void replace_inline(ulint offset, const byte *&ptr, ulint &want, mtr_t *mtr);
395
396 ulint get_data_len() const {
397 return (mach_read_from_4(frame() + OFFSET_DATA_LEN));
398 }
399
400 /** Read data from the first page.
401 @param[in] offset the offset from where read starts.
402 @param[out] ptr the output buffer
403 @param[in] want number of bytes to read.
404 @return number of bytes read. */
405 ulint read(ulint offset, byte *ptr, ulint want);
406
407 void set_page_type() {
408 ut_ad(m_mtr != nullptr);
409
410 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_LOB_FIRST,
411 MLOG_2BYTES, m_mtr);
412 }
413
414 flst_base_node_t *index_list() const { return (frame() + OFFSET_INDEX_LIST); }
415
416 flst_base_node_t *free_list() const {
417 return (frame() + OFFSET_INDEX_FREE_NODES);
418 }
419
420 /** Get the number of bytes used to store LOB data in the first page
421 of uncompressed LOB.
422 @return Number of bytes available for LOB data. */
423 static ulint max_space_available() {
424 const ulint index_array_size = node_count() * index_entry_t::SIZE;
425
426 return (payload() - index_array_size);
427 }
428
429 /** Get the number of index entries this page can hold.
430 @return Number of index entries this page can hold. */
431 constexpr static ulint node_count() {
432 /* Each index entry is of size 60 bytes. We store only 10
433 index entries in the first page of the LOB. This means that
434 only 600 bytes are used for index data in the first page of
435 LOB. This will help to reserve more space in the first page
436 for the LOB data.*/
437 return (10);
438 }
439
440 std::ostream &print_index_entries(std::ostream &out) const;
441
442 std::ostream &print_index_entries_cache_s(std::ostream &out,
443 BlockCache &cache) const;
444
445 /** Obtain the location where the data begins.
446 @return pointer to location within page where data begins. */
447 byte *data_begin() const {
448 ut_ad(buf_block_get_page_zip(m_block) == nullptr);
449
450 constexpr ulint index_array_size = node_count() * index_entry_t::SIZE;
451
452 return (frame() + LOB_PAGE_DATA + index_array_size);
453 }
454
455 /** Append data into a LOB first page. */
456 ulint append(trx_id_t trxid, byte *&data, ulint &len);
457
458 #ifdef UNIV_DEBUG
459 /** Validate the first page. */
460 bool validate() const;
461 #endif /* UNIV_DEBUG */
462
463 page_type_t get_page_type() { return (basic_page_t::get_page_type()); }
464
465 static page_type_t get_page_type(dict_index_t *index,
466 const page_id_t &page_id,
467 const page_size_t &page_size) {
468 mtr_t local_mtr;
469 mtr_start(&local_mtr);
470 first_page_t first(&local_mtr, index);
471 first.load_x(page_id, page_size);
472 page_type_t page_type = first.get_page_type();
473 mtr_commit(&local_mtr);
474 return (page_type);
475 }
476
477 public:
478 /** Restart the given mtr. The first page must already be x-latched by
479 the m_mtr.
480 @param[in] mtr Mini-transaction context which is to be restarted. */
481 void restart_mtr(mtr_t *mtr) {
482 ut_ad(mtr != m_mtr);
483 mtr_commit(mtr);
484 mtr_start(mtr);
485 mtr->set_log_mode(m_mtr->get_log_mode());
486 load_x(mtr);
487 }
488 };
489
490 } /* namespace lob */
491
492 #endif /* lob0first_h */