Debian Codegraph @

storage/innobase/lob/lob0first.cc

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 #include "lob0first.h"
29 #include "lob0impl.h"
30 #include "lob0index.h"
31 #include "lob0pages.h"
32 #include "trx0trx.h"
33
34 namespace lob {
35
36 void first_page_t::replace_inline(ulint offset, const byte *&ptr, ulint &want,
37 mtr_t *mtr) {
38 byte *old_ptr = data_begin();
39 old_ptr += offset;
40
41 ulint data_avail = get_data_len() - offset;
42 ulint data_to_copy = (want > data_avail) ? data_avail : want;
43 mlog_write_string(old_ptr, ptr, data_to_copy, mtr);
44
45 ptr += data_to_copy;
46 want -= data_to_copy;
47 }
48
49 /** Replace data in the page by making a copy-on-write.
50 @param[in] trx Current transaction.
51 @param[in] offset Location where replace operation starts.
52 @param[in,out] ptr Buffer containing new data. after the call it will
53 point to remaining data.
54 @param[in,out] want Requested amount of data to be replaced. After the
55 call it will contain amount of data yet to be replaced.
56 @param[in] mtr Mini-transaction context.
57 @return the newly allocated buffer block.
58 @return nullptr if new page could not be allocated
59 (DB_OUT_OF_FILE_SPACE). */
60 buf_block_t *first_page_t::replace(trx_t *trx, ulint offset, const byte *&ptr,
61 ulint &want, mtr_t *mtr) {
62 DBUG_TRACE;
63
64 buf_block_t *new_block = nullptr;
65
66 /** Allocate a new data page. */
67 data_page_t new_page(mtr, m_index);
68 new_block = new_page.alloc(mtr, false);
69
70 DBUG_EXECUTE_IF("innodb_lob_first_page_replace_failed", new_block = nullptr;);
71
72 if (new_block == nullptr) {
73 return nullptr;
74 }
75
76 byte *new_ptr = new_page.data_begin();
77 byte *old_ptr = data_begin();
78
79 DBUG_LOG("first_page_t", PrintBuffer(old_ptr, get_data_len()));
80 DBUG_LOG("first_page_t", PrintBuffer(ptr, want));
81
82 new_page.set_trx_id(trx->id);
83 new_page.set_data_len(get_data_len());
84
85 /** Copy contents from old page to new page. */
86 mlog_write_string(new_ptr, old_ptr, offset, mtr);
87
88 new_ptr += offset;
89 old_ptr += offset;
90
91 /** Copy the new data to new page. */
92 ulint data_avail = get_data_len() - offset;
93 ulint data_to_copy = (want > data_avail) ? data_avail : want;
94 mlog_write_string(new_ptr, ptr, data_to_copy, mtr);
95
96 new_ptr += data_to_copy;
97 old_ptr += data_to_copy;
98 ptr += data_to_copy;
99
100 #ifdef UNIV_DEBUG
101 /* The old data that is being replaced. */
102 DBUG_LOG("lob::first_page_t",
103 "old data=" << PrintBuffer(old_ptr - data_to_copy, data_to_copy));
104 /* The new data that replaces old data. */
105 DBUG_LOG("lob::first_page_t",
106 "new data=" << PrintBuffer(new_ptr - data_to_copy, data_to_copy));
107 #endif /* UNIV_DEBUG */
108
109 /** Copy contents from old page to new page. */
110 if (want < data_avail) {
111 ut_ad(data_to_copy == want);
112 ulint remain = data_avail - want;
113 mlog_write_string(new_ptr, old_ptr, remain, mtr);
114 }
115
116 DBUG_LOG("first_page_t",
117 PrintBuffer(new_page.data_begin(), new_page.get_data_len()));
118
119 want -= data_to_copy;
120
121 return new_block;
122 }
123
124 std::ostream &first_page_t::print_index_entries_cache_s(
125 std::ostream &out, BlockCache &cache) const {
126 if (m_block == nullptr) {
127 return (out);
128 }
129
130 flst_base_node_t *base = index_list();
131 fil_addr_t node_loc = flst_get_first(base, m_mtr);
132 ulint n_entries = flst_get_len(base);
133
134 out << "[n_entries=" << n_entries << ", " << std::endl;
135 while (!fil_addr_is_null(node_loc)) {
136 flst_node_t *node = addr2ptr_s_cache(cache, node_loc);
137 index_entry_t entry(node, m_mtr, m_index);
138
139 flst_base_node_t *vers = entry.get_versions_list();
140 fil_addr_t ver_loc = flst_get_first(vers, m_mtr);
141
142 out << entry << std::endl;
143
144 uint32_t depth = 0;
145 while (!fil_addr_is_null(ver_loc)) {
146 depth++;
147
148 for (uint32_t i = 0; i < depth; ++i) {
149 out << "+";
150 }
151 flst_node_t *ver_node = addr2ptr_s_cache(cache, ver_loc);
152 index_entry_t vers_entry(ver_node, m_mtr, m_index);
153 out << vers_entry << std::endl;
154 ver_loc = vers_entry.get_next();
155 }
156
157 node_loc = entry.get_next();
158 }
159
160 out << "]" << std::endl;
161 return (out);
162 }
163
164 std::ostream &first_page_t::print_index_entries(std::ostream &out) const {
165 space_id_t space = dict_index_get_space(m_index);
166 const page_size_t page_size = dict_table_page_size(m_index->table);
167
168 if (m_block == nullptr) {
169 return (out);
170 }
171
172 flst_base_node_t *base = index_list();
173 fil_addr_t node_loc = flst_get_first(base, m_mtr);
174 ulint n_entries = flst_get_len(base);
175
176 out << "[n_entries=" << n_entries << ", " << std::endl;
177 while (!fil_addr_is_null(node_loc)) {
178 flst_node_t *node =
179 fut_get_ptr(space, page_size, node_loc, RW_X_LATCH, m_mtr);
180 index_entry_t entry(node, m_mtr, m_index);
181
182 flst_base_node_t *vers = entry.get_versions_list();
183 fil_addr_t ver_loc = flst_get_first(vers, m_mtr);
184
185 out << entry << std::endl;
186
187 uint32_t depth = 0;
188 while (!fil_addr_is_null(ver_loc)) {
189 depth++;
190
191 for (uint32_t i = 0; i < depth; ++i) {
192 out << "+";
193 }
194 flst_node_t *ver_node = addr2ptr_x(ver_loc);
195 index_entry_t vers_entry(ver_node, m_mtr, m_index);
196 out << vers_entry << std::endl;
197 ver_loc = vers_entry.get_next();
198 }
199
200 node_loc = entry.get_next();
201 }
202
203 out << "]" << std::endl;
204 return (out);
205 }
206
207 #ifdef UNIV_DEBUG
208 bool first_page_t::validate() const {
209 flst_base_node_t *idx_list = index_list();
210 ut_d(flst_validate(idx_list, m_mtr));
211 return (true);
212 }
213 #endif /* UNIV_DEBUG */
214
215 /** Allocate the first page for uncompressed LOB.
216 @param[in,out] alloc_mtr the allocation mtr.
217 @param[in] is_bulk true if it is bulk operation.
218 (OPCODE_INSERT_BULK)
219 return the allocated buffer block.*/
220 buf_block_t *first_page_t::alloc(mtr_t *alloc_mtr, bool is_bulk) {
221 ut_ad(m_index != nullptr);
222 ut_ad(m_block == nullptr);
223
224 page_no_t hint = FIL_NULL;
225 m_block = alloc_lob_page(m_index, alloc_mtr, hint, is_bulk);
226
227 if (m_block == nullptr) {
228 return (nullptr);
229 }
230
231 ut_ad(rw_lock_get_x_lock_count(&m_block->lock) == 1);
232
233 /* After allocation, first set the page type. */
234 set_page_type();
235
236 set_version_0();
237 set_data_len(0);
238 set_trx_id(0);
239 byte *free_lst = free_list();
240 byte *index_lst = index_list();
241 flst_init(index_lst, m_mtr);
242 flst_init(free_lst, m_mtr);
243
244 ulint nc = node_count();
245
246 byte *cur = nodes_begin();
247 for (ulint i = 0; i < nc; ++i) {
248 index_entry_t entry(cur, m_mtr, m_index);
249 entry.init();
250 flst_add_last(free_lst, cur, m_mtr);
251 cur += index_entry_t::SIZE;
252 }
253 ut_d(flst_validate(free_lst, m_mtr));
254 set_next_page_null();
255 ut_ad(get_page_type() == FIL_PAGE_TYPE_LOB_FIRST);
256 return (m_block);
257 }
258
259 /** Allocate one index entry. If required an index page (of type
260 FIL_PAGE_TYPE_LOB_INDEX) will be allocated.
261 @param[in] bulk true if it is a bulk operation
262 (OPCODE_INSERT_BULK), false otherwise.
263 @return the file list node of the index entry. */
264 flst_node_t *first_page_t::alloc_index_entry(bool bulk) {
265 flst_base_node_t *f_list = free_list();
266 fil_addr_t node_addr = flst_get_first(f_list, m_mtr);
267 if (fil_addr_is_null(node_addr)) {
268 node_page_t node_page(m_mtr, m_index);
269 buf_block_t *block = node_page.alloc(*this, bulk);
270
271 if (block == nullptr) {
272 return (nullptr);
273 }
274
275 node_addr = flst_get_first(f_list, m_mtr);
276 }
277 flst_node_t *node = addr2ptr_x(node_addr);
278 flst_remove(f_list, node, m_mtr);
279 return (node);
280 }
281
282 void first_page_t::free_all_data_pages() {
283 mtr_t local_mtr;
284 mtr_start(&local_mtr);
285 local_mtr.set_log_mode(m_mtr->get_log_mode());
286 load_x(&local_mtr);
287 index_entry_t cur_entry(&local_mtr, m_index);
288 flst_base_node_t *flst = index_list();
289 fil_addr_t node_loc = flst_get_first(flst, &local_mtr);
290
291 const page_no_t first_page_no = get_page_no();
292 while (!fil_addr_is_null(node_loc)) {
293 flst_node_t *node = addr2ptr_x(node_loc, &local_mtr);
294 cur_entry.reset(node);
295 cur_entry.free_data_page(first_page_no);
296
297 flst_base_node_t *vers = cur_entry.get_versions_list();
298 fil_addr_t ver_loc = flst_get_first(vers, &local_mtr);
299
300 while (!fil_addr_is_null(ver_loc)) {
301 flst_node_t *ver_node = addr2ptr_x(ver_loc, &local_mtr);
302 index_entry_t vers_entry(ver_node, &local_mtr, m_index);
303 vers_entry.free_data_page(first_page_no);
304 ver_loc = vers_entry.get_next();
305
306 ut_ad(!local_mtr.conflicts_with(m_mtr));
307 restart_mtr(&local_mtr);
308 node = addr2ptr_x(node_loc, &local_mtr);
309 cur_entry.reset(node);
310 }
311 cur_entry.set_versions_null();
312 node_loc = cur_entry.get_next();
313 cur_entry.reset(nullptr);
314
315 ut_ad(!local_mtr.conflicts_with(m_mtr));
316 restart_mtr(&local_mtr);
317 }
318 flst_init(flst, &local_mtr);
319 byte *free_lst = free_list();
320 flst_init(free_lst, &local_mtr);
321 ut_ad(!local_mtr.conflicts_with(m_mtr));
322 mtr_commit(&local_mtr);
323 }
324
325 void first_page_t::free_all_index_pages() {
326 mtr_t local_mtr;
327 mtr_start(&local_mtr);
328 local_mtr.set_log_mode(m_mtr->get_log_mode());
329 load_x(&local_mtr);
330
331 space_id_t space_id = dict_index_get_space(m_index);
332 page_size_t page_size(dict_table_page_size(m_index->table));
333
334 while (true) {
335 page_no_t page_no = get_next_page();
336
337 if (page_no == FIL_NULL) {
338 break;
339 }
340
341 node_page_t index_page(&local_mtr, m_index);
342 page_id_t page_id(space_id, page_no);
343 index_page.load_x(page_id, page_size);
344 page_no_t next_page = index_page.get_next_page();
345 set_next_page(next_page, &local_mtr);
346 index_page.dealloc();
347
348 ut_ad(!local_mtr.conflicts_with(m_mtr));
349 restart_mtr(&local_mtr);
350 }
351
352 ut_ad(!local_mtr.conflicts_with(m_mtr));
353 mtr_commit(&local_mtr);
354 }
355
356 /** Load the first page of LOB with x-latch.
357 @param[in] page_id Page identifier of the first page.
358 @param[in] page_size Page size information.
359 @param[in] mtr Mini-transaction context for latch.
360 @return the buffer block of the first page. */
361 buf_block_t *first_page_t::load_x(const page_id_t &page_id,
362 const page_size_t &page_size, mtr_t *mtr) {
363 m_block = buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr);
364
365 ut_ad(m_block != nullptr);
366 #ifdef UNIV_DEBUG
367 /* Dump the page into the log file, if the page type is not
368 matching one of the first page types. */
369 page_type_t page_type = get_page_type();
370 switch (page_type) {
371 case FIL_PAGE_TYPE_BLOB:
372 case FIL_PAGE_TYPE_ZBLOB:
373 case FIL_PAGE_TYPE_LOB_FIRST:
374 case FIL_PAGE_TYPE_ZLOB_FIRST:
375 case FIL_PAGE_SDI_ZBLOB:
376 case FIL_PAGE_SDI_BLOB:
377 /* Valid first page type.*/
378 case FIL_PAGE_TYPE_ZBLOB2:
379 /* Because of partially purged ZBLOB, we might see this page type as
380 the first page of the ZBLOB. */
381 break;
382 default:
383 std::cerr << "Unexpected LOB first page type=" << page_type << std::endl;
384 ut_print_buf(std::cerr, m_block->frame, page_size.physical());
385 ut_error;
386 }
387 #endif /* UNIV_DEBUG */
388 return (m_block);
389 }
390
391 /** Increment the lob version number by 1. */
392 uint32_t first_page_t::incr_lob_version() {
393 ut_ad(m_mtr != nullptr);
394
395 const uint32_t cur = get_lob_version();
396 const uint32_t val = cur + 1;
397 mlog_write_ulint(frame() + OFFSET_LOB_VERSION, val, MLOG_4BYTES, m_mtr);
398 return (val);
399 }
400
401 /** When the bit is set, the LOB is not partially updatable anymore.
402 Enable the bit. */
403 void first_page_t::mark_cannot_be_partially_updated(trx_t *trx) {
404 const trx_id_t trxid = (trx == nullptr) ? 0 : trx->id;
405 const undo_no_t undo_no = (trx == nullptr) ? 0 : (trx->undo_no - 1);
406
407 uint8_t flags = get_flags();
408 flags |= 0x01;
409 mlog_write_ulint(frame() + OFFSET_FLAGS, flags, MLOG_1BYTE, m_mtr);
410
411 set_last_trx_id(trxid);
412 set_last_trx_undo_no(undo_no);
413 }
414
415 /** Read data from the first page.
416 @param[in] offset the offset from where read starts.
417 @param[out] ptr the output buffer
418 @param[in] want number of bytes to read.
419 @return number of bytes read. */
420 ulint first_page_t::read(ulint offset, byte *ptr, ulint want) {
421 byte *start = data_begin();
422 start += offset;
423 ulint avail_data = get_data_len() - offset;
424
425 ulint copy_len = (want < avail_data) ? want : avail_data;
426 memcpy(ptr, start, copy_len);
427 return (copy_len);
428 }
429
430 /** Write as much as possible of the given data into the page.
431 @param[in] trxid the current transaction.
432 @param[in] data the data to be written.
433 @param[in] len the length of the given data.
434 @return number of bytes actually written. */
435 ulint first_page_t::write(trx_id_t trxid, const byte *&data, ulint &len) {
436 byte *ptr = data_begin();
437 ulint written = (len > max_space_available()) ? max_space_available() : len;
438
439 /* Write the data into the page. */
440 mlog_write_string(ptr, data, written, m_mtr);
441
442 set_data_len(written);
443 set_trx_id(trxid);
444
445 data += written;
446 len -= written;
447
448 return (written);
449 }
450
451 void first_page_t::import(trx_id_t trx_id) {
452 set_trx_id_no_redo(trx_id);
453 set_last_trx_id_no_redo(trx_id);
454
455 byte *cur = nodes_begin();
456 ulint nc = node_count();
457
458 for (ulint i = 0; i < nc; ++i) {
459 index_entry_t entry(cur, m_mtr, m_index);
460 entry.set_trx_id_no_redo(trx_id);
461 entry.set_trx_id_modifier_no_redo(trx_id);
462
463 cur += index_entry_t::SIZE;
464 }
465 }
466
467 void first_page_t::dealloc() {
468 ut_ad(m_mtr != nullptr);
469 ut_ad(get_next_page() == FIL_NULL);
470
471 btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
472 m_block = nullptr;
473 }
474
475 void first_page_t::destroy() {
476 make_empty();
477 dealloc();
478 }
479
480 void first_page_t::make_empty() {
481 free_all_data_pages();
482 free_all_index_pages();
483 }
484
485 flst_node_t *first_page_t::addr2ptr_x(const fil_addr_t &addr,
486 mtr_t *mtr) const {
487 const space_id_t space = dict_index_get_space(m_index);
488 const page_size_t page_size = dict_table_page_size(m_index->table);
489 buf_block_t *block = nullptr;
490 flst_node_t *result =
491 fut_get_ptr(space, page_size, addr, RW_X_LATCH, mtr, &block);
492 const page_type_t type = block->get_page_type();
493 ut_a(type == FIL_PAGE_TYPE_LOB_FIRST || type == FIL_PAGE_TYPE_LOB_INDEX);
494 return result;
495 }
496
497 } // namespace lob