Debian Codegraph @

storage/innobase/include/lob0impl.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 #ifndef lob0impl_h
28 #define lob0impl_h
29
30 #include "btr0btr.h"
31 #include "fut0lst.h"
32 #include "lob0first.h"
33 #include "lob0lob.h"
34 #include "mach0data.h"
35 #include "mtr0log.h"
36 #include "mtr0mtr.h"
37 #include "mtr0types.h"
38
39 namespace lob {
40
41 struct z_index_entry_t;
42 struct z_first_page_t;
43 struct z_frag_page_t;
44 struct index_entry_t;
45 struct first_page_t;
46
47 using paddr_t = ulint;
48
49 /** The node of page list. The page list is similar to the file list
50 (flst_node_t) except that it is completely within one page. */
51 class plist_node_t {
52 public:
53 /** Offset of the previous node. (2 bytes) */
54 static const uint16_t OFFSET_PREV = 0;
55
56 /** Offset of the next node. (2 bytes) */
57 static const uint16_t OFFSET_NEXT = 2;
58
59 /** The size of a page list node. */
60 static const uint8_t SIZE = 4;
61
62 /** Constructor.
63 @param[in] mtr Mini-transaction context. */
64 explicit plist_node_t(mtr_t *mtr)
65 : m_frame(nullptr), m_node(nullptr), m_mtr(mtr) {}
66
67 /** Default constructor. */
68 plist_node_t() : m_frame(nullptr), m_node(nullptr), m_mtr(nullptr) {}
69
70 /** Constructor.
71 @param[in] mtr Mini-transaction context
72 @param[in] frame the page frame of this plist. */
73 plist_node_t(mtr_t *mtr, byte *frame)
74 : m_frame(frame), m_node(nullptr), m_mtr(mtr) {}
75
76 /** Constructor.
77 @param[in] frame the page frame of this plist.
78 @param[in] node the location of plist node. */
79 plist_node_t(byte *frame, byte *node)
80 : m_frame(frame), m_node(node), m_mtr(nullptr) {}
81
82 /** Constructor.
83 @param[in] frame Page frame where the page list node is
84 located.
85 @param[in] node Location of page list node within page
86 frame.
87 @param[in] mtr Mini-transaction context. */
88 plist_node_t(byte *frame, byte *node, mtr_t *mtr)
89 : m_frame(frame), m_node(node), m_mtr(mtr) {}
90
91 /** Copy constructor. */
92 plist_node_t(const plist_node_t &other) = default;
93
94 plist_node_t &operator=(const plist_node_t &) = default;
95
96 /** Check if the current node is before the given node in the
97 page (w.r.t the offset).
98 @param[in] node the other node.
99 @return true if current node is before the given node.
100 @return false if current node is after the given node. */
101 bool is_before(const plist_node_t &node) const {
102 ut_ad(!is_null());
103 ut_ad(!node.is_null());
104 return (addr() < node.addr());
105 }
106
107 /** Initialize the current page list node. The offset of next and
108 previous nodes are set to 0. */
109 void init() {
110 ut_ad(!is_null());
111 ut_ad(m_mtr != nullptr);
112
113 mlog_write_ulint(m_node + OFFSET_PREV, 0, MLOG_2BYTES, m_mtr);
114 mlog_write_ulint(m_node + OFFSET_NEXT, 0, MLOG_2BYTES, m_mtr);
115 }
116
117 /** Set the offset of the previous node.
118 @param[in] addr the offset of previous node.*/
119 void set_prev(paddr_t addr) {
120 ut_ad(addr < UNIV_PAGE_SIZE);
121 ut_ad(m_mtr != nullptr);
122
123 mlog_write_ulint(m_node + OFFSET_PREV, addr, MLOG_2BYTES, m_mtr);
124 }
125
126 /** Set the previous page list node.
127 @param[in] prev the previous page list node.*/
128 void set_prev_node(plist_node_t &prev) { set_prev(prev.addr()); }
129
130 /** Set the offset of the next node.
131 @param[in] addr the offset of next node.*/
132 void set_next(paddr_t addr) {
133 ut_ad(!is_null());
134 ut_ad(addr < UNIV_PAGE_SIZE);
135 ut_ad(m_mtr != nullptr);
136
137 mlog_write_ulint(m_node + OFFSET_NEXT, addr, MLOG_2BYTES, m_mtr);
138 }
139
140 /** Set the next page list node.
141 @param[in] next the next page list node.*/
142 void set_next_node(const plist_node_t &next) { set_next(next.addr()); }
143
144 /** Get the offset of the previous page list node.
145 @return offset of previous node of the page list. */
146 paddr_t get_prev() const { return (mach_read_from_2(m_node + OFFSET_PREV)); }
147
148 /** Get the offset of the next page list node.
149 @return offset of next node of the page list. */
150 paddr_t get_next() const { return (mach_read_from_2(m_node + OFFSET_NEXT)); }
151
152 /** Get the next page list node.
153 @return next node of the page list. */
154 plist_node_t get_next_node() const {
155 paddr_t addr = get_next();
156 byte *node = nullptr;
157
158 if (addr != 0) {
159 node = m_frame + addr;
160 ut_ad(addr < UNIV_PAGE_SIZE);
161 }
162
163 return (plist_node_t(m_frame, node, m_mtr));
164 }
165
166 /** Get the previous page list node.
167 @return previous node of the page list. */
168 plist_node_t get_prev_node() const {
169 paddr_t addr = get_prev();
170 byte *node = nullptr;
171
172 if (addr != 0) {
173 ut_ad(addr < UNIV_PAGE_SIZE);
174 node = m_frame + addr;
175 }
176
177 return (plist_node_t(m_frame, node, m_mtr));
178 }
179
180 /** Obtain the offset of the page list node within the given
181 page frame.
182 @return offset from the beginning of the page. */
183 paddr_t addr() const {
184 return ((m_node == nullptr) ? 0 : (m_node - m_frame));
185 }
186
187 /** Obtain the memory location of the page list node.
188 @return the pointer to the page list node. */
189 byte *ptr() const { return (m_node); }
190
191 /** Check if the given page list node is null.
192 @return true if null, false otherwise. */
193 bool is_null() const { return (m_node == nullptr); }
194
195 /** Print the page list node into the given output stream.
196 @param[in] out the output stream.
197 @return the output stream. */
198 std::ostream &print(std::ostream &out) const {
199 out << "[plist_node_t: next=" << get_next() << ", prev=" << get_prev()
200 << ", this=" << addr() << ", frame=" << (void *)m_frame
201 << ", m_node=" << (void *)m_node << "]";
202 return (out);
203 }
204
205 /** Set the page frame to the given value.
206 @param[in] frame the page frame */
207 void set_frame(byte *frame) { m_frame = frame; }
208
209 /** Set the page list node to the given value.
210 @param[in] node the page list node. */
211 void set_node(byte *node) { m_node = node; }
212
213 /** Set the mini-transaction context to the given value.
214 @param[in] mtr Mini-transaction context. */
215 void set_mtr(mtr_t *mtr) { m_mtr = mtr; }
216
217 /** Get the page frame where this page list exists.
218 @return the page frame. */
219 byte *get_frame() const { return (m_frame); }
220
221 bool is_equal(const plist_node_t &that) const {
222 if (m_node == nullptr || that.m_node == nullptr) {
223 return (false);
224 }
225 return (m_node == that.m_node);
226 }
227
228 private:
229 /** The page frame where this page list exists. */
230 byte *m_frame;
231
232 /** The plist node is located at this address. */
233 byte *m_node;
234
235 /** The mini-transaction context. */
236 mtr_t *m_mtr;
237 };
238
239 inline std::ostream &operator<<(std::ostream &out, const plist_node_t &obj) {
240 return (obj.print(out));
241 }
242
243 /** The base node of page list. */
244 struct plist_base_node_t {
245 /** The offset where the length of the page list is stored.
246 This is 4 bytes long.*/
247 static const ulint OFFSET_LEN = 0;
248
249 /** The offset where the first node is located.
250 This is 2 bytes long. */
251 static const ulint OFFSET_FIRST = 4;
252
253 /** The offset where the last node is located.
254 This is 2 bytes long. */
255 static const ulint OFFSET_LAST = 6;
256
257 /** The total size (in bytes) of a page list base node. */
258 static const ulint SIZE = 8;
259
260 plist_base_node_t(byte *frame, byte *base, mtr_t *mtr)
261 : m_frame(frame), m_base(base), m_mtr(mtr) {}
262
263 void init() {
264 ut_ad(m_mtr != nullptr);
265
266 mlog_write_ulint(m_base + OFFSET_LEN, 0, MLOG_4BYTES, m_mtr);
267 mlog_write_ulint(m_base + OFFSET_FIRST, 0, MLOG_2BYTES, m_mtr);
268 mlog_write_ulint(m_base + OFFSET_LAST, 0, MLOG_2BYTES, m_mtr);
269 }
270
271 void remove(plist_node_t &node) {
272 ut_ad(m_mtr != nullptr);
273
274 plist_node_t prev = node.get_prev_node();
275 plist_node_t next = node.get_next_node();
276
277 if (prev.is_null()) {
278 set_first(next.addr());
279 } else {
280 prev.set_next(next.addr());
281 }
282
283 if (next.is_null()) {
284 set_last(prev.addr());
285 } else {
286 next.set_prev(prev.addr());
287 }
288
289 node.set_next(0);
290 node.set_prev(0);
291
292 decr_len();
293 }
294
295 void push_front(plist_node_t &node) {
296 ut_ad(m_mtr != nullptr);
297
298 if (get_len() == 0) {
299 add_to_empty(node);
300 } else {
301 paddr_t cur_addr = node.addr();
302 paddr_t first_addr = get_first();
303 plist_node_t first_node = get_node(first_addr);
304 node.set_next(first_addr);
305 node.set_prev(0);
306 first_node.set_prev(cur_addr);
307 set_first(cur_addr);
308 incr_len();
309 }
310 }
311
312 /** Insert node2 after node1. */
313 void insert_after(plist_node_t &node1, plist_node_t &node2) {
314 ut_ad(m_mtr != nullptr);
315
316 if (node1.is_null()) {
317 push_back(node2);
318 } else {
319 plist_node_t node3 = node1.get_next_node();
320 node1.set_next_node(node2);
321 node2.set_next_node(node3);
322
323 if (node3.is_null()) {
324 set_last(node2.addr());
325 } else {
326 node3.set_prev_node(node2);
327 }
328
329 node2.set_prev_node(node1);
330
331 incr_len();
332 }
333 }
334
335 /** Insert node2 before node3. */
336 void insert_before(plist_node_t &node3, plist_node_t &node2) {
337 ut_ad(m_mtr != nullptr);
338
339 if (node3.is_null()) {
340 push_back(node2);
341 } else {
342 plist_node_t node1 = node3.get_prev_node();
343
344 if (node1.is_null()) {
345 set_first(node2.addr());
346 } else {
347 node1.set_next_node(node2);
348 }
349
350 node2.set_next_node(node3);
351 node3.set_prev_node(node2);
352 node2.set_prev_node(node1);
353
354 incr_len();
355 }
356 }
357
358 void add_to_empty(plist_node_t &node) {
359 ut_ad(m_mtr != nullptr);
360 ut_ad(get_len() == 0);
361
362 set_first(node.addr());
363 set_last(node.addr());
364 incr_len();
365 }
366
367 void push_back(plist_node_t &node) {
368 ut_ad(m_mtr != nullptr);
369
370 if (get_len() == 0) {
371 add_to_empty(node);
372 } else {
373 paddr_t cur_addr = node.addr();
374 paddr_t last_addr = get_last();
375 plist_node_t last_node = get_node(last_addr);
376 node.set_next(0);
377 node.set_prev_node(last_node);
378 last_node.set_next(cur_addr);
379 set_last(cur_addr);
380 incr_len();
381 }
382 }
383
384 bool empty() const { return (get_len() == 0); }
385
386 ulint get_len() const { return (mach_read_from_4(m_base + OFFSET_LEN)); }
387
388 paddr_t get_first() const {
389 return (mach_read_from_2(m_base + OFFSET_FIRST));
390 }
391
392 plist_node_t get_first_node() const {
393 plist_node_t result(m_mtr, m_frame);
394
395 if (!empty()) {
396 byte *node = m_frame + get_first();
397 result.set_node(node);
398 }
399 return (result);
400 }
401
402 paddr_t get_last() const { return (mach_read_from_2(m_base + OFFSET_LAST)); }
403
404 plist_node_t get_last_node() const {
405 plist_node_t result(m_mtr, m_frame);
406
407 if (!empty()) {
408 result.set_node(m_frame + get_last());
409 }
410
411 return (result);
412 }
413
414 void set_len(ulint len) {
415 ut_ad(m_mtr != nullptr);
416
417 mlog_write_ulint(m_base + OFFSET_LEN, len, MLOG_4BYTES, m_mtr);
418 }
419
420 void incr_len() {
421 ut_ad(m_mtr != nullptr);
422
423 ulint len = mach_read_from_4(m_base + OFFSET_LEN);
424 mlog_write_ulint(m_base + OFFSET_LEN, len + 1, MLOG_4BYTES, m_mtr);
425 }
426
427 void decr_len() {
428 ut_ad(m_mtr != nullptr);
429
430 ulint len = mach_read_from_4(m_base + OFFSET_LEN);
431
432 ut_ad(len > 0);
433
434 mlog_write_ulint(m_base + OFFSET_LEN, len - 1, MLOG_4BYTES, m_mtr);
435 }
436
437 void set_first(paddr_t addr) {
438 ut_ad(m_mtr != nullptr);
439
440 mlog_write_ulint(m_base + OFFSET_FIRST, addr, MLOG_2BYTES, m_mtr);
441 }
442
443 void set_last(paddr_t addr) {
444 ut_ad(m_mtr != nullptr);
445
446 mlog_write_ulint(m_base + OFFSET_LAST, addr, MLOG_2BYTES, m_mtr);
447 }
448
449 plist_node_t get_node(paddr_t addr) {
450 byte *node = m_frame + addr;
451 return (plist_node_t(m_frame, node, m_mtr));
452 }
453
454 paddr_t addr() const { return (m_base - m_frame); }
455
456 std::ostream &print(std::ostream &out) const {
457 out << "[plist_base_node_t: len=" << get_len() << ", first=" << get_first()
458 << ", last=" << get_last() << ", this=" << addr() << "]";
459 return (out);
460 }
461
462 std::ostream &print_list(std::ostream &out) const {
463 print(out);
464 out << std::endl;
465
466 for (plist_node_t cur = get_first_node(); !cur.is_null();
467 cur = cur.get_next_node()) {
468 out << cur << std::endl;
469 }
470 return (out);
471 }
472
473 #ifdef UNIV_DEBUG
474 /** Validate the page list.
475 @return true if valid, false otherwise. */
476 bool validate() const;
477 #endif /* UNIV_DEBUG */
478
479 byte *m_frame;
480 byte *m_base;
481 mtr_t *m_mtr;
482 };
483
484 inline std::ostream &operator<<(std::ostream &out,
485 const plist_base_node_t &obj) {
486 return (obj.print(out));
487 }
488
489 using frag_id_t = ulint;
490 const ulint FRAG_ID_NULL = std::numeric_limits<uint16_t>::max();
491 const ulint KB16 = 16 * 1024;
492
493 /** The node page (also can be called as the index page) contains a list of
494 index_entry_t objects. */
495 struct node_page_t : public basic_page_t {
496 /** Version information. One byte. */
497 static const ulint OFFSET_VERSION = FIL_PAGE_DATA;
498 static const ulint LOB_PAGE_DATA = OFFSET_VERSION + 1;
499
500 void set_version_0() {
501 mlog_write_ulint(frame() + OFFSET_VERSION, 0, MLOG_1BYTE, m_mtr);
502 }
503
504 /** Default ctor */
505 node_page_t() = default;
506
507 node_page_t(buf_block_t *block, mtr_t *mtr) : basic_page_t(block, mtr) {}
508
509 node_page_t(buf_block_t *block, mtr_t *mtr, dict_index_t *index)
510 : basic_page_t(block, mtr, index) {}
511
512 node_page_t(mtr_t *mtr, dict_index_t *index)
513 : basic_page_t(nullptr, mtr, index) {}
514
515 /** Constructor
516 @param[in] block the buffer block. */
517 node_page_t(buf_block_t *block) : basic_page_t(block, nullptr, nullptr) {}
518
519 /** Import the node page or the index page.
520 @param[in] trx_id transaction identifier. */
521 void import(trx_id_t trx_id);
522
523 buf_block_t *alloc(first_page_t &first_page, bool bulk);
524
525 buf_block_t *load_x(page_id_t page_id, page_size_t page_size) {
526 m_block =
527 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr);
528 return (m_block);
529 }
530
531 void dealloc() {
532 btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
533 m_block = nullptr;
534 }
535
536 static ulint payload() {
537 return (UNIV_PAGE_SIZE - LOB_PAGE_DATA - FIL_PAGE_DATA_END);
538 }
539
540 static ulint max_space_available() { return (payload()); }
541
542 /** Get the number of index entries this page can hold.
543 @return Number of index entries this page can hold. */
544 static ulint node_count();
545
546 void set_page_type() {
547 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_LOB_INDEX,
548 MLOG_2BYTES, m_mtr);
549 }
550
551 byte *nodes_begin() const { return (frame() + LOB_PAGE_DATA); }
552 };
553
554 /** An entry representing one fragment page. */
555 struct z_frag_entry_t {
556 public:
557 /** Offset within frag entry pointing to prev frag entry. */
558 static const ulint OFFSET_PREV = 0;
559
560 /** Offset within frag entry pointing to next frag entry. */
561 static const ulint OFFSET_NEXT = OFFSET_PREV + FIL_ADDR_SIZE;
562
563 /** Offset within frag entry holding the page number of frag page. */
564 static const ulint OFFSET_PAGE_NO = OFFSET_NEXT + FIL_ADDR_SIZE;
565
566 /** Number of used fragments. */
567 static const ulint OFFSET_N_FRAGS = OFFSET_PAGE_NO + 4;
568
569 /** Used space in bytes. */
570 static const ulint OFFSET_USED_LEN = OFFSET_N_FRAGS + 2;
571
572 /** Total free space in bytes. */
573 static const ulint OFFSET_TOTAL_FREE_LEN = OFFSET_USED_LEN + 2;
574
575 /** The biggest free frag space in bytes. */
576 static const ulint OFFSET_BIG_FREE_LEN = OFFSET_TOTAL_FREE_LEN + 2;
577
578 /** Total size of one frag entry. */
579 static const ulint SIZE = OFFSET_BIG_FREE_LEN + 2;
580
581 /** Constructor. */
582 z_frag_entry_t(flst_node_t *node, mtr_t *mtr) : m_node(node), m_mtr(mtr) {}
583
584 /** Constructor. */
585 z_frag_entry_t() : m_node(nullptr), m_mtr(nullptr) {}
586
587 /** Constructor. */
588 z_frag_entry_t(mtr_t *mtr) : m_node(nullptr), m_mtr(mtr) {}
589
590 /** Initialize the fragment entry contents. For this to correctly
591 work, the current object must be initialized with proper file list
592 node and the mini-transaction context. */
593 void init() {
594 ut_ad(m_mtr != nullptr);
595 ut_ad(m_node != nullptr);
596
597 set_prev_null();
598 set_next_null();
599 set_page_no(FIL_NULL);
600 set_n_frags(0);
601 set_used_len(0);
602 set_total_free_len(0);
603 set_big_free_len(0);
604 }
605
606 /** Set the current fragment entry to null. */
607 void set_null() { m_node = nullptr; }
608
609 /** Check if the current fragment entry is null.
610 @return true if the current fragment entry is null, false otherwise. */
611 bool is_null() const { return (m_node == nullptr); }
612
613 fil_addr_t get_self_addr() const {
614 page_t *frame = page_align(m_node);
615 page_no_t page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET);
616 uint16_t offset = static_cast<uint16_t>(m_node - frame);
617 ut_ad(offset < UNIV_PAGE_SIZE);
618 return (fil_addr_t(page_no, offset));
619 }
620
621 /** Update the current fragment entry with information about
622 the given fragment page.
623 @param[in] frag_page the fragment page whose information
624 will be stored in current fragment entry. */
625 void update(const z_frag_page_t &frag_page);
626
627 /** Remove this node from the given list.
628 @param[in] bnode the base node of the list from which to remove
629 current node. */
630 void remove(flst_base_node_t *bnode) {
631 ut_ad(m_mtr != nullptr);
632
633 flst_remove(bnode, m_node, m_mtr);
634 }
635
636 /** Add this node as the last node in the given list.
637 @param[in] bnode the base node of the file list. */
638 void push_back(flst_base_node_t *bnode) {
639 ut_ad(m_mtr != nullptr);
640
641 flst_add_last(bnode, m_node, m_mtr);
642 }
643
644 /** Add this node as the first node in the given list.
645 @param[in] bnode the base node of the file list. */
646 void push_front(flst_base_node_t *bnode) {
647 ut_ad(m_mtr != nullptr);
648
649 flst_add_first(bnode, m_node, m_mtr);
650 }
651
652 /** Point to another frag entry.
653 @param[in] node point to this file list node. */
654 void reset(flst_node_t *node) { m_node = node; }
655
656 /** Set the previous frag entry as null. */
657 void set_prev_null() {
658 ut_ad(m_mtr != nullptr);
659
660 flst_write_addr(m_node + OFFSET_PREV, fil_addr_null, m_mtr);
661 }
662
663 /** Set the previous frag entry as null. */
664 void set_prev(const fil_addr_t &addr) {
665 ut_ad(m_mtr != nullptr);
666
667 flst_write_addr(m_node + OFFSET_PREV, addr, m_mtr);
668 }
669
670 /** Get the location of previous frag entry. */
671 fil_addr_t get_prev() const {
672 return (flst_read_addr(m_node + OFFSET_PREV, m_mtr));
673 }
674
675 /** Set the next frag entry as null. */
676 void set_next_null() {
677 ut_ad(m_mtr != nullptr);
678
679 flst_write_addr(m_node + OFFSET_NEXT, fil_addr_null, m_mtr);
680 }
681
682 /** Set the next frag entry. */
683 void set_next(const fil_addr_t &addr) {
684 ut_ad(m_mtr != nullptr);
685
686 flst_write_addr(m_node + OFFSET_NEXT, addr, m_mtr);
687 }
688
689 /** Get the location of next frag entry. */
690 fil_addr_t get_next() const {
691 return (flst_read_addr(m_node + OFFSET_NEXT, m_mtr));
692 }
693
694 /** Get the frag page number. */
695 page_no_t get_page_no() const {
696 return (mach_read_from_4(m_node + OFFSET_PAGE_NO));
697 }
698
699 /** Set the frag page number. */
700 void set_page_no(page_no_t page_no) const {
701 ut_ad(m_mtr != nullptr);
702
703 mlog_write_ulint(m_node + OFFSET_PAGE_NO, page_no, MLOG_4BYTES, m_mtr);
704 }
705
706 /** Free the fragment page pointed to by this entry.
707 @param[in] mtr Mini-transaction to be used for this operation.
708 @param[in] index The index to which this LOB belongs. */
709 void free_frag_page(mtr_t *mtr, dict_index_t *index);
710
711 /** Get the frag page number. */
712 ulint get_n_frags() const {
713 return (mach_read_from_2(m_node + OFFSET_N_FRAGS));
714 }
715
716 /** Set the frag page number. */
717 void set_n_frags(ulint frags) const {
718 ut_ad(m_mtr != nullptr);
719
720 mlog_write_ulint(m_node + OFFSET_N_FRAGS, frags, MLOG_2BYTES, m_mtr);
721 }
722
723 /** Get the used bytes. */
724 ulint get_used_len() const {
725 return (mach_read_from_2(m_node + OFFSET_USED_LEN));
726 }
727
728 /** Set the used bytes. */
729 void set_used_len(ulint used) const {
730 ut_ad(m_mtr != nullptr);
731
732 mlog_write_ulint(m_node + OFFSET_USED_LEN, used, MLOG_2BYTES, m_mtr);
733 }
734
735 /** Get the total cumulative free bytes. */
736 ulint get_total_free_len() const {
737 return (mach_read_from_2(m_node + OFFSET_TOTAL_FREE_LEN));
738 }
739
740 /** Get the biggest free frag bytes. */
741 ulint get_big_free_len() const {
742 return (mach_read_from_2(m_node + OFFSET_BIG_FREE_LEN));
743 }
744
745 /** Set the total free bytes. */
746 void set_total_free_len(ulint n) {
747 ut_ad(m_mtr != nullptr);
748
749 mlog_write_ulint(m_node + OFFSET_TOTAL_FREE_LEN, n, MLOG_2BYTES, m_mtr);
750 }
751
752 /** Set the big free frag bytes. */
753 void set_big_free_len(ulint n) {
754 ut_ad(m_mtr != nullptr);
755
756 mlog_write_ulint(m_node + OFFSET_BIG_FREE_LEN, n, MLOG_2BYTES, m_mtr);
757 }
758
759 void purge(flst_base_node_t *used_lst, flst_base_node_t *free_lst);
760
761 std::ostream &print(std::ostream &out) const;
762
763 private:
764 /** The location where the fragment entry node is located. */
765 flst_node_t *m_node;
766
767 /** The mini-transaction context for operating on this fragment
768 entry. */
769 mtr_t *m_mtr;
770 };
771
772 inline std::ostream &operator<<(std::ostream &out, const z_frag_entry_t &obj) {
773 return (obj.print(out));
774 }
775
776 /** An index page containing an array of z_index_entry_t objects. */
777 struct z_index_page_t {
778 /** Version information. One byte. */
779 static const ulint OFFSET_VERSION = FIL_PAGE_DATA;
780 static const ulint LOB_PAGE_DATA = OFFSET_VERSION + 1;
781
782 /** Constructor.
783 @param[in] mtr mini-transaction context. */
784 explicit z_index_page_t(mtr_t *mtr) : m_block(nullptr), m_mtr(mtr) {}
785
786 /** Constructor.
787 @param[in] block the buffer block.
788 @param[in] mtr mini-transaction context.
789 @param[in] index the index to which the LOB belongs. */
790 z_index_page_t(buf_block_t *block, mtr_t *mtr, dict_index_t *index)
791 : m_block(block), m_mtr(mtr), m_index(index) {}
792
793 /** Constructor.
794 @param[in] mtr mini-transaction context.
795 @param[in] index the index to which the LOB belongs. */
796 z_index_page_t(mtr_t *mtr, dict_index_t *index)
797 : z_index_page_t(nullptr, mtr, index) {}
798
799 /** Constructor
800 @param[in] block the buffer block. */
801 explicit z_index_page_t(buf_block_t *block)
802 : z_index_page_t(block, nullptr, nullptr) {}
803
804 /** Constructor.
805 @param[in] block the buffer block.
806 @param[in] index the index to which the LOB belongs. */
807 z_index_page_t(buf_block_t *block, dict_index_t *index)
808 : z_index_page_t(block, nullptr, index) {}
809
810 /** Write the space identifier to the page header, without generating
811 redo log records.
812 @param[in] space_id the space identifier. */
813 void set_space_id_no_redo(space_id_t space_id) {
814 mlog_write_ulint(frame() + FIL_PAGE_SPACE_ID, space_id, MLOG_4BYTES,
815 nullptr);
816 }
817
818 /** Set the correct page type. */
819 void set_page_type(mtr_t *mtr) {
820 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_INDEX,
821 MLOG_2BYTES, mtr);
822 }
823
824 void set_version_0() {
825 mlog_write_ulint(frame() + OFFSET_VERSION, 0, MLOG_1BYTE, m_mtr);
826 }
827
828 /** Set the next page number. */
829 void set_next_page_no(page_no_t page_no) {
830 ut_ad(m_mtr != nullptr);
831
832 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES, m_mtr);
833 }
834
835 /** Get the page number. */
836 page_no_t get_page_no() const {
837 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
838 }
839
840 /** Get the next page number. */
841 page_no_t get_next_page_no() const {
842 return (mach_read_from_4(frame() + FIL_PAGE_NEXT));
843 }
844
845 /** Allocate an ZLOB index page.
846 @return the buffer block of the allocated zlob index page. */
847 buf_block_t *alloc(z_first_page_t &first, bool bulk);
848
849 void import(trx_id_t trx_id);
850
851 /** Load the given compressed LOB index page.
852 @param[in] page_no compressed LOB index page number.
853 @return the buffer block of the given page number. */
854 buf_block_t *load_x(page_no_t page_no) {
855 page_id_t page_id(dict_index_get_space(m_index), page_no);
856 page_size_t page_size(dict_table_page_size(m_index->table));
857 m_block =
858 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr);
859
860 ut_ad(m_block->get_page_type() == FIL_PAGE_TYPE_ZLOB_INDEX);
861 return (m_block);
862 }
863
864 void dealloc() {
865 btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
866 m_block = nullptr;
867 }
868
869 void init(flst_base_node_t *free_lst, mtr_t *mtr);
870
871 ulint payload() const {
872 const page_size_t page_size(dict_table_page_size(m_index->table));
873
874 return (page_size.physical() - FIL_PAGE_DATA_END - LOB_PAGE_DATA);
875 }
876
877 ulint get_n_index_entries() const;
878
879 byte *frame() const { return (buf_block_get_frame(m_block)); }
880
881 /** The buffer block of the compressed LOB index page. */
882 buf_block_t *m_block;
883
884 /** The mini-transaction context. */
885 mtr_t *m_mtr;
886
887 /** The index to which the LOB belongs. */
888 dict_index_t *m_index;
889 };
890
891 /** The data page holding the zlob. */
892 struct z_data_page_t {
893 /** Version information. One byte. */
894 static const ulint OFFSET_VERSION = FIL_PAGE_DATA;
895
896 /* The length of compressed data stored in this page. */
897 static const ulint OFFSET_DATA_LEN = OFFSET_VERSION + 1;
898
899 /* The transaction that created this page. */
900 static const ulint OFFSET_TRX_ID = OFFSET_DATA_LEN + 4;
901
902 /* The data stored in this page begins at this offset. */
903 static const ulint OFFSET_DATA_BEGIN = OFFSET_TRX_ID + 6;
904
905 ulint payload() {
906 page_size_t page_size(dict_table_page_size(m_index->table));
907 return (page_size.physical() - OFFSET_DATA_BEGIN - FIL_PAGE_DATA_END);
908 }
909
910 z_data_page_t(mtr_t *mtr, dict_index_t *index)
911 : m_block(nullptr), m_mtr(mtr), m_index(index) {}
912
913 z_data_page_t(buf_block_t *block, mtr_t *mtr, dict_index_t *index)
914 : m_block(block), m_mtr(mtr), m_index(index) {}
915
916 /* Constructor.
917 @param[in] block the buffer block. */
918 z_data_page_t(buf_block_t *block)
919 : m_block(block), m_mtr(nullptr), m_index(nullptr) {}
920
921 /** Write the space identifier to the page header, without generating
922 redo log records.
923 @param[in] space_id the space identifier. */
924 void set_space_id_no_redo(space_id_t space_id) {
925 mlog_write_ulint(frame() + FIL_PAGE_SPACE_ID, space_id, MLOG_4BYTES,
926 nullptr);
927 }
928
929 /** Allocate one data page.
930 @param[in] hint hint page number for allocation.
931 @param[in] bulk true if bulk operation (OPCODE_INSERT_BULK)
932 false otherwise.
933 @return the allocated buffer block. */
934 buf_block_t *alloc(page_no_t hint, bool bulk);
935
936 /** Free this data page holding the zlob data. */
937 void dealloc() {
938 btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
939 m_block = nullptr;
940 }
941
942 /** Set the correct page type. */
943 void set_page_type() {
944 ut_ad(m_mtr != nullptr);
945
946 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_DATA,
947 MLOG_2BYTES, m_mtr);
948 }
949
950 void set_version_0() {
951 ut_ad(m_mtr != nullptr);
952
953 mlog_write_ulint(frame() + OFFSET_VERSION, 0, MLOG_1BYTE, m_mtr);
954 }
955
956 /** Set the next page. */
957 void set_next_page(page_no_t page_no) {
958 ut_ad(m_mtr != nullptr);
959
960 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES, m_mtr);
961 }
962
963 void init() {
964 ut_ad(m_mtr != nullptr);
965
966 set_page_type();
967 set_version_0();
968 set_next_page(FIL_NULL);
969 set_data_len(0);
970 set_trx_id(0);
971 }
972
973 byte *begin_data_ptr() const { return (frame() + OFFSET_DATA_BEGIN); }
974
975 void set_data_len(ulint len) {
976 ut_ad(m_mtr != nullptr);
977
978 mlog_write_ulint(frame() + OFFSET_DATA_LEN, len, MLOG_4BYTES, m_mtr);
979 }
980
981 ulint get_data_len() const {
982 return (mach_read_from_4(frame() + OFFSET_DATA_LEN));
983 }
984
985 void set_trx_id(trx_id_t tid) {
986 ut_ad(m_mtr != nullptr);
987
988 byte *ptr = frame() + OFFSET_TRX_ID;
989 mach_write_to_6(ptr, tid);
990 mlog_log_string(ptr, 6, m_mtr);
991 }
992
993 /** Update the header with given transaction identifier, without
994 writing redo log records.
995 @param[in] tid transaction identifier.*/
996 void set_trx_id_no_redo(trx_id_t tid) {
997 byte *ptr = frame() + OFFSET_TRX_ID;
998 mach_write_to_6(ptr, tid);
999 }
1000
1001 /** Get the page number. */
1002 page_no_t get_page_no() const {
1003 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
1004 }
1005
1006 fil_addr_t get_self_addr() const {
1007 page_no_t page_no = get_page_no();
1008 return (fil_addr_t(page_no, OFFSET_DATA_BEGIN));
1009 }
1010
1011 byte *frame() const { return (buf_block_get_frame(m_block)); }
1012
1013 buf_block_t *m_block;
1014 mtr_t *m_mtr;
1015 dict_index_t *m_index;
1016 };
1017
1018 /** A frag nodes page containing an array of z_frag_entry_t objects. */
1019 struct z_frag_node_page_t {
1020 /** Version information. One byte. */
1021 static const ulint OFFSET_VERSION = FIL_PAGE_DATA;
1022 static const ulint LOB_PAGE_DATA = OFFSET_VERSION + 1;
1023
1024 z_frag_node_page_t(mtr_t *mtr, dict_index_t *index)
1025 : m_block(nullptr), m_mtr(mtr), m_index(index) {}
1026
1027 /** Constructor
1028 @param[in] block the buffer block.*/
1029 explicit z_frag_node_page_t(buf_block_t *block)
1030 : m_block(block), m_mtr(nullptr), m_index(nullptr) {}
1031
1032 /** Write the space identifier to the page header, without generating
1033 redo log records.
1034 @param[in] space_id the space identifier. */
1035 void set_space_id_no_redo(space_id_t space_id) {
1036 mlog_write_ulint(frame() + FIL_PAGE_SPACE_ID, space_id, MLOG_4BYTES,
1037 nullptr);
1038 }
1039
1040 /** Set the correct page type. */
1041 void set_page_type() {
1042 ut_ad(m_mtr != nullptr);
1043
1044 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY,
1045 MLOG_2BYTES, m_mtr);
1046 }
1047
1048 /** Set the next page number. */
1049 void set_next_page_no(page_no_t page_no) {
1050 ut_ad(m_mtr != nullptr);
1051
1052 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES, m_mtr);
1053 }
1054
1055 void set_version_0() {
1056 ut_ad(m_mtr != nullptr);
1057
1058 mlog_write_ulint(frame() + OFFSET_VERSION, 0, MLOG_1BYTE, m_mtr);
1059 }
1060
1061 /** Get the page number. */
1062 page_no_t get_page_no() const {
1063 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
1064 }
1065
1066 /** Get the next page number. */
1067 page_no_t get_next_page_no() const {
1068 return (mach_read_from_4(frame() + FIL_PAGE_NEXT));
1069 }
1070
1071 /** Allocate a fragment nodes page.
1072 @return buffer block of the allocated fragment nodes page or nullptr. */
1073 buf_block_t *alloc(z_first_page_t &first, bool bulk);
1074
1075 void dealloc() {
1076 btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
1077 m_block = nullptr;
1078 }
1079
1080 /** Load the given compressed LOB fragment page.
1081 @param[in] page_no compressed LOB fragment page number.
1082 @return the buffer block of the given page number. */
1083 buf_block_t *load_x(page_no_t page_no) {
1084 page_id_t page_id(dict_index_get_space(m_index), page_no);
1085 page_size_t page_size(dict_table_page_size(m_index->table));
1086 m_block =
1087 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr);
1088
1089 ut_ad(m_block->get_page_type() == FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY);
1090
1091 return (m_block);
1092 }
1093
1094 void init(flst_base_node_t *free_lst) {
1095 ut_ad(m_mtr != nullptr);
1096
1097 ulint n = get_n_frag_entries();
1098 for (ulint i = 0; i < n; ++i) {
1099 byte *ptr = frame() + LOB_PAGE_DATA;
1100 ptr += (i * z_frag_entry_t::SIZE);
1101 z_frag_entry_t entry(ptr, m_mtr);
1102 entry.init();
1103 entry.push_back(free_lst);
1104 }
1105 }
1106
1107 ulint payload() const {
1108 const page_size_t page_size = dict_table_page_size(m_index->table);
1109 return (page_size.physical() - FIL_PAGE_DATA_END - LOB_PAGE_DATA);
1110 }
1111
1112 ulint get_n_frag_entries() const {
1113 return (payload() / z_frag_entry_t::SIZE);
1114 }
1115
1116 byte *frame() const { return (buf_block_get_frame(m_block)); }
1117
1118 /** The buffer block of the fragment page. */
1119 buf_block_t *m_block;
1120
1121 /** The mini-transaction context. */
1122 mtr_t *m_mtr;
1123
1124 /** The index to which the LOB belongs. */
1125 dict_index_t *m_index;
1126 }; // struct z_frag_node_page_t
1127
1128 /** Print information about the given compressed lob.
1129 @param[in] index the index dictionary object.
1130 @param[in] ref the LOB reference
1131 @param[out] out the output stream where information is printed.
1132 @return DB_SUCCESS on success, or an error code. */
1133 dberr_t z_print_info(const dict_index_t *index, const lob::ref_t &ref,
1134 std::ostream &out);
1135
1136 /** The fragment node represents one fragment. */
1137 struct frag_node_t {
1138 /** The offset where the length of fragment is stored. The length
1139 includes both the payload and the meta data overhead. */
1140 static const ulint OFFSET_LEN = plist_node_t::SIZE;
1141
1142 /** The offset where fragment id is stored. */
1143 static const ulint OFFSET_FRAG_ID = OFFSET_LEN + 2;
1144
1145 /** The offset where fragment data is stored. */
1146 static const ulint OFFSET_DATA = OFFSET_FRAG_ID + 2;
1147
1148 /** The size of a page directory entry in a fragment page in bytes.
1149 This must be equal to z_frag_page_t::SIZE_OF_PAGE_DIR_ENTRY*/
1150 static const ulint SIZE_OF_PAGE_DIR_ENTRY = 2;
1151
1152 /** Constructor.
1153 @param[in] node Page list node.
1154 @param[in] mtr Mini-transaction. */
1155 frag_node_t(const plist_node_t &node, mtr_t *mtr)
1156 : m_node(node), m_mtr(mtr) {}
1157
1158 frag_node_t(byte *frame, byte *ptr) : m_node(frame, ptr), m_mtr(nullptr) {}
1159
1160 /** Constructor.
1161 @param[in] frame Page frame where the fragment node is
1162 located.
1163 @param[in] ptr Location of fragment node within page
1164 frame.
1165 @param[in] mtr Mini-transaction context. */
1166 frag_node_t(byte *frame, byte *ptr, mtr_t *mtr)
1167 : m_node(frame, ptr, mtr), m_mtr(mtr) {}
1168
1169 /** Amount of space that will be used up by meta data. When a free
1170 space is taken from the fragment page to be used as a fragment
1171 node, header and footer will be the overhead. Footer is the page dir
1172 entry. The page dir entry may not be contiguous with the fragment.*/
1173 static ulint overhead() { return (SIZE_OF_PAGE_DIR_ENTRY + OFFSET_DATA); }
1174
1175 /** Only the header size. Don't include the page dir entry size here.*/
1176 static ulint header_size() { return (OFFSET_DATA); }
1177
1178 /** Constructor.
1179 @param[in] frame Page frame where the fragment node is
1180 located.
1181 @param[in] ptr Location of fragment node within page
1182 frame.
1183 @param[in] len Length of the fragment.
1184 @param[in] mtr Mini-transaction context. */
1185 frag_node_t(byte *frame, byte *ptr, ulint len, mtr_t *mtr)
1186 : m_node(frame, ptr, mtr), m_mtr(mtr) {
1187 ut_ad(mtr != nullptr);
1188
1189 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len, MLOG_2BYTES, mtr);
1190 }
1191
1192 byte *frag_begin() const { return (m_node.ptr() + OFFSET_DATA); }
1193
1194 byte *data_begin() const { return (m_node.ptr() + OFFSET_DATA); }
1195
1196 void set_total_len(ulint len) {
1197 ut_ad(m_mtr != nullptr);
1198
1199 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len, MLOG_2BYTES, m_mtr);
1200 }
1201
1202 /** Increment the total length of this fragment by 2 bytes. */
1203 void incr_length_by_2() {
1204 ut_ad(m_mtr != nullptr);
1205
1206 ulint len = mach_read_from_2(m_node.ptr() + OFFSET_LEN);
1207 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len + SIZE_OF_PAGE_DIR_ENTRY,
1208 MLOG_2BYTES, m_mtr);
1209 }
1210
1211 /** Decrement the total length of this fragment by 2 bytes. */
1212 void decr_length_by_2() {
1213 ut_ad(m_mtr != nullptr);
1214
1215 ulint len = mach_read_from_2(m_node.ptr() + OFFSET_LEN);
1216 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len - SIZE_OF_PAGE_DIR_ENTRY,
1217 MLOG_2BYTES, m_mtr);
1218 }
1219
1220 bool is_before(const frag_node_t &frag) const {
1221 return (m_node.is_before(frag.m_node));
1222 }
1223
1224 void set_frag_id_null() {
1225 ut_ad(m_mtr != nullptr);
1226
1227 mlog_write_ulint(m_node.ptr() + OFFSET_FRAG_ID, FRAG_ID_NULL, MLOG_2BYTES,
1228 m_mtr);
1229 }
1230
1231 void set_frag_id(ulint id) {
1232 ut_ad(m_mtr != nullptr);
1233
1234 mlog_write_ulint(m_node.ptr() + OFFSET_FRAG_ID, id, MLOG_2BYTES, m_mtr);
1235 }
1236
1237 ulint get_frag_id() const {
1238 return (mach_read_from_2(m_node.ptr() + OFFSET_FRAG_ID));
1239 }
1240
1241 /** Get the space available in this fragment for storing data. */
1242 ulint payload() const { return (get_total_len() - header_size()); }
1243
1244 /** Get the total length of this fragment, including its metadata. */
1245 ulint get_total_len() const {
1246 return (mach_read_from_2(m_node.ptr() + OFFSET_LEN));
1247 }
1248
1249 /** Get the offset of the current fragment within page.
1250 @return the offset of the current fragment within. */
1251 paddr_t addr() const { return (m_node.addr()); }
1252
1253 /** Gets the pointer to the beginning of the current fragment. Note
1254 that the beginning of the fragment contains meta data.
1255 @return pointer to the beginning of the current fragment. */
1256 byte *ptr() const {
1257 ut_ad(!m_node.is_null());
1258 return (m_node.ptr());
1259 }
1260
1261 /** Gets the pointer just after the current fragment. The pointer
1262 returned does not belong to this fragment. This is used to check
1263 adjacency.
1264 @return pointer to the end of the current fragment. */
1265 byte *end_ptr() const {
1266 ut_ad(!m_node.is_null());
1267 return (ptr() + get_total_len());
1268 }
1269
1270 /** Get the page frame.
1271 @return the page frame. */
1272 byte *frame() const { return (m_node.get_frame()); }
1273
1274 std::ostream &print(std::ostream &out) const {
1275 if (!m_node.is_null()) {
1276 ulint len = get_total_len();
1277 out << "[frag_node_t: " << m_node << ", len=" << len << "/" << payload()
1278 << ", frag_id=" << get_frag_id() << "]";
1279 } else {
1280 out << "[frag_node_t: null, len=0]";
1281 }
1282 return (out);
1283 }
1284
1285 frag_node_t get_next_frag() {
1286 ut_ad(!is_null());
1287 plist_node_t next = m_node.get_next_node();
1288 return (frag_node_t(next, m_mtr));
1289 }
1290
1291 frag_node_t get_next_node() { return (get_next_frag()); }
1292
1293 frag_node_t get_prev_node() { return (get_prev_frag()); }
1294
1295 frag_node_t get_prev_frag() {
1296 ut_ad(!is_null());
1297 plist_node_t prev = m_node.get_prev_node();
1298 return (frag_node_t(prev, m_mtr));
1299 }
1300
1301 /** Merge the current fragment node with the given next fragment node.
1302 This will succeed only if they are adjacent to each other.
1303 Detailed Note: There is a new page type FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY
1304 - and we can call it the fragment pages. Each fragment page contains
1305 one or more fragments. Each fragment is represented by a frag_node_t.
1306 And each fragment can be of different size. Consider a fragment page
1307 containing 4 fragments - f1, f2, f3 and f4. Suppose we free f2 and
1308 f3, then we can merge them into one single bigger fragment which is
1309 free.
1310 @param[in] next the next fragment.
1311 @return true if merge done, false otherwise. */
1312 bool merge(frag_node_t &next) {
1313 ut_ad(m_mtr != nullptr);
1314
1315 byte *p1 = ptr();
1316 ulint len1 = get_total_len();
1317 byte *p2 = next.ptr();
1318 ulint len2 = next.get_total_len();
1319
1320 if (p2 == (p1 + len1)) {
1321 set_total_len(len1 + len2);
1322 return (true);
1323 }
1324
1325 return (false);
1326 }
1327
1328 bool is_null() const { return (m_node.is_null()); }
1329
1330 bool is_equal(const frag_node_t &that) const {
1331 return (m_node.is_equal(that.m_node));
1332 }
1333
1334 bool is_equal(const plist_node_t &node) const {
1335 return (m_node.is_equal(node));
1336 }
1337
1338 /** The page list node. */
1339 plist_node_t m_node;
1340
1341 private:
1342 /** The mini-transaction context. It is only in-memory. */
1343 mtr_t *m_mtr;
1344 };
1345
1346 inline std::ostream &operator<<(std::ostream &out, const frag_node_t &obj) {
1347 return (obj.print(out));
1348 }
1349
1350 /** The fragment page. This page will contain fragments from different
1351 zlib streams. */
1352 struct z_frag_page_t {
1353 /** Version information. One byte. */
1354 static const ulint OFFSET_VERSION = FIL_PAGE_DATA;
1355
1356 /** The location of z_frag_entry_t for this page. */
1357 static const ulint OFFSET_FRAG_ENTRY = OFFSET_VERSION + 1;
1358
1359 /** The offset within page where the free space list begins. */
1360 static const ulint OFFSET_FREE_LIST = OFFSET_FRAG_ENTRY + FIL_ADDR_SIZE;
1361
1362 /** The offset within page where the fragment list begins. */
1363 static const ulint OFFSET_FRAGS_LIST =
1364 OFFSET_FREE_LIST + plist_base_node_t::SIZE;
1365
1366 /** The offset within page where the fragments can occupy . */
1367 static const ulint OFFSET_FRAGS_BEGIN =
1368 OFFSET_FRAGS_LIST + plist_base_node_t::SIZE;
1369
1370 /** Offset of number of page directory entries (from end) */
1371 static const ulint OFFSET_PAGE_DIR_ENTRY_COUNT = FIL_PAGE_DATA_END + 2;
1372
1373 /** Offset of first page directory entry (from end) */
1374 static const ulint OFFSET_PAGE_DIR_ENTRY_FIRST =
1375 OFFSET_PAGE_DIR_ENTRY_COUNT + 2;
1376
1377 static const ulint SIZE_OF_PAGE_DIR_ENTRY = 2; /* bytes */
1378
1379 /** Constructor.
1380 @param[in] block Buffer block containing the fragment page.
1381 @param[in] mtr Mini-transaction context.
1382 @param[in] index Clustered index to which LOB belongs. */
1383 z_frag_page_t(buf_block_t *block, mtr_t *mtr, dict_index_t *index)
1384 : m_block(block), m_mtr(mtr), m_index(index) {
1385 static_assert(frag_node_t::SIZE_OF_PAGE_DIR_ENTRY ==
1386 z_frag_page_t::SIZE_OF_PAGE_DIR_ENTRY);
1387 }
1388
1389 /** Constructor.
1390 @param[in] mtr Mini-transaction context.
1391 @param[in] index Clustered index to which LOB belongs. */
1392 z_frag_page_t(mtr_t *mtr, dict_index_t *index)
1393 : z_frag_page_t(nullptr, mtr, index) {}
1394
1395 /** Constructor.
1396 @param[in] block the buffer block containing the fragment page.*/
1397 explicit z_frag_page_t(buf_block_t *block)
1398 : m_block(block), m_mtr(nullptr), m_index(nullptr) {
1399 static_assert(frag_node_t::SIZE_OF_PAGE_DIR_ENTRY ==
1400 z_frag_page_t::SIZE_OF_PAGE_DIR_ENTRY);
1401 }
1402
1403 /** Write the space identifier to the page header, without generating
1404 redo log records.
1405 @param[in] space_id the space identifier. */
1406 void set_space_id_no_redo(space_id_t space_id) {
1407 mlog_write_ulint(frame() + FIL_PAGE_SPACE_ID, space_id, MLOG_4BYTES,
1408 nullptr);
1409 }
1410
1411 z_frag_entry_t get_frag_entry_x();
1412 z_frag_entry_t get_frag_entry_s();
1413
1414 void update_frag_entry() {
1415 z_frag_entry_t entry = get_frag_entry_x();
1416 entry.update(*this);
1417 }
1418
1419 void set_version_0() {
1420 mlog_write_ulint(frame() + OFFSET_VERSION, 0, MLOG_1BYTE, m_mtr);
1421 }
1422
1423 flst_node_t *addr2ptr_x(fil_addr_t &addr) {
1424 space_id_t space = dict_index_get_space(m_index);
1425 const page_size_t page_size = dict_table_page_size(m_index->table);
1426 return (fut_get_ptr(space, page_size, addr, RW_X_LATCH, m_mtr));
1427 }
1428
1429 flst_node_t *addr2ptr_s(fil_addr_t &addr) {
1430 space_id_t space = dict_index_get_space(m_index);
1431 const page_size_t page_size = dict_table_page_size(m_index->table);
1432 return (fut_get_ptr(space, page_size, addr, RW_S_LATCH, m_mtr));
1433 }
1434
1435 void set_frag_entry(const fil_addr_t &addr) const {
1436 ut_a(addr.boffset < get_page_size());
1437 return (flst_write_addr(frame() + OFFSET_FRAG_ENTRY, addr, m_mtr));
1438 }
1439
1440 /** Obtain the file address of the fragment entry that denotes the
1441 current fragment page.
1442 @return the file address of the fragment entry. */
1443 fil_addr_t get_frag_entry() const {
1444 return (flst_read_addr(frame() + OFFSET_FRAG_ENTRY, m_mtr));
1445 }
1446
1447 void set_frag_entry_null() const {
1448 return (flst_write_addr(frame() + OFFSET_FRAG_ENTRY, fil_addr_null, m_mtr));
1449 }
1450
1451 ulint get_n_dir_entries() const {
1452 byte *ptr = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_COUNT;
1453 return (mach_read_from_2(ptr));
1454 }
1455
1456 void set_n_dir_entries(ulint n) const {
1457 byte *ptr = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_COUNT;
1458 mlog_write_ulint(ptr, n, MLOG_2BYTES, m_mtr);
1459 }
1460
1461 bool is_border_frag(const frag_node_t &node) const {
1462 return (slots_end_ptr() == node.end_ptr());
1463 }
1464
1465 byte *slots_end_ptr() const {
1466 ulint n = get_n_dir_entries();
1467 byte *first = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_COUNT;
1468 byte *ptr = first - (n * SIZE_OF_PAGE_DIR_ENTRY);
1469 return (ptr);
1470 }
1471
1472 paddr_t frag_id_to_addr(ulint frag_id) const {
1473 byte *first = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_FIRST;
1474 byte *ptr = first - (frag_id * SIZE_OF_PAGE_DIR_ENTRY);
1475 return (mach_read_from_2(ptr));
1476 }
1477
1478 ulint get_nth_dir_entry(ulint frag_id) {
1479 byte *first = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_FIRST;
1480 byte *ptr = first - (frag_id * SIZE_OF_PAGE_DIR_ENTRY);
1481 return (mach_read_from_2(ptr));
1482 }
1483
1484 void set_nth_dir_entry(ulint frag_id, paddr_t val) {
1485 byte *first = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_FIRST;
1486 byte *ptr = first - (frag_id * SIZE_OF_PAGE_DIR_ENTRY);
1487 mlog_write_ulint(ptr, val, MLOG_2BYTES, m_mtr);
1488 }
1489
1490 ulint init_last_dir_entry() {
1491 ulint n = get_n_dir_entries();
1492 set_nth_dir_entry(n - 1, 0);
1493 return (n - 1);
1494 }
1495
1496 void incr_n_dir_entries() const {
1497 byte *ptr = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_COUNT;
1498 ulint n = mach_read_from_2(ptr);
1499 ut_a(n < FRAG_ID_NULL);
1500 mlog_write_ulint(ptr, n + 1, MLOG_2BYTES, m_mtr);
1501 }
1502
1503 void decr_n_dir_entries() const {
1504 byte *ptr = frame() + get_page_size() - OFFSET_PAGE_DIR_ENTRY_COUNT;
1505 ulint n = mach_read_from_2(ptr);
1506 ut_a(n > 0);
1507 mlog_write_ulint(ptr, n - 1, MLOG_2BYTES, m_mtr);
1508 }
1509
1510 ulint get_page_size() const {
1511 const page_size_t page_size = dict_table_page_size(m_index->table);
1512 return (page_size.physical());
1513 }
1514
1515 ulint space_used_by_dir() const {
1516 ulint n = get_n_dir_entries();
1517 return (n * SIZE_OF_PAGE_DIR_ENTRY);
1518 }
1519
1520 ulint locate_free_slot() {
1521 ulint n = get_n_dir_entries();
1522
1523 for (ulint frag_id = 0; frag_id < n; frag_id++) {
1524 ulint paddr = get_nth_dir_entry(frag_id);
1525
1526 if (paddr == 0) {
1527 return (frag_id);
1528 }
1529 }
1530
1531 return (FRAG_ID_NULL);
1532 }
1533
1534 /** Allocate a fragment id.
1535 @return On success, return fragment id.
1536 @return On failure, return FRAG_ID_NULL. */
1537 ulint alloc_frag_id() {
1538 ulint id = locate_free_slot();
1539
1540 if (id == FRAG_ID_NULL) {
1541 return (alloc_dir_entry());
1542 }
1543
1544 return (id);
1545 }
1546
1547 std::ostream &print_frag_id(std::ostream &out) {
1548 ulint n = get_n_dir_entries();
1549 out << "FRAG IDS: " << std::endl;
1550
1551 for (ulint frag_id = 0; frag_id < n; frag_id++) {
1552 out << "id=" << frag_id << ", addr=" << frag_id_to_addr(frag_id)
1553 << std::endl;
1554 }
1555
1556 return (out);
1557 }
1558
1559 /** Grow the frag directory by one entry.
1560 @return the fragment identifier that was newly added. */
1561 ulint alloc_dir_entry();
1562
1563 /** Set the next page. */
1564 void set_page_next(page_no_t page_no) {
1565 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES, m_mtr);
1566 }
1567
1568 /** Set the prev page. */
1569 void set_page_prev(page_no_t page_no) { set_page_prev(page_no, m_mtr); }
1570
1571 /** Set the prev page. */
1572 void set_page_prev(page_no_t page_no, mtr_t *mtr) {
1573 mlog_write_ulint(frame() + FIL_PAGE_PREV, page_no, MLOG_4BYTES, mtr);
1574 }
1575
1576 /** Get the next page number.
1577 @return next page number. */
1578 page_no_t get_next_page_no() const { return (m_block->get_next_page_no()); }
1579
1580 /** Get the prev page number (FIL_PAGE_PREV).
1581 @param[in] mtr Mini-transaction latch context.
1582 @return prev page number. */
1583 page_no_t get_prev_page_no(mtr_t *mtr) const {
1584 return (mtr_read_ulint(frame() + FIL_PAGE_PREV, MLOG_4BYTES, mtr));
1585 }
1586
1587 /** Get the prev page number.
1588 @return prev page number. */
1589 page_no_t get_prev_page_no() const { return (get_prev_page_no(m_mtr)); }
1590
1591 /** Allocate the fragment page.
1592 @param[in] first first page of this LOB.
1593 @param[in] hint hint page number for allocation.
1594 @param[in] bulk true if bulk operation (OPCODE_INSERT_BULK)
1595 false otherwise.
1596 @return the allocated buffer block. */
1597 buf_block_t *alloc(z_first_page_t &first, page_no_t hint, bool bulk);
1598
1599 /** Free the fragment page along with its entry.
1600 @param[in] first first page of LOB.
1601 @param[in] alloc_mtr mini trx to perform this modification. */
1602 void dealloc_with_entry(z_first_page_t &first, mtr_t *alloc_mtr);
1603
1604 /** Free the fragment page. */
1605 void dealloc() {
1606 btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
1607 m_block = nullptr;
1608 }
1609
1610 buf_block_t *load_x(page_no_t page_no) {
1611 page_id_t page_id(dict_index_get_space(m_index), page_no);
1612 page_size_t page_size(dict_table_page_size(m_index->table));
1613 m_block =
1614 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr);
1615 return (m_block);
1616 }
1617
1618 void merge_free_frags() {
1619 plist_base_node_t free_lst = free_list();
1620 frag_node_t frag(free_lst.get_first_node(), m_mtr);
1621 frag_node_t next = frag.get_next_frag();
1622
1623 while (!next.is_null() && frag.merge(next)) {
1624 free_lst.remove(next.m_node);
1625 next = frag.get_next_frag();
1626 }
1627 }
1628
1629 void merge_free_frags(frag_node_t &frag) {
1630 ut_ad(!frag.is_null());
1631 plist_base_node_t free_lst = free_list();
1632 frag_node_t next = frag.get_next_frag();
1633
1634 while (!next.is_null() && frag.merge(next)) {
1635 free_lst.remove(next.m_node);
1636 next = frag.get_next_frag();
1637 }
1638 }
1639
1640 bool validate_lists() const {
1641 plist_base_node_t free_lst = free_list();
1642 plist_base_node_t frag_lst = frag_list();
1643 plist_node_t free_node = free_lst.get_first_node();
1644
1645 while (!free_node.is_null()) {
1646 plist_node_t frag_node = frag_lst.get_first_node();
1647
1648 while (!frag_node.is_null()) {
1649 ut_ad(frag_node.addr() != free_node.addr());
1650 frag_node = frag_node.get_next_node();
1651 }
1652
1653 free_node = free_node.get_next_node();
1654 }
1655 return (true);
1656 }
1657
1658 void insert_into_free_list(frag_node_t &frag) {
1659 ut_ad(frag.get_frag_id() == FRAG_ID_NULL);
1660
1661 plist_base_node_t free_lst = free_list();
1662
1663 plist_node_t node = free_lst.get_first_node();
1664 plist_node_t prev_node(m_mtr);
1665
1666 while (!node.is_null()) {
1667 ut_ad(frag.addr() != node.addr());
1668 if (frag.addr() < node.addr()) {
1669 break;
1670 }
1671 prev_node = node;
1672 node = node.get_next_node();
1673 }
1674
1675 free_lst.insert_before(node, frag.m_node);
1676
1677 if (prev_node.is_null()) {
1678 merge_free_frags();
1679 } else {
1680 frag_node_t prev_frag(prev_node, m_mtr);
1681 merge_free_frags(prev_frag);
1682 }
1683 }
1684
1685 /** Insert the given fragment node into the fragment list.
1686 @param[in,out] frag the fragment node to be inserted.*/
1687 void insert_into_frag_list(frag_node_t &frag) {
1688 plist_base_node_t frag_lst = frag_list();
1689 plist_node_t node = frag_lst.get_first_node();
1690
1691 while (!node.is_null()) {
1692 ut_ad(frag.addr() != node.addr());
1693 if (frag.addr() < node.addr()) {
1694 break;
1695 }
1696 node = node.get_next_node();
1697 }
1698
1699 frag_lst.insert_before(node, frag.m_node);
1700 }
1701
1702 /** Split one free fragment into two. This is not splitting a
1703 fragment page. This is just splitting one free fragment into two.
1704 When we want to allocate one fragment, we identify a big enough free
1705 fragment and split it into two - one will be the allocated portion and
1706 other will become a free fragment.
1707 @param[in] free_frag the free fragment that will be split.
1708 @param[in] size the payload size in bytes. */
1709 void split_free_frag(frag_node_t &free_frag, ulint size) {
1710 ut_ad(size < free_frag.payload());
1711 const ulint old_total_len = free_frag.get_total_len();
1712 plist_base_node_t free_lst = free_list();
1713
1714 /* Locate the next fragment */
1715 byte *p2 = free_frag.data_begin() + size;
1716
1717 ulint remain =
1718 free_frag.get_total_len() - frag_node_t::header_size() - size;
1719
1720 ut_a(remain >= frag_node_t::OFFSET_DATA);
1721
1722 free_frag.set_total_len(frag_node_t::header_size() + size);
1723
1724 frag_node_t frag2(frame(), p2, remain, m_mtr);
1725 frag2.set_total_len(remain);
1726 frag2.set_frag_id_null();
1727 free_lst.insert_after(free_frag.m_node, frag2.m_node);
1728
1729 ut_a(free_frag.get_total_len() + frag2.get_total_len() == old_total_len);
1730
1731 ut_ad(validate_lists());
1732 }
1733
1734 frag_node_t get_frag_node(frag_id_t id) const {
1735 ut_ad(id != FRAG_ID_NULL);
1736
1737 paddr_t off = frag_id_to_addr(id);
1738 byte *f = frame();
1739 return (frag_node_t(f, f + off));
1740 }
1741
1742 void dealloc_fragment(ulint frag_id) {
1743 ut_ad(frag_id != FRAG_ID_NULL);
1744
1745 paddr_t off = frag_id_to_addr(frag_id);
1746 byte *f = frame();
1747 frag_node_t frag(f, f + off, m_mtr);
1748 dealloc_fragment(frag);
1749 dealloc_frag_id(frag_id);
1750
1751 /* Update the index entry. */
1752 update_frag_entry();
1753 }
1754
1755 /** Allocate a fragment with the given payload.
1756 @param[in] size the payload size.
1757 @param[in] entry the index entry of the given frag page.
1758 @return the frag_id of the allocated fragment.
1759 @return FRAG_ID_NULL if fragment could not be allocated. */
1760 frag_id_t alloc_fragment(ulint size, z_frag_entry_t &entry);
1761
1762 plist_base_node_t free_list() const {
1763 byte *f = frame();
1764 return (plist_base_node_t(f, f + OFFSET_FREE_LIST, m_mtr));
1765 }
1766
1767 plist_base_node_t frag_list() const {
1768 byte *f = frame();
1769 return (plist_base_node_t(f, f + OFFSET_FRAGS_LIST, m_mtr));
1770 }
1771
1772 void set_page_type() {
1773 byte *ptr = frame() + FIL_PAGE_TYPE;
1774 mlog_write_ulint(ptr, FIL_PAGE_TYPE_ZLOB_FRAG, MLOG_2BYTES, m_mtr);
1775 }
1776
1777 page_type_t get_page_type() const {
1778 return (mach_read_from_2(frame() + FIL_PAGE_TYPE));
1779 }
1780
1781 const char *get_page_type_str() const {
1782 page_type_t type = get_page_type();
1783 ut_a(type == FIL_PAGE_TYPE_ZLOB_FRAG);
1784 return ("FIL_PAGE_TYPE_ZLOB_FRAG");
1785 }
1786
1787 /** The maximum free space available in a fragment page. Adjustment
1788 needs to be done with the frag_node_t::overhead().*/
1789 ulint payload() { return (z_frag_page_t::max_payload(m_index)); }
1790
1791 /** The maximum free space available in a fragment page. Adjustment
1792 needs to be done with the frag_node_t::overhead().*/
1793 static ulint max_payload(dict_index_t *index) {
1794 page_size_t page_size(dict_table_page_size(index->table));
1795 return (page_size.physical() - OFFSET_FRAGS_BEGIN -
1796 OFFSET_PAGE_DIR_ENTRY_COUNT);
1797 }
1798
1799 /** Determine if the given length of data can fit into a fragment
1800 page.
1801 @param[in] index the clust index into which LOB is inserted.
1802 @param[in] data_size The length of data to operate.
1803 @return true if data can fit into fragment page, false otherwise. */
1804 static bool can_data_fit(dict_index_t *index, ulint data_size);
1805
1806 /** Get the frag page number. */
1807 page_no_t get_page_no() const { return (m_block->get_page_no()); }
1808
1809 byte *frame() const { return (buf_block_get_frame(m_block)); }
1810
1811 std::ostream &print(std::ostream &out) const {
1812 print_free_list(out);
1813 print_frag_list(out);
1814 print_frags_in_order(out);
1815 print_page_dir(out);
1816 return (out);
1817 }
1818
1819 /** Get the total amount of stored data in this page. */
1820 ulint get_total_stored_data() const;
1821
1822 /** Get the total cumulative free space in this page. */
1823 ulint get_total_free_len() const;
1824
1825 /** Get the big free space in this page. */
1826 ulint get_big_free_len() const;
1827
1828 /** Get the number of fragments in this frag page. */
1829 ulint get_n_frags() const {
1830 plist_base_node_t frag_lst = frag_list();
1831 return (frag_lst.get_len());
1832 }
1833
1834 std::ostream &print_frags_in_order(std::ostream &out) const;
1835
1836 std::ostream &print_free_list(std::ostream &out) const {
1837 if (m_block == nullptr) {
1838 return (out);
1839 }
1840
1841 plist_base_node_t free_lst = free_list();
1842 out << "[Free List: " << free_lst << "]" << std::endl;
1843
1844 for (plist_node_t cur = free_lst.get_first_node(); !cur.is_null();
1845 cur = cur.get_next_node()) {
1846 frag_node_t frag(cur, m_mtr);
1847 out << frag << std::endl;
1848 }
1849 return (out);
1850 }
1851
1852 std::ostream &print_frag_list(std::ostream &out) const {
1853 if (m_block == nullptr) {
1854 return (out);
1855 }
1856
1857 plist_base_node_t frag_lst = frag_list();
1858 out << "[Frag List: " << frag_lst << "]" << std::endl;
1859
1860 for (plist_node_t cur = frag_lst.get_first_node(); !cur.is_null();
1861 cur = cur.get_next_node()) {
1862 frag_node_t frag(cur, m_mtr);
1863 out << frag << std::endl;
1864 }
1865 return (out);
1866 }
1867
1868 std::ostream &print_page_dir(std::ostream &out) const {
1869 if (m_block == nullptr) {
1870 return (out);
1871 }
1872
1873 ulint n = get_n_dir_entries();
1874
1875 for (ulint frag_id = 0; frag_id < n; ++frag_id) {
1876 paddr_t off = frag_id_to_addr(frag_id);
1877 out << "[frag_id=" << frag_id << ", addr=" << off << "]" << std::endl;
1878 }
1879
1880 return (out);
1881 }
1882
1883 void set_mtr(mtr_t *mtr) { m_mtr = mtr; }
1884
1885 void set_index(dict_index_t *index) { m_index = index; }
1886
1887 void set_block_null() { m_block = nullptr; }
1888
1889 /** Determine if the given fragment node is the last fragment
1890 node adjacent to the directory.
1891 @return true if it is last fragment node, false otherwise. */
1892 bool is_last_frag(const frag_node_t &node) const {
1893 return (node.end_ptr() == slots_end_ptr());
1894 }
1895
1896 private:
1897 fil_addr_t get_frag_entry_addr() const {
1898 return (flst_read_addr(frame() + OFFSET_FRAG_ENTRY, m_mtr));
1899 }
1900
1901 void dealloc_fragment(frag_node_t &frag) {
1902 plist_base_node_t frag_lst = frag_list();
1903 frag_lst.remove(frag.m_node);
1904 frag.set_frag_id_null();
1905 insert_into_free_list(frag);
1906 }
1907
1908 /** Deallocate all the free slots from the end of the page
1909 directory. */
1910 void dealloc_frag_id();
1911
1912 /** Deallocate the given fragment id.
1913 @param[in] frag_id The fragment that needs to be deallocated. */
1914 void dealloc_frag_id(ulint frag_id) {
1915 set_nth_dir_entry(frag_id, 0);
1916 dealloc_frag_id();
1917 }
1918
1919 buf_block_t *m_block;
1920 mtr_t *m_mtr;
1921 dict_index_t *m_index;
1922 };
1923
1924 /** Insert one chunk of input. The maximum size of a chunk is Z_CHUNK_SIZE.
1925 @param[in] index Clustered index in which LOB is inserted.
1926 @param[in] first First page of the LOB.
1927 @param[in] trx Transaction doing the insertion.
1928 @param[in] blob Uncompressed LOB to be inserted.
1929 @param[in] len Length of the blob.
1930 @param[out] out_entry Newly inserted index entry. can be NULL.
1931 @param[in] mtr Mini-transaction
1932 @param[in] bulk true if it is bulk operation, false otherwise.
1933 @return DB_SUCCESS on success, error code on failure. */
1934 dberr_t z_insert_chunk(dict_index_t *index, z_first_page_t &first, trx_t *trx,
1935 byte *blob, ulint len, z_index_entry_t *out_entry,
1936 mtr_t *mtr, bool bulk);
1937
1938 } // namespace lob
1939
1940 #endif // lob0impl_h