Debian Codegraph @

storage/innobase/lob/zlob0update.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 <table.h>
29 #include <memory>
30 #include "db0err.h"
31 #include "field.h"
32 #include "fil0fil.h"
33 #include "fut0lst.h"
34 #include "lob0impl.h"
35 #include "lob0lob.h"
36 #include "row0upd.h"
37 #include "trx0trx.h"
38 #include "zlob0first.h"
39 #include "zlob0read.h"
40
41 namespace lob {
42
43 /** Replace a large object (LOB) with the given new data.
44 @param[in] ctx replace operation context.
45 @param[in] trx the transaction that is doing the read.
46 @param[in] index the clust index that contains the LOB.
47 @param[in] ref the LOB reference identifying the LOB.
48 @param[in] first_page the first page of the LOB.
49 @param[in] offset replace the LOB from the given offset.
50 @param[in] len the length of LOB data that needs to be
51 replaced.
52 @param[in] buf the buffer (owned by caller) with new data
53 (len bytes).
54 @return DB_SUCCESS on success, error code on failure. */
55 static dberr_t z_replace(InsertContext &ctx, trx_t *trx, dict_index_t *index,
56 ref_t ref, z_first_page_t &first_page, ulint offset,
57 ulint len, byte *buf);
58
59 #ifdef UNIV_DEBUG
60 /** Print an information message in the server log file, informing
61 that the ZLOB partial update feature code is hit.
62 @param[in] uf the update field information
63 @param[in] index index where partial update happens.*/
64 static void z_print_partial_update_hit(const upd_field_t *uf,
65 const dict_index_t *index) {
66 ib::info(ER_IB_MSG_633) << "ZLOB partial update of field=("
67 << uf->mysql_field->field_name << ") on index=("
68 << index->name << ") in table=(" << index->table_name
69 << ")";
70 }
71 #endif /* UNIV_DEBUG */
72
73 /** Update a portion of the given LOB.
74 @param[in] ctx update operation context information.
75 @param[in] trx the transaction that is doing the modification.
76 @param[in] index the clustered index containing the LOB.
77 @param[in] upd update vector
78 @param[in] field_no the LOB field number
79 @param[in] blobref LOB reference stored in clust record.
80 @return DB_SUCCESS on success, error code on failure. */
81 dberr_t z_update(InsertContext &ctx, trx_t *trx, dict_index_t *index,
82 const upd_t *upd, ulint field_no, ref_t blobref) {
83 DBUG_TRACE;
84 dberr_t err = DB_SUCCESS;
85 mtr_t *mtr = ctx.get_mtr();
86
87 const Binary_diff_vector *bdiff_vector =
88 upd->get_binary_diff_by_field_no(field_no);
89
90 const upd_field_t *const uf = upd->get_field_by_field_no(field_no, index);
91
92 #ifdef UNIV_DEBUG
93 /* Print information on server error log file, which can be
94 used to confirm if InnoDB did partial update or not. */
95 DBUG_EXECUTE_IF("zlob_print_partial_update_hit",
96 z_print_partial_update_hit(uf, index););
97 #endif /* UNIV_DEBUG */
98 page_no_t first_page_no = blobref.page_no();
99 space_id_t space_id = blobref.space_id();
100 const page_size_t page_size = dict_table_page_size(index->table);
101 const page_id_t first_page_id(space_id, first_page_no);
102
103 z_first_page_t first_page(mtr, index);
104 first_page.load_x(first_page_id, page_size);
105
106 ut_ad(first_page.get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST);
107
108 const uint32_t lob_version = first_page.incr_lob_version();
109
110 for (Binary_diff_vector::const_iterator iter = bdiff_vector->begin();
111 iter != bdiff_vector->end(); ++iter) {
112 const Binary_diff *bdiff = iter;
113
114 err = z_replace(ctx, trx, index, blobref, first_page, bdiff->offset(),
115 bdiff->length(), (byte *)bdiff->new_data(uf->mysql_field));
116
117 if (err != DB_SUCCESS) {
118 break;
119 }
120 }
121
122 blobref.set_offset(lob_version, nullptr);
123
124 if (!ctx.is_bulk()) {
125 ctx.zblob_write_blobref(field_no, ctx.m_mtr);
126 }
127
128 return err;
129 }
130
131 /** Find the location of the given offset within LOB.
132 @param[in] index The index where LOB is located.
133 @param[in] node_loc The location of first page.
134 @param[in,out] offset The requested offset.
135 @param[in] mtr Mini-transaction context.
136 @return the file address of requested offset or fil_addr_null. */
137 fil_addr_t z_find_offset(dict_index_t *index, fil_addr_t node_loc,
138 ulint &offset, mtr_t *mtr) {
139 space_id_t space = dict_index_get_space(index);
140 const page_size_t page_size = dict_table_page_size(index->table);
141
142 while (!fil_addr_is_null(node_loc)) {
143 flst_node_t *node =
144 fut_get_ptr(space, page_size, node_loc, RW_X_LATCH, mtr);
145
146 z_index_entry_t entry(node, mtr, index);
147
148 /* Get the amount of data */
149 ulint data_len = entry.get_data_len();
150
151 if (offset < data_len) {
152 break;
153 }
154
155 offset -= data_len;
156
157 /* The next node should not be the same as the current node. */
158 ut_ad(!node_loc.is_equal(entry.get_next()));
159
160 node_loc = flst_get_next_addr(node, mtr);
161 }
162
163 return (node_loc);
164 }
165
166 /** Replace a large object (LOB) with the given new data.
167 @param[in] ctx replace operation context.
168 @param[in] trx the transaction that is doing the read.
169 @param[in] index the clust index that contains the LOB.
170 @param[in] ref the LOB reference identifying the LOB.
171 @param[in] first_page the first page of the LOB.
172 @param[in] offset replace the LOB from the given offset.
173 @param[in] len the length of LOB data that needs to be
174 replaced.
175 @param[in] buf the buffer (owned by caller) with new data
176 (len bytes).
177 @return DB_SUCCESS on success, error code on failure. */
178 static dberr_t z_replace(InsertContext &ctx, trx_t *trx, dict_index_t *index,
179 ref_t ref, z_first_page_t &first_page, ulint offset,
180 ulint len, byte *buf) {
181 DBUG_TRACE;
182 dberr_t ret(DB_SUCCESS);
183 trx_id_t trxid = (trx == nullptr) ? 0 : trx->id;
184 const undo_no_t undo_no = (trx == nullptr ? 0 : trx->undo_no - 1);
185 const uint32_t lob_version = first_page.get_lob_version();
186
187 ut_ad(offset < ref.length());
188
189 ut_ad(dict_table_is_comp(index->table));
190
191 #ifdef LOB_DEBUG
192 std::cout << "thread=" << std::this_thread::get_id()
193 << ", lob::z_replace(): table=" << index->table->name
194 << ", ref=" << ref << ", trx->id=" << trx->id << std::endl;
195 #endif /* LOB_DEBUG */
196
197 mtr_t *mtr = ctx.get_mtr();
198
199 page_no_t first_page_no = ref.page_no();
200 space_id_t space_id = ref.space_id();
201 const page_id_t first_page_id(space_id, first_page_no);
202
203 first_page.set_last_trx_id(trx->id);
204 first_page.set_last_trx_undo_no(undo_no);
205
206 ut_ad(first_page.get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST);
207
208 flst_base_node_t *base_node = first_page.index_list();
209 fil_addr_t node_loc = flst_get_first(base_node, mtr);
210
211 ulint yet_to_skip = offset;
212 node_loc = z_find_offset(index, node_loc, yet_to_skip, mtr);
213
214 ut_ad(!node_loc.is_null());
215
216 /* The current entry - it is the latest version. */
217 z_index_entry_t cur_entry(mtr, index);
218 z_index_entry_t new_entry(mtr, index);
219
220 /* The cur_entry points to the chunk that needs to be
221 partially replaced. */
222 std::unique_ptr<byte[]> tmp(new (std::nothrow) byte[Z_CHUNK_SIZE]);
223
224 if (tmp == nullptr) {
225 return (DB_OUT_OF_MEMORY);
226 }
227
228 byte *chunk = tmp.get();
229 ut_ad(yet_to_skip < Z_CHUNK_SIZE);
230
231 byte *ptr = chunk;
232 byte *from_ptr = buf;
233 ulint replace_len = len; /* bytes remaining to be replaced. */
234
235 /* Replace remaining. */
236 while (replace_len > 0 && !fil_addr_is_null(node_loc)) {
237 cur_entry.load_x(node_loc);
238
239 ulint size = cur_entry.get_data_len();
240 ut_ad(size > yet_to_skip);
241 ulint avail = size - yet_to_skip;
242
243 if (yet_to_skip > 0 || replace_len < avail) {
244 /* Only partial chunk is to be replaced. Read old
245 data. */
246 ulint read_len = size;
247 ptr = chunk;
248
249 ulint len1 = z_read_chunk(index, cur_entry, 0, read_len, ptr, mtr);
250
251 ut_ad(len1 == cur_entry.get_data_len());
252 ut_ad(read_len == 0);
253 ut_ad(len1 == size);
254
255 ptr = chunk;
256 ptr += yet_to_skip;
257 const ulint can_replace = (replace_len > avail ? avail : replace_len);
258 memcpy(ptr, from_ptr, can_replace);
259 replace_len -= can_replace;
260 from_ptr += can_replace;
261 ptr += can_replace;
262
263 #ifdef UNIV_DEBUG
264 if (can_replace < avail) {
265 const ulint rest_of_data = avail - can_replace;
266 ut_ad(size == (yet_to_skip + can_replace + rest_of_data));
267 } else {
268 ut_ad(size == (yet_to_skip + can_replace));
269 }
270 #endif /* UNIV_DEBUG */
271
272 ut_ad(first_page.get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST);
273
274 /* Chunk now contains new data to be inserted. */
275 ret = z_insert_chunk(index, first_page, trx, chunk, len1, &new_entry, mtr,
276 false);
277
278 if (ret != DB_SUCCESS) {
279 return (ret);
280 }
281
282 cur_entry.insert_after(base_node, new_entry);
283 cur_entry.remove(base_node);
284 cur_entry.set_trx_id_modifier(trxid);
285 cur_entry.set_trx_undo_no_modifier(undo_no);
286 new_entry.set_old_version(cur_entry);
287 new_entry.set_lob_version(lob_version);
288 yet_to_skip = 0;
289
290 } else {
291 ut_ad(yet_to_skip == 0);
292 ut_ad(replace_len >= avail);
293 ut_ad(avail == size);
294
295 /* Full chunk is to be replaced. No need to read
296 old data. */
297 ret = z_insert_chunk(index, first_page, trx, from_ptr, size, &new_entry,
298 mtr, false);
299
300 if (ret != DB_SUCCESS) {
301 return (ret);
302 }
303
304 ut_ad(new_entry.get_trx_id() == trx->id);
305
306 from_ptr += size;
307 ut_a(size <= replace_len);
308 replace_len -= size;
309
310 cur_entry.set_trx_id_modifier(trxid);
311 cur_entry.set_trx_undo_no_modifier(undo_no);
312 cur_entry.insert_after(base_node, new_entry);
313 cur_entry.remove(base_node);
314 new_entry.set_old_version(cur_entry);
315 new_entry.set_lob_version(lob_version);
316 }
317
318 node_loc = new_entry.get_next();
319 new_entry.reset(nullptr);
320 cur_entry.reset(nullptr);
321 }
322
323 ut_ad(replace_len == 0);
324 ut_ad(first_page.get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST);
325
326 #ifdef LOB_DEBUG
327 first_page.print_index_entries(std::cout);
328 ut_ad(first_page.validate());
329 #endif /* LOB_DEBUG */
330
331 return ret;
332 }
333
334 } /* namespace lob */