Debian Codegraph @

unittest/gunit/innodb/lob/lob0int.h

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 #ifndef _lob0int_h_
28 #define _lob0int_h_
29
30 #include <string.h>
31
32 #include "buf0buf.h"
33 #include "fut0fut.h"
34 #include "fut0lst.h"
35 #include "lot0buf.h"
36 #include "lot0types.h"
37 #include "trx0types.h"
38
39 namespace lob {
40 const ulint LOB_HDR_PART_LEN = 0;
41 const ulint LOB_HDR_TRX_ID = 4;
42 const ulint LOB_HDR_SIZE = 10;
43 const ulint LOB_PAGE_DATA = FIL_PAGE_DATA + LOB_HDR_SIZE;
44
45 /** The offset where the list base node is located. This is the list
46 of LOB pages. */
47 const ulint LOB_INDEX_LIST = LOB_PAGE_DATA;
48
49 /** The offset where the list base node is located. This is the list
50 of free pages. */
51 const ulint LOB_INDEX_FREE_NODES = LOB_PAGE_DATA + 16;
52
53 const ulint LOB_PAGE_TRAILER_LEN = 8;
54
55 /** This page type refers to the first page of an LOB, which contains
56 half data and half meta data. */
57 #define FIL_PAGE_TYPE_LOB_BASE 21
58
59 struct base_node_page_t;
60
61 /** An index entry pointing to an LOB page. */
62 struct index_entry_t {
63 index_entry_t(flst_node_t *node) : m_node(node) {}
64 void reset(flst_node_t *node) { m_node = node; }
65
66 /** Index entry offsets within node. */
67 static const ulint OFFSET_PREV = 0;
68 static const ulint OFFSET_NEXT = 6;
69
70 /** Points to base node of the list of versions. The size of base node is
71 16 bytes. */
72 static const ulint OFFSET_VERSIONS = 12;
73 static const ulint OFFSET_TRXID = 28;
74 static const ulint OFFSET_PAGE_NO = 34;
75 static const ulint OFFSET_DATA_LEN = 38;
76
77 /** Total length of an index node. */
78 static const ulint SIZE = 40;
79
80 /** The versions base node is set to NULL. */
81 void set_versions_null() {
82 byte *base_node = get_versions_ptr();
83 flst_init(base_node);
84 }
85
86 /** Can this index entry be purged.
87 @param[in] trxid the transaction that is being purged.
88 @return true if this entry can be purged, false otherwise. */
89 bool can_be_purged(trx_id_t trxid) { return (trxid == get_trx_id()); }
90
91 /* The given entry becomes the old version of the current entry.
92 Move the version base node from old entry to current entry.
93 @param[in] entry the old entry */
94 void set_old_version(index_entry_t &entry) {
95 flst_node_t *node = entry.get_node_ptr();
96 flst_base_node_t *version_list = get_versions_ptr();
97 ut_ad(flst_get_len(version_list) == 0);
98
99 entry.move_version_base_node(*this);
100 flst_add_first(version_list, node);
101 }
102
103 /** The current index entry points to a latest LOB page. It may or may
104 not have older versions. If older version is there, bring it back to the
105 index list from the versions list. Then remove the current entry from
106 the index list. Move the versions list from current entry to older entry.
107 @param[in] trxid The transaction identifier.
108 @param[in] first_page The first lob page containing index list and free
109 list. */
110 void make_old_version_current(trx_id_t trxid, base_node_page_t &first_page);
111
112 fil_addr_t purge_version(trx_id_t trxid, flst_base_node_t *ver_list,
113 flst_base_node_t *free_list);
114
115 void add_version(index_entry_t &entry) const {
116 flst_node_t *node = entry.get_node_ptr();
117 flst_base_node_t *version_list = get_versions_ptr();
118 flst_add_first(version_list, node);
119 }
120
121 flst_base_node_t *get_versions_list() const { return (get_versions_ptr()); }
122
123 trx_id_t get_trx_id() const {
124 byte *ptr = get_trxid_ptr();
125 return (mach_read_from_6(ptr));
126 }
127
128 void set_trx_id(trx_id_t id) {
129 byte *ptr = get_trxid_ptr();
130 return (mach_write_to_6(ptr, id));
131 }
132
133 void set_page_no(page_no_t num) {
134 byte *ptr = get_pageno_ptr();
135 return (mach_write_to_4(ptr, num));
136 }
137
138 void set_prev_null() { flst_write_addr(m_node + OFFSET_PREV, fil_addr_null); }
139
140 void set_next_null() { flst_write_addr(m_node + OFFSET_NEXT, fil_addr_null); }
141
142 page_no_t get_page_no() {
143 byte *ptr = get_pageno_ptr();
144 return (mach_read_from_4(ptr));
145 }
146
147 void set_data_len(ulint len) {
148 byte *ptr = get_datalen_ptr();
149 return (mach_write_to_2(ptr, len));
150 }
151
152 ulint get_data_len() {
153 byte *ptr = get_datalen_ptr();
154 return (mach_read_from_2(ptr));
155 }
156
157 std::ostream &print(std::ostream &out) {
158 out << "[index_entry_t: trxid=" << get_trx_id()
159 << ", page_no=" << get_page_no() << ", data_len=" << get_data_len()
160 << "]";
161 return (out);
162 }
163
164 bool is_same(const index_entry_t &that) { return (m_node == that.m_node); }
165
166 private:
167 /** Move the version base node from current entry to the given entry.
168 @param[in] entry The index entry to which the version base node is moved
169 to. */
170 void move_version_base_node(index_entry_t &entry);
171
172 /** Purge the current index entry. An index entry points to either a FIRST
173 page or DATA page. That LOB page will be freed if it is DATA page. A FIRST
174 page should not be freed. */
175 void purge();
176
177 byte *get_versions_ptr() const { return (m_node + OFFSET_VERSIONS); }
178 byte *get_trxid_ptr() const { return (m_node + OFFSET_TRXID); }
179 byte *get_pageno_ptr() const { return (m_node + OFFSET_PAGE_NO); }
180 byte *get_datalen_ptr() const { return (m_node + OFFSET_DATA_LEN); }
181 byte *get_node_ptr() const { return (m_node); }
182
183 byte *m_node;
184 };
185
186 struct page_t {
187 page_t() : m_block(nullptr) {}
188 page_t(buf_block_t *block) : m_block(block) {}
189
190 trx_id_t get_trx_id() const {
191 return (mach_read_from_6(frame() + FIL_PAGE_DATA + LOB_HDR_TRX_ID));
192 }
193
194 page_no_t get_page_no() const {
195 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
196 }
197
198 void set_data_len(ulint len) {
199 mach_write_to_4(frame() + FIL_PAGE_DATA + LOB_HDR_PART_LEN, len);
200 }
201
202 void set_next_page(page_no_t page_no) {
203 mach_write_to_4(frame() + FIL_PAGE_NEXT, page_no);
204 }
205
206 void set_next_page_null() {
207 mach_write_to_4(frame() + FIL_PAGE_NEXT, FIL_NULL);
208 }
209
210 page_no_t get_next_page() {
211 return (mach_read_from_4(frame() + FIL_PAGE_NEXT));
212 }
213
214 ulint get_data_len() {
215 return (mach_read_from_4(frame() + FIL_PAGE_DATA + LOB_HDR_PART_LEN));
216 }
217
218 byte *frame() const { return (buf_block_get_frame(m_block)); }
219
220 static ulint payload();
221 ulint max_space_available();
222
223 protected:
224 buf_block_t *m_block;
225 };
226
227 struct data_page_t : public page_t {
228 data_page_t() = default;
229 data_page_t(buf_block_t *block) : page_t(block) {}
230
231 buf_block_t *alloc() {
232 ut_ad(m_block == nullptr);
233 m_block = btr_page_alloc();
234 set_page_type();
235 set_next_page_null();
236 return (m_block);
237 }
238
239 void set_page_type() {
240 mach_write_to_2(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_LOB_DATA);
241 }
242
243 void set_trx_id(trx_id_t id) {
244 mach_write_to_6(frame() + FIL_PAGE_DATA + LOB_HDR_TRX_ID, id);
245 }
246
247 static ulint payload() {
248 return (UNIV_PAGE_SIZE - LOB_PAGE_DATA - LOB_PAGE_TRAILER_LEN);
249 }
250
251 byte *data_begin() const { return (frame() + LOB_PAGE_DATA); }
252
253 buf_block_t *replace(trx_id_t trxid, ulint offset, byte *&ptr, ulint &want);
254
255 ulint read(trx_id_t trxid, ulint offset, byte *ptr, ulint want) {
256 byte *start = data_begin();
257 start += offset;
258 ulint avail_data = get_data_len() - offset;
259
260 ulint copy_len = want < avail_data ? want : avail_data;
261 memcpy(ptr, start, copy_len);
262 return (copy_len);
263 }
264
265 ulint write(trx_id_t trxid, byte *&data, ulint &len) {
266 byte *ptr = data_begin();
267 ulint written = (len > payload()) ? payload() : len;
268 memcpy(ptr, data, written);
269 set_data_len(written);
270
271 data += written;
272 len -= written;
273
274 return (written);
275 }
276
277 ulint append(trx_id_t trxid, byte *&data, ulint &len);
278
279 std::pair<ulint, byte *> insert_middle(trx_id_t trxid, ulint offset,
280 byte *&data, ulint &len,
281 buf_block_t *&new_block);
282
283 buf_block_t *remove_middle(trx_id_t trxid, ulint offset, ulint &len);
284
285 ulint max_space_available() const { return (payload()); }
286 };
287
288 struct base_node_page_t : public page_t {
289 base_node_page_t() = default;
290 base_node_page_t(buf_block_t *block) : page_t(block) {}
291
292 buf_block_t *alloc();
293
294 void dealloc() {
295 btr_page_free(m_block);
296 m_block = nullptr;
297 }
298
299 bool empty() const {
300 flst_base_node_t *base = index_list();
301 return (flst_get_len(base) == 0);
302 }
303
304 /** Allocate one index entry. */
305 flst_node_t *alloc_index_entry();
306
307 byte *nodes_begin() const {
308 return (frame() + LOB_PAGE_DATA + FLST_BASE_NODE_SIZE +
309 FLST_BASE_NODE_SIZE);
310 }
311
312 static ulint payload() {
313 return (UNIV_PAGE_SIZE - LOB_PAGE_DATA - LOB_PAGE_TRAILER_LEN -
314 FLST_BASE_NODE_SIZE - FLST_BASE_NODE_SIZE);
315 }
316
317 void set_trx_id(trx_id_t id) {
318 mach_write_to_6(frame() + FIL_PAGE_DATA + LOB_HDR_TRX_ID, id);
319 }
320
321 std::pair<ulint, byte *> insert_middle(trx_id_t trxid, ulint offset,
322 byte *&data, ulint &len,
323 buf_block_t *&new_block);
324
325 buf_block_t *remove_middle(trx_id_t trxid, ulint offset, ulint &len);
326
327 /** Write as much as possible of the given data into the page. */
328 ulint write(trx_id_t trxid, byte *&data, ulint &len) {
329 byte *ptr = data_begin();
330 ulint space_available = payload() / 2;
331 ulint written = (len > space_available) ? space_available : len;
332 memcpy(ptr, data, written);
333 set_data_len(written);
334 set_trx_id(trxid);
335
336 data += written;
337 len -= written;
338
339 return (written);
340 }
341
342 buf_block_t *replace(trx_id_t trxid, ulint offset, byte *&ptr, ulint &want);
343
344 ulint read(trx_id_t trxid, ulint offset, byte *ptr, ulint want) {
345 byte *start = data_begin();
346 start += offset;
347 ulint avail_data = get_data_len() - offset;
348
349 ulint copy_len = want < avail_data ? want : avail_data;
350 memcpy(ptr, start, copy_len);
351 return (copy_len);
352 }
353
354 void set_page_type() {
355 mach_write_to_2(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_LOB_FIRST);
356 }
357
358 flst_base_node_t *index_list() const { return (frame() + LOB_INDEX_LIST); }
359
360 flst_base_node_t *free_list() const {
361 return (frame() + LOB_INDEX_FREE_NODES);
362 }
363
364 static ulint max_space_available() { return (payload() / 2); }
365
366 /** Get the number of index entries this page can hold.
367 @return Number of index entries this page can hold. */
368 static ulint node_count() {
369 return (max_space_available() / index_entry_t::SIZE);
370 }
371
372 std::ostream &print_index_entries(std::ostream &out) const {
373 flst_base_node_t *base = index_list();
374 fil_addr_t node_loc = flst_get_first(base);
375
376 while (!fil_addr_is_null(node_loc)) {
377 flst_node_t *node = fut_get_ptr(node_loc);
378 out << (void *)node << std::endl;
379 index_entry_t entry(node);
380 entry.print(out) << std::endl;
381 node_loc = flst_get_next_addr(node);
382 }
383 return (out);
384 }
385
386 byte *data_begin() const {
387 ulint space_available = payload() / 2;
388 return (frame() + UNIV_PAGE_SIZE - LOB_PAGE_TRAILER_LEN - space_available);
389 }
390
391 /** Append data into a LOB first page. */
392 ulint append(trx_id_t trxid, byte *&data, ulint &len);
393 };
394
395 struct node_page_t : public page_t {
396 node_page_t() = default;
397 node_page_t(buf_block_t *block) : page_t(block) {}
398
399 buf_block_t *alloc(base_node_page_t &first_page);
400
401 static ulint payload() {
402 return (UNIV_PAGE_SIZE - FIL_PAGE_DATA - FIL_PAGE_DATA_END);
403 }
404
405 static ulint max_space_available() { return (payload()); }
406
407 /** Get the number of index entries this page can hold.
408 @return Number of index entries this page can hold. */
409 static ulint node_count() {
410 return (max_space_available() / index_entry_t::SIZE);
411 }
412
413 void set_page_type() {
414 mach_write_to_2(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_LOB_INDEX);
415 }
416
417 byte *nodes_begin() const { return (frame() + LOB_PAGE_DATA); }
418 };
419
420 } // namespace lob
421 #endif // _lob0int_h_