Debian Codegraph @

unittest/gunit/innodb/lob/lob0int.cc

raw symbols SCIP
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 "lob0int.h"
29
30 #ifdef UNIV_DEBUG
31 #define Fname(x) const char *fname = x;
32 #define LOG(x) \
33 { std::cout << fname << ":" << x << std::endl; }
34 #else
35 #define Fname(x)
36 #define LOG(x)
37 #endif /* UNIV_DEBUG */
38
39 namespace lob {
40
41 static buf_block_t *create_first_page() {
42 Fname("lob::create_first_page");
43
44 /* Allocate the page. */
45 buf_block_t *block = btr_page_alloc();
46 base_node_page_t page(block);
47
48 byte *free_list = page.free_list();
49 byte *index_list = page.index_list();
50 flst_init(index_list);
51 flst_init(free_list);
52
53 ulint node_count = page.node_count();
54 LOG("Number of LOB index entries = " << node_count);
55
56 byte *cur = page.nodes_begin();
57 for (ulint i = 0; i < node_count; ++i) {
58 flst_add_last(free_list, cur);
59 cur += index_entry_t::SIZE;
60 }
61 ut_a(flst_validate(free_list));
62
63 return (block);
64 }
65
66 buf_block_t *base_node_page_t::alloc() {
67 ut_ad(m_block == nullptr);
68 m_block = create_first_page();
69 set_page_type();
70 set_next_page_null();
71 return (m_block);
72 }
73
74 /** Insert data in the middle of the given LOB page.
75 @param[in] lob_page A large object page of type FIL_PAGE_TYPE_LOB_FIRST or
76 FIL_PAGE_TYPE_LOB_DATA.
77 @param[in] trxid The transaction identifier.
78 @param[in] offset The given data must be inserted at this offset within the
79 given page.
80 @param[in] data The new data that needs to be inserted in the middle.
81 @param[in] len The length of the new data in bytes.
82 @param[out] new_block The new version of the page. We do copy-on-write.
83 @return Saved data and its length. Some data from the current page that needs
84 to be inserted at the end of the given new data. */
85 template <typename T>
86 std::pair<ulint, byte *> insert_middle_page(T *lob_page, trx_id_t trxid,
87 ulint offset, byte *&data,
88 ulint &len,
89 buf_block_t *&new_block);
90
91 /** Insert data in the middle of the given LOB page.
92 @param[in] trxid The transaction identifier.
93 @param[in] offset The given data must be inserted at this offset within the
94 given page.
95 @param[in] data The new data that needs to be inserted in the middle.
96 @param[in] len The length of the new data in bytes.
97 @param[out] new_block The new version of the page. We do copy-on-write.
98 @return Saved data and its length. Some data from the current page that needs
99 to be inserted at the end of the given new data. */
100 std::pair<ulint, byte *> base_node_page_t::insert_middle(
101 trx_id_t trxid, ulint offset, byte *&data, ulint &len,
102 buf_block_t *&new_block) {
103 ut_ad(trxid >= get_trx_id());
104
105 return (insert_middle_page(this, trxid, offset, data, len, new_block));
106 }
107
108 /** Insert data in the middle of the given LOB page.
109 @param[in] trxid The transaction identifier.
110 @param[in] offset The given data must be inserted at this offset within the
111 given page.
112 @param[in] data The new data that needs to be inserted in the middle.
113 @param[in] len The length of the new data in bytes.
114 @param[out] new_block The new version of the page. We do copy-on-write.
115 @return Saved data and its length. Some data from the current page that needs
116 to be inserted at the end of the given new data. */
117 std::pair<ulint, byte *> data_page_t::insert_middle(trx_id_t trxid,
118 ulint offset, byte *&data,
119 ulint &len,
120 buf_block_t *&new_block) {
121 ut_ad(trxid >= get_trx_id());
122
123 return (insert_middle_page(this, trxid, offset, data, len, new_block));
124 }
125
126 /** Insert data in the middle of an LOB.
127 @param[in] lob_page A large object page of type FIL_PAGE_TYPE_LOB_FIRST or
128 FIL_PAGE_TYPE_LOB_DATA.
129 @param[in] trxid The transaction identifier.
130 @param[in] offset The given data must be inserted at this offset.
131 @param[in] data The new data that needs to be inserted in the middle.
132 @param[in] len The length of the new data in bytes.
133 @param[out] new_block The new version of the page. We do copy-on-write.
134 @return Saved data and its length. Some data from the current page that needs
135 to be inserted at the end of the given new data. */
136 template <typename T>
137 std::pair<ulint, byte *> insert_middle_page(T *lob_page, trx_id_t trxid,
138 ulint offset, byte *&data,
139 ulint &len,
140 buf_block_t *&new_block) {
141 Fname("insert_middle_page");
142 ut_ad(trxid >= lob_page->get_trx_id());
143 std::pair<ulint, byte *> result(0, nullptr);
144
145 ulint data_len = lob_page->get_data_len();
146 ulint save_len = data_len - offset;
147
148 /* Don't modify existing page. Create new page. */
149 data_page_t page;
150 new_block = page.alloc();
151 ulint avail = page.max_space_available();
152
153 byte *old_page_begin = lob_page->data_begin();
154 byte *new_page_begin = page.data_begin();
155
156 /* Copy the old data in the beginning. */
157 memcpy(new_page_begin, old_page_begin, offset);
158
159 if (data_len + len <= avail) {
160 /* Insert will complete within this page. */
161 /* Copy new data in middle. */
162 memcpy(new_page_begin + offset, data, len);
163 /* Copy remaining old data in end. */
164 memcpy(new_page_begin + offset + len, old_page_begin + offset, save_len);
165 ut_a((data_len + len) == (offset + len + save_len));
166 page.set_data_len(data_len + len);
167 data += len;
168 len = 0;
169
170 } else {
171 /* Will span multiple pages. */
172 /* Save the existing data that will be over-written. */
173 LOG("spanning multiple pages");
174
175 byte *from = lob_page->data_begin() + offset;
176 byte *save_mem = new byte[save_len];
177 memcpy(save_mem, from, save_len);
178 page.set_data_len(offset);
179
180 /* Over-write the existing data with new data. */
181 page.append(trxid, data, len);
182
183 /* There maybe some space left. */
184 result = std::pair<ulint, byte *>(save_len, save_mem);
185 }
186 return (result);
187 }
188
189 template <typename PageType>
190 ulint append_page(PageType *page, trx_id_t trxid, byte *&data, ulint &len) {
191 Fname("append_page");
192
193 LOG("Need to append bytes: " << len);
194
195 ulint old_data_len = page->get_data_len();
196 byte *ptr = page->data_begin() + old_data_len;
197 ulint space_available = page->max_space_available() - old_data_len;
198
199 if (space_available == 0 || len == 0) {
200 return (0);
201 }
202
203 ulint written = (len > space_available) ? space_available : len;
204
205 memcpy(ptr, data, written);
206 page->set_data_len(old_data_len + written);
207 page->set_trx_id(trxid);
208
209 data += written;
210 len -= written;
211
212 LOG("Written " << written << " bytes into page_no=" << page->get_page_no());
213 LOG("remaining=" << len);
214 return (written);
215 }
216
217 ulint base_node_page_t::append(trx_id_t trxid, byte *&data, ulint &len) {
218 return (append_page(this, trxid, data, len));
219 }
220
221 ulint data_page_t::append(trx_id_t trxid, byte *&data, ulint &len) {
222 return (append_page(this, trxid, data, len));
223 }
224
225 template <typename PageType>
226 buf_block_t *remove_middle_page(PageType *page, trx_id_t trxid, ulint offset,
227 ulint &len) {
228 buf_block_t *new_block = nullptr;
229
230 /* Total data available in the given page. */
231 ulint data_len = page->get_data_len();
232
233 /* Data that can be removed from the given page. */
234 ulint can_be_removed = data_len - offset;
235
236 /* Don't modify existing page. Create new page. */
237 data_page_t new_page;
238 new_block = new_page.alloc();
239 byte *to = new_page.data_begin();
240 byte *from = page->data_begin();
241
242 /* Copy the beginning portion. */
243 if (offset > 0) {
244 memcpy(to, from, offset);
245 }
246
247 if (len < can_be_removed) {
248 /* Only a single page modification. */
249 to += offset;
250 from += (offset + len);
251 ulint trail = data_len - offset - len;
252
253 /* Copy the end portion. */
254 memcpy(to, from, trail);
255
256 new_page.set_data_len(data_len - len);
257 len = 0;
258 } else {
259 new_page.set_data_len(data_len - can_be_removed);
260 len -= can_be_removed;
261 }
262
263 return (new_block);
264 }
265
266 buf_block_t *base_node_page_t::remove_middle(trx_id_t trxid, ulint offset,
267 ulint &len) {
268 return (remove_middle_page(this, trxid, offset, len));
269 }
270
271 buf_block_t *data_page_t::remove_middle(trx_id_t trxid, ulint offset,
272 ulint &len) {
273 return (remove_middle_page(this, trxid, offset, len));
274 }
275
276 /** The current index entry points to a latest LOB page. It may or may
277 not have older versions. If older version is there, bring it back to the
278 index list from the versions list. Then remove the current entry from
279 the index list. Move the versions list from current entry to older entry.
280 @param[in] trxid The transaction identifier.
281 @param[in] first_page The first lob page containing index list and free
282 list. */
283 void index_entry_t::make_old_version_current(trx_id_t trxid,
284 base_node_page_t &first_page) {
285 flst_base_node_t *base = first_page.index_list();
286 flst_base_node_t *free_list = first_page.free_list();
287 flst_base_node_t *version_list = get_versions_ptr();
288
289 if (flst_get_len(version_list) > 0) {
290 /* Remove the old version from versions list. */
291 fil_addr_t old_node_addr = flst_get_first(version_list);
292 flst_node_t *old_node = fut_get_ptr(old_node_addr);
293 flst_remove(version_list, old_node);
294
295 /* Copy the version base node from current to old entry. */
296 index_entry_t old_entry(old_node);
297 move_version_base_node(old_entry);
298
299 flst_insert_before(base, old_node, m_node);
300 }
301
302 purge_version(trxid, base, free_list);
303
304 ut_ad(flst_validate(base));
305 }
306
307 /** Move the version base node from current entry to the given entry.
308 @param[in] old_entry The index entry to which the version base node is moved
309 to. */
310 void index_entry_t::move_version_base_node(index_entry_t &old_entry) {
311 flst_base_node_t *from_node = get_versions_list();
312 flst_base_node_t *to_node = old_entry.get_versions_list();
313
314 memcpy(to_node, from_node, FLST_BASE_NODE_SIZE);
315 ut_ad(flst_get_len(from_node) == flst_get_len(to_node));
316 flst_init(from_node);
317 }
318
319 /** Purge the current index entry. An index entry points to either a FIRST
320 page or DATA page. That LOB page will be freed if it is DATA page. A FIRST
321 page should not be freed. */
322 void index_entry_t::purge() {
323 page_no_t page_no = get_page_no();
324 buf_block_t *block = buf_page_get(page_no);
325 page_type_t type = fil_page_get_type(block->m_frame);
326 if (type != FIL_PAGE_TYPE_LOB_FIRST) {
327 btr_page_free(block);
328 }
329 set_prev_null();
330 set_next_null();
331 set_versions_null();
332 set_page_no(0);
333 set_trx_id(0);
334 set_data_len(0);
335 }
336
337 fil_addr_t index_entry_t::purge_version(trx_id_t trxid, flst_base_node_t *lst,
338 flst_base_node_t *free_list) {
339 /* Save the location of next node. */
340 fil_addr_t next_loc = flst_get_next_addr(m_node);
341
342 /* Remove the current node from the list it belongs. */
343 flst_remove(lst, m_node);
344
345 /* Purge the current node. */
346 purge();
347
348 /* Add the current node to the free list. */
349 flst_add_first(free_list, m_node);
350
351 /* Return the next node location. */
352 return (next_loc);
353 }
354
355 buf_block_t *node_page_t::alloc(base_node_page_t &first_page) {
356 /* Allocate the page. */
357 m_block = btr_page_alloc();
358 set_page_type();
359 set_next_page(first_page.get_next_page());
360 first_page.set_next_page(get_page_no());
361
362 /* Use fully for the LOB index contents */
363 ulint lob_metadata_len = payload();
364 ulint node_count = lob_metadata_len / index_entry_t::SIZE;
365
366 flst_base_node_t *free_list = first_page.free_list();
367
368 byte *cur = nodes_begin();
369 for (ulint i = 0; i < node_count; ++i) {
370 flst_add_last(free_list, cur);
371 cur += index_entry_t::SIZE;
372 }
373 ut_a(flst_validate(free_list));
374
375 return (m_block);
376 }
377
378 /** Allocate one index entry. */
379 flst_node_t *base_node_page_t::alloc_index_entry() {
380 flst_base_node_t *f_list = free_list();
381 fil_addr_t node_addr = flst_get_first(f_list);
382 if (fil_addr_is_null(node_addr)) {
383 node_page_t node_page;
384 node_page.alloc(*this);
385 node_addr = flst_get_first(f_list);
386 }
387 flst_node_t *node = fut_get_ptr(node_addr);
388 flst_remove(f_list, node);
389 return (node);
390 }
391
392 } // namespace lob