Debian Codegraph @

unittest/gunit/innodb/lob/zlob0int.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 _zlob0int_h_
28 #define _zlob0int_h_
29
30 /** Internal header file for compressed lob (zlob). */
31
32 #include <string.h>
33 #include "db0err.h"
34 #include "fil0fil.h"
35 #include "fil0types.h"
36 #include "fut0lst.h"
37 #include "lot0buf.h"
38 #include "lot0lob.h"
39 #include "lot0plist.h"
40 #include "lot0types.h"
41 #include "page0page.h"
42
43 namespace zlob {
44
45 struct z_first_page_t;
46 struct z_frag_page_t;
47
48 /** An index entry pointing to one zlib stream. */
49 struct z_index_entry_t {
50 /** Offset with index entry pointing to the prev index entry. */
51 static const ulint OFFSET_PREV = 0;
52
53 /** Offset with index entry pointing to the next index entry. */
54 static const ulint OFFSET_NEXT = OFFSET_PREV + FIL_ADDR_SIZE;
55
56 /** Offset within index entry pointing to base node of list of versions.*/
57 static const ulint OFFSET_VERSIONS = OFFSET_NEXT + FIL_ADDR_SIZE;
58
59 /** Offset within index entry pointing to trxid.*/
60 static const ulint OFFSET_TRXID = OFFSET_VERSIONS + FLST_BASE_NODE_SIZE;
61
62 /** Offset within index entry pointing to page number where zlib stream
63 starts. This could be a data page or a fragment page. */
64 static const ulint OFFSET_Z_PAGE_NO = OFFSET_TRXID + 6;
65
66 /** Offset within index entry pointing to location of zlib stream.*/
67 static const ulint OFFSET_Z_FRAG_ID = OFFSET_Z_PAGE_NO + 4;
68
69 /** Offset within index entry pointing to uncompressed data len (bytes).*/
70 static const ulint OFFSET_DATA_LEN = OFFSET_Z_FRAG_ID + 2;
71
72 /** Offset within index entry pointing to compressed data len (bytes).*/
73 static const ulint OFFSET_ZDATA_LEN = OFFSET_DATA_LEN + 4;
74
75 /** Total size of one index entry. */
76 static const ulint SIZE = OFFSET_ZDATA_LEN + 4;
77
78 /** Constructor. */
79 z_index_entry_t() : m_node(nullptr) {}
80 z_index_entry_t(flst_node_t *node) : m_node(node) {}
81
82 /** Point to another index entry.
83 @param[in] node point to this file list node. */
84 void reset(flst_node_t *node) { m_node = node; }
85
86 /** Point to another index entry. */
87 void reset(const z_index_entry_t &entry) { m_node = entry.m_node; }
88
89 /** Initialize an index entry to some sane value. */
90 void init() {
91 set_prev_null();
92 set_next_null();
93 set_versions_null();
94 set_trx_id(0);
95 set_z_page_no(FIL_NULL);
96 set_z_frag_id(FRAG_ID_NULL);
97 set_data_len(0);
98 set_zdata_len(0);
99 }
100
101 /** Can this index entry be purged.
102 @param[in] trxid the transaction that is being purged.
103 @return true if this entry can be purged, false otherwise. */
104 bool can_be_purged(trx_id_t trxid) { return (trxid == get_trx_id()); }
105
106 /** Purge one index entry.
107 @param[in] trxid purging data belonging to trxid.
108 @param[in,out] lst the list from which this entry will be removed.
109 @param[in,out] free_list the list to which this entry will be added. */
110 fil_addr_t purge_version(trx_id_t trxid, z_first_page_t &first,
111 flst_base_node_t *lst, flst_base_node_t *free_list);
112
113 /** Purge the current index entry. An index entry points to either a FIRST
114 page or DATA page. That LOB page will be freed if it is DATA page. A FIRST
115 page should not be freed. */
116 void purge(z_first_page_t &first);
117
118 /** Remove this node from the given list.
119 @param[in] bnode the base node of the list from which to remove current
120 node. */
121 void remove(flst_base_node_t *bnode) { flst_remove(bnode, m_node); }
122
123 void insert_after(flst_base_node_t *base, z_index_entry_t &entry) {
124 flst_insert_after(base, m_node, entry.get_node());
125 }
126
127 void insert_before(flst_base_node_t *base, z_index_entry_t &entry) {
128 flst_insert_before(base, entry.get_node(), m_node);
129 }
130
131 /** Add this node as the last node in the given list.
132 @param[in] bnode the base node of the file list. */
133 void push_back(flst_base_node_t *bnode) { flst_add_last(bnode, m_node); }
134
135 /** Add this node as the last node in the given list.
136 @param[in] bnode the base node of the file list. */
137 void push_front(flst_base_node_t *bnode) { flst_add_first(bnode, m_node); }
138
139 /** Set the previous index entry as null. */
140 void set_prev_null() { flst_write_addr(m_node + OFFSET_PREV, fil_addr_null); }
141
142 /** Get the location of previous index entry. */
143 fil_addr_t get_prev() const { return (flst_read_addr(m_node + OFFSET_PREV)); }
144
145 /** Get the location of next index entry. */
146 fil_addr_t get_next() const { return (flst_read_addr(m_node + OFFSET_NEXT)); }
147
148 /** Set the next index entry as null. */
149 void set_next_null() { flst_write_addr(m_node + OFFSET_NEXT, fil_addr_null); }
150
151 /** Set the versions list as null. */
152 void set_versions_null() {
153 flst_base_node_t *bnode = get_versions_list();
154 flst_init(bnode);
155 }
156
157 /** Get the base node of the list of versions. */
158 flst_base_node_t *get_versions_list() const {
159 return (m_node + OFFSET_VERSIONS);
160 }
161
162 trx_id_t get_trx_id() const {
163 return (mach_read_from_6(m_node + OFFSET_TRXID));
164 }
165
166 /** Check to see if the given transaction can see this entry.
167 @param[in] trxid the transaction wanting to read the entry.
168 @return true if the transaction can see the entry, false otherwise.
169 @todo This check is for the prototype. When porting to InnoDB this
170 will take into account the read view. Fix it. */
171 bool can_see(trx_id_t trxid) const {
172 trx_id_t entry_trxid = get_trx_id();
173 return (entry_trxid <= trxid);
174 }
175
176 void set_trx_id(trx_id_t id) {
177 return (mach_write_to_6(m_node + OFFSET_TRXID, id));
178 }
179
180 page_no_t get_z_page_no() const {
181 return (mach_read_from_4(m_node + OFFSET_Z_PAGE_NO));
182 }
183
184 void set_z_page_no(page_no_t page_no) {
185 mlog_write_ulint(m_node + OFFSET_Z_PAGE_NO, page_no, MLOG_4BYTES);
186 }
187
188 page_no_t get_z_frag_id() const {
189 return (mach_read_from_2(m_node + OFFSET_Z_FRAG_ID));
190 }
191
192 void set_z_frag_id(frag_id_t id) {
193 mlog_write_ulint(m_node + OFFSET_Z_FRAG_ID, id, MLOG_2BYTES);
194 }
195
196 /** Get the uncompressed data length in bytes. */
197 ulint get_data_len() const {
198 return (mach_read_from_4(m_node + OFFSET_DATA_LEN));
199 }
200
201 /** Set the uncompressed data length in bytes.
202 @param[in] len the uncompressed data length in bytes */
203 void set_data_len(ulint len) {
204 mlog_write_ulint(m_node + OFFSET_DATA_LEN, len, MLOG_4BYTES);
205 }
206
207 /** Get the compressed data length in bytes. */
208 ulint get_zdata_len() const {
209 return (mach_read_from_4(m_node + OFFSET_ZDATA_LEN));
210 }
211
212 /** Set the compressed data length in bytes.
213 @param[in] len the compressed data length in bytes */
214 void set_zdata_len(ulint len) {
215 mlog_write_ulint(m_node + OFFSET_ZDATA_LEN, len, MLOG_4BYTES);
216 }
217
218 /* The given entry becomes the old version of the current entry.
219 Move the version base node from old entry to current entry.
220 @param[in] entry the old entry */
221 void set_old_version(z_index_entry_t &entry) {
222 flst_base_node_t *version_list = get_versions_list();
223 ut_ad(flst_get_len(version_list) == 0);
224
225 entry.move_version_base_node(*this);
226 entry.push_front(get_versions_list());
227 }
228
229 /** The current index entry points to a latest LOB page. It may or may
230 not have older versions. If older version is there, bring it back to the
231 index list from the versions list. Then remove the current entry from
232 the index list. Move the versions list from current entry to older entry.
233 @param[in] trxid The transaction identifier.
234 @param[in] first The first lob page containing index list and free
235 list. */
236 fil_addr_t make_old_version_current(trx_id_t trxid, z_first_page_t &first);
237
238 flst_node_t *get_node() { return (m_node); }
239 bool is_null() const { return (m_node == nullptr); }
240
241 std::ostream &print(std::ostream &out) const;
242 std::ostream &print_pages(std::ostream &out) const;
243
244 private:
245 /** Move the version base node from current entry to the given entry.
246 @param[in] entry The index entry to which the version base node is moved
247 to. */
248 void move_version_base_node(z_index_entry_t &entry) {
249 flst_base_node_t *from_node = get_versions_list();
250 flst_base_node_t *to_node = entry.get_versions_list();
251
252 memcpy(to_node, from_node, FLST_BASE_NODE_SIZE);
253 ut_ad(flst_get_len(from_node) == flst_get_len(to_node));
254 fil_addr_t addr1 = flst_get_first(from_node);
255 fil_addr_t addr2 = flst_get_first(to_node);
256 ut_ad(addr1.is_equal(addr2));
257 addr1 = flst_get_last(from_node);
258 addr2 = flst_get_last(to_node);
259 ut_ad(addr1.is_equal(addr2));
260 flst_init(from_node);
261 }
262
263 flst_node_t *m_node;
264 };
265
266 inline std::ostream &operator<<(std::ostream &out, const z_index_entry_t &obj) {
267 return (obj.print(out));
268 }
269
270 /** An entry representing one fragment page. */
271 struct z_frag_entry_t {
272 public:
273 /** Offset withing frag entry pointing to prev frag entry. */
274 static const ulint OFFSET_PREV = 0;
275
276 /** Offset withing frag entry pointing to next frag entry. */
277 static const ulint OFFSET_NEXT = OFFSET_PREV + FIL_ADDR_SIZE;
278
279 /** Offset withing frag entry holding the page number of frag page. */
280 static const ulint OFFSET_PAGE_NO = OFFSET_NEXT + FIL_ADDR_SIZE;
281
282 /** Number of used fragments. */
283 static const ulint OFFSET_N_FRAGS = OFFSET_PAGE_NO + 4;
284
285 /** Used space in bytes. */
286 static const ulint OFFSET_USED_LEN = OFFSET_N_FRAGS + 2;
287
288 /** Total free space in bytes. */
289 static const ulint OFFSET_TOTAL_FREE_LEN = OFFSET_USED_LEN + 2;
290
291 /** The biggest free frag space in bytes. */
292 static const ulint OFFSET_BIG_FREE_LEN = OFFSET_TOTAL_FREE_LEN + 2;
293
294 /** Total size of one frag entry. */
295 static const ulint SIZE = OFFSET_BIG_FREE_LEN + 2;
296
297 /** Constructor. */
298 z_frag_entry_t(flst_node_t *node) : m_node(node) {}
299
300 /** Constructor. */
301 z_frag_entry_t() : m_node(nullptr) {}
302
303 /** Initialize */
304 void init() {
305 set_prev_null();
306 set_next_null();
307 set_page_no(FIL_NULL);
308 set_n_frags(0);
309 set_used_len(0);
310 set_total_free_len(0);
311 set_big_free_len(0);
312 }
313
314 fil_addr_t get_self_addr() const {
315 page_t *frame = page_align(m_node);
316 page_no_t page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET);
317 ulint offset = m_node - frame;
318 ut_ad(offset < UNIV_PAGE_SIZE);
319 return (fil_addr_t(page_no, offset));
320 }
321
322 void update(const z_frag_page_t &frag_page);
323
324 /** Remove this node from the given list.
325 @param[in] bnode the base node of the list from which to remove current
326 node. */
327 void remove(flst_base_node_t *bnode) { flst_remove(bnode, m_node); }
328
329 /** Add this node as the last node in the given list.
330 @param[in] bnode the base node of the file list. */
331 void push_back(flst_base_node_t *bnode) { flst_add_last(bnode, m_node); }
332
333 /** Add this node as the first node in the given list.
334 @param[in] bnode the base node of the file list. */
335 void push_front(flst_base_node_t *bnode) { flst_add_first(bnode, m_node); }
336
337 /** Point to another frag entry.
338 @param[in] node point to this file list node. */
339 void reset(flst_node_t *node) { m_node = node; }
340
341 /** Set the previous frag entry as null. */
342 void set_prev_null() { flst_write_addr(m_node + OFFSET_PREV, fil_addr_null); }
343
344 /** Set the previous frag entry as null. */
345 void set_prev(const fil_addr_t &addr) {
346 flst_write_addr(m_node + OFFSET_PREV, addr);
347 }
348
349 /** Get the location of previous frag entry. */
350 fil_addr_t get_prev() const { return (flst_read_addr(m_node + OFFSET_PREV)); }
351
352 /** Set the next frag entry as null. */
353 void set_next_null() { flst_write_addr(m_node + OFFSET_NEXT, fil_addr_null); }
354
355 /** Set the next frag entry. */
356 void set_next_null(const fil_addr_t &addr) {
357 flst_write_addr(m_node + OFFSET_NEXT, addr);
358 }
359
360 /** Get the location of next frag entry. */
361 fil_addr_t get_next() const { return (flst_read_addr(m_node + OFFSET_NEXT)); }
362
363 /** Get the frag page number. */
364 page_no_t get_page_no() const {
365 return (mach_read_from_4(m_node + OFFSET_PAGE_NO));
366 }
367
368 /** Set the frag page number. */
369 void set_page_no(page_no_t page_no) const {
370 mlog_write_ulint(m_node + OFFSET_PAGE_NO, page_no, MLOG_4BYTES);
371 }
372
373 /** Get the frag page number. */
374 ulint get_n_frags() const {
375 return (mach_read_from_2(m_node + OFFSET_N_FRAGS));
376 }
377
378 /** Set the frag page number. */
379 void set_n_frags(ulint frags) const {
380 mlog_write_ulint(m_node + OFFSET_N_FRAGS, frags, MLOG_2BYTES);
381 }
382
383 /** Get the used bytes. */
384 ulint get_used_len() const {
385 return (mach_read_from_2(m_node + OFFSET_USED_LEN));
386 }
387
388 /** Set the used bytes. */
389 void set_used_len(ulint used) const {
390 mlog_write_ulint(m_node + OFFSET_USED_LEN, used, MLOG_2BYTES);
391 }
392
393 /** Get the total cumulative free bytes. */
394 ulint get_total_free_len() const {
395 return (mach_read_from_2(m_node + OFFSET_TOTAL_FREE_LEN));
396 }
397
398 /** Get the biggest free frag bytes. */
399 ulint get_big_free_len() const {
400 return (mach_read_from_2(m_node + OFFSET_BIG_FREE_LEN));
401 }
402
403 /** Set the total free bytes. */
404 void set_total_free_len(ulint n) {
405 mlog_write_ulint(m_node + OFFSET_TOTAL_FREE_LEN, n, MLOG_2BYTES);
406 }
407
408 /** Set the big free frag bytes. */
409 void set_big_free_len(ulint n) {
410 mlog_write_ulint(m_node + OFFSET_BIG_FREE_LEN, n, MLOG_2BYTES);
411 }
412
413 void purge(flst_base_node_t *used_lst, flst_base_node_t *free_lst);
414
415 std::ostream &print(std::ostream &out) const;
416
417 private:
418 flst_node_t *m_node;
419 };
420
421 inline std::ostream &operator<<(std::ostream &out, const z_frag_entry_t &obj) {
422 return (obj.print(out));
423 }
424
425 /** The first page of an zlob. */
426 struct z_first_page_t {
427 /* The length of compressed data stored in this page. */
428 static const ulint OFFSET_DATA_LEN = FIL_PAGE_DATA;
429
430 /* The transaction that created data in the data portion. */
431 static const ulint OFFSET_TRX_ID = FIL_PAGE_DATA + 4;
432
433 /** The next index page. */
434 static const ulint OFFSET_INDEX_PAGE_NO = OFFSET_TRX_ID + 6;
435
436 /** The next frag nodes page. */
437 static const ulint OFFSET_FRAG_NODES_PAGE_NO = OFFSET_INDEX_PAGE_NO + 4;
438
439 /* List of free index entries. */
440 static const ulint OFFSET_FREE_LIST = OFFSET_FRAG_NODES_PAGE_NO + 4;
441
442 /* List of index entries. */
443 static const ulint OFFSET_INDEX_LIST = OFFSET_FREE_LIST + FLST_BASE_NODE_SIZE;
444
445 /* List of free frag entries. */
446 static const ulint OFFSET_FREE_FRAG_LIST =
447 OFFSET_INDEX_LIST + FLST_BASE_NODE_SIZE;
448
449 /* List of frag entries. */
450 static const ulint OFFSET_FRAG_LIST =
451 OFFSET_FREE_FRAG_LIST + FLST_BASE_NODE_SIZE;
452
453 /* Begin of index entries. */
454 static const ulint OFFSET_INDEX_BEGIN =
455 OFFSET_FRAG_LIST + FLST_BASE_NODE_SIZE;
456
457 /** Given the page size, what is the number of index entries the first page
458 can contain. */
459 static ulint get_n_index_entries() {
460 const ulint page_size = UNIV_PAGE_SIZE;
461 switch (page_size) {
462 case KB16:
463 /* For a page size of 16KB, there are 100 index entries in the first
464 page of the zlob. */
465 return (100);
466 break;
467 default:
468 ut_error;
469 }
470 }
471
472 /** Given the page size, what is the number of frag entries the first page
473 can contain. */
474 static ulint get_n_frag_entries() {
475 const ulint page_size = UNIV_PAGE_SIZE;
476 switch (page_size) {
477 case KB16:
478 /* For a page size of 16KB, there are 200 frag entries in the first
479 page of the zlob. */
480 return (200);
481 break;
482 default:
483 ut_error;
484 }
485 }
486
487 static ulint size_of_index_entries() {
488 return (z_index_entry_t::SIZE * get_n_index_entries());
489 }
490
491 static ulint size_of_frag_entries() {
492 return (z_frag_entry_t::SIZE * get_n_frag_entries());
493 }
494
495 static ulint begin_frag_entries() {
496 return (OFFSET_INDEX_BEGIN + size_of_index_entries());
497 }
498
499 static ulint begin_data() {
500 return (begin_frag_entries() + size_of_frag_entries());
501 }
502
503 bool empty() const {
504 flst_base_node_t *flst = index_list();
505 return (flst_get_len(flst) == 0);
506 }
507
508 byte *begin_data_ptr() const { return (frame() + begin_data()); }
509
510 /** Amount of zlob data that can be stored in first page (in bytes). */
511 static ulint payload() {
512 return (UNIV_PAGE_SIZE - begin_data() - FIL_PAGE_DATA_END);
513 }
514
515 z_first_page_t() : m_block(nullptr) {}
516 z_first_page_t(buf_block_t *block) : m_block(block) {}
517
518 buf_block_t *alloc() {
519 m_block = btr_page_alloc();
520 init();
521 return (m_block);
522 }
523
524 buf_block_t *load(page_no_t page_no) {
525 m_block = buf_page_get(page_no);
526 return (m_block);
527 }
528
529 void dealloc() {
530 ut_ad(empty());
531 btr_page_free(m_block);
532 m_block = nullptr;
533 }
534
535 void init() {
536 set_page_type();
537 set_data_len(0);
538 set_trx_id(0);
539 flst_base_node_t *flst = free_list();
540 flst_init(flst);
541 flst_base_node_t *ilst = index_list();
542 flst_init(ilst);
543 flst_base_node_t *free_frag_lst = free_frag_list();
544 flst_init(free_frag_lst);
545 flst_base_node_t *frag_lst = frag_list();
546 flst_init(frag_lst);
547 init_index_entries();
548 init_frag_entries();
549 }
550
551 /** Get the amount of zlob data stored in this page. */
552 ulint get_data_len() const {
553 return (mach_read_from_4(frame() + OFFSET_DATA_LEN));
554 }
555
556 /** Get the page number. */
557 ulint get_page_no() const {
558 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
559 }
560
561 fil_addr_t get_self_addr() const {
562 page_no_t page_no = get_page_no();
563 ulint offset = begin_data();
564 return (fil_addr_t(page_no, offset));
565 }
566
567 /** All the index pages are singly linked with each other, and the first
568 page contains the link to one index page.
569 @param[in] page_no the page number of an index page. */
570 void set_index_page_no(page_no_t page_no) {
571 mlog_write_ulint(frame() + OFFSET_INDEX_PAGE_NO, page_no, MLOG_4BYTES);
572 }
573
574 /** All the index pages are singly linked with each other, and the first
575 page contains the link to one index page. Get that index page number.
576 @return the index page number. */
577 page_no_t get_index_page_no() const {
578 return (mach_read_from_4(frame() + OFFSET_INDEX_PAGE_NO));
579 }
580
581 /** All the frag node pages are singly linked with each other, and the
582 first page contains the link to one frag node page.
583 @param[in] page_no the page number of an frag node page. */
584 void set_frag_node_page_no(page_no_t page_no) {
585 mlog_write_ulint(frame() + OFFSET_FRAG_NODES_PAGE_NO, page_no, MLOG_4BYTES);
586 }
587
588 /** Free all the z_frag_page_t pages. All the z_frag_page_t pages are
589 singly linked to each other. The head of the list is maintained in the
590 first page. */
591 void free_all_frag_node_pages();
592
593 /** Free all the index pages. */
594 void free_all_index_pages();
595
596 /** All the frag node pages are singly linked with each other, and the
597 first page contains the link to one frag node page. Get that frag node
598 page number.
599 @return the index page number. */
600 page_no_t get_frag_node_page_no() {
601 return (mach_read_from_4(frame() + OFFSET_FRAG_NODES_PAGE_NO));
602 }
603
604 void set_page_type() {
605 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_FIRST,
606 MLOG_2BYTES);
607 }
608
609 void set_data_len(ulint len) {
610 mlog_write_ulint(frame() + OFFSET_DATA_LEN, len, MLOG_4BYTES);
611 }
612
613 void set_trx_id(trx_id_t tid) {
614 mach_write_to_6(frame() + OFFSET_TRX_ID, tid);
615 }
616
617 flst_base_node_t *free_list() const { return (frame() + OFFSET_FREE_LIST); }
618
619 flst_base_node_t *index_list() const { return (frame() + OFFSET_INDEX_LIST); }
620
621 flst_base_node_t *free_frag_list() const {
622 return (frame() + OFFSET_FREE_FRAG_LIST);
623 }
624
625 flst_base_node_t *frag_list() const { return (frame() + OFFSET_FRAG_LIST); }
626
627 void init_frag_entries() {
628 flst_base_node_t *free_frag_lst = free_frag_list();
629 ulint n = get_n_frag_entries();
630 for (ulint i = 0; i < n; ++i) {
631 flst_node_t *ptr = frame() + begin_frag_entries();
632 ptr += (i * z_frag_entry_t::SIZE);
633 z_frag_entry_t frag_entry(ptr);
634 frag_entry.init();
635 frag_entry.push_back(free_frag_lst);
636 }
637 }
638
639 void init_index_entries() {
640 flst_base_node_t *flst = free_list();
641 ulint n = get_n_index_entries();
642 for (ulint i = 0; i < n; ++i) {
643 flst_node_t *ptr = frame() + OFFSET_INDEX_BEGIN;
644 ptr += (i * z_index_entry_t::SIZE);
645 z_index_entry_t entry(ptr);
646 entry.init();
647 entry.push_back(flst);
648 }
649 }
650
651 /** Find a fragment page, that has atleast len free space. */
652 z_frag_entry_t find_frag_page(ulint len);
653
654 /** Allocate one index entry. If there is no free index entry, allocate
655 an index page (a page full of z_index_entry_t objects) and service the
656 request.
657 @return the allocated index entry. */
658 z_index_entry_t alloc_index_entry();
659
660 /** Allocate one frag page entry. If there is no free frag entry, allocate
661 an frag node page (a page full of z_frag_entry_t objects) and service the
662 request.
663 @return the allocated frag entry. */
664 z_frag_entry_t alloc_frag_entry();
665
666 /** Print the index entries. */
667 std::ostream &print_index_entries(std::ostream &out) const;
668
669 /** Print the index entries. */
670 std::ostream &print_frag_entries(std::ostream &out) const;
671
672 /** Print the page. */
673 std::ostream &print(std::ostream &out) const;
674
675 byte *frame() const { return (buf_block_get_frame(m_block)); }
676
677 private:
678 buf_block_t *m_block;
679 }; // struct z_first_page_t
680
681 inline std::ostream &operator<<(std::ostream &out, const z_first_page_t &obj) {
682 return (obj.print(out));
683 }
684
685 /** An index page containing an array of z_index_entry_t objects. */
686 struct z_index_page_t {
687 /** Set the correct page type. */
688 void set_page_type() {
689 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_INDEX,
690 MLOG_2BYTES);
691 }
692
693 /** Set the next page number. */
694 void set_next_page_no(page_no_t page_no) {
695 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES);
696 }
697
698 /** Get the page number. */
699 ulint get_page_no() const {
700 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
701 }
702
703 /** Get the next page number. */
704 ulint get_next_page_no() const {
705 return (mach_read_from_4(frame() + FIL_PAGE_NEXT));
706 }
707
708 buf_block_t *alloc(z_first_page_t &first) {
709 m_block = btr_page_alloc();
710 set_page_type();
711 flst_base_node_t *free_lst = first.free_list();
712 init(free_lst);
713
714 /* Link the allocated index page to the first page. */
715 page_no_t page_no = first.get_index_page_no();
716 set_next_page_no(page_no);
717 first.set_index_page_no(get_page_no());
718 return (m_block);
719 }
720
721 buf_block_t *load(page_no_t page_no) {
722 m_block = buf_page_get(page_no);
723 return (m_block);
724 }
725
726 void dealloc() {
727 btr_page_free(m_block);
728 m_block = nullptr;
729 }
730
731 void init(flst_base_node_t *free_lst) {
732 ulint n = get_n_index_entries();
733 for (ulint i = 0; i < n; ++i) {
734 byte *ptr = frame() + FIL_PAGE_DATA;
735 ptr += (i * z_index_entry_t::SIZE);
736 z_index_entry_t entry(ptr);
737 entry.init();
738 entry.push_back(free_lst);
739 }
740 }
741
742 ulint payload() const {
743 return (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END - FIL_PAGE_DATA);
744 }
745
746 ulint get_n_index_entries() const {
747 return (payload() / z_index_entry_t::SIZE);
748 }
749
750 byte *frame() const { return (buf_block_get_frame(m_block)); }
751
752 buf_block_t *m_block;
753 };
754
755 /** The data page holding the zlob. */
756 struct z_data_page_t {
757 /* The length of compressed data stored in this page. */
758 static const ulint OFFSET_DATA_LEN = FIL_PAGE_DATA;
759
760 /* The transaction that created this page. */
761 static const ulint OFFSET_TRX_ID = OFFSET_DATA_LEN + 4;
762
763 /* The data stored in this page begins at this offset. */
764 static const ulint OFFSET_DATA_BEGIN = OFFSET_TRX_ID + 6;
765
766 static ulint payload() {
767 return (UNIV_PAGE_SIZE - OFFSET_DATA_BEGIN - FIL_PAGE_DATA_END);
768 }
769
770 z_data_page_t() : m_block(nullptr) {}
771 z_data_page_t(buf_block_t *block) : m_block(block) {}
772
773 buf_block_t *alloc() {
774 m_block = btr_page_alloc();
775 init();
776 return (m_block);
777 }
778
779 /** Set the correct page type. */
780 void set_page_type() {
781 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_DATA,
782 MLOG_2BYTES);
783 }
784
785 /** Set the next page. */
786 void set_next_page(page_no_t page_no) {
787 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES);
788 }
789
790 void init() {
791 set_page_type();
792 set_next_page(FIL_NULL);
793 set_data_len(0);
794 set_trx_id(0);
795 }
796
797 byte *begin_data_ptr() const { return (frame() + OFFSET_DATA_BEGIN); }
798
799 void set_data_len(ulint len) {
800 mlog_write_ulint(frame() + OFFSET_DATA_LEN, len, MLOG_4BYTES);
801 }
802
803 ulint get_data_len() const {
804 return (mach_read_from_4(frame() + OFFSET_DATA_LEN));
805 }
806
807 void set_trx_id(trx_id_t tid) {
808 mach_write_to_6(frame() + OFFSET_TRX_ID, tid);
809 }
810
811 /** Get the page number. */
812 page_no_t get_page_no() const {
813 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
814 }
815
816 fil_addr_t get_self_addr() const {
817 page_no_t page_no = get_page_no();
818 return (fil_addr_t(page_no, OFFSET_DATA_BEGIN));
819 }
820
821 byte *frame() const { return (buf_block_get_frame(m_block)); }
822
823 buf_block_t *m_block;
824 };
825
826 /** A frag nodes page containing an array of z_frag_entry_t objects. */
827 struct z_frag_node_page_t {
828 /** Set the correct page type. */
829 void set_page_type() {
830 mlog_write_ulint(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY,
831 MLOG_2BYTES);
832 }
833
834 /** Set the next page number. */
835 void set_next_page_no(page_no_t page_no) {
836 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES);
837 }
838
839 /** Get the page number. */
840 ulint get_page_no() const {
841 return (mach_read_from_4(frame() + FIL_PAGE_OFFSET));
842 }
843
844 /** Get the next page number. */
845 ulint get_next_page_no() const {
846 return (mach_read_from_4(frame() + FIL_PAGE_NEXT));
847 }
848
849 buf_block_t *alloc(z_first_page_t &first) {
850 m_block = btr_page_alloc();
851 set_page_type();
852 flst_base_node_t *free_lst = first.free_frag_list();
853 init(free_lst);
854
855 /* Link the allocated index page to the first page. */
856 page_no_t page_no = first.get_frag_node_page_no();
857 set_next_page_no(page_no);
858 first.set_frag_node_page_no(get_page_no());
859 return (m_block);
860 }
861
862 void dealloc() {
863 btr_page_free(m_block);
864 m_block = nullptr;
865 }
866
867 buf_block_t *load(page_no_t page_no) {
868 m_block = buf_page_get(page_no);
869 return (m_block);
870 }
871
872 void init(flst_base_node_t *free_lst) {
873 ulint n = get_n_frag_entries();
874 for (ulint i = 0; i < n; ++i) {
875 byte *ptr = frame() + FIL_PAGE_DATA;
876 ptr += (i * z_frag_entry_t::SIZE);
877 z_frag_entry_t entry(ptr);
878 entry.init();
879 entry.push_back(free_lst);
880 }
881 }
882
883 ulint payload() const {
884 return (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END - FIL_PAGE_DATA);
885 }
886
887 ulint get_n_frag_entries() const {
888 return (payload() / z_frag_entry_t::SIZE);
889 }
890
891 byte *frame() const { return (buf_block_get_frame(m_block)); }
892
893 buf_block_t *m_block;
894 }; // struct z_frag_node_page_t
895
896 /** Insert a large object (LOB) into the system.
897 @param[in,out] ref the LOB reference.
898 @param[in] blob the large object.
899 @param[in] len the length of the large object.*/
900 dberr_t z_insert(trx_id_t trxid, lob::ref_t ref, byte *blob, ulint len);
901
902 /** Print information about the given compressed lob. */
903 dberr_t z_print_info(lob::ref_t ref, std::ostream &out);
904
905 /** Fetch a compressed large object (ZLOB) from the system.
906 @param[in] trxid the transaction that is doing the read.
907 @param[in] ref the LOB reference identifying the LOB.
908 @param[in] offset read the LOB from the given offset.
909 @param[in] len the length of LOB data that needs to be fetched.
910 @param[out] buf the output buffer (owned by caller) of minimum len bytes.
911 @return the amount of data (in bytes) that was actually read. */
912 ulint z_read(trx_id_t trxid, lob::ref_t ref, ulint offset, ulint len,
913 byte *buf);
914
915 /** Replace a large object (LOB) with the given new data.
916 @param[in] trxid the transaction that is doing the read.
917 @param[in] ref the LOB reference identifying the LOB.
918 @param[in] offset replace the LOB from the given offset.
919 @param[in] len the length of LOB data that needs to be replaced.
920 @param[in] buf the buffer (owned by caller) with new data (len bytes).
921 @return the actual number of bytes replaced. */
922 ulint z_replace(trx_id_t trxid, lob::ref_t ref, ulint offset, ulint len,
923 byte *buf);
924
925 /** Insert data into the middle of an LOB */
926 ulint z_insert_middle(trx_id_t trxid, lob::ref_t ref, ulint offset, byte *data,
927 ulint len);
928
929 /** Delete a portion of the given large object (LOB)
930 @param[in] trxid the transaction that is doing the read.
931 @param[in] ref the LOB reference identifying the LOB.
932 @param[in] offset remove the LOB from the given offset.
933 @param[in] len the length of LOB data that needs to be removed.
934 @return actual number of bytes removed. */
935 ulint z_remove_middle(trx_id_t trxid, lob::ref_t ref, ulint offset, ulint len);
936
937 void z_purge(trx_id_t trxid, lob::ref_t ref);
938
939 struct frag_node_t {
940 static const ulint OFFSET_LEN = plist_node_t::SIZE;
941 static const ulint OFFSET_FRAG_ID = OFFSET_LEN + 2;
942 static const ulint OFFSET_DATA = OFFSET_FRAG_ID + 2;
943
944 frag_node_t(flst_node_t *node) : m_node(page_align(node), node) {}
945
946 frag_node_t() : m_node() {}
947 frag_node_t(const plist_node_t &node) : m_node(node) {}
948
949 frag_node_t(byte *frame, byte *ptr) : m_node(frame, ptr) {}
950
951 frag_node_t(byte *frame, byte *ptr, ulint len) : m_node(frame, ptr) {
952 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len, MLOG_2BYTES);
953 }
954
955 byte *frag_begin() const { return (m_node.ptr() + OFFSET_DATA); }
956 byte *data_begin() const { return (m_node.ptr() + OFFSET_DATA); }
957
958 void set_total_len(ulint len) {
959 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len, MLOG_2BYTES);
960 }
961
962 /** Increment the total length of this fragment by 2 bytes. */
963 void incr_length_by_2() {
964 ulint len = mach_read_from_2(m_node.ptr() + OFFSET_LEN);
965 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len + 2, MLOG_2BYTES);
966 }
967
968 /** Decrement the total length of this fragment by 2 bytes. */
969 void decr_length_by_2() {
970 ulint len = mach_read_from_2(m_node.ptr() + OFFSET_LEN);
971 mlog_write_ulint(m_node.ptr() + OFFSET_LEN, len - 2, MLOG_2BYTES);
972 }
973
974 bool is_before(const frag_node_t &frag) const {
975 return (m_node.is_before(frag.m_node));
976 }
977
978 void set_frag_id_null() {
979 mlog_write_ulint(m_node.ptr() + OFFSET_FRAG_ID, FRAG_ID_NULL, MLOG_2BYTES);
980 }
981
982 void set_frag_id(ulint id) {
983 mlog_write_ulint(m_node.ptr() + OFFSET_FRAG_ID, id, MLOG_2BYTES);
984 }
985
986 ulint get_frag_id() const {
987 return (mach_read_from_2(m_node.ptr() + OFFSET_FRAG_ID));
988 }
989
990 /** Get the space available in this fragment for storing data. */
991 ulint payload() const { return (get_total_len() - OFFSET_DATA); }
992
993 /** Get the total length of this fragment, including its metadata. */
994 ulint get_total_len() const {
995 return (mach_read_from_2(m_node.ptr() + OFFSET_LEN));
996 }
997
998 /** Get the offset of the current fragment within page.
999 @return the offset of the current fragment within. */
1000 paddr_t addr() const { return m_node.addr(); }
1001
1002 /** Gets the pointer to the beginning of the current fragment. Note that
1003 the beginning of the fragment contains meta data.
1004 @return pointer to the beginning of the current fragment. */
1005 byte *ptr() const {
1006 ut_ad(!m_node.is_null());
1007 return (m_node.ptr());
1008 }
1009
1010 /** Gets the pointer just after the current fragment. The pointer returned
1011 does not belong to this fragment. This is used to check adjacency.
1012 @return pointer to the end of the current fragment. */
1013 byte *end_ptr() const {
1014 ut_ad(!m_node.is_null());
1015 return (ptr() + get_total_len());
1016 }
1017
1018 byte *frame() const { return (m_node.m_frame); }
1019
1020 std::ostream &print(std::ostream &out) const {
1021 if (!m_node.is_null()) {
1022 ulint len = get_total_len();
1023 out << "[frag_node_t: " << m_node << ", len=" << len << "/" << payload()
1024 << ", frag_id=" << get_frag_id() << "]";
1025 } else {
1026 out << "[frag_node_t: null, len=0]";
1027 }
1028 return (out);
1029 }
1030
1031 frag_node_t get_next_frag() {
1032 ut_ad(!is_null());
1033 plist_node_t next = m_node.get_next_node();
1034 return (frag_node_t(next));
1035 }
1036
1037 frag_node_t get_next_node() { return (get_next_frag()); }
1038 frag_node_t get_prev_node() { return (get_prev_frag()); }
1039
1040 frag_node_t get_prev_frag() {
1041 ut_ad(!is_null());
1042 plist_node_t prev = m_node.get_prev_node();
1043 return (frag_node_t(prev));
1044 }
1045
1046 bool merge(frag_node_t &next) {
1047 byte *p1 = ptr();
1048 ulint len1 = get_total_len();
1049 byte *p2 = next.ptr();
1050 ulint len2 = next.get_total_len();
1051
1052 if (p2 == (p1 + len1)) {
1053 set_total_len(len1 + len2);
1054 return (true);
1055 }
1056 return (false);
1057 }
1058
1059 bool is_null() const { return (m_node.is_null()); }
1060
1061 plist_node_t m_node;
1062 };
1063
1064 inline std::ostream &operator<<(std::ostream &out, const frag_node_t &obj) {
1065 return (obj.print(out));
1066 }
1067
1068 /** The fragment page. This page will contain fragments from different
1069 zlib streams. */
1070 struct z_frag_page_t {
1071 /** The location of z_frag_entry_t for this page. */
1072 static const ulint OFFSET_FRAG_ENTRY = FIL_PAGE_DATA;
1073
1074 /** The offset within page where the free space list begins. */
1075 static const ulint OFFSET_FREE_LIST = OFFSET_FRAG_ENTRY + FIL_ADDR_SIZE;
1076
1077 /** The offset within page where the fragment list begins. */
1078 static const ulint OFFSET_FRAGS_LIST =
1079 OFFSET_FREE_LIST + plist_base_node_t::SIZE;
1080
1081 /** The offset within page where the fragments can occupy . */
1082 static const ulint OFFSET_FRAGS_BEGIN =
1083 OFFSET_FRAGS_LIST + plist_base_node_t::SIZE;
1084
1085 /** Offset of number of page directory entries (from end) */
1086 static const ulint OFFSET_PAGE_DIR_ENTRY_COUNT = FIL_PAGE_DATA_END + 2;
1087
1088 /** Offset of first page directory entry (from end) */
1089 static const ulint OFFSET_PAGE_DIR_ENTRY_FIRST =
1090 OFFSET_PAGE_DIR_ENTRY_COUNT + 2;
1091
1092 static const ulint SIZE_OF_PAGE_DIR_ENTRY = 2; /* bytes */
1093
1094 z_frag_page_t() : m_block(nullptr) {}
1095 z_frag_page_t(buf_block_t *block) : m_block(block) {}
1096
1097 z_frag_entry_t get_frag_entry();
1098
1099 void update_frag_entry() {
1100 z_frag_entry_t entry = get_frag_entry();
1101 entry.update(*this);
1102 }
1103
1104 void set_frag_entry(const fil_addr_t &addr) const {
1105 ut_ad(addr.boffset < UNIV_PAGE_SIZE);
1106 return (flst_write_addr(frame() + OFFSET_FRAG_ENTRY, addr));
1107 }
1108
1109 void set_frag_entry_null() const {
1110 return (flst_write_addr(frame() + OFFSET_FRAG_ENTRY, fil_addr_null));
1111 }
1112
1113 ulint get_n_dir_entries() const {
1114 byte *ptr = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_COUNT;
1115 return (mach_read_from_2(ptr));
1116 }
1117
1118 void set_n_dir_entries(ulint n) const {
1119 byte *ptr = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_COUNT;
1120 mlog_write_ulint(ptr, n, MLOG_2BYTES);
1121 }
1122
1123 bool is_border_frag(const frag_node_t &node) const {
1124 return (slots_end_ptr() == node.end_ptr());
1125 }
1126
1127 byte *slots_end_ptr() const {
1128 ulint n = get_n_dir_entries();
1129 byte *first = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_COUNT;
1130 byte *ptr = first - (n * SIZE_OF_PAGE_DIR_ENTRY);
1131 return (ptr);
1132 }
1133
1134 paddr_t frag_id_to_addr(ulint frag_id) const {
1135 byte *first = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_FIRST;
1136 byte *ptr = first - (frag_id * SIZE_OF_PAGE_DIR_ENTRY);
1137 return (mach_read_from_2(ptr));
1138 }
1139
1140 ulint get_nth_dir_entry(ulint frag_id) {
1141 byte *first = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_FIRST;
1142 byte *ptr = first - (frag_id * SIZE_OF_PAGE_DIR_ENTRY);
1143 return (mach_read_from_2(ptr));
1144 }
1145
1146 void set_nth_dir_entry(ulint frag_id, paddr_t val) {
1147 byte *first = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_FIRST;
1148 byte *ptr = first - (frag_id * SIZE_OF_PAGE_DIR_ENTRY);
1149 mlog_write_ulint(ptr, val, MLOG_2BYTES);
1150 }
1151
1152 ulint init_last_dir_entry() {
1153 ulint n = get_n_dir_entries();
1154 set_nth_dir_entry(n - 1, 0);
1155 return (n - 1);
1156 }
1157
1158 void incr_n_dir_entries() const {
1159 byte *ptr = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_COUNT;
1160 ulint n = mach_read_from_2(ptr);
1161 ut_a(n < FRAG_ID_NULL);
1162 mlog_write_ulint(ptr, n + 1, MLOG_2BYTES);
1163 }
1164
1165 void decr_n_dir_entries() const {
1166 byte *ptr = frame() + UNIV_PAGE_SIZE - OFFSET_PAGE_DIR_ENTRY_COUNT;
1167 ulint n = mach_read_from_2(ptr);
1168 ut_a(n > 0);
1169 mlog_write_ulint(ptr, n - 1, MLOG_2BYTES);
1170 }
1171
1172 ulint space_used_by_dir() const {
1173 ulint n = get_n_dir_entries();
1174 return (n * SIZE_OF_PAGE_DIR_ENTRY);
1175 }
1176
1177 ulint locate_free_slot() {
1178 ulint n = get_n_dir_entries();
1179 for (ulint frag_id = 0; frag_id < n; frag_id++) {
1180 ulint paddr = get_nth_dir_entry(frag_id);
1181 if (paddr == 0) {
1182 return (frag_id);
1183 }
1184 }
1185
1186 return (FRAG_ID_NULL);
1187 }
1188
1189 /** Allocate a fragment id.
1190 @return On success, return fragment id.
1191 @return On failure, return FRAG_ID_NULL. */
1192 ulint alloc_frag_id() {
1193 ulint id = locate_free_slot();
1194 if (id == FRAG_ID_NULL) {
1195 return (alloc_dir_entry());
1196 }
1197 return (id);
1198 }
1199
1200 std::ostream &print_frag_id(std::ostream &out) {
1201 ulint n = get_n_dir_entries();
1202 out << "FRAG IDS: " << std::endl;
1203 for (ulint frag_id = 0; frag_id < n; frag_id++) {
1204 out << "id=" << frag_id << ", addr=" << frag_id_to_addr(frag_id)
1205 << std::endl;
1206 }
1207 return (out);
1208 }
1209
1210 /** Grow the frag directory by one entry.
1211 @return the fragment identifier that was newly added. */
1212 ulint alloc_dir_entry() {
1213 plist_base_node_t free_lst = free_list();
1214 plist_node_t last = free_lst.get_last_node();
1215 frag_node_t frag(last);
1216 ulint len = frag.payload();
1217
1218 /* The last free fragment must be adjacent to the directory.
1219 Then only it can give space to one slot. */
1220 if (frag.end_ptr() != slots_end_ptr()) {
1221 return (FRAG_ID_NULL);
1222 }
1223
1224 if (len <= SIZE_OF_PAGE_DIR_ENTRY) {
1225 return (FRAG_ID_NULL);
1226 }
1227
1228 incr_n_dir_entries();
1229 frag.decr_length_by_2();
1230 return (init_last_dir_entry());
1231 }
1232
1233 /** Set the next page. */
1234 void set_page_next(page_no_t page_no) {
1235 mlog_write_ulint(frame() + FIL_PAGE_NEXT, page_no, MLOG_4BYTES);
1236 }
1237
1238 /** Allocate the fragment page.
1239 @return the allocated buffer block. */
1240 buf_block_t *alloc();
1241
1242 /** Free the fragment page along with its entry. */
1243 void dealloc(z_first_page_t &first);
1244
1245 buf_block_t *load(page_no_t page_no) {
1246 m_block = buf_page_get(page_no);
1247 return (m_block);
1248 }
1249
1250 void merge_free_frags() {
1251 plist_base_node_t free_lst = free_list();
1252 frag_node_t frag(free_lst.get_first_node());
1253 frag_node_t next = frag.get_next_frag();
1254
1255 while (!next.is_null() && frag.merge(next)) {
1256 free_lst.remove(next.m_node);
1257 next = frag.get_next_frag();
1258 }
1259 }
1260
1261 void merge_free_frags(frag_node_t frag) {
1262 ut_ad(!frag.is_null());
1263 plist_base_node_t free_lst = free_list();
1264 frag_node_t next = frag.get_next_frag();
1265
1266 while (!next.is_null() && frag.merge(next)) {
1267 free_lst.remove(next.m_node);
1268 next = frag.get_next_frag();
1269 }
1270 }
1271
1272 void insert_into_free_list(frag_node_t &frag) {
1273 ut_ad(frag.get_frag_id() == FRAG_ID_NULL);
1274
1275 plist_base_node_t free_lst = free_list();
1276
1277 plist_node_t node = free_lst.get_first_node();
1278 plist_node_t prev_node;
1279
1280 while (!node.is_null()) {
1281 if (frag.addr() < node.addr()) {
1282 break;
1283 }
1284 prev_node = node;
1285 node = node.get_next_node();
1286 }
1287
1288 free_lst.insert_before(node, frag.m_node);
1289 if (prev_node.is_null()) {
1290 merge_free_frags();
1291 } else {
1292 merge_free_frags(prev_node);
1293 }
1294 }
1295
1296 void insert_into_frag_list(frag_node_t &frag) {
1297 plist_base_node_t frag_lst = frag_list();
1298 plist_node_t node = frag_lst.get_first_node();
1299
1300 while (!node.is_null()) {
1301 ut_ad(frag.addr() != node.addr());
1302 if (frag.addr() < node.addr()) {
1303 break;
1304 }
1305 node = node.get_next_node();
1306 }
1307
1308 frag_lst.insert_before(node, frag.m_node);
1309 }
1310
1311 /** Split one free fragment into two.
1312 @param[in] free_frag the free fragment that will be split.
1313 @param[in] size the payload size in bytes. */
1314 void split_free_frag(frag_node_t &free_frag, ulint size) {
1315 ut_ad(size < free_frag.payload());
1316 const ulint old_total_len = free_frag.get_total_len();
1317 plist_base_node_t free_lst = free_list();
1318
1319 byte *p1 = free_frag.ptr();
1320 byte *p2 = p1 + frag_node_t::OFFSET_DATA + size;
1321 ulint remain = free_frag.get_total_len() - frag_node_t::OFFSET_DATA - size;
1322 ut_a(remain >= frag_node_t::OFFSET_DATA);
1323 free_frag.set_total_len(frag_node_t::OFFSET_DATA + size);
1324 frag_node_t frag2(frame(), p2, remain);
1325 frag2.set_total_len(remain);
1326 frag2.set_frag_id_null();
1327 free_lst.insert_after(free_frag.m_node, frag2.m_node);
1328
1329 ut_a(free_frag.get_total_len() + frag2.get_total_len() == old_total_len);
1330 }
1331
1332 frag_node_t get_frag_node(frag_id_t id) const {
1333 paddr_t off = frag_id_to_addr(id);
1334 byte *f = frame();
1335 return (frag_node_t(f, f + off));
1336 }
1337
1338 void dealloc_fragment(ulint frag_id) {
1339 paddr_t off = frag_id_to_addr(frag_id);
1340 byte *f = frame();
1341 frag_node_t frag(f, f + off);
1342 dealloc_fragment(frag);
1343 dealloc_frag_id(frag_id);
1344
1345 /* Update the index entry. */
1346 update_frag_entry();
1347 }
1348
1349 /** Allocate a fragment with the given payload.
1350 @return the frag_id of the allocated fragment. */
1351 frag_id_t alloc_fragment(ulint size) {
1352 ulint frag_id = alloc_frag_id();
1353
1354 if (frag_id == FRAG_ID_NULL) {
1355 return (FRAG_ID_NULL);
1356 }
1357
1358 plist_base_node_t free_lst = free_list();
1359
1360 for (plist_node_t cur = free_lst.get_first_node(); !cur.is_null();
1361 cur = cur.get_next_node()) {
1362 frag_node_t frag(cur);
1363
1364 if (frag.payload() == size) {
1365 // this is the requested fragment.
1366 free_lst.remove(cur);
1367 insert_into_frag_list(frag);
1368
1369 frag.set_frag_id(frag_id);
1370 set_nth_dir_entry(frag_id, frag.addr());
1371 return (frag_id);
1372 } else if (frag.payload() >= (size + frag_node_t::OFFSET_DATA + 2)) {
1373 // break the current fragment into two.
1374 split_free_frag(frag, size);
1375 free_lst.remove(frag.m_node);
1376 insert_into_frag_list(frag);
1377 frag.set_frag_id(frag_id);
1378 set_nth_dir_entry(frag_id, frag.addr());
1379 return (frag_id);
1380 }
1381 }
1382 return (FRAG_ID_NULL);
1383 }
1384
1385 plist_base_node_t free_list() const {
1386 byte *f = frame();
1387 return (plist_base_node_t(f, f + OFFSET_FREE_LIST));
1388 }
1389
1390 plist_base_node_t frag_list() const {
1391 byte *f = frame();
1392 return (plist_base_node_t(f, f + OFFSET_FRAGS_LIST));
1393 }
1394
1395 void set_page_type() {
1396 mach_write_to_2(frame() + FIL_PAGE_TYPE, FIL_PAGE_TYPE_ZLOB_FRAG);
1397 }
1398
1399 ulint get_page_type() const {
1400 return (mach_read_from_2(frame() + FIL_PAGE_TYPE));
1401 }
1402
1403 const char *get_page_type_str() const {
1404 ulint type = get_page_type();
1405 ut_a(type == FIL_PAGE_TYPE_ZLOB_FRAG);
1406 return ("FIL_PAGE_TYPE_ZLOB_FRAG");
1407 }
1408
1409 static ulint payload() {
1410 return (UNIV_PAGE_SIZE - OFFSET_FRAGS_BEGIN - OFFSET_PAGE_DIR_ENTRY_COUNT);
1411 }
1412
1413 /** The maximum amount of data that can be stored in a fragment page. */
1414 static ulint max_payload() {
1415 return (UNIV_PAGE_SIZE - OFFSET_FRAGS_BEGIN - OFFSET_PAGE_DIR_ENTRY_COUNT -
1416 SIZE_OF_PAGE_DIR_ENTRY - frag_node_t::OFFSET_DATA);
1417 }
1418
1419 frag_node_t find_frag(ulint size) {
1420 plist_base_node_t free_lst = free_list();
1421 for (plist_node_t cur = free_lst.get_first_node(); !cur.is_null();
1422 cur = cur.get_next_node()) {
1423 frag_node_t frag(cur);
1424 if (frag.payload() >= size) {
1425 return (frag);
1426 }
1427 }
1428 return (frag_node_t());
1429 }
1430
1431 /** Get the frag page number. */
1432 page_no_t get_page_no() const { return (m_block->get_page_no()); }
1433
1434 byte *frame() const { return (buf_block_get_frame(m_block)); }
1435
1436 std::ostream &print(std::ostream &out) const {
1437 print_free_list(out);
1438 print_frag_list(out);
1439 print_frags_in_order(out);
1440 print_page_dir(out);
1441 return (out);
1442 }
1443
1444 /** Get the total amount of stored data in this page. */
1445 ulint get_total_stored_data() const;
1446
1447 /** Get the total cumulative free space in this page. */
1448 ulint get_total_free_len() const;
1449
1450 /** Get the big free space in this page. */
1451 ulint get_big_free_len() const;
1452
1453 /** Get the number of fragments in this frag page. */
1454 ulint get_n_frags() const {
1455 plist_base_node_t frag_lst = frag_list();
1456 return (frag_lst.get_len());
1457 }
1458
1459 std::ostream &print_frags_in_order(std::ostream &out) const;
1460
1461 std::ostream &print_free_list(std::ostream &out) const {
1462 if (m_block == nullptr) {
1463 return (out);
1464 }
1465 plist_base_node_t free_lst = free_list();
1466 out << "[Free List: " << free_lst << "]" << std::endl;
1467 for (plist_node_t cur = free_lst.get_first_node(); !cur.is_null();
1468 cur = cur.get_next_node()) {
1469 frag_node_t frag(cur);
1470 out << frag << std::endl;
1471 }
1472 return (out);
1473 }
1474
1475 std::ostream &print_frag_list(std::ostream &out) const {
1476 if (m_block == nullptr) {
1477 return (out);
1478 }
1479 plist_base_node_t frag_lst = frag_list();
1480 out << "[Frag List: " << frag_lst << "]" << std::endl;
1481 for (plist_node_t cur = frag_lst.get_first_node(); !cur.is_null();
1482 cur = cur.get_next_node()) {
1483 frag_node_t frag(cur);
1484 out << frag << std::endl;
1485 }
1486 return (out);
1487 }
1488
1489 std::ostream &print_page_dir(std::ostream &out) const {
1490 if (m_block == nullptr) {
1491 return (out);
1492 }
1493
1494 ulint n = get_n_dir_entries();
1495
1496 for (ulint frag_id = 0; frag_id < n; ++frag_id) {
1497 paddr_t off = frag_id_to_addr(frag_id);
1498 out << "[frag_id=" << frag_id << ", addr=" << off << "]" << std::endl;
1499 }
1500
1501 return (out);
1502 }
1503
1504 private:
1505 fil_addr_t get_frag_entry_addr() const {
1506 return (flst_read_addr(frame() + OFFSET_FRAG_ENTRY));
1507 }
1508
1509 void dealloc_fragment(frag_node_t frag) {
1510 plist_base_node_t frag_lst = frag_list();
1511 frag_lst.remove(frag.m_node);
1512 frag.set_frag_id_null();
1513 insert_into_free_list(frag);
1514 }
1515
1516 /** Deallocate all the free slots from the end of the page directory. */
1517 void dealloc_frag_id();
1518
1519 /** Deallocate the given fragment id.
1520 @param[in] frag_id The fragment that needs to be deallocated. */
1521 void dealloc_frag_id(ulint frag_id) {
1522 set_nth_dir_entry(frag_id, 0);
1523 dealloc_frag_id();
1524 }
1525
1526 buf_block_t *m_block;
1527 };
1528
1529 } // namespace zlob
1530
1531 #endif // _zlob0int_h_