Debian Codegraph @

storage/innobase/lob/zlob0read.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2016, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 #include "zlob0read.h"
29 #include "lob0impl.h"
30 #include "lob0lob.h"
31 #include "lob0zip.h"
32 #include "my_dbug.h"
33 #include "trx0trx.h"
34 #include "univ.i"
35 #include "ut0ut.h"
36 #include "zlob0first.h"
37
38 namespace lob {
39
40 /** Fetch a compressed large object (ZLOB) from the system.
41 @param[in] ctx the read context information.
42 @param[in] ref the LOB reference identifying the LOB.
43 @param[in] offset read the LOB from the given offset.
44 @param[in] len the length of LOB data that needs to be fetched.
45 @param[out] buf the output buffer (owned by caller) of minimum len bytes.
46 @return the amount of data (in bytes) that was actually read. */
47 ulint z_read(ReadContext *ctx, lob::ref_t ref, ulint offset, ulint len,
48 byte *buf) {
49 ut_ad(offset == 0);
50 ut_ad(len > 0);
51
52 const ulint avail_lob = ref.length();
53
54 if (avail_lob == 0) {
55 return (0);
56 }
57
58 if (ref.is_being_modified()) {
59 /* This should happen only for READ UNCOMMITTED transactions. */
60 ut_ad(ctx->assert_read_uncommitted());
61 return (0);
62 }
63
64 const uint32_t lob_version = ref.version();
65
66 fil_addr_t old_node_loc = fil_addr_null;
67 fil_addr_t node_loc = fil_addr_null;
68
69 ut_ad(ctx->m_index->is_clustered());
70 ut_ad(ctx->m_space_id == ref.space_id());
71
72 const page_no_t first_page_no = ref.page_no();
73 const space_id_t space_id = ctx->m_space_id;
74 const page_id_t first_page_id(space_id, first_page_no);
75
76 mtr_t mtr;
77 mtr_start(&mtr);
78
79 /* The current entry - it is the latest version. */
80 z_index_entry_t cur_entry(&mtr, ctx->m_index);
81
82 z_first_page_t first(&mtr, ctx->m_index);
83 first.load_x(first_page_no);
84
85 page_type_t page_type = first.get_page_type();
86
87 if (page_type == FIL_PAGE_TYPE_ZBLOB || page_type == FIL_PAGE_SDI_ZBLOB) {
88 mtr_commit(&mtr);
89 lob::zReader reader(*ctx);
90 reader.fetch();
91 return (reader.length());
92 }
93
94 if (page_type != FIL_PAGE_TYPE_ZLOB_FIRST) {
95 /* Assume that the BLOB has been freed and return without taking further
96 action. This condition is hit when there are stale LOB references in the
97 clustered index record, especially when there are server crashes during
98 updation of delete-marked clustered index record with external fields. */
99 mtr_commit(&mtr);
100 return (0);
101 }
102
103 flst_base_node_t *flst = first.index_list();
104
105 #ifdef UNIV_DEBUG
106 /* The length of this list cannot be equal to 0 */
107 ulint flst_len = flst_get_len(flst);
108 ut_a(flst_len != 0);
109
110 std::vector<trx_id_t> trxids;
111 #endif /* UNIV_DEBUG */
112
113 node_loc = flst_get_first(flst, &mtr);
114
115 byte *ptr = buf;
116 ulint remain = len;
117
118 while (remain > 0 && !fil_addr_is_null(node_loc)) {
119 flst_node_t *node = first.addr2ptr_x(node_loc);
120 cur_entry.reset(node);
121
122 #ifdef UNIV_DEBUG
123 trxids.push_back(cur_entry.get_trx_id());
124 #endif /* UNIV_DEBUG */
125
126 /* Obtain the next index entry location. */
127 node_loc = cur_entry.get_next();
128 const uint32_t entry_lob_version = cur_entry.get_lob_version();
129
130 if (entry_lob_version <= lob_version) {
131 /* Can read the entry. */
132 ut_ad(entry_lob_version <= lob_version);
133 z_read_chunk(ctx->m_index, cur_entry, 0, remain, ptr, &mtr);
134 } else {
135 ut_ad(entry_lob_version > lob_version);
136
137 /* Cannot read the entry. Look at older versions. */
138 flst_base_node_t *ver_lst = cur_entry.get_versions_list();
139 old_node_loc = flst_get_first(ver_lst, &mtr);
140
141 bool got_data = false;
142 while (!fil_addr_is_null(old_node_loc)) {
143 flst_node_t *old_node = first.addr2ptr_x(old_node_loc);
144
145 /* The older version of the current entry. */
146 z_index_entry_t old_version(old_node, &mtr, ctx->m_index);
147 #ifdef UNIV_DEBUG
148 trxids.push_back(old_version.get_trx_id());
149 #endif /* UNIV_DEBUG */
150 const uint32_t old_lob_version = old_version.get_lob_version();
151
152 if (old_lob_version <= lob_version) {
153 /* Can read the entry. */
154 ut_ad(old_version.get_lob_version() <= lob_version);
155 z_read_chunk(ctx->m_index, old_version, 0, remain, ptr, &mtr);
156 got_data = true;
157 break;
158 }
159 old_node_loc = old_version.get_next();
160 }
161
162 /* For this offset, if none of the versions are
163 visible, go ahead and read the latest version. The
164 DB_TRX_ID in the clustered index record can be checked
165 here to verify that this is correct.*/
166 if (!got_data) {
167 z_read_chunk(ctx->m_index, cur_entry, 0, remain, ptr, &mtr);
168 }
169 }
170
171 cur_entry.reset(nullptr);
172 mtr_commit(&mtr);
173 mtr_start(&mtr);
174 first.load_x(first_page_no);
175 }
176
177 const ulint total_read = len - remain;
178 ut_ad(total_read == len || total_read == avail_lob);
179 mtr_commit(&mtr);
180 return (total_read);
181 }
182
183 /** Read one data chunk associated with one index entry.
184 @param[in] index The clustered index containing the LOB.
185 @param[in] entry Pointer to the index entry
186 @param[in] offset The offset from which to read the chunk.
187 @param[in,out] len The length of the output buffer. This length can
188 be greater than the chunk size.
189 @param[in,out] buf The output buffer.
190 @param[in] mtr Mini-transaction context.
191 @return number of bytes copied into the output buffer. */
192 ulint z_read_chunk(dict_index_t *index, z_index_entry_t &entry, ulint offset,
193 ulint &len, byte *&buf, mtr_t *mtr) {
194 const ulint data_len = entry.get_data_len();
195
196 if (entry.get_z_page_no() == FIL_NULL || data_len == 0) {
197 return (0);
198 }
199
200 const ulint zbuf_size = entry.get_zdata_len();
201 std::unique_ptr<byte[]> zbuf(new byte[zbuf_size]);
202
203 ulint zbytes = z_read_strm(index, entry, zbuf.get(), zbuf_size, mtr);
204 ut_a(zbytes == zbuf_size);
205
206 z_stream strm;
207 strm.zalloc = nullptr;
208 strm.zfree = nullptr;
209 strm.opaque = nullptr;
210
211 int ret = inflateInit(&strm);
212 ut_a(ret == Z_OK);
213
214 strm.avail_in = static_cast<uInt>(zbytes);
215 strm.next_in = zbuf.get();
216
217 ulint to_copy = 0;
218 if (offset == 0 && len >= data_len) {
219 /* The full chunk is needed for output. */
220 strm.avail_out = static_cast<uInt>(len);
221 strm.next_out = buf;
222
223 ret = inflate(&strm, Z_FINISH);
224 ut_a(ret == Z_STREAM_END);
225
226 to_copy = strm.total_out;
227
228 } else {
229 /* Only part of the chunk is needed. */
230 byte ubuf[Z_CHUNK_SIZE];
231 strm.avail_out = Z_CHUNK_SIZE;
232 strm.next_out = ubuf;
233
234 ret = inflate(&strm, Z_FINISH);
235 ut_a(ret == Z_STREAM_END);
236
237 ulint chunk_size = strm.total_out;
238 ut_a(chunk_size == data_len);
239 ut_a(offset < chunk_size);
240
241 byte *ptr = ubuf + offset;
242 ulint remain = chunk_size - offset;
243 to_copy = (len > remain) ? remain : len;
244 memcpy(buf, ptr, to_copy);
245 }
246
247 len -= to_copy;
248 buf += to_copy;
249 inflateEnd(&strm);
250
251 return (to_copy);
252 }
253
254 /** Read one zlib stream fully, given its index entry.
255 @param[in] index The index dictionary object.
256 @param[in] entry The index entry (memory copy).
257 @param[in,out] zbuf The output buffer
258 @param[in] zbuf_size The size of the output buffer.
259 @param[in,out] mtr Mini-transaction.
260 @return the size of the zlib stream.*/
261 ulint z_read_strm(dict_index_t *index, z_index_entry_t &entry, byte *zbuf,
262 ulint zbuf_size, mtr_t *mtr) {
263 page_no_t page_no = entry.get_z_page_no();
264 byte *ptr = zbuf;
265 ulint remain = zbuf_size;
266
267 while (remain > 0 && page_no != FIL_NULL) {
268 buf_block_t *block = buf_page_get(
269 page_id_t(dict_index_get_space(index), page_no),
270 dict_table_page_size(index->table), RW_X_LATCH, UT_LOCATION_HERE, mtr);
271
272 page_type_t ptype = block->get_page_type();
273 byte *data = nullptr;
274 ulint data_size = 0;
275 if (ptype == FIL_PAGE_TYPE_ZLOB_FRAG) {
276 frag_id_t fid = entry.get_z_frag_id();
277 z_frag_page_t frag_page(block, mtr, index);
278 frag_node_t node = frag_page.get_frag_node(fid);
279 data = node.data_begin();
280 data_size = node.payload();
281 } else if (ptype == FIL_PAGE_TYPE_ZLOB_FIRST) {
282 z_first_page_t first(block, mtr, index);
283 data = first.begin_data_ptr();
284 data_size = first.get_data_len();
285 } else {
286 ut_a(ptype == FIL_PAGE_TYPE_ZLOB_DATA);
287 z_data_page_t data_page(block, mtr, index);
288 data = data_page.begin_data_ptr();
289 data_size = data_page.get_data_len();
290 ut_a(data_size <= data_page.payload());
291 }
292 memcpy(ptr, data, data_size);
293 ptr += data_size;
294 ut_a(data_size <= remain);
295 remain -= data_size;
296 page_no = block->get_next_page_no();
297 }
298
299 ulint zbytes = (zbuf_size - remain);
300 return (zbytes);
301 }
302
303 #ifdef UNIV_DEBUG
304 static bool z_validate_strm_low(dict_index_t *index, z_index_entry_t &entry,
305 mtr_t *mtr) {
306 /* Expected length of compressed data. */
307 const ulint exp_zlen = entry.get_zdata_len();
308 page_no_t page_no = entry.get_z_page_no();
309 ulint remain = exp_zlen;
310
311 while (remain > 0 && page_no != FIL_NULL) {
312 buf_block_t *block = buf_page_get(
313 page_id_t(dict_index_get_space(index), page_no),
314 dict_table_page_size(index->table), RW_X_LATCH, UT_LOCATION_HERE, mtr);
315
316 page_type_t ptype = block->get_page_type();
317 ulint data_size = 0;
318 if (ptype == FIL_PAGE_TYPE_ZLOB_FRAG) {
319 frag_id_t fid = entry.get_z_frag_id();
320 z_frag_page_t frag_page(block, mtr, index);
321 frag_node_t node = frag_page.get_frag_node(fid);
322 data_size = node.payload();
323 } else if (ptype == FIL_PAGE_TYPE_ZLOB_FIRST) {
324 z_first_page_t first(block, mtr, index);
325 data_size = first.get_data_len();
326 } else {
327 ut_a(ptype == FIL_PAGE_TYPE_ZLOB_DATA);
328 z_data_page_t data_page(block, mtr, index);
329 data_size = data_page.get_data_len();
330 ut_a(data_size <= data_page.payload());
331 }
332 ut_a(data_size <= remain);
333 remain -= data_size;
334 page_no = block->get_next_page_no();
335 }
336
337 ut_ad(remain == 0);
338 return (true);
339 }
340
341 bool z_validate_strm(dict_index_t *index, z_index_entry_t &entry, mtr_t *mtr) {
342 static const uint32_t FREQ = 50;
343 static std::atomic<uint32_t> n{0};
344 bool ret = true;
345 if (++n % FREQ == 0) {
346 ret = z_validate_strm_low(index, entry, mtr);
347 }
348 return (ret);
349 }
350 #endif /* UNIV_DEBUG */
351
352 } // namespace lob