MySQL 8.0.40
Source Code Documentation
lob0zip.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2016, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27#ifndef lob0zip_h
28#define lob0zip_h
29
30#include "lob0ins.h"
31
32namespace lob {
33
34/** Insert or write the compressed BLOB as a single zlib stream. */
35class zInserter : private BaseInserter {
36 public:
37 /** Constructor.
38 @param[in] ctx blob operation context. */
40
41 /** Destructor. */
42 ~zInserter();
43
44 /** Prepare to write a compressed BLOB. Setup the zlib
45 compression stream.
46 @return DB_SUCCESS on success, error code on failure. */
48
49 /** Write all the BLOBs of the clustered index record.
50 @return DB_SUCCESS on success, error code on failure. */
51 dberr_t write();
52
53 /** Write one blob field data.
54 @param[in] blob_j the blob field number
55 @return DB_SUCCESS on success, error code on failure. */
56 dberr_t write_one_blob(size_t blob_j);
57
58 /** Cleanup after completing the write of compressed BLOB.
59 @param[in] validate if true, validate all the
60 lob references. if false,
61 skip this validation.
62 @return DB_SUCCESS on success, error code on failure. */
63 dberr_t finish(bool validate = true) {
64 int ret = deflateEnd(&m_stream);
65 ut_ad(ret == Z_OK);
67
68 if (ret != Z_OK) {
69 m_err = DB_FAIL;
70 }
71 return (m_err);
72 }
73
74 /** Write the page type of the BLOB page and also generate the
75 redo log record.
76 @param[in] blob_page the BLOB page
77 @param[in] nth_blob_page the count of BLOB page from
78 the beginning of the BLOB. */
79 void log_page_type(page_t *blob_page, ulint nth_blob_page) {
80 page_type_t page_type;
81
82 if (is_index_sdi()) {
83 page_type = FIL_PAGE_SDI_ZBLOB;
84 } else if (nth_blob_page == 0) {
85 page_type = FIL_PAGE_TYPE_ZBLOB;
86 } else {
87 page_type = FIL_PAGE_TYPE_ZBLOB2;
88 }
89
90 mlog_write_ulint(blob_page + FIL_PAGE_TYPE, page_type, MLOG_2BYTES,
91 &m_blob_mtr);
92 }
93
94 /** Calculate the total number of pages needed to store
95 the given blobs */
97 const page_size_t page_size = m_ctx->page_size();
98
99 /* Space available in compressed page to carry blob data */
100 const ulint payload_size_zip = page_size.physical() - FIL_PAGE_DATA;
101
102 const big_rec_t *vec = m_ctx->get_big_rec_vec();
103
104 ulint total_blob_pages = 0;
105 for (ulint i = 0; i < vec->n_fields; i++) {
106 total_blob_pages +=
107 static_cast<ulint>(
108 deflateBound(&m_stream, static_cast<uLong>(vec->fields[i].len)) +
109 payload_size_zip - 1) /
110 payload_size_zip;
111 }
112
113 return (total_blob_pages);
114 }
115
116 /** Write contents into a single BLOB page.
117 @return code as returned by zlib. */
119
120 /** Commit the BLOB mtr. */
122
123 /** Write one blob page. This function will be repeatedly called
124 with an increasing nth_blob_page to completely write a BLOB.
125 @param[in] field the big record field.
126 @param[in] nth_blob_page count of the BLOB page (starting from 1).
127 @return code as returned by the zlib. */
128 int write_single_blob_page(big_rec_field_t &field, ulint nth_blob_page);
129
130 /** Write first blob page.
131 @param[in] field the big record field.
132 @return code as returned by the zlib. */
134
135 /** Verify that all pointers to externally stored columns in the record
136 is be valid. If validation fails, this function doesn't return.
137 @return true if valid. */
138 bool validate_blobrefs() const {
139 const ulint *offsets = m_ctx->get_offsets();
140
141 for (ulint i = 0; i < rec_offs_n_fields(offsets); i++) {
142 if (!rec_offs_nth_extern(m_ctx->index(), offsets, i)) {
143 continue;
144 }
145
146 byte *field_ref =
147 btr_rec_get_field_ref(m_ctx->index(), m_ctx->rec(), offsets, i);
148
149 ref_t blobref(field_ref);
150
151 /* The pointer must not be zero if the operation
152 succeeded. */
153 ut_a(!blobref.is_null() || m_err != DB_SUCCESS);
154
155 /* The column must not be disowned by this record. */
156 ut_a(blobref.is_owner());
157 }
158 return (true);
159 }
160
161 /** For the given blob field, update its length in the blob reference
162 which is available in the clustered index record.
163 @param[in] field the concerned blob field. */
165
166 /** Make the current page as next page of previous page. In other
167 words, make the page m_cur_blob_page_no as the next page
168 (FIL_PAGE_NEXT) of page m_prev_page_no.
169 @return DB_SUCCESS on success, or error code on failure. */
171
172 /** Write one small blob field data. Refer to ref_t to determine
173 the definition of small blob.
174 @param[in] blob_j the blob field number
175 @return DB_SUCCESS on success, error code on failure. */
176 dberr_t write_one_small_blob(size_t blob_j);
177
178 private:
179 /** Add the BLOB page information to the directory
180 @param[in] page_info BLOB page information. */
181 void add_to_blob_dir(const blob_page_info_t &page_info) {
182 m_dir.add(page_info);
183 }
184
186 z_stream m_stream;
187
188 /** The BLOB directory information. */
190};
191
193 if (m_heap != nullptr) {
195 }
196}
197
198} // namespace lob
199
200#endif // lob0zip_h
#define Z_OK
Definition: azlib.h:163
rec_t * rec() const
Get the clustered index record pointer.
Definition: lob0lob.h:824
ulint * get_offsets() const
Get the record offsets array.
Definition: lob0lob.h:1027
dict_index_t * index() const
Get the index object.
Definition: lob0lob.h:973
const page_size_t page_size() const
Obtain the page size of the underlying table.
Definition: lob0lob.h:993
Insert or write the compressed BLOB as a single zlib stream.
Definition: lob0zip.h:35
z_stream m_stream
Definition: lob0zip.h:186
dberr_t write_one_blob(size_t blob_j)
Write one blob field data.
Definition: zlob0ins.cc:132
blob_dir_t m_dir
The BLOB directory information.
Definition: lob0zip.h:189
bool validate_blobrefs() const
Verify that all pointers to externally stored columns in the record is be valid.
Definition: lob0zip.h:138
void add_to_blob_dir(const blob_page_info_t &page_info)
Add the BLOB page information to the directory.
Definition: lob0zip.h:181
int write_single_blob_page(big_rec_field_t &field, ulint nth_blob_page)
Write one blob page.
Definition: zlob0ins.cc:233
~zInserter()
Destructor.
Definition: lob0zip.h:192
dberr_t write_one_small_blob(size_t blob_j)
Write one small blob field data.
Definition: zlob0ins.cc:108
int write_into_single_page()
Write contents into a single BLOB page.
Definition: zlob0ins.cc:163
void log_page_type(page_t *blob_page, ulint nth_blob_page)
Write the page type of the BLOB page and also generate the redo log record.
Definition: lob0zip.h:79
ulint calc_total_pages()
Calculate the total number of pages needed to store the given blobs.
Definition: lob0zip.h:96
dberr_t finish(bool validate=true)
Cleanup after completing the write of compressed BLOB.
Definition: lob0zip.h:63
zInserter(InsertContext *ctx)
Constructor.
Definition: lob0zip.h:39
dberr_t prepare()
Prepare to write a compressed BLOB.
Definition: zlob0ins.cc:269
void update_length_in_blobref(big_rec_field_t &field)
For the given blob field, update its length in the blob reference which is available in the clustered...
Definition: zlob0ins.cc:87
int write_first_page(big_rec_field_t &field)
Write first blob page.
Definition: zlob0ins.cc:33
dberr_t write()
Write all the BLOBs of the clustered index record.
Definition: zlob0ins.cc:293
mem_heap_t * m_heap
Definition: lob0zip.h:185
void commit_blob_mtr()
Commit the BLOB mtr.
Definition: lob0zip.h:121
dberr_t set_page_next()
Make the current page as next page of previous page.
Definition: zlob0ins.cc:309
Page size descriptor.
Definition: page0size.h:50
size_t physical() const
Retrieve the physical page size (on-disk).
Definition: page0size.h:121
dberr_t
Definition: db0err.h:39
@ DB_FAIL
Definition: db0err.h:208
@ DB_SUCCESS
Definition: db0err.h:43
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
constexpr page_type_t FIL_PAGE_TYPE_ZBLOB
First compressed BLOB page.
Definition: fil0fil.h:1240
uint16_t page_type_t
Definition: fil0fil.h:1193
constexpr page_type_t FIL_PAGE_TYPE_ZBLOB2
Subsequent compressed BLOB page.
Definition: fil0fil.h:1243
constexpr page_type_t FIL_PAGE_SDI_ZBLOB
Compressed SDI BLOB page.
Definition: fil0fil.h:1265
constexpr uint32_t FIL_PAGE_TYPE
file page type: FIL_PAGE_INDEX,..., 2 bytes.
Definition: fil0types.h:76
constexpr uint32_t FIL_PAGE_DATA
start of the data on the page
Definition: fil0types.h:111
static void mem_heap_free(mem_heap_t *heap)
Frees the space occupied by a memory heap.
void mlog_write_ulint(byte *ptr, ulint val, mlog_id_t type, mtr_t *mtr)
Writes 1, 2 or 4 bytes to a file page.
Definition: mtr0log.cc:256
#define mtr_commit(m)
Commit a mini-transaction.
Definition: mtr0mtr.h:59
@ MLOG_2BYTES
2 bytes ...
Definition: mtr0types.h:73
Provides the large objects (LOB) module.
Definition: lob0del.h:32
static const byte * btr_rec_get_field_ref(const dict_index_t *index, const byte *rec, const ulint *offsets, ulint n)
Gets a pointer to the externally stored part of a field.
Definition: lob0lob.h:628
static mysql_service_status_t validate(reference_caching_channel channel) noexcept
Definition: component.cc:67
byte page_t
Type of the index page.
Definition: page0types.h:152
static ulint rec_offs_n_fields(const ulint *offsets)
The following function returns the number of fields in a record.
Definition: rec.h:484
ulint rec_offs_nth_extern(const dict_index_t *index, const ulint *offsets, ulint n)
Returns nonzero if the extern bit is set in nth field of rec.
Definition: rem0wrec.h:150
A slot for a field in a big rec vector.
Definition: data0data.h:799
ulint len
stored data length, in bytes
Definition: data0data.h:814
Storage format for overflow data in a big record, that is, a clustered index record which needs exter...
Definition: data0data.h:842
big_rec_field_t * fields
stored fields
Definition: data0data.h:847
ulint n_fields
number of stored fields
Definition: data0data.h:846
This struct can hold BLOB routines/functions, and state variables, that are common for compressed and...
Definition: lob0ins.h:36
mtr_t m_blob_mtr
The mini trx used to write into blob pages.
Definition: lob0ins.h:87
bool is_index_sdi()
Check if the index is SDI index.
Definition: lob0ins.h:73
InsertContext * m_ctx
The BLOB operation context.
Definition: lob0ins.h:81
dberr_t m_err
Success or failure status of the operation so far.
Definition: lob0ins.h:84
The context for a LOB operation.
Definition: lob0lob.h:1099
const big_rec_t * get_big_rec_vec()
Get the vector containing fields to be stored externally.
Definition: lob0lob.h:1108
The in-memory blob directory.
Definition: lob0lob.h:1174
dberr_t add(const blob_page_info_t &page)
Append the given blob page information.
Definition: lob0lob.h:1188
Information about data stored in one BLOB page.
Definition: lob0lob.h:1122
The struct 'lob::ref_t' represents an external field reference.
Definition: lob0lob.h:198
bool is_null() const
Check if the field reference is made of zeroes.
Definition: lob0lob.h:279
bool is_owner() const
Check if the current row is the owner of the blob.
Definition: lob0lob.h:368
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
unsigned long int ulint
Definition: univ.i:406
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:69
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:57