Debian Codegraph @

storage/innobase/lob/zlob0ins.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 "db0err.h"
29 #include "lob0zip.h"
30
31 namespace lob {
32
33 int zInserter::write_first_page(big_rec_field_t &field) {
34 buf_block_t *rec_block = m_ctx->block();
35 mtr_t *mtr = start_blob_mtr();
36
37 buf_page_get(rec_block->page.id, rec_block->page.size, RW_X_LATCH,
38 UT_LOCATION_HERE, mtr);
39
40 buf_block_t *blob_block = alloc_blob_page();
41
42 if (dict_index_is_online_ddl(m_ctx->index())) {
43 row_log_table_blob_alloc(m_ctx->index(), m_cur_blob_page_no);
44 }
45
46 page_t *blob_page = buf_block_get_frame(blob_block);
47
48 log_page_type(blob_page, 0);
49
50 int err = write_into_single_page();
51
52 ut_ad(!dict_index_is_spatial(m_ctx->index()));
53
54 const ulint field_no = field.field_no;
55 byte *field_ref = btr_rec_get_field_ref(m_ctx->index(), m_ctx->rec(),
56 m_ctx->get_offsets(), field_no);
57 ref_t blobref(field_ref);
58
59 if (err == Z_OK) {
60 blobref.set_length(0, nullptr);
61 } else if (err == Z_STREAM_END) {
62 blobref.set_length(m_stream.total_in, nullptr);
63 } else {
64 ut_d(ut_error);
65 ut_o(return (err));
66 }
67
68 blobref.update(m_ctx->space(), m_cur_blob_page_no, FIL_PAGE_NEXT, nullptr);
69
70 /* After writing the first blob page, update the blob reference. */
71 if (!m_ctx->is_bulk()) {
72 m_ctx->zblob_write_blobref(m_ctx->index()->get_field_off_pos(field_no),
73 &m_blob_mtr);
74 }
75
76 m_prev_page_no = page_get_page_no(blob_page);
77
78 /* Commit mtr and release uncompressed page frame to save memory.*/
79 blob_free(m_ctx->index(), m_cur_blob_block, false, mtr);
80
81 return (err);
82 }
83
84 /** For the given blob field, update its length in the blob reference
85 which is available in the clustered index record.
86 @param[in] field the concerned blob field. */
87 void zInserter::update_length_in_blobref(big_rec_field_t &field) {
88 /* After writing the last blob page, update the blob reference
89 with the correct length. */
90
91 const ulint field_no = field.field_no;
92 /* DO HERE */
93 byte *field_ref = btr_rec_get_field_ref(m_ctx->index(), m_ctx->rec(),
94 m_ctx->get_offsets(), field_no);
95
96 ref_t blobref(field_ref);
97 blobref.set_length(m_stream.total_in, nullptr);
98
99 if (!m_ctx->is_bulk()) {
100 m_ctx->zblob_write_blobref(field_no, &m_blob_mtr);
101 }
102 }
103
104 /** Write one small blob field data. Refer to ref_t to determine
105 the definition of small blob.
106 @param[in] blob_j the blob field number
107 @return DB_SUCCESS on success, error code on failure. */
108 dberr_t zInserter::write_one_small_blob(size_t blob_j) {
109 const big_rec_t *vec = m_ctx->get_big_rec_vec();
110 big_rec_field_t &field = vec->fields[blob_j];
111
112 int err = deflateReset(&m_stream);
113 ut_a(err == Z_OK);
114
115 m_stream.next_in = (Bytef *)field.data;
116 m_stream.avail_in = static_cast<uInt>(field.len);
117
118 err = write_first_page(field);
119
120 for (ulint nth_blob_page = 1; err == Z_OK; ++nth_blob_page) {
121 err = write_single_blob_page(field, nth_blob_page);
122 }
123
124 ut_ad(err == Z_STREAM_END);
125 m_ctx->make_nth_extern(field.field_no);
126 return (DB_SUCCESS);
127 }
128
129 /** Write one blob field data.
130 @param[in] blob_j the blob field number
131 @return DB_SUCCESS on success, error code on failure. */
132 dberr_t zInserter::write_one_blob(size_t blob_j) {
133 const big_rec_t *vec = m_ctx->get_big_rec_vec();
134 big_rec_field_t &field = vec->fields[blob_j];
135
136 int err = deflateReset(&m_stream);
137 ut_a(err == Z_OK);
138
139 m_stream.next_in = (Bytef *)field.data;
140 m_stream.avail_in = static_cast<uInt>(field.len);
141
142 m_ctx->check_redolog();
143
144 err = write_first_page(field);
145
146 for (ulint nth_blob_page = 1; err == Z_OK; ++nth_blob_page) {
147 const ulint commit_freq = 4;
148
149 err = write_single_blob_page(field, nth_blob_page);
150
151 if (nth_blob_page % commit_freq == 0) {
152 m_ctx->check_redolog();
153 }
154 }
155
156 ut_ad(err == Z_STREAM_END);
157 m_ctx->make_nth_extern(field.field_no);
158 return (DB_SUCCESS);
159 }
160
161 /** Write contents into a single BLOB page.
162 @return code as returned by zlib. */
163 int zInserter::write_into_single_page() {
164 const uint in_before = m_stream.avail_in;
165
166 mtr_t *const mtr = &m_blob_mtr;
167
168 /* Space available in compressed page to carry blob data */
169 const page_size_t page_size = m_ctx->page_size();
170 const uint payload_size_zip = page_size.physical() - FIL_PAGE_DATA;
171
172 page_t *blob_page = buf_block_get_frame(m_cur_blob_block);
173
174 m_stream.next_out = blob_page + FIL_PAGE_DATA;
175 m_stream.avail_out = static_cast<uInt>(payload_size_zip);
176
177 int err = deflate(&m_stream, Z_FINISH);
178 ut_a(err == Z_OK || err == Z_STREAM_END);
179 ut_a(err == Z_STREAM_END || m_stream.avail_out == 0);
180
181 const blob_page_info_t page_info(m_cur_blob_page_no,
182 in_before - m_stream.avail_in,
183 payload_size_zip - m_stream.avail_out);
184
185 add_to_blob_dir(page_info);
186
187 /* Write the "next BLOB page" pointer */
188 mlog_write_ulint(blob_page + FIL_PAGE_NEXT, FIL_NULL, MLOG_4BYTES, mtr);
189
190 /* Initialize the unused "prev page" pointer */
191 mlog_write_ulint(blob_page + FIL_PAGE_PREV, FIL_NULL, MLOG_4BYTES, mtr);
192
193 /* Write a back pointer to the record into the otherwise unused area.
194 This information could be useful in debugging. Later, we might want
195 to implement the possibility to relocate BLOB pages. Then, we would
196 need to be able to adjust the BLOB pointer in the record. We do not
197 store the heap number of the record, because it can change in
198 page_zip_reorganize() or btr_page_reorganize(). However, also the
199 page number of the record may change when B-tree nodes are split or
200 merged. */
201 mlog_write_ulint(blob_page + FIL_PAGE_FILE_FLUSH_LSN, m_ctx->space(),
202 MLOG_4BYTES, mtr);
203
204 mlog_write_ulint(blob_page + FIL_PAGE_FILE_FLUSH_LSN + 4,
205 m_ctx->get_page_no(), MLOG_4BYTES, mtr);
206
207 if (m_stream.avail_out > 0) {
208 /* Zero out the unused part of the page. */
209 memset(blob_page + page_zip_get_size(m_ctx->get_page_zip()) -
210 m_stream.avail_out,
211 0, m_stream.avail_out);
212 }
213
214 /* Redo log the page contents (the page is not modified). */
215 mlog_log_string(
216 blob_page + FIL_PAGE_FILE_FLUSH_LSN,
217 page_zip_get_size(m_ctx->get_page_zip()) - FIL_PAGE_FILE_FLUSH_LSN, mtr);
218
219 /* Copy the page to compressed storage, because it will be flushed
220 to disk from there. */
221 page_zip_des_t *blob_page_zip = buf_block_get_page_zip(m_cur_blob_block);
222
223 ut_ad(blob_page_zip);
224 ut_ad(page_zip_get_size(blob_page_zip) ==
225 page_zip_get_size(m_ctx->get_page_zip()));
226
227 page_zip_des_t *page_zip = buf_block_get_page_zip(m_ctx->block());
228 memcpy(blob_page_zip->data, blob_page, page_zip_get_size(page_zip));
229
230 return (err);
231 }
232
233 int zInserter::write_single_blob_page(big_rec_field_t &field,
234 ulint nth_blob_page) {
235 ut_ad(nth_blob_page > 0);
236
237 buf_block_t *rec_block = m_ctx->block();
238 mtr_t *mtr = start_blob_mtr();
239
240 buf_page_get(rec_block->page.id, rec_block->page.size, RW_X_LATCH,
241 UT_LOCATION_HERE, mtr);
242
243 buf_block_t *blob_block = alloc_blob_page();
244 page_t *blob_page = buf_block_get_frame(blob_block);
245
246 set_page_next();
247
248 m_prev_page_no = page_get_page_no(blob_page);
249
250 log_page_type(blob_page, nth_blob_page);
251
252 int err = write_into_single_page();
253
254 ut_ad(!dict_index_is_spatial(m_ctx->index()));
255
256 if (err == Z_STREAM_END) {
257 update_length_in_blobref(field);
258 }
259
260 /* Commit mtr and release uncompressed page frame to save memory.*/
261 blob_free(m_ctx->index(), m_cur_blob_block, false, mtr);
262
263 return (err);
264 }
265
266 /** Prepare to write a compressed BLOB. Setup the zlib
267 compression stream.
268 @return DB_SUCCESS on success, error code on failure. */
269 dberr_t zInserter::prepare() {
270 /* Zlib deflate needs 128 kilobytes for the default
271 window size, plus 512 << memLevel, plus a few
272 kilobytes for small objects. We use reduced memLevel
273 to limit the memory consumption, and preallocate the
274 heap, hoping to avoid memory fragmentation. */
275 m_heap = mem_heap_create(250000, UT_LOCATION_HERE);
276
277 if (m_heap == nullptr) {
278 return (DB_OUT_OF_MEMORY);
279 }
280
281 page_zip_set_alloc(&m_stream, m_heap);
282 int ret = deflateInit2(&m_stream, page_zip_level, Z_DEFLATED, 15, 7,
283 Z_DEFAULT_STRATEGY);
284 if (ret != Z_OK) {
285 return (DB_FAIL);
286 }
287
288 return (DB_SUCCESS);
289 }
290
291 /** Write all the BLOBs of the clustered index record.
292 @return DB_SUCCESS on success, error code on failure. */
293 dberr_t zInserter::write() {
294 /* Loop through each blob field of the record and write one blob
295 at a time.*/
296 for (ulint i = 0; i < m_ctx->get_big_rec_vec_size() && m_err == DB_SUCCESS;
297 i++) {
298 ut_d(m_dir.clear(););
299 m_err = write_one_blob(i);
300 }
301
302 return (m_err);
303 }
304
305 /** Make the current page as next page of previous page. In other
306 words, make the page m_cur_blob_page_no as the next page
307 (FIL_PAGE_NEXT) of page m_prev_page_no.
308 @return DB_SUCCESS on success, or error code on failure. */
309 dberr_t zInserter::set_page_next() {
310 buf_block_t *prev_block = get_previous_blob_block();
311 page_t *prev_page = buf_block_get_frame(prev_block);
312
313 mlog_write_ulint(prev_page + FIL_PAGE_NEXT, m_cur_blob_page_no, MLOG_4BYTES,
314 &m_blob_mtr);
315
316 memcpy(buf_block_get_page_zip(prev_block)->data + FIL_PAGE_NEXT,
317 prev_page + FIL_PAGE_NEXT, 4);
318
319 return (m_err);
320 }
321
322 } // namespace lob