Debian Codegraph @

storage/innobase/include/lob0lob.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2015, 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 lob0lob_h
28 #define lob0lob_h
29
30 #include <my_dbug.h>
31 #include "btr0pcur.h"
32 #include "dict0mem.h"
33 #include "page0page.h"
34 #include "row0log.h"
35 #include "univ.i"
36
37 /* Uncomment the following line to generate debug data, useful to analyze
38 LOB issues. */
39 /* #define LOB_DEBUG */
40 /* #define ZLOB_DEBUG */
41
42 struct upd_t;
43 typedef std::map<page_no_t, buf_block_t *> BlockCache;
44
45 /**
46 @file
47 @brief Implements the large objects (LOB) module.
48
49 InnoDB supports large objects (LOB). Previously, the LOB was called as
50 externally stored fields. A large object contains a singly linked list of
51 database pages, aka LOB pages. A reference to the first LOB page is stored
52 along with the clustered index record. This reference is called the LOB
53 reference (lob::ref_t). A single clustered index record can have many LOB
54 references. Secondary indexes cannot have LOB references.
55
56 There are two types of LOB - compressed and uncompressed.
57
58 The main operations implemented for LOB are - INSERT, DELETE and FETCH. To
59 carry out these main operations the following classes are provided.
60
61 Inserter - for inserting uncompressed LOB data.
62 zInserter - for inserting compressed LOB data.
63 BaseInserter - a base class containing common state and functions useful for
64 both Inserter and zInserter. Inserter and zInserter derives
65 from this base class.
66 Reader - for reading uncompressed LOB data.
67 zReader - for reading compressed LOB data.
68 Deleter - for deleting both compressed and uncompressed LOB data.
69
70 For each main operation, the context information is identified separately.
71 They are as follows:
72
73 InsertContext - context information for doing insert of LOB. `
74 DeleteContext - context information for doing delete of LOB. `
75 ReadContext - context information for doing fetch of LOB. `
76
77 */
78
79 /** Provides the large objects (LOB) module. Previously, the LOB was called as
80 externally stored fields. */
81 namespace lob {
82
83 /** The maximum size possible for an LOB */
84 const ulint MAX_SIZE = UINT32_MAX;
85
86 /** The compressed LOB is stored as a collection of zlib streams. The
87 uncompressed LOB is divided into chunks of size Z_CHUNK_SIZE and each of
88 these chunks are compressed individually and stored as compressed LOB.
89 data. */
90 constexpr uint32_t KB128 = 128 * 1024;
91 constexpr uint32_t Z_CHUNK_SIZE = KB128;
92
93 /** The reference in a field for which data is stored on a different page.
94 The reference is at the end of the 'locally' stored part of the field.
95 'Locally' means storage in the index record.
96 We store locally a long enough prefix of each column so that we can determine
97 the ordering parts of each index record without looking into the externally
98 stored part. */
99 /*-------------------------------------- @{ */
100
101 /** Space identifier where stored. */
102 const ulint BTR_EXTERN_SPACE_ID = 0;
103
104 /** page number where stored */
105 const ulint BTR_EXTERN_PAGE_NO = 4;
106
107 /** offset of BLOB header on that page */
108 const ulint BTR_EXTERN_OFFSET = 8;
109
110 /** Version number of LOB (LOB in new format)*/
111 const ulint BTR_EXTERN_VERSION = BTR_EXTERN_OFFSET;
112
113 /** 8 bytes containing the length of the externally stored part of the LOB.
114 The 2 highest bits are reserved to the flags below. */
115 const ulint BTR_EXTERN_LEN = 12;
116
117 /*-------------------------------------- @} */
118
119 /** The most significant bit of BTR_EXTERN_LEN (i.e., the most
120 significant bit of the byte at smallest address) is set to 1 if this
121 field does not 'own' the externally stored field; only the owner field
122 is allowed to free the field in purge! */
123 const ulint BTR_EXTERN_OWNER_FLAG = 128UL;
124
125 /** If the second most significant bit of BTR_EXTERN_LEN (i.e., the
126 second most significant bit of the byte at smallest address) is 1 then
127 it means that the externally stored field was inherited from an
128 earlier version of the row. In rollback we are not allowed to free an
129 inherited external field. */
130 const ulint BTR_EXTERN_INHERITED_FLAG = 64UL;
131
132 /** If the 3rd most significant bit of BTR_EXTERN_LEN is 1, then it
133 means that the externally stored field is currently being modified.
134 This is mainly used by the READ UNCOMMITTED transaction to avoid returning
135 inconsistent blob data. */
136 const ulint BTR_EXTERN_BEING_MODIFIED_FLAG = 32UL;
137
138 /** The structure of uncompressed LOB page header */
139
140 /** Offset within header of LOB length on this page. */
141 const ulint LOB_HDR_PART_LEN = 0;
142
143 /** Offset within header of next BLOB part page no.
144 FIL_NULL if none */
145 const ulint LOB_HDR_NEXT_PAGE_NO = 4;
146
147 /** Size of an uncompressed LOB page header, in bytes */
148 const ulint LOB_HDR_SIZE = 8;
149
150 /** Start of the data on an LOB page */
151 const uint ZLOB_PAGE_DATA = FIL_PAGE_DATA;
152
153 /** In memory representation of the LOB reference. */
154 struct ref_mem_t {
155 /** Space Identifier of the clustered index. */
156 space_id_t m_space_id;
157
158 /** Page number of first LOB page. */
159 page_no_t m_page_no;
160
161 /** Offset within m_page_no where LOB begins. */
162 ulint m_offset;
163
164 /** Length of LOB */
165 ulint m_length;
166
167 /** Whether the LOB is null. */
168 bool m_null;
169
170 /** Whether the clustered index record owns this LOB. */
171 bool m_owner;
172
173 /** Whether the clustered index record inherited this LOB from
174 another clustered index record. */
175 bool m_inherit;
176
177 /** Whether the LOB is partially updated. */
178 bool m_partial;
179
180 /** Whether the blob is being modified. */
181 bool m_being_modified;
182
183 /** Check if the LOB has already been purged.
184 @return true if LOB has been purged, false otherwise. */
185 bool is_purged() const {
186 return ((m_page_no == FIL_NULL) && (m_length == 0));
187 }
188 };
189
190 extern const byte field_ref_almost_zero[FIELD_REF_SIZE];
191
192 /** The struct 'lob::ref_t' represents an external field reference. The
193 reference in a field for which data is stored on a different page. The
194 reference is at the end of the 'locally' stored part of the field. 'Locally'
195 means storage in the index record. We store locally a long enough prefix of
196 each column so that we can determine the ordering parts of each index record
197 without looking into the externally stored part. */
198 struct ref_t {
199 private:
200 /** If the LOB size is equal to or above this limit (in physical page
201 size terms), then the LOB is big enough to be partially updated. Only
202 in this case LOB index needs to be built. */
203 static const ulint LOB_BIG_THRESHOLD_SIZE = 2;
204
205 public:
206 /** If the total number of bytes modified in an LOB, in an update
207 operation, is less than or equal to this threshold LOB_SMALL_CHANGE_THRESHOLD,
208 then it is considered as a small change. For small changes to LOB,
209 the changes are undo logged like any other update operation. */
210 static const ulint LOB_SMALL_CHANGE_THRESHOLD = 100;
211
212 /** Constructor.
213 @param[in] ptr Pointer to the external field reference. */
214 explicit ref_t(byte *ptr) : m_ref(ptr) {}
215
216 /** For compressed LOB, if the length is less than or equal to Z_CHUNK_SIZE
217 then use the older single z stream format to store the LOB. */
218 bool use_single_z_stream() const { return (length() <= Z_CHUNK_SIZE); }
219
220 /** For compressed LOB, if the length is less than or equal to Z_CHUNK_SIZE
221 then use the older single z stream format to store the LOB. */
222 static bool use_single_z_stream(ulint len) { return (len <= Z_CHUNK_SIZE); }
223
224 /** Check if this LOB is big enough to do partial update.
225 @param[in] page_size the page size
226 @param[in] lob_length the size of BLOB in bytes.
227 @return true if LOB is big enough, false otherwise. */
228 static bool is_big(const page_size_t &page_size [[maybe_unused]],
229 const ulint lob_length [[maybe_unused]]) {
230 /* Disable a performance optimization */
231 return true;
232 }
233
234 /** Check if this LOB is big enough to do partial update.
235 @param[in] page_size the page size
236 @return true if LOB is big enough, false otherwise. */
237 bool is_big(const page_size_t &page_size [[maybe_unused]]) const {
238 /* Disable a performance optimization */
239 return true;
240 }
241
242 /** Parse the LOB reference object and copy data into the given
243 ref_mem_t object.
244 @param[out] obj LOB reference memory object. */
245 void parse(ref_mem_t &obj) const {
246 obj.m_space_id = space_id();
247 obj.m_page_no = page_no();
248 obj.m_offset = offset();
249 obj.m_length = length();
250 obj.m_null = is_null();
251 obj.m_owner = is_owner();
252 obj.m_inherit = is_inherited();
253 obj.m_being_modified = is_being_modified();
254 }
255
256 /** Copy the LOB reference into the given memory location.
257 @param[out] field_ref write LOB reference in this
258 location.*/
259 void copy(byte *field_ref) const { memcpy(field_ref, m_ref, SIZE); }
260
261 /** Check whether the stored external field reference is equal to the
262 given field reference.
263 @param[in] ptr supplied external field reference. */
264 bool is_equal(const byte *ptr) const { return (m_ref == ptr); }
265
266 /** Set the external field reference to the given memory location.
267 @param[in] ptr the new external field reference. */
268 void set_ref(byte *ptr) { m_ref = ptr; }
269
270 /** Check if the field reference is made of zeroes except the being_modified
271 bit.
272 @return true if field reference is made of zeroes, false otherwise. */
273 bool is_null_relaxed() const {
274 return (is_null() || memcmp(field_ref_almost_zero, m_ref, SIZE) == 0);
275 }
276
277 /** Check if the field reference is made of zeroes.
278 @return true if field reference is made of zeroes, false otherwise. */
279 bool is_null() const { return (memcmp(field_ref_zero, m_ref, SIZE) == 0); }
280
281 #ifdef UNIV_DEBUG
282 /** Check if the LOB reference is null (all zeroes) except the "is being
283 modified" bit.
284 @param[in] ref the LOB reference.
285 @return true if the LOB reference is null (all zeros) except the "is being
286 modified" bit, false otherwise. */
287 static bool is_null_relaxed(const byte *ref) {
288 return (is_null(ref) || memcmp(field_ref_almost_zero, ref, SIZE) == 0);
289 }
290
291 /** Check if the LOB reference is null (all zeroes).
292 @param[in] ref the LOB reference.
293 @return true if the LOB reference is null (all zeros), false otherwise. */
294 static bool is_null(const byte *ref) {
295 return (memcmp(field_ref_zero, ref, SIZE) == 0);
296 }
297 #endif /* UNIV_DEBUG */
298
299 /** Set the ownership flag in the blob reference.
300 @param[in] owner Whether to own or disown. If owner, unset
301 the owner flag.
302 @param[in] mtr Mini-transaction or NULL.*/
303 void set_owner(bool owner, mtr_t *mtr) {
304 ulint byte_val = mach_read_from_1(m_ref + BTR_EXTERN_LEN);
305
306 if (owner) {
307 /* owns the blob */
308 byte_val &= ~BTR_EXTERN_OWNER_FLAG;
309 } else {
310 byte_val |= BTR_EXTERN_OWNER_FLAG;
311 }
312
313 mlog_write_ulint(m_ref + BTR_EXTERN_LEN, byte_val, MLOG_1BYTE, mtr);
314 }
315
316 /** Set the being_modified flag in the field reference.
317 @param[in,out] ref The LOB reference
318 @param[in] modifying true, if blob is being modified.
319 @param[in] mtr Mini-transaction context.*/
320 static void set_being_modified(byte *ref, bool modifying, mtr_t *mtr) {
321 ulint byte_val = mach_read_from_1(ref + BTR_EXTERN_LEN);
322
323 if (modifying) {
324 byte_val |= BTR_EXTERN_BEING_MODIFIED_FLAG;
325 } else {
326 byte_val &= ~BTR_EXTERN_BEING_MODIFIED_FLAG;
327 }
328
329 mlog_write_ulint(ref + BTR_EXTERN_LEN, byte_val, MLOG_1BYTE, mtr);
330 }
331
332 /** Set the being_modified flag in the field reference.
333 @param[in] modifying true, if blob is being modified.
334 @param[in] mtr Mini-transaction context.*/
335 void set_being_modified(bool modifying, mtr_t *mtr) {
336 set_being_modified(m_ref, modifying, mtr);
337 }
338
339 /** Check if the current blob is being modified
340 @param[in] field_ref blob field reference
341 @return true if it is being modified, false otherwise. */
342 bool static is_being_modified(const byte *field_ref) {
343 const ulint byte_val = mach_read_from_1(field_ref + BTR_EXTERN_LEN);
344 return (byte_val & BTR_EXTERN_BEING_MODIFIED_FLAG);
345 }
346
347 /** Check if the current blob is being modified
348 @return true if it is being modified, false otherwise. */
349 bool is_being_modified() const { return (is_being_modified(m_ref)); }
350
351 /** Set the inherited flag in the field reference.
352 @param[in] inherited true, if inherited.
353 @param[in] mtr Mini-transaction context.*/
354 void set_inherited(bool inherited, mtr_t *mtr) {
355 ulint byte_val = mach_read_from_1(m_ref + BTR_EXTERN_LEN);
356
357 if (inherited) {
358 byte_val |= BTR_EXTERN_INHERITED_FLAG;
359 } else {
360 byte_val &= ~BTR_EXTERN_INHERITED_FLAG;
361 }
362
363 mlog_write_ulint(m_ref + BTR_EXTERN_LEN, byte_val, MLOG_1BYTE, mtr);
364 }
365
366 /** Check if the current row is the owner of the blob.
367 @return true if owner, false otherwise. */
368 bool is_owner() const {
369 ulint byte_val = mach_read_from_1(m_ref + BTR_EXTERN_LEN);
370 return (!(byte_val & BTR_EXTERN_OWNER_FLAG));
371 }
372
373 /** Check if the current row inherited the blob from parent row.
374 @return true if inherited, false otherwise. */
375 bool is_inherited() const {
376 const ulint byte_val = mach_read_from_1(m_ref + BTR_EXTERN_LEN);
377 return (byte_val & BTR_EXTERN_INHERITED_FLAG);
378 }
379
380 #ifdef UNIV_DEBUG
381 /** Read the space id from the given blob reference.
382 @param[in] ref the blob reference.
383 @return the space id */
384 static space_id_t space_id(const byte *ref) {
385 return (mach_read_from_4(ref));
386 }
387
388 /** Read the page no from the blob reference.
389 @return the page no */
390 static page_no_t page_no(const byte *ref) {
391 return (mach_read_from_4(ref + BTR_EXTERN_PAGE_NO));
392 }
393 #endif /* UNIV_DEBUG */
394
395 /** Read the space id from the blob reference.
396 @return the space id */
397 space_id_t space_id() const { return (mach_read_from_4(m_ref)); }
398
399 /** Read the page number from the blob reference.
400 @return the page number */
401 page_no_t page_no() const {
402 return (mach_read_from_4(m_ref + BTR_EXTERN_PAGE_NO));
403 }
404
405 /** Read the offset of blob header from the blob reference.
406 @return the offset of the blob header */
407 ulint offset() const { return (mach_read_from_4(m_ref + BTR_EXTERN_OFFSET)); }
408
409 /** Read the LOB version from the blob reference.
410 @return the LOB version number. */
411 uint32_t version() const {
412 return (mach_read_from_4(m_ref + BTR_EXTERN_VERSION));
413 }
414
415 /** Read the length from the blob reference.
416 @return length of the blob */
417 ulint length() const {
418 return (mach_read_from_4(m_ref + BTR_EXTERN_LEN + 4));
419 }
420
421 /** Update the information stored in the external field reference.
422 @param[in] space_id the space identifier.
423 @param[in] page_no the page number.
424 @param[in] offset the offset within the page_no
425 @param[in] mtr the mini trx or NULL. */
426 void update(space_id_t space_id, ulint page_no, ulint offset, mtr_t *mtr) {
427 set_space_id(space_id, mtr);
428 set_page_no(page_no, mtr);
429 set_offset(offset, mtr);
430 }
431
432 /** Set the space_id in the external field reference.
433 @param[in] space_id the space identifier.
434 @param[in] mtr mini-trx or NULL. */
435 void set_space_id(const space_id_t space_id, mtr_t *mtr) {
436 mlog_write_ulint(m_ref + BTR_EXTERN_SPACE_ID, space_id, MLOG_4BYTES, mtr);
437 }
438
439 /** Set the page number in the external field reference.
440 @param[in] page_no the page number.
441 @param[in] mtr mini-trx or NULL. */
442 void set_page_no(const ulint page_no, mtr_t *mtr) {
443 mlog_write_ulint(m_ref + BTR_EXTERN_PAGE_NO, page_no, MLOG_4BYTES, mtr);
444 }
445
446 /** Set the offset information in the external field reference.
447 @param[in] offset the offset.
448 @param[in] mtr mini-trx or NULL. */
449 void set_offset(const ulint offset, mtr_t *mtr) {
450 mlog_write_ulint(m_ref + BTR_EXTERN_OFFSET, offset, MLOG_4BYTES, mtr);
451 }
452
453 /** Set the length of blob in the external field reference.
454 @param[in] len the blob length .
455 @param[in] mtr mini-trx or NULL. */
456 void set_length(const ulint len, mtr_t *mtr) {
457 ut_ad(len <= MAX_SIZE);
458 mlog_write_ulint(m_ref + BTR_EXTERN_LEN + 4, len, MLOG_4BYTES, mtr);
459 }
460
461 /** Get the start of a page containing this blob reference.
462 @return start of the page */
463 page_t *page_align() const { return (::page_align(m_ref)); }
464
465 #ifdef UNIV_DEBUG
466 /** Check if the given mtr has necessary latches to update this LOB
467 reference.
468 @param[in] mtr Mini-transaction that needs to
469 be checked.
470 @return true if valid, false otherwise. */
471 bool validate(mtr_t *mtr) {
472 ut_ad(m_ref != nullptr);
473 ut_ad(mtr != nullptr);
474
475 if (mtr->get_log_mode() == MTR_LOG_NO_REDO) {
476 return (true);
477 }
478
479 buf_block_t *block = mtr->memo_contains_page_flagged(
480 m_ref, MTR_MEMO_PAGE_X_FIX | MTR_MEMO_PAGE_SX_FIX);
481 ut_ad(block != nullptr);
482 return (true);
483 }
484
485 /** Check if the space_id in the LOB reference is equal to the
486 space_id of the index to which it belongs.
487 @param[in] index the index to which LOB belongs.
488 @return true if space is valid in LOB reference, false otherwise. */
489 bool check_space_id(dict_index_t *index) const;
490 #endif /* UNIV_DEBUG */
491
492 /** Check if the LOB can be partially updated. This is done by loading
493 the first page of LOB and looking at the flags.
494 @param[in] index the index to which LOB belongs.
495 @return true if LOB is partially updatable, false otherwise.*/
496 bool is_lob_partially_updatable(const dict_index_t *index) const;
497
498 /** Load the first page of the LOB and mark it as not partially
499 updatable anymore.
500 @param[in] trx Current transaction
501 @param[in] mtr Mini-transaction context.
502 @param[in] index Index dictionary object.
503 @param[in] page_size Page size information. */
504 void mark_not_partially_updatable(trx_t *trx, mtr_t *mtr, dict_index_t *index,
505 const page_size_t &page_size);
506
507 /** Load the first page of LOB and read its page type.
508 @param[in] index the index object.
509 @param[in] page_size the page size of LOB.
510 @param[out] is_partially_updatable is the LOB partially updatable.
511 @return the page type of first page of LOB.*/
512 ulint get_lob_page_info(const dict_index_t *index,
513 const page_size_t &page_size,
514 bool &is_partially_updatable) const;
515
516 /** Print this LOB reference into the given output stream.
517 @param[in] out the output stream.
518 @return the output stream. */
519 std::ostream &print(std::ostream &out) const;
520
521 /** The size of an LOB reference object (in bytes) */
522 static const uint SIZE = BTR_EXTERN_FIELD_REF_SIZE;
523
524 private:
525 /** Pointing to a memory of size BTR_EXTERN_FIELD_REF_SIZE */
526 byte *m_ref;
527 };
528
529 /** Overload the global output stream operator to easily print the
530 lob::ref_t object into the output stream.
531 @param[in,out] out the output stream.
532 @param[in] obj the lob::ref_t object to be printed
533 @return the output stream. */
534 inline std::ostream &operator<<(std::ostream &out, const ref_t &obj) {
535 return (obj.print(out));
536 }
537
538 /** LOB operation code for btr_store_big_rec_extern_fields(). */
539 enum opcode {
540
541 /** Store off-page columns for a freshly inserted record */
542 OPCODE_INSERT = 0,
543
544 /** Store off-page columns for an insert by update */
545 OPCODE_INSERT_UPDATE,
546
547 /** Store off-page columns for an update */
548 OPCODE_UPDATE,
549
550 /** Store off-page columns for a freshly inserted record by bulk */
551 OPCODE_INSERT_BULK,
552
553 /** The operation code is unknown or not important. */
554 OPCODE_UNKNOWN
555 };
556
557 /** Stores the fields in big_rec_vec to the tablespace and puts pointers to
558 them in rec. The extern flags in rec will have to be set beforehand. The
559 fields are stored on pages allocated from leaf node file segment of the index
560 tree.
561
562 TODO: If the allocation extends the tablespace, it will not be redo logged, in
563 any mini-transaction. Tablespace extension should be redo-logged, so that
564 recovery will not fail when the big_rec was written to the extended portion of
565 the file, in case the file was somehow truncated in the crash.
566 @param[in] trx the trx doing LOB store. If unavailable it
567 could be nullptr.
568 @param[in,out] pcur a persistent cursor. if btr_mtr is restarted,
569 then this can be repositioned.
570 @param[in] upd update vector
571 @param[in,out] offsets rec_get_offsets() on pcur. the "external in
572 offsets will correctly correspond storage"
573 flagsin offsets will correctly correspond to
574 rec when this function returns
575 @param[in] big_rec_vec vector containing fields to be stored
576 externally
577 @param[in,out] btr_mtr mtr containing the latches to the clustered
578 index. can be committed and restarted.
579 @param[in] op operation code
580 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
581 [[nodiscard]] dberr_t btr_store_big_rec_extern_fields(
582 trx_t *trx, btr_pcur_t *pcur, const upd_t *upd, ulint *offsets,
583 const big_rec_t *big_rec_vec, mtr_t *btr_mtr, opcode op);
584
585 /** Copies an externally stored field of a record to mem heap.
586 @param[in] trx the current transaction.
587 @param[in] index the clustered index
588 @param[in] rec record in a clustered index; must be
589 protected by a lock or a page latch
590 @param[in] offsets array returned by rec_get_offsets()
591 @param[in] page_size BLOB page size
592 @param[in] no field number
593 @param[out] len length of the field
594 @param[out] lob_version version of lob that has been copied
595 @param[in] is_sdi true for SDI Indexes
596 @param[in,out] heap mem heap
597 @param[in] is_rebuilt true if rebuilt
598 @return the field copied to heap, or NULL if the field is incomplete */
599 byte *btr_rec_copy_externally_stored_field_func(
600 trx_t *trx, const dict_index_t *index, const rec_t *rec,
601 const ulint *offsets, const page_size_t &page_size, ulint no, ulint *len,
602 size_t *lob_version, IF_DEBUG(bool is_sdi, ) mem_heap_t *heap,
603 bool is_rebuilt);
604
605 static inline byte *btr_rec_copy_externally_stored_field(
606 trx_t *trx, const dict_index_t *index, const rec_t *rec,
607 const ulint *offsets, const page_size_t &page_size, ulint no, ulint *len,
608 size_t *ver, bool is_sdi [[maybe_unused]], mem_heap_t *heap) {
609 return btr_rec_copy_externally_stored_field_func(
610 trx, index, rec, offsets, page_size, no, len, ver,
611 IF_DEBUG(is_sdi, ) heap, false);
612 }
613
614 /** Gets the offset of the pointer to the externally stored part of a field.
615 @param[in] index Index
616 @param[in] offsets array returned by rec_get_offsets()
617 @param[in] n index of the external field
618 @return offset of the pointer to the externally stored part */
619 ulint btr_rec_get_field_ref_offs(const dict_index_t *index,
620 const ulint *offsets, ulint n);
621
622 /** Gets a pointer to the externally stored part of a field.
623 @param[in] index index
624 @param rec record
625 @param offsets rec_get_offsets(rec)
626 @param n index of the externally stored field
627 @return pointer to the externally stored part */
628 static inline const byte *btr_rec_get_field_ref(const dict_index_t *index,
629 const byte *rec,
630 const ulint *offsets, ulint n) {
631 return rec + lob::btr_rec_get_field_ref_offs(index, offsets, n);
632 }
633
634 /** Gets a pointer to the externally stored part of a field.
635 @param index record descriptor
636 @param rec record
637 @param offsets rec_get_offsets(rec)
638 @param n index of the externally stored field
639 @return pointer to the externally stored part */
640 static inline byte *btr_rec_get_field_ref(const dict_index_t *index, byte *rec,
641 const ulint *offsets, ulint n) {
642 return rec + lob::btr_rec_get_field_ref_offs(index, offsets, n);
643 }
644
645 /** Deallocate a buffer block that was reserved for a BLOB part.
646 @param[in] index Index
647 @param[in] block Buffer block
648 @param[in] all true=remove also the compressed page
649 if there is one
650 @param[in] mtr Mini-transaction to commit */
651 void blob_free(dict_index_t *index, buf_block_t *block, bool all, mtr_t *mtr);
652
653 /** The B-tree context under which the LOB operation is done. */
654 class BtrContext {
655 public:
656 /** Default Constructor */
657 BtrContext()
658 : m_mtr(nullptr),
659 m_pcur(nullptr),
660 m_index(nullptr),
661 m_rec(nullptr),
662 m_offsets(nullptr),
663 m_block(nullptr),
664 m_op(OPCODE_UNKNOWN),
665 m_btr_page_no(FIL_NULL) {}
666
667 /** Constructor **/
668 BtrContext(mtr_t *mtr, dict_index_t *index, buf_block_t *block)
669 : m_mtr(mtr),
670 m_pcur(nullptr),
671 m_index(index),
672 m_rec(nullptr),
673 m_offsets(nullptr),
674 m_block(block),
675 m_op(OPCODE_UNKNOWN),
676 m_btr_page_no(FIL_NULL) {}
677
678 /** Constructor **/
679 BtrContext(mtr_t *mtr, btr_pcur_t *pcur, dict_index_t *index, rec_t *rec,
680 ulint *offsets, buf_block_t *block)
681 : m_mtr(mtr),
682 m_pcur(pcur),
683 m_index(index),
684 m_rec(rec),
685 m_offsets(offsets),
686 m_block(block),
687 m_op(OPCODE_UNKNOWN),
688 m_btr_page_no(FIL_NULL) {
689 ut_ad(m_pcur == nullptr || rec_offs_validate());
690 ut_ad(m_block == nullptr || m_rec == nullptr ||
691 m_block->frame == page_align(m_rec));
692 ut_ad(m_pcur == nullptr || m_rec == m_pcur->get_rec());
693 }
694
695 /** Constructor **/
696 BtrContext(mtr_t *mtr, btr_pcur_t *pcur, dict_index_t *index, rec_t *rec,
697 ulint *offsets, buf_block_t *block, opcode op)
698 : m_mtr(mtr),
699 m_pcur(pcur),
700 m_index(index),
701 m_rec(rec),
702 m_offsets(offsets),
703 m_block(block),
704 m_op(op),
705 m_btr_page_no(FIL_NULL) {
706 ut_ad(m_pcur == nullptr || rec_offs_validate());
707 ut_ad(m_block->frame == page_align(m_rec));
708 ut_ad(m_pcur == nullptr || m_rec == m_pcur->get_rec());
709 }
710
711 /** Copy Constructor **/
712 BtrContext(const BtrContext &other)
713 : m_mtr(other.m_mtr),
714 m_pcur(other.m_pcur),
715 m_index(other.m_index),
716 m_rec(other.m_rec),
717 m_offsets(other.m_offsets),
718 m_block(other.m_block),
719 m_op(other.m_op),
720 m_btr_page_no(other.m_btr_page_no) {}
721
722 /** Marks non-updated off-page fields as disowned by this record.
723 The ownership must be transferred to the updated record which is
724 inserted elsewhere in the index tree. In purge only the owner of
725 externally stored field is allowed to free the field.
726 @param[in] update update vector. */
727 void disown_inherited_fields(const upd_t *update);
728
729 /** Sets the ownership bit of an externally stored field in a record.
730 @param[in] i field number
731 @param[in] val value to set */
732 void set_ownership_of_extern_field(ulint i, bool val) {
733 byte *data;
734 ulint local_len;
735
736 data = rec_get_nth_field(m_index, m_rec, m_offsets, i, &local_len);
737 ut_ad(rec_offs_nth_extern(m_index, m_offsets, i));
738 ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
739
740 local_len -= BTR_EXTERN_FIELD_REF_SIZE;
741
742 ref_t ref(data + local_len);
743
744 ut_a(val || ref.is_owner());
745
746 page_zip_des_t *page_zip = get_page_zip();
747
748 if (page_zip) {
749 ref.set_owner(val, nullptr);
750 page_zip_write_blob_ptr(page_zip, m_rec, m_index, m_offsets, i, m_mtr);
751 } else {
752 ref.set_owner(val, m_mtr);
753 }
754 }
755
756 /** Marks all extern fields in a record as owned by the record.
757 This function should be called if the delete mark of a record is
758 removed: a not delete marked record always owns all its extern
759 fields.*/
760 void unmark_extern_fields() {
761 ut_ad(!rec_offs_comp(m_offsets) || !rec_get_node_ptr_flag(m_rec));
762
763 ulint n = rec_offs_n_fields(m_offsets);
764
765 if (!rec_offs_any_extern(m_offsets)) {
766 return;
767 }
768
769 for (ulint i = 0; i < n; i++) {
770 if (rec_offs_nth_extern(m_index, m_offsets, i)) {
771 set_ownership_of_extern_field(i, true);
772 }
773 }
774 }
775
776 /** Frees the externally stored fields for a record.
777 @param[in] trx_id transaction identifier whose LOB is
778 being freed.
779 @param[in] undo_no undo number within a transaction whose
780 LOB is being freed.
781 @param[in] rollback performing rollback?
782 @param[in] rec_type undo record type.
783 @param[in] node purge node or nullptr */
784 void free_externally_stored_fields(trx_id_t trx_id, undo_no_t undo_no,
785 bool rollback, ulint rec_type,
786 purge_node_t *node);
787
788 /** Frees the externally stored fields for a record, if the field
789 is mentioned in the update vector.
790 @param[in] trx_id the transaction identifier.
791 @param[in] undo_no undo number within a transaction whose
792 LOB is being freed.
793 @param[in] update update vector
794 @param[in] rollback performing rollback?
795 @param[in] big_rec_vec big record vector */
796 void free_updated_extern_fields(trx_id_t trx_id, undo_no_t undo_no,
797 const upd_t *update, bool rollback,
798 big_rec_t *big_rec_vec);
799
800 /** Gets the compressed page descriptor
801 @return the compressed page descriptor. */
802 page_zip_des_t *get_page_zip() const {
803 return (buf_block_get_page_zip(m_block));
804 }
805
806 /** Get the page number of clustered index block.
807 @return the page number. */
808 page_no_t get_page_no() const {
809 return (page_get_page_no(buf_block_get_frame(m_block)));
810 }
811
812 /** Get the record offset within page of the clustered index record.
813 @return the record offset. */
814 ulint get_rec_offset() const { return (page_offset(m_rec)); }
815
816 /** Check if there is a need to recalculate the context information.
817 @return true if there is a need to recalculate, false otherwise. */
818 bool need_recalc() const {
819 return ((m_pcur != nullptr) && (m_rec != m_pcur->get_rec()));
820 }
821
822 /** Get the clustered index record pointer.
823 @return clustered index record pointer. */
824 rec_t *rec() const {
825 ut_ad(m_pcur == nullptr || m_rec == m_pcur->get_rec());
826 return (m_rec);
827 }
828
829 /** Get the LOB reference for the given field number.
830 @param[in] field_no field number.
831 @return LOB reference (aka external field reference).*/
832 byte *get_field_ref(ulint field_no) const {
833 return (btr_rec_get_field_ref(m_index, m_rec, get_offsets(), field_no));
834 }
835
836 #ifdef UNIV_DEBUG
837 /** Validate the current BLOB context object. The BLOB context object
838 is valid if the necessary latches are being held by the
839 mini-transaction of the B-tree (btr mtr). Does not return if the
840 validation fails.
841 @return true if valid */
842 bool validate() const {
843 rec_offs_make_valid(rec(), index(), m_offsets);
844
845 ut_ad(m_mtr->memo_contains_page_flagged(
846 m_rec, MTR_MEMO_PAGE_X_FIX | MTR_MEMO_PAGE_SX_FIX) ||
847 table()->is_intrinsic());
848
849 ut_ad(mtr_memo_contains_flagged(m_mtr, dict_index_get_lock(index()),
850 MTR_MEMO_SX_LOCK | MTR_MEMO_X_LOCK) ||
851 table()->is_intrinsic());
852
853 return (true);
854 }
855
856 /** Check to see if all pointers to externally stored columns in
857 the record must be valid.
858 @return true if all blob references are valid.
859 @return will not return if any blob reference is invalid. */
860 bool are_all_blobrefs_valid() const {
861 for (ulint i = 0; i < rec_offs_n_fields(m_offsets); i++) {
862 if (!rec_offs_nth_extern(m_index, m_offsets, i)) {
863 continue;
864 }
865
866 byte *field_ref = btr_rec_get_field_ref(m_index, rec(), m_offsets, i);
867
868 ref_t blobref(field_ref);
869
870 /* The pointer must not be zero if the operation
871 succeeded. */
872 ut_a(!blobref.is_null());
873
874 /* The column must not be disowned by this record. */
875 ut_a(blobref.is_owner());
876 }
877
878 return (true);
879 }
880 #endif /* UNIV_DEBUG */
881
882 /** Determine whether current operation is a bulk insert operation.
883 @return true, if bulk insert operation, false otherwise. */
884 bool is_bulk() const { return (m_op == OPCODE_INSERT_BULK); }
885
886 /** Get the beginning of the B-tree clustered index page frame
887 that contains the current clustered index record (m_rec).
888 @return the page frame containing the clust rec. */
889 const page_t *rec_frame() const {
890 ut_ad(m_block->frame == page_align(m_rec));
891 return (m_block->frame);
892 }
893
894 /** Commit the mini-transaction that is holding the latches
895 of the clustered index record block. */
896 void commit_btr_mtr() { m_mtr->commit(); }
897
898 /** Start the mini-transaction that will be holding the latches
899 of the clustered index record block. */
900 void start_btr_mtr() {
901 mtr_log_t log_mode = m_mtr->get_log_mode();
902 m_mtr->start();
903 m_mtr->set_log_mode(log_mode);
904 }
905
906 /** Get the page number of clustered index record.
907 @return page number of clustered index record. */
908 page_no_t get_btr_page_no() const {
909 return (page_get_page_no(buf_block_get_frame(m_block)));
910 }
911
912 #ifndef UNIV_HOTBACKUP
913
914 /** Increment the buffer fix count of the clustered index record block.
915 This is to be called before commit_btr_mtr() which decrements the count when
916 you want to prevent the block from being freed:
917 rec_block_fix(); // buf_fix_count++
918 commit_btr_mtr(); // releasing mtr internally does buf_fix_count--
919 start_btr_mtr();
920 rec_block_unfix(); // calls btr_block_get() which does buf_fix_count++ and
921 // then does buf_fix_count--
922 */
923 void rec_block_fix() {
924 m_rec_offset = page_offset(m_rec);
925 m_btr_page_no = page_get_page_no(buf_block_get_frame(m_block));
926 buf_block_buf_fix_inc(m_block, UT_LOCATION_HERE);
927 }
928
929 /** Decrement the buffer fix count of the clustered index record block,
930 X-latching it before, so that the overall buffer_fix_count doesn't change.
931 This is done to restore X-latch on the page after mtr restart:
932 rec_block_fix(); // buf_fix_count++
933 commit_btr_mtr(); // releasing mtr internally does buf_fix_count--
934 start_btr_mtr();
935 rec_block_unfix(); // calls btr_block_get() which does buf_fix_count++ and
936 // then does buf_fix_count--
937 */
938 void rec_block_unfix() {
939 space_id_t space_id = space();
940 page_id_t page_id(space_id, m_btr_page_no);
941 page_size_t page_size(dict_table_page_size(table()));
942 page_cur_t *page_cur = &m_pcur->m_btr_cur.page_cur;
943
944 mtr_x_lock(dict_index_get_lock(index()), m_mtr, UT_LOCATION_HERE);
945
946 page_cur->block = btr_block_get(page_id, page_size, RW_X_LATCH,
947 UT_LOCATION_HERE, index(), m_mtr);
948
949 page_cur->rec = buf_block_get_frame(page_cur->block) + m_rec_offset;
950
951 buf_block_buf_fix_dec(page_cur->block);
952 /* This decrement above is paired with increment in rec_block_fix(), and
953 there is another increment done within btr_block_get(), so overall the block
954 should be buffer-fixed and thus safe to be used. */
955 ut_ad(page_cur->block->page.buf_fix_count > 0);
956 recalc();
957 }
958 #endif /* !UNIV_HOTBACKUP */
959
960 /** Restore the position of the persistent cursor. */
961 void restore_position() {
962 ut_ad(m_pcur->m_rel_pos == BTR_PCUR_ON);
963 bool ret =
964 m_pcur->restore_position(m_pcur->m_latch_mode, m_mtr, UT_LOCATION_HERE);
965
966 ut_a(ret);
967
968 recalc();
969 }
970
971 /** Get the index object.
972 @return index object */
973 dict_index_t *index() const { return (m_index); }
974
975 /** Get the table object.
976 @return table object or NULL. */
977 dict_table_t *table() const {
978 dict_table_t *result = nullptr;
979
980 if (m_pcur != nullptr && m_pcur->index() != nullptr) {
981 result = m_pcur->index()->table;
982 }
983
984 return (result);
985 }
986
987 /** Get the space id.
988 @return space id. */
989 space_id_t space() const { return (index()->space); }
990
991 /** Obtain the page size of the underlying table.
992 @return page size of the underlying table. */
993 const page_size_t page_size() const {
994 return (dict_table_page_size(table()));
995 }
996
997 /** Determine the extent size (in pages) for the underlying table
998 @return extent size in pages */
999 page_no_t pages_in_extent() const {
1000 return (dict_table_extent_size(table()));
1001 }
1002
1003 /** Check if there is enough space in the redo log file. The btr
1004 mini-transaction will be restarted. */
1005 void check_redolog() {
1006 is_bulk() ? check_redolog_bulk() : check_redolog_normal();
1007 }
1008
1009 /** Mark the nth field as externally stored.
1010 @param[in] field_no the field number. */
1011 void make_nth_extern(ulint field_no) {
1012 rec_offs_make_nth_extern(m_index, m_offsets, field_no);
1013 }
1014
1015 /** Get the log mode of the btr mtr.
1016 @return the log mode. */
1017 mtr_log_t get_log_mode() { return (m_mtr->get_log_mode()); }
1018
1019 /** Get flush observer
1020 @return flush observer */
1021 Flush_observer *get_flush_observer() const {
1022 return (m_mtr->get_flush_observer());
1023 }
1024
1025 /** Get the record offsets array.
1026 @return the record offsets array. */
1027 ulint *get_offsets() const { return (m_offsets); }
1028
1029 /** Validate the record offsets array.
1030 @return true if validation succeeds, false otherwise. */
1031 bool rec_offs_validate() const {
1032 if (m_rec != nullptr) {
1033 ut_ad(::rec_offs_validate(m_rec, m_index, m_offsets));
1034 }
1035 return (true);
1036 }
1037
1038 /** Get the associated mini-transaction.
1039 @return the mini-transaction. */
1040 mtr_t *get_mtr() { return (m_mtr); }
1041
1042 /** Get the pointer to the clustered record block.
1043 @return pointer to the clustered rec block. */
1044 buf_block_t *block() const { return (m_block); }
1045
1046 /** Save the position of the persistent cursor. */
1047 void store_position() { m_pcur->store_position(m_mtr); }
1048
1049 /** Check if there is enough space in log file. Commit and re-start the
1050 mini-transaction. */
1051 void check_redolog_normal();
1052
1053 /** When bulk load is being done, check if there is enough space in redo
1054 log file. */
1055 void check_redolog_bulk();
1056
1057 /** Recalculate some of the members after restoring the persistent
1058 cursor. */
1059 void recalc() {
1060 m_block = m_pcur->get_block();
1061 m_rec = m_pcur->get_rec();
1062 m_btr_page_no = page_get_page_no(buf_block_get_frame(m_block));
1063 m_rec_offset = page_offset(m_rec);
1064
1065 rec_offs_make_valid(rec(), index(), m_offsets);
1066 }
1067
1068 /** Write a blob reference of a field into a clustered index record
1069 in a compressed leaf page. The information must already have been
1070 updated on the uncompressed page.
1071 @param[in] field_no BLOB field number
1072 @param[in] mtr Mini-transaction to update blob page. */
1073 void zblob_write_blobref(ulint field_no, mtr_t *mtr) {
1074 page_zip_write_blob_ptr(get_page_zip(), m_rec, index(), m_offsets, field_no,
1075 mtr);
1076 }
1077
1078 /** The btr mtr that is holding the latch on the B-tree index page
1079 containing the clustered index record.*/
1080 mtr_t *m_mtr;
1081
1082 /** Persistent cursor positioned on the clustered index record.*/
1083 btr_pcur_t *m_pcur;
1084 dict_index_t *m_index;
1085 rec_t *m_rec;
1086 ulint *m_offsets;
1087 buf_block_t *m_block;
1088 opcode m_op;
1089
1090 /** Record offset within the page. */
1091 ulint m_rec_offset;
1092
1093 /** Page number where the clust rec is present. */
1094 page_no_t m_btr_page_no;
1095 };
1096
1097 /** The context for a LOB operation. It contains the necessary information
1098 to carry out a LOB operation. */
1099 struct InsertContext : public BtrContext {
1100 /** Constructor
1101 @param[in] btr_ctx b-tree context for lob operation.
1102 @param[in] big_rec_vec array of blobs */
1103 InsertContext(const BtrContext &btr_ctx, const big_rec_t *big_rec_vec)
1104 : BtrContext(btr_ctx), m_big_rec_vec(big_rec_vec) {}
1105
1106 /** Get the vector containing fields to be stored externally.
1107 @return the big record vector */
1108 const big_rec_t *get_big_rec_vec() { return (m_big_rec_vec); }
1109
1110 /** Get the size of vector containing fields to be stored externally.
1111 @return the big record vector size */
1112 ulint get_big_rec_vec_size() { return (m_big_rec_vec->n_fields); }
1113
1114 /** The B-tree Context */
1115 // const BtrContext m_btr_ctx;
1116
1117 /** vector containing fields to be stored externally */
1118 const big_rec_t *m_big_rec_vec;
1119 };
1120
1121 /** Information about data stored in one BLOB page. */
1122 struct blob_page_info_t {
1123 /** Constructor.
1124 @param[in] page_no the BLOB page number.
1125 @param[in] bytes amount of uncompressed BLOB data
1126 in BLOB page in bytes.
1127 @param[in] zbytes amount of compressed BLOB data
1128 in BLOB page in bytes. */
1129 blob_page_info_t(page_no_t page_no, uint bytes, uint zbytes)
1130 : m_page_no(page_no), m_bytes(bytes), m_zbytes(zbytes) {}
1131
1132 /** Re-initialize the current object. */
1133 void reset() {
1134 m_page_no = 0;
1135 m_bytes = 0;
1136 m_zbytes = 0;
1137 }
1138
1139 /** Print this blob_page_into_t object into the given output stream.
1140 @param[in] out the output stream.
1141 @return the output stream. */
1142 std::ostream &print(std::ostream &out) const;
1143
1144 /** Set the compressed data size in bytes.
1145 @param[in] bytes the new compressed data size. */
1146 void set_compressed_size(uint bytes) { m_zbytes = bytes; }
1147
1148 /** Set the uncompressed data size in bytes.
1149 @param[in] bytes the new uncompressed data size. */
1150 void set_uncompressed_size(uint bytes) { m_bytes = bytes; }
1151
1152 /** Set the page number.
1153 @param[in] page_no the page number */
1154 void set_page_no(page_no_t page_no) { m_page_no = page_no; }
1155
1156 private:
1157 /** The BLOB page number */
1158 page_no_t m_page_no;
1159
1160 /** Amount of uncompressed data (in bytes) in the BLOB page. */
1161 uint m_bytes;
1162
1163 /** Amount of compressed data (in bytes) in the BLOB page. */
1164 uint m_zbytes;
1165 };
1166
1167 inline std::ostream &operator<<(std::ostream &out,
1168 const blob_page_info_t &obj) {
1169 return (obj.print(out));
1170 }
1171
1172 /** The in-memory blob directory. Each blob contains a sequence of pages.
1173 This directory contains a list of those pages along with their metadata. */
1174 struct blob_dir_t {
1175 typedef std::vector<blob_page_info_t>::const_iterator const_iterator;
1176
1177 /** Print this blob directory into the given output stream.
1178 @param[in] out the output stream.
1179 @return the output stream. */
1180 std::ostream &print(std::ostream &out) const;
1181
1182 /** Clear the contents of this blob directory. */
1183 void clear() { m_pages.clear(); }
1184
1185 /** Append the given blob page information.
1186 @param[in] page the blob page information to be added.
1187 @return DB_SUCCESS on success, error code on failure. */
1188 dberr_t add(const blob_page_info_t &page) {
1189 m_pages.push_back(page);
1190 return (DB_SUCCESS);
1191 }
1192
1193 /** A vector of blob pages along with its metadata. */
1194 std::vector<blob_page_info_t> m_pages;
1195 };
1196
1197 /** Overloading the global output operator to print the blob_dir_t
1198 object into an output stream.
1199 @param[in,out] out the output stream.
1200 @param[in] obj the object to be printed.
1201 @return the output stream. */
1202 inline std::ostream &operator<<(std::ostream &out, const blob_dir_t &obj) {
1203 return (obj.print(out));
1204 }
1205
1206 /** The context information for reading a single BLOB */
1207 struct ReadContext {
1208 /** Constructor
1209 @param[in] page_size page size information.
1210 @param[in] data 'internally' stored part of the field
1211 containing also the reference to the
1212 external part; must be protected by
1213 a lock or a page latch.
1214 @param[in] prefix_len length of BLOB data stored inline in
1215 the clustered index record, including
1216 the blob reference.
1217 @param[out] buf the output buffer.
1218 @param[in] len the output buffer length.
1219 @param[in] is_sdi true for SDI Indexes. */
1220 ReadContext(const page_size_t &page_size, const byte *data, ulint prefix_len,
1221 byte *buf, ulint len IF_DEBUG(, bool is_sdi))
1222 : m_page_size(page_size),
1223 m_data(data),
1224 m_local_len(prefix_len),
1225 m_blobref(const_cast<byte *>(data) + prefix_len -
1226 BTR_EXTERN_FIELD_REF_SIZE),
1227 m_buf(buf),
1228 m_len(len),
1229 m_lob_version(0) IF_DEBUG(, m_is_sdi(is_sdi)) {
1230 read_blobref();
1231 }
1232
1233 /** Read the space_id, page_no and offset information from the BLOB
1234 reference object and update the member variables. */
1235 void read_blobref() {
1236 m_space_id = m_blobref.space_id();
1237 m_page_no = m_blobref.page_no();
1238 m_offset = m_blobref.offset();
1239 }
1240
1241 /** Check if the BLOB reference is valid. For this particular check,
1242 if the length of the BLOB is greater than 0, it is considered
1243 valid.
1244 @return true if valid. */
1245 bool is_valid_blob() const { return (m_blobref.length() > 0); }
1246
1247 dict_index_t *index() { return (m_index); }
1248
1249 /** The page size information. */
1250 const page_size_t &m_page_size;
1251
1252 /** The 'internally' stored part of the field containing also the
1253 reference to the external part; must be protected by a lock or a page
1254 latch */
1255 const byte *m_data;
1256
1257 /** Length (in bytes) of BLOB prefix stored inline in clustered
1258 index record. */
1259 ulint m_local_len;
1260
1261 /** The blob reference of the blob that is being read. */
1262 const ref_t m_blobref;
1263
1264 /** Buffer into which data is read. */
1265 byte *m_buf;
1266
1267 /** Length of the buffer m_buf. */
1268 ulint m_len;
1269
1270 /** The identifier of the space in which blob is available. */
1271 space_id_t m_space_id;
1272
1273 /** The page number obtained from the blob reference. */
1274 page_no_t m_page_no;
1275
1276 /** The offset information obtained from the blob reference. */
1277 ulint m_offset;
1278
1279 dict_index_t *m_index;
1280
1281 ulint m_lob_version;
1282
1283 #ifdef UNIV_DEBUG
1284 /** Is it a space dictionary index (SDI)?
1285 @return true if SDI, false otherwise. */
1286 bool is_sdi() const { return (m_is_sdi); }
1287
1288 /** Is it a tablespace dictionary index (SDI)? */
1289 const bool m_is_sdi;
1290
1291 /** Assert that current trx is using isolation level read uncommitted.
1292 @return true if transaction is using read uncommitted, false otherwise. */
1293 bool assert_read_uncommitted() const;
1294 #endif /* UNIV_DEBUG */
1295
1296 /** The transaction that is reading. */
1297 trx_t *m_trx = nullptr;
1298 };
1299
1300 /** Fetch compressed BLOB */
1301 struct zReader {
1302 /** Constructor. */
1303 explicit zReader(const ReadContext &ctx) : m_rctx(ctx) {}
1304
1305 /** Fetch the BLOB.
1306 @return DB_SUCCESS on success. */
1307 dberr_t fetch();
1308
1309 /** Fetch one BLOB page.
1310 @return DB_SUCCESS on success. */
1311 dberr_t fetch_page();
1312
1313 /** Get the length of data that has been read.
1314 @return the length of data that has been read. */
1315 ulint length() const { return (m_stream.total_out); }
1316
1317 private:
1318 /** Do setup of the zlib stream.
1319 @return code returned by zlib. */
1320 int setup_zstream();
1321
1322 #ifdef UNIV_DEBUG
1323 /** Assert that the local prefix is empty. For compressed row format,
1324 there is no local prefix stored. This function doesn't return if the
1325 local prefix is non-empty.
1326 @return true if local prefix is empty*/
1327 bool assert_empty_local_prefix();
1328 #endif /* UNIV_DEBUG */
1329
1330 ReadContext m_rctx;
1331
1332 /** Bytes yet to be read. */
1333 ulint m_remaining;
1334
1335 /** The zlib stream used to uncompress while fetching blob. */
1336 z_stream m_stream;
1337
1338 /** The memory heap that will be used by zlib allocator. */
1339 mem_heap_t *m_heap;
1340
1341 /* There is no latch on m_bpage directly. Instead,
1342 m_bpage is protected by the B-tree page latch that
1343 is being held on the clustered index record, or,
1344 in row_merge_copy_blobs(), by an exclusive table lock. */
1345 buf_page_t *m_bpage;
1346
1347 #ifdef UNIV_DEBUG
1348 /** The expected page type. */
1349 ulint m_page_type_ex;
1350 #endif /* UNIV_DEBUG */
1351 };
1352
1353 /** Fetch uncompressed BLOB */
1354 struct Reader {
1355 /** Constructor. */
1356 Reader(const ReadContext &ctx)
1357 : m_rctx(ctx), m_cur_block(nullptr), m_copied_len(0) {}
1358
1359 /** Fetch the complete or prefix of the uncompressed LOB data.
1360 @return bytes of LOB data fetched. */
1361 ulint fetch();
1362
1363 /** Fetch one BLOB page. */
1364 void fetch_page();
1365
1366 ReadContext m_rctx;
1367
1368 /** Buffer block of the current BLOB page */
1369 buf_block_t *m_cur_block;
1370
1371 /** Total bytes of LOB data that has been copied from multiple
1372 LOB pages. This is a cumulative value. When this value reaches
1373 m_rctx.m_len, then the read operation is completed. */
1374 ulint m_copied_len;
1375 };
1376
1377 /** The context information when the delete operation on LOB is
1378 taking place. */
1379 struct DeleteContext : public BtrContext {
1380 /** Constructor.
1381 @param[in] btr the B-tree context of the blob operation.
1382 @param[in] field_ref reference to a blob
1383 @param[in] field_no field containing the blob.
1384 @param[in] rollback true if it is rollback, false if it is purge. */
1385 DeleteContext(const BtrContext &btr, byte *field_ref, ulint field_no,
1386 bool rollback);
1387
1388 /** Constructor.
1389 @param[in] btr the B-tree context of the blob operation.
1390 @param[in] rollback true if it is rollback, false if it is purge. */
1391 DeleteContext(const BtrContext &btr, bool rollback);
1392
1393 /** Update the delete context to point to a different blob.
1394 @param[in] field_ref blob reference
1395 @param[in] field_no the field that contains the blob. */
1396 void set_blob(byte *field_ref, ulint field_no);
1397
1398 /** Check if the blob reference is valid.
1399 @return true if valid, false otherwise. */
1400 bool is_ref_valid() const {
1401 return (m_blobref_mem.m_page_no == m_blobref.page_no());
1402 }
1403
1404 /** Determine if it is compressed page format.
1405 @return true if compressed. */
1406 bool is_compressed() const { return (m_page_size.is_compressed()); }
1407
1408 /** Check if tablespace supports atomic blobs.
1409 @return true if tablespace has atomic blobs. */
1410 bool has_atomic_blobs() const {
1411 space_id_t space_id = m_blobref.space_id();
1412 uint32_t flags = fil_space_get_flags(space_id);
1413 return (DICT_TF_HAS_ATOMIC_BLOBS(flags));
1414 }
1415
1416 bool is_delete_marked() const {
1417 rec_t *clust_rec = rec();
1418 if (clust_rec == nullptr) {
1419 return (true);
1420 }
1421 return (rec_get_deleted_flag(clust_rec, page_rec_is_comp(clust_rec)));
1422 }
1423
1424 #ifdef UNIV_DEBUG
1425 /** Validate the LOB reference object.
1426 @return true if valid, false otherwise. */
1427 bool validate_blobref() const {
1428 rec_t *clust_rec = rec();
1429 if (clust_rec != nullptr) {
1430 const byte *v2 =
1431 btr_rec_get_field_ref(m_index, clust_rec, get_offsets(), m_field_no);
1432
1433 ut_ad(m_blobref.is_equal(v2));
1434 }
1435 return (true);
1436 }
1437 #endif /* UNIV_DEBUG */
1438
1439 /** Acquire an x-latch on the index page containing the clustered
1440 index record, in the given mini-transaction context.
1441 @param[in] mtr Mini-transaction context. */
1442 void x_latch_rec_page(mtr_t *mtr);
1443
1444 /** the BLOB reference or external field reference. */
1445 ref_t m_blobref;
1446
1447 /** field number of externally stored column; ignored if rec == NULL */
1448 ulint m_field_no;
1449
1450 /** Is this operation part of rollback? */
1451 bool m_rollback;
1452
1453 /** The page size of the tablespace.*/
1454 page_size_t m_page_size;
1455
1456 /** Add a buffer block that is to be freed.
1457 @param[in] block buffer block to be freed.*/
1458 void add_lob_block(buf_block_t *block);
1459
1460 /** Free all the stored lob blocks. */
1461 void free_lob_blocks();
1462
1463 /** Destructor. Just add some asserts to ensure that resources are freed. */
1464 ~DeleteContext();
1465
1466 private:
1467 /** Memory copy of the original LOB reference. */
1468 ref_mem_t m_blobref_mem;
1469
1470 using Block_vector = std::vector<buf_block_t *, ut::allocator<buf_block_t *>>;
1471
1472 /** The buffer blocks of lob to be freed. */
1473 Block_vector m_lob_blocks;
1474
1475 /** Obtain the page size from the tablespace flags.
1476 @return the page size. */
1477 page_size_t get_page_size() const;
1478 };
1479
1480 inline DeleteContext::DeleteContext(const BtrContext &btr, bool rollback)
1481 : DeleteContext(btr, nullptr, 0, rollback) {}
1482
1483 inline DeleteContext::DeleteContext(const BtrContext &btr, byte *field_ref,
1484 ulint field_no, bool rollback)
1485 : BtrContext(btr),
1486 m_blobref(field_ref),
1487 m_field_no(field_no),
1488 m_rollback(rollback),
1489 m_page_size(table() == nullptr ? get_page_size()
1490 : dict_table_page_size(table())) {
1491 if (field_ref != nullptr) {
1492 m_blobref.parse(m_blobref_mem);
1493 }
1494 }
1495
1496 inline void DeleteContext::set_blob(byte *field_ref, ulint field_no) {
1497 m_blobref.set_ref(field_ref);
1498 m_field_no = field_no;
1499 if (field_ref != nullptr) {
1500 m_blobref.parse(m_blobref_mem);
1501 }
1502 }
1503
1504 inline page_size_t DeleteContext::get_page_size() const {
1505 bool found;
1506 space_id_t space_id = m_blobref.space_id();
1507 const page_size_t &tmp = fil_space_get_page_size(space_id, &found);
1508 ut_ad(found);
1509 return tmp;
1510 }
1511
1512 inline DeleteContext::~DeleteContext() { ut_ad(m_lob_blocks.empty()); }
1513
1514 inline void DeleteContext::add_lob_block(buf_block_t *block) {
1515 ut_ad(mtr_memo_contains_flagged(m_mtr, block, MTR_MEMO_PAGE_X_FIX));
1516 m_lob_blocks.push_back(block);
1517 }
1518
1519 inline void DeleteContext::free_lob_blocks() {
1520 for (auto block_ptr : m_lob_blocks) {
1521 ut_ad(mtr_memo_contains_flagged(m_mtr, block_ptr, MTR_MEMO_PAGE_X_FIX));
1522 btr_page_free_low(m_index, block_ptr, ULINT_UNDEFINED, m_mtr);
1523 }
1524 m_lob_blocks.clear();
1525 }
1526
1527 /** Determine if an operation on off-page columns is an update.
1528 @param[in] op type of BLOB operation.
1529 @return true if op != OPCODE_INSERT */
1530 inline bool btr_lob_op_is_update(opcode op) {
1531 switch (op) {
1532 case OPCODE_INSERT:
1533 case OPCODE_INSERT_BULK:
1534 return (false);
1535 case OPCODE_INSERT_UPDATE:
1536 case OPCODE_UPDATE:
1537 return (true);
1538 case OPCODE_UNKNOWN:
1539 break;
1540 }
1541
1542 ut_d(ut_error);
1543 ut_o(return false);
1544 }
1545
1546 /** Copies the prefix of an externally stored field of a record.
1547 The clustered index record must be protected by a lock or a page latch.
1548 @param[in] trx the current transaction object if available
1549 or nullptr.
1550 @param[in] index the clust index in which lob is read.
1551 @param[out] buf the field, or a prefix of it
1552 @param[in] len length of buf, in bytes
1553 @param[in] page_size BLOB page size
1554 @param[in] data 'internally' stored part of the field
1555 containing also the reference to the external
1556 part; must be protected by a lock or a page
1557 latch.
1558 @param[in] is_sdi true for SDI indexes
1559 @param[in] local_len length of data, in bytes
1560 @return the length of the copied field, or 0 if the column was being
1561 or has been deleted */
1562 ulint btr_copy_externally_stored_field_prefix_func(
1563 trx_t *trx, const dict_index_t *index, byte *buf, ulint len,
1564 const page_size_t &page_size, const byte *data,
1565 IF_DEBUG(bool is_sdi, ) ulint local_len);
1566
1567 /** Copies an externally stored field of a record to mem heap.
1568 The clustered index record must be protected by a lock or a page latch.
1569 @param[in] trx the current trx object or nullptr
1570 @param[in] index the clust index in which lob is read.
1571 @param[out] len length of the whole field
1572 @param[out] lob_version LOB version number.
1573 @param[in] data 'internally' stored part of the field
1574 containing also the reference to the external
1575 part; must be protected by a lock or a page
1576 latch.
1577 @param[in] page_size BLOB page size
1578 @param[in] local_len length of data
1579 @param[in] is_sdi true for SDI Indexes
1580 @param[in,out] heap mem heap
1581 @return the whole field copied to heap */
1582 byte *btr_copy_externally_stored_field_func(
1583 trx_t *trx, const dict_index_t *index, ulint *len, size_t *lob_version,
1584 const byte *data, const page_size_t &page_size, ulint local_len,
1585 IF_DEBUG(bool is_sdi, ) mem_heap_t *heap);
1586
1587 static inline ulint btr_copy_externally_stored_field_prefix(
1588 trx_t *trx, const dict_index_t *index, byte *buf, ulint len,
1589 const page_size_t &page_size, const byte *data, bool is_sdi,
1590 ulint local_len) {
1591 return btr_copy_externally_stored_field_prefix_func(
1592 trx, index, buf, len, page_size, data, IF_DEBUG(is_sdi, ) local_len);
1593 }
1594 static inline byte *btr_copy_externally_stored_field(
1595 trx_t *trx, const dict_index_t *index, ulint *len, size_t *ver,
1596 const byte *data, const page_size_t &page_size, ulint local_len,
1597 bool is_sdi, mem_heap_t *heap) {
1598 return btr_copy_externally_stored_field_func(trx, index, len, ver, data,
1599 page_size, local_len,
1600 IF_DEBUG(is_sdi, ) heap);
1601 }
1602
1603 /** Gets the externally stored size of a record, in units of a database page.
1604 @param[in] index index
1605 @param[in] rec record
1606 @param[in] offsets array returned by rec_get_offsets()
1607 @return externally stored part, in units of a database page */
1608 ulint btr_rec_get_externally_stored_len(const dict_index_t *index,
1609 const rec_t *rec, const ulint *offsets);
1610
1611 /** Purge an LOB (either of compressed or uncompressed).
1612 @param[in] ctx the delete operation context information.
1613 @param[in] index clustered index in which LOB is present
1614 @param[in] trxid the transaction that is being purged.
1615 @param[in] undo_no during rollback to savepoint, purge only up to
1616 this undo number.
1617 @param[in] rec_type undo record type.
1618 @param[in] uf the update vector for the field.
1619 @param[in] node the purge node or nullptr. */
1620 void purge(lob::DeleteContext *ctx, dict_index_t *index, trx_id_t trxid,
1621 undo_no_t undo_no, ulint rec_type, const upd_field_t *uf,
1622 purge_node_t *node);
1623
1624 /** Update a portion of the given LOB.
1625 @param[in] ctx update operation context information.
1626 @param[in] trx the transaction that is doing the
1627 modification.
1628 @param[in] index the clustered index containing the LOB.
1629 @param[in] upd update vector
1630 @param[in] field_no the LOB field number
1631 @param[in] blobref LOB reference stored in clust record.
1632 @return DB_SUCCESS on success, error code on failure. */
1633 dberr_t update(InsertContext &ctx, trx_t *trx, dict_index_t *index,
1634 const upd_t *upd, ulint field_no, ref_t blobref);
1635
1636 /** Update a portion of the given LOB.
1637 @param[in] ctx update operation context information.
1638 @param[in] trx the transaction that is doing the
1639 modification.
1640 @param[in] index the clustered index containing the LOB.
1641 @param[in] upd update vector
1642 @param[in] field_no the LOB field number
1643 @param[in] blobref LOB reference stored in clust record.
1644 @return DB_SUCCESS on success, error code on failure. */
1645 dberr_t z_update(InsertContext &ctx, trx_t *trx, dict_index_t *index,
1646 const upd_t *upd, ulint field_no, ref_t blobref);
1647
1648 /** Print information about the given LOB.
1649 @param[in] trx the current transaction.
1650 @param[in] index the clust index that contains the LOB.
1651 @param[in] out the output stream into which LOB info is printed.
1652 @param[in] ref the LOB reference
1653 @param[in] fatal if true assert at end of function. */
1654 void print(trx_t *trx, dict_index_t *index, std::ostream &out, ref_t ref,
1655 bool fatal);
1656
1657 /** Import the given LOB. Update the creator trx id and the modifier trx
1658 id to the given import trx id.
1659 @param[in] index clustered index containing the lob.
1660 @param[in] field_ref the lob reference.
1661 @param[in] trx_id the import trx id. */
1662 void z_import(const dict_index_t *index, byte *field_ref, trx_id_t trx_id);
1663
1664 /** Import the given LOB. Update the creator trx id and the modifier trx
1665 id to the given import trx id.
1666 @param[in] index clustered index containing the lob.
1667 @param[in] field_ref the lob reference.
1668 @param[in] trx_id the import trx id. */
1669 void import(const dict_index_t *index, byte *field_ref, trx_id_t trx_id);
1670
1671 #ifdef UNIV_DEBUG
1672 /** Check if all the LOB references in the given clustered index record has
1673 valid space_id in it.
1674 @param[in] index the index to which the LOB belongs.
1675 @param[in] rec the clust_rec in which the LOB references are checked.
1676 @param[in] offsets the field offsets of the given rec.
1677 @return true if LOB references have valid space_id, false otherwise. */
1678 bool rec_check_lobref_space_id(dict_index_t *index, const rec_t *rec,
1679 const ulint *offsets);
1680 #endif /* UNIV_DEBUG */
1681
1682 /** Mark an LOB that it is not partially updatable anymore.
1683 @param[in] trx Current transaction.
1684 @param[in] index Clustered index to which the LOB belongs.
1685 @param[in] update Update vector.
1686 @param[in] btr_mtr Mini-transaction context holding latches on the B-tree.
1687 This function does not generate redo log using this btr_mtr. It only obtains
1688 the log mode.
1689 @return DB_SUCCESS on success, error code on failure. */
1690 dberr_t mark_not_partially_updatable(trx_t *trx, dict_index_t *index,
1691 const upd_t *update, const mtr_t *btr_mtr);
1692
1693 } // namespace lob
1694
1695 #endif /* lob0lob_h */