Debian Codegraph @

storage/innobase/lob/lob0ins.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 "lob0ins.h"
29 #include "buf0buf.h"
30
31 namespace lob {
32
33 /** Allocate one BLOB page.
34 @return the allocated block of the BLOB page. */
35 buf_block_t *BaseInserter::alloc_blob_page() {
36 ulint r_extents;
37 mtr_t mtr_bulk;
38 mtr_t *alloc_mtr;
39
40 ut_ad(fsp_check_tablespace_size(m_ctx->space()));
41
42 if (m_ctx->is_bulk()) {
43 mtr_start(&mtr_bulk);
44 alloc_mtr = &mtr_bulk;
45 } else {
46 alloc_mtr = &m_blob_mtr;
47 }
48
49 page_no_t hint_page_no = m_prev_page_no + 1;
50
51 if (!fsp_reserve_free_extents(&r_extents, m_ctx->space(), 1, FSP_BLOB,
52 alloc_mtr, 1)) {
53 alloc_mtr->commit();
54 m_err = DB_OUT_OF_FILE_SPACE;
55 return (nullptr);
56 }
57
58 m_cur_blob_block = btr_page_alloc(m_ctx->index(), hint_page_no, FSP_NO_DIR, 0,
59 alloc_mtr, &m_blob_mtr);
60
61 fil_space_release_free_extents(m_ctx->space(), r_extents);
62
63 if (m_ctx->is_bulk()) {
64 alloc_mtr->commit();
65 }
66
67 m_cur_blob_page_no = page_get_page_no(buf_block_get_frame(m_cur_blob_block));
68
69 return (m_cur_blob_block);
70 }
71
72 /** Get the previous BLOB page block. This will return a BLOB block.
73 It should not be called for the first BLOB page, because it will not
74 have a previous BLOB page.
75 @return the previous BLOB block. */
76 buf_block_t *BaseInserter::get_previous_blob_block() {
77 DBUG_TRACE;
78
79 DBUG_LOG("lob", "m_prev_page_no=" << m_prev_page_no);
80 ut_ad(m_prev_page_no != m_ctx->get_page_no());
81
82 space_id_t space_id = m_ctx->space();
83 buf_block_t *rec_block = m_ctx->block();
84
85 buf_block_t *prev_block =
86 buf_page_get(page_id_t(space_id, m_prev_page_no), rec_block->page.size,
87 RW_X_LATCH, UT_LOCATION_HERE, &m_blob_mtr);
88
89 buf_block_dbg_add_level(prev_block, SYNC_EXTERN_STORAGE);
90
91 return prev_block;
92 }
93
94 /** Get the previous BLOB page frame. This will return a BLOB page.
95 It should not be called for the first BLOB page, because it will not
96 have a previous BLOB page.
97 @return the previous BLOB page frame. */
98 page_t *BaseInserter::get_previous_blob_page() {
99 buf_block_t *prev_block = get_previous_blob_block();
100 return (buf_block_get_frame(prev_block));
101 }
102
103 /** Write all the BLOBs of the clustered index record.
104 @return DB_SUCCESS on success, error code on failure. */
105 dberr_t Inserter::write() {
106 /* Loop through each blob field of the record and write one blob
107 at a time. */
108 for (ulint i = 0; i < m_ctx->get_big_rec_vec_size() && is_ok(); i++) {
109 ut_d(m_dir.clear(););
110 m_err = write_one_blob(i);
111
112 DBUG_EXECUTE_IF("btr_store_big_rec_extern", m_err = DB_OUT_OF_FILE_SPACE;);
113 }
114
115 ut_ad(m_err != DB_SUCCESS || m_ctx->are_all_blobrefs_valid());
116
117 return (m_err);
118 }
119
120 /** Write one small blob field data.
121 @param[in] blob_j the blob field number
122 @return DB_SUCCESS on success, error code on failure. */
123 dberr_t Inserter::write_one_small_blob(size_t blob_j) {
124 const big_rec_t *vec = m_ctx->get_big_rec_vec();
125 big_rec_field_t &field = vec->fields[blob_j];
126
127 m_err = write_first_page(field);
128
129 for (ulint nth_blob_page = 1; is_ok() && m_remaining > 0; ++nth_blob_page) {
130 m_err = write_single_blob_page(field, nth_blob_page);
131 }
132
133 m_ctx->make_nth_extern(field.field_no);
134
135 ut_ad(m_remaining == 0);
136
137 return (m_err);
138 }
139
140 /** Write one blob field data.
141 @param[in] blob_j the blob field number
142 @return DB_SUCCESS on success, error code on failure. */
143 dberr_t Inserter::write_one_blob(size_t blob_j) {
144 const big_rec_t *vec = m_ctx->get_big_rec_vec();
145 big_rec_field_t &field = vec->fields[blob_j];
146
147 m_ctx->check_redolog();
148
149 m_err = write_first_page(field);
150
151 for (ulint nth_blob_page = 1; is_ok() && m_remaining > 0; ++nth_blob_page) {
152 const ulint commit_freq = 4;
153
154 if (nth_blob_page % commit_freq == 0) {
155 m_ctx->check_redolog();
156 }
157
158 m_err = write_single_blob_page(field, nth_blob_page);
159 }
160
161 m_ctx->make_nth_extern(field.field_no);
162
163 ut_ad(m_remaining == 0);
164
165 return (m_err);
166 }
167
168 /** Make the current page as next page of previous page. In other
169 words, make the page m_cur_blob_page_no as the next page of page
170 m_prev_page_no. */
171 void Inserter::set_page_next() {
172 page_t *prev_page = get_previous_blob_page();
173
174 mlog_write_ulint(prev_page + FIL_PAGE_DATA + LOB_HDR_NEXT_PAGE_NO,
175 m_cur_blob_page_no, MLOG_4BYTES, &m_blob_mtr);
176 }
177
178 dberr_t Inserter::write_first_page(big_rec_field_t &field) {
179 buf_block_t *rec_block = m_ctx->block();
180 mtr_t *mtr = start_blob_mtr();
181
182 buf_page_get(rec_block->page.id, rec_block->page.size, RW_X_LATCH,
183 UT_LOCATION_HERE, mtr);
184
185 alloc_blob_page();
186
187 if (dict_index_is_online_ddl(m_ctx->index())) {
188 row_log_table_blob_alloc(m_ctx->index(), m_cur_blob_page_no);
189 }
190
191 log_page_type();
192
193 m_remaining = field.len;
194 write_into_single_page(field);
195
196 const ulint field_no = field.field_no;
197 byte *field_ref = btr_rec_get_field_ref(m_ctx->index(), m_ctx->rec(),
198 m_ctx->get_offsets(), field_no);
199 ref_t blobref(field_ref);
200
201 blobref.set_length(field.len - m_remaining, mtr);
202 blobref.update(m_ctx->space(), m_cur_blob_page_no, FIL_PAGE_DATA, mtr);
203
204 m_prev_page_no = m_cur_blob_page_no;
205
206 mtr->commit();
207
208 return (m_err);
209 }
210
211 dberr_t Inserter::write_single_blob_page(big_rec_field_t &field,
212 ulint nth_blob_page) {
213 buf_block_t *rec_block = m_ctx->block();
214 mtr_t *mtr = start_blob_mtr();
215 ut_a(nth_blob_page > 0);
216
217 buf_page_get(rec_block->page.id, rec_block->page.size, RW_X_LATCH,
218 UT_LOCATION_HERE, mtr);
219
220 alloc_blob_page();
221 set_page_next();
222 log_page_type();
223 write_into_single_page(field);
224 const ulint field_no = field.field_no;
225 byte *field_ref = btr_rec_get_field_ref(m_ctx->index(), m_ctx->rec(),
226 m_ctx->get_offsets(), field_no);
227 ref_t blobref(field_ref);
228 blobref.set_length(field.len - m_remaining, mtr);
229 m_prev_page_no = m_cur_blob_page_no;
230 mtr->commit();
231
232 return (m_err);
233 }
234
235 /** Write contents into a single BLOB page.
236 @param[in] field the big record field. */
237 void Inserter::write_into_single_page(big_rec_field_t &field) {
238 const ulint payload_size = payload();
239 const ulint store_len =
240 (m_remaining > payload_size) ? payload_size : m_remaining;
241
242 page_t *page = buf_block_get_frame(m_cur_blob_block);
243
244 mlog_write_string(page + FIL_PAGE_DATA + LOB_HDR_SIZE,
245 (const byte *)field.data + field.len - m_remaining,
246 store_len, &m_blob_mtr);
247
248 mlog_write_ulint(page + FIL_PAGE_DATA + LOB_HDR_PART_LEN, store_len,
249 MLOG_4BYTES, &m_blob_mtr);
250
251 mlog_write_ulint(page + FIL_PAGE_DATA + LOB_HDR_NEXT_PAGE_NO, FIL_NULL,
252 MLOG_4BYTES, &m_blob_mtr);
253
254 m_remaining -= store_len;
255 }
256
257 } // namespace lob