Debian Codegraph @

storage/innobase/lob/lob0util.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 <list>
29 #include "btr0btr.h"
30 #include "buf0buf.h"
31 #include "dict0dict.h"
32 #include "lob0first.h"
33 #include "lob0index.h"
34 #include "lob0lob.h"
35 #include "table.h"
36 #include "trx0trx.h"
37
38 namespace lob {
39
40 /** Allocate one LOB page.
41 @param[in] index Index in which LOB exists.
42 @param[in] lob_mtr Mini-transaction context.
43 @param[in] hint Hint page number for allocation.
44 @param[in] bulk true if operation is OPCODE_INSERT_BULK,
45 false otherwise.
46 @return the allocated block of the BLOB page or nullptr. */
47 buf_block_t *alloc_lob_page(dict_index_t *index, mtr_t *lob_mtr, page_no_t hint,
48 bool bulk) {
49 ulint r_extents;
50 mtr_t mtr_bulk;
51 mtr_t *alloc_mtr;
52 buf_block_t *block = nullptr;
53
54 space_id_t space_id = dict_index_get_space(index);
55
56 ut_ad(fsp_check_tablespace_size(space_id));
57
58 if (bulk) {
59 mtr_start(&mtr_bulk);
60 alloc_mtr = &mtr_bulk;
61 } else {
62 alloc_mtr = lob_mtr;
63 }
64
65 bool success =
66 fsp_reserve_free_extents(&r_extents, space_id, 1, FSP_BLOB, alloc_mtr, 1);
67
68 DBUG_EXECUTE_IF("innodb_alloc_lob_page_failed", success = false;);
69
70 if (!success) {
71 if (bulk) {
72 ut_ad(alloc_mtr == &mtr_bulk);
73 alloc_mtr->commit();
74 }
75 return (nullptr);
76 }
77
78 block = btr_page_alloc(index, hint, FSP_NO_DIR, 0, alloc_mtr, lob_mtr);
79
80 fil_space_release_free_extents(space_id, r_extents);
81
82 if (bulk) {
83 ut_ad(alloc_mtr == &mtr_bulk);
84 alloc_mtr->commit();
85 }
86
87 return (block);
88 }
89
90 dberr_t get_affected_index_entries(const ref_t &ref, dict_index_t *index,
91 const Binary_diff &bdiff,
92 List_iem_t &entries, mtr_t *mtr) {
93 page_no_t first_page_no = ref.page_no();
94 space_id_t space_id = ref.space_id();
95 const page_size_t page_size = dict_table_page_size(index->table);
96 const page_id_t first_page_id(space_id, first_page_no);
97 size_t offset = bdiff.offset();
98 ulint data_len = 0;
99
100 if (!dict_table_has_atomic_blobs(index->table)) {
101 /* For compact and redundant row format, remove the local
102 prefix length from the offset. */
103
104 ut_ad(offset >= DICT_ANTELOPE_MAX_INDEX_COL_LEN);
105 offset -= DICT_ANTELOPE_MAX_INDEX_COL_LEN;
106 }
107
108 /* Currently only working with uncompressed LOB */
109 ut_ad(!page_size.is_compressed());
110
111 first_page_t first_page(mtr, index);
112 first_page.load_x(first_page_id, page_size);
113
114 #ifdef UNIV_DEBUG
115 {
116 page_type_t page_type = first_page.get_page_type();
117 ut_ad(page_type == FIL_PAGE_TYPE_LOB_FIRST);
118 }
119 #endif /* UNIV_DEBUG */
120
121 /* Obtain the first index entry. */
122 flst_base_node_t *base_node = first_page.index_list();
123 fil_addr_t node_loc = flst_get_first(base_node, mtr);
124
125 buf_block_t *block = nullptr;
126 index_entry_t entry(mtr, index);
127
128 while (!fil_addr_is_null(node_loc)) {
129 if (block == nullptr) {
130 block = entry.load_x(node_loc);
131 } else if (block->page.id.page_no() != node_loc.page) {
132 block = entry.load_x(node_loc);
133 } else {
134 /* Next entry in the same page. */
135 ut_ad(block == entry.get_block());
136 entry.reset(node_loc);
137 }
138
139 /* Get the amount of data */
140 data_len = entry.get_data_len();
141
142 if (offset < data_len) {
143 index_entry_mem_t entry_mem;
144 entry.read(entry_mem);
145 entries.push_back(entry_mem);
146
147 size_t remain = data_len - offset;
148
149 if (bdiff.length() > remain) {
150 block = entry.next();
151
152 if (block != nullptr) {
153 entry.read(entry_mem);
154 entries.push_back(entry_mem);
155 }
156 }
157
158 break;
159 }
160
161 offset -= data_len;
162
163 /* The next node should not be the same as the current node. */
164 ut_ad(!node_loc.is_equal(entry.get_next()));
165
166 node_loc = entry.get_next();
167 }
168
169 ut_ad(entries.size() == 1 || entries.size() == 2);
170
171 return (DB_SUCCESS);
172 }
173
174 /** Get information about the given LOB.
175 @param[in] ref LOB reference.
176 @param[in] index Clustered index to which LOB belongs.
177 @param[out] lob_version LOB version number.
178 @param[out] last_trx_id trx_id that modified the lob last.
179 @param[out] last_undo_no Trx undo no that modified the lob last.
180 @param[out] page_type the Page type of first lob page.
181 @param[in] mtr Mini-transaction context.
182 @return always returns DB_SUCCESS. */
183 dberr_t get_info(ref_t &ref, dict_index_t *index, ulint &lob_version,
184 trx_id_t &last_trx_id, undo_no_t &last_undo_no,
185 page_type_t &page_type, mtr_t *mtr) {
186 page_no_t first_page_no = ref.page_no();
187 space_id_t space_id = ref.space_id();
188 const page_size_t page_size = dict_table_page_size(index->table);
189 const page_id_t first_page_id(space_id, first_page_no);
190
191 /* Currently only working with uncompressed LOB */
192 ut_ad(!page_size.is_compressed());
193
194 first_page_t first_page(mtr, index);
195 first_page.load_x(first_page_id, page_size);
196
197 page_type = first_page.get_page_type();
198 lob_version = first_page.get_lob_version();
199 last_trx_id = first_page.get_last_trx_id();
200 last_undo_no = first_page.get_last_trx_undo_no();
201
202 return (DB_SUCCESS);
203 }
204
205 } /* namespace lob */