Debian Codegraph @

unittest/gunit/innodb/lob/zlob0int-t.cc

raw symbols SCIP
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 "my_config.h"
29
30 #include <memory>
31
32 #include <gtest/gtest.h>
33
34 #include "db0err.h"
35 #include "lot0dat.h"
36 #include "lot0lob.h"
37 #include "zlob0int.h"
38
39 #define Fname(x) const char *fname = x;
40 #define LOG(x) \
41 { std::cout << fname << ":" << x << std::endl; }
42
43 using namespace zlob;
44
45 void index_entry_test_00() {
46 std::unique_ptr<byte[]> ptr(new byte[300]);
47 z_index_entry_t ie(ptr.get());
48 ie.init();
49 std::cout << ie << std::endl;
50 std::cout << "Size of one z_index_entry_t: " << ie.SIZE << std::endl;
51 }
52
53 void frag_entry_test_00() {
54 std::unique_ptr<byte[]> ptr(new byte[300]);
55 z_frag_entry_t fe(ptr.get());
56 fe.init();
57 std::cout << fe << std::endl;
58 std::cout << "Size of one z_frag_entry_t: " << fe.SIZE << std::endl;
59 }
60
61 void z_first_page_basic_test() {
62 z_first_page_t first;
63 std::cout << "Size of index entries=" << first.size_of_index_entries()
64 << std::endl;
65 std::cout << "Size of frag entries=" << first.size_of_frag_entries()
66 << std::endl;
67 std::cout << "Payload=" << first.payload() << std::endl;
68 first.alloc();
69
70 std::cout << first << std::endl;
71 }
72
73 void basic_insert_test(ulint size) {
74 lobid_t lobid;
75 byte *lob = lob_data::generate_lob(&lobid, size);
76
77 trx_id_t trxid = 28;
78
79 lob::ref_t ref;
80 ref.m_id = lobid;
81 dberr_t err = zlob::z_insert(trxid, ref, lob, size);
82 ASSERT_TRUE(err == DB_SUCCESS);
83
84 std::unique_ptr<byte[]> buf(new byte[size]);
85 ulint n = zlob::z_read(trxid, ref, 0, size, buf.get());
86 ASSERT_EQ(n, size);
87 ASSERT_TRUE(memcmp(lob, buf.get(), size) == 0);
88
89 lob_data::remove_lob(lobid);
90 buf_pool_reset();
91 }
92
93 void basic_insert_test_2() {
94 lobid_t lobid;
95 std::pair<byte *, ulint> result = lob_data::get_lob(&lobid, "earth215kb.jpg");
96
97 byte *lob = result.first;
98 ulint size = result.second;
99
100 std::cout << "lobid=" << lobid << ", size=" << size << std::endl;
101
102 trx_id_t trxid = 30;
103
104 lob::ref_t ref;
105 ref.m_id = lobid;
106 dberr_t err = zlob::z_insert(trxid, ref, lob, size);
107 ut_a(err == DB_SUCCESS);
108
109 zlob::z_print_info(ref, std::cout);
110
111 lob_data::remove_lob(lobid);
112 }
113
114 void basic_insert_read_test() {
115 lobid_t lobid;
116 std::pair<byte *, ulint> result =
117 lob_data::get_lob(&lobid, "/home/innodb/x.avi");
118
119 byte *lob = result.first;
120 ulint size = result.second;
121
122 std::cout << "lobid=" << lobid << ", size=" << size << std::endl;
123
124 trx_id_t trxid = 50;
125
126 lob::ref_t ref;
127 ref.m_id = lobid;
128 dberr_t err = zlob::z_insert(trxid, ref, lob, size);
129 ut_a(err == DB_SUCCESS);
130
131 zlob::z_print_info(ref, std::cout);
132
133 std::unique_ptr<byte[]> buf(new byte[size]);
134
135 ulint n = zlob::z_read(trxid, ref, 0, size, buf.get());
136 ut_a(n == size);
137 ut_a(memcmp(lob, buf.get(), size) == 0);
138 lob_data::remove_lob(lobid);
139 }
140
141 void z_replace_generic(ulint size, ulint offset, ulint replace_len) {
142 Fname("z_replace_generic");
143
144 LOG("size=" << size << ", offset=" << offset << ", len=" << replace_len);
145
146 const ulint SIZE = size;
147 lobid_t lobid;
148 byte *lob = lob_data::generate_lob(&lobid, SIZE);
149
150 /* Ensure that the requested replace length is within limits. */
151 ulint can_be_replaced = SIZE - offset;
152
153 /* alen is for actual replace length. */
154 ulint alen = replace_len;
155 if (replace_len > can_be_replaced) {
156 alen = can_be_replaced;
157 }
158
159 trx_id_t trx1 = 100;
160
161 /* Insert the LOB */
162 lob::ref_t ref;
163 ref.m_id = lobid;
164 zlob::z_insert(trx1, ref, lob, SIZE);
165
166 /* Fetch the LOB that has been inserted. */
167 ulint fetch_offset = 0;
168 ulint fetch_bytes = SIZE;
169 std::unique_ptr<byte[]> buf(new byte[fetch_bytes]);
170 zlob::z_read(trx1, ref, fetch_offset, fetch_bytes, buf.get());
171 ut_ad(memcmp(buf.get(), lob, SIZE) == 0);
172
173 /* Replace */
174 lobid_t lobid2;
175 trx_id_t trx2 = 300;
176 ulint replace_offset = offset;
177 byte *replace_lob = lob_data::generate_lob(&lobid2, '|', replace_len);
178 ulint l1 =
179 zlob::z_replace(trx2, ref, replace_offset, replace_len, replace_lob);
180
181 if (l1 != alen) {
182 ut_ad(0);
183 }
184
185 ASSERT_TRUE(l1 == alen);
186
187 /* Fetch the older LOB that has been originally inserted. */
188 trx_id_t trx3 = 250;
189 std::unique_ptr<byte[]> buf2(new byte[SIZE]);
190 zlob::z_read(trx3, ref, fetch_offset, fetch_bytes, buf2.get());
191
192 if (memcmp(buf2.get(), lob, SIZE) != 0) {
193 ulint i = 0;
194
195 for (i = 0; i < SIZE; ++i) {
196 if (lob[i] != buf2.get()[i]) {
197 break;
198 }
199 }
200
201 std::cout << i << std::endl;
202 std::cout << replace_lob << std::endl;
203 std::cout << lob << std::endl;
204 std::cout << buf2.get() << std::endl;
205 }
206
207 ut_ad(memcmp(buf2.get(), lob, SIZE) == 0);
208
209 /* Fetch the newer LOB that has been replaced. */
210 trx_id_t trx4 = 350;
211 memset(buf2.get(), '\0', SIZE);
212 ulint len = zlob::z_read(trx4, ref, fetch_offset, fetch_bytes, buf2.get());
213
214 ut_ad(len == fetch_bytes);
215
216 // Compare the initial bytes.
217 ut_ad(memcmp(buf2.get(), lob, replace_offset) == 0);
218
219 // Compare the replaced bytes.
220 ut_ad(memcmp(buf2.get() + replace_offset, replace_lob, alen) == 0);
221
222 // Compare the trailer
223 ulint trailer_len = SIZE - replace_offset - alen;
224 ut_ad(memcmp(buf2.get() + replace_offset + alen, lob + replace_offset + alen,
225 trailer_len) == 0);
226
227 lob_data::remove_lob(lobid);
228 lob_data::remove_lob(lobid2);
229 buf_pool_reset();
230 }
231
232 void z_insert_middle_generic(ulint size, ulint offset, ulint insert_len) {
233 Fname("z_insert_middle_generic");
234 const ulint SIZE = size;
235 lobid_t lobid;
236 byte *lob = lob_data::generate_lob(&lobid, SIZE);
237
238 LOG("lob_size=" << size << ", offset=" << offset << ", len=" << insert_len);
239
240 trx_id_t trx1 = 100;
241
242 /* Insert the LOB */
243 lob::ref_t ref;
244 ref.m_id = lobid;
245 zlob::z_insert(trx1, ref, lob, SIZE);
246
247 /* Fetch the LOB that has been inserted. */
248 ulint fetch_offset = 0;
249 ulint fetch_bytes = SIZE;
250 std::unique_ptr<byte[]> buf(new byte[fetch_bytes]);
251 zlob::z_read(trx1, ref, fetch_offset, fetch_bytes, buf.get());
252
253 ASSERT_TRUE(memcmp(buf.get(), lob, SIZE) == 0);
254
255 /* Insert middle */
256 lobid_t lobid2;
257 trx_id_t trx2 = 300;
258 ulint insert_offset = offset;
259 byte *insert_lob = lob_data::generate_lob(&lobid2, '|', insert_len);
260 zlob::z_insert_middle(trx2, ref, insert_offset, insert_lob, insert_len);
261
262 /* Fetch the older LOB that has been originally inserted. */
263 trx_id_t trx3 = 250;
264 std::unique_ptr<byte[]> buf2(new byte[SIZE]);
265 zlob::z_read(trx3, ref, fetch_offset, fetch_bytes, buf2.get());
266
267 ASSERT_TRUE(memcmp(buf2.get(), lob, SIZE) == 0);
268
269 /* Fetch the newer LOB that has been enlarged. */
270 trx_id_t trx4 = 350;
271 ulint new_size = SIZE + insert_len;
272 std::unique_ptr<byte[]> buf3(new byte[new_size]);
273 memset(buf3.get(), '\0', new_size);
274 ulint len = zlob::z_read(trx4, ref, 0, new_size, buf3.get());
275 ASSERT_TRUE(len == new_size);
276
277 // Compare the initial bytes.
278 ASSERT_TRUE(memcmp(buf3.get(), lob, offset) == 0);
279
280 // Compare the inserted bytes.
281 if (memcmp(buf3.get() + offset, insert_lob, insert_len) != 0) {
282 std::cout << buf3.get() << std::endl;
283 }
284
285 ASSERT_TRUE(memcmp(buf3.get() + offset, insert_lob, insert_len) == 0);
286
287 // Compare the trailer
288 ulint trailer_len = SIZE - offset;
289 ASSERT_TRUE(
290 memcmp(buf3.get() + offset + insert_len, lob + offset, trailer_len) == 0);
291
292 lob_data::remove_lob(lobid);
293 lob_data::remove_lob(lobid2);
294 buf_pool_reset();
295 }
296
297 void z_remove_middle_generic(ulint size, ulint offset, ulint remove_len) {
298 Fname("z_remove_middle_generic");
299
300 LOG("size=" << size << ", offset=" << offset << ", len=" << remove_len);
301 const ulint SIZE = size;
302 lobid_t lobid;
303 byte *lob = lob_data::generate_lob(&lobid, SIZE);
304
305 trx_id_t trx1 = 100;
306
307 /* Insert the LOB */
308 lob::ref_t ref;
309 ref.m_id = lobid;
310 zlob::z_insert(trx1, ref, lob, SIZE);
311
312 /* Fetch the LOB that has been inserted. */
313 ulint fetch_offset = 0;
314 ulint fetch_bytes = SIZE;
315 std::unique_ptr<byte[]> buf(new byte[fetch_bytes]);
316 zlob::z_read(trx1, ref, fetch_offset, fetch_bytes, buf.get());
317
318 ASSERT_TRUE(memcmp(buf.get(), lob, SIZE) == 0);
319
320 /* Remove middle */
321 trx_id_t trx2 = 300;
322 zlob::z_remove_middle(trx2, ref, offset, remove_len);
323
324 /* Fetch the older LOB that has been originally inserted. */
325 trx_id_t trx3 = 250;
326 std::unique_ptr<byte[]> buf2(new byte[SIZE]);
327 zlob::z_read(trx3, ref, fetch_offset, fetch_bytes, buf2.get());
328
329 if (memcmp(buf2.get(), lob, SIZE) != 0) {
330 std::cout << lob << std::endl;
331 std::cout << buf2.get() << std::endl;
332 }
333 ASSERT_TRUE(memcmp(buf2.get(), lob, SIZE) == 0);
334
335 /* Fetch the newer LOB that has been shortened. */
336 trx_id_t trx4 = 350;
337
338 /* If there is request to remove more than available data, adjust it
339 properly. */
340 ulint can_delete = (SIZE - offset);
341 ulint actually_deleted = remove_len > can_delete ? can_delete : remove_len;
342 ulint new_size = SIZE - actually_deleted;
343 std::unique_ptr<byte[]> buf3(new byte[new_size]);
344 memset(buf3.get(), '\0', new_size);
345 ulint len = zlob::z_read(trx4, ref, 0, new_size, buf3.get());
346 ASSERT_EQ(len, new_size);
347
348 ASSERT_TRUE(memcmp(buf3.get(), lob, offset) == 0);
349
350 // Compare the trailer
351 ulint trailer_len = SIZE - offset - actually_deleted;
352 if (memcmp(buf3.get() + offset, lob + offset + actually_deleted,
353 trailer_len) != 0) {
354 std::cout << buf3.get() << std::endl;
355 std::cout << lob << std::endl;
356 ut_ad(0);
357 }
358 ASSERT_TRUE(memcmp(buf3.get() + offset, lob + offset + actually_deleted,
359 trailer_len) == 0);
360
361 zlob::z_purge(300, ref);
362
363 trx_id_t trx5 = 500;
364
365 /* Fetch the LOB that has been inserted. */
366 memset(buf.get(), 0x00, SIZE);
367 zlob::z_read(trx5, ref, 0, SIZE, buf.get());
368 ASSERT_TRUE(memcmp(buf.get(), lob, SIZE) == 0);
369
370 lob_data::remove_lob(lobid);
371 buf_pool_reset();
372 }
373
374 TEST(z_insert, StressUptoMB1) {
375 for (ulint size = 0; size < MB1; size += KB1) {
376 basic_insert_test(size);
377 }
378 }
379
380 TEST(z_insert, StressUptoMB100) {
381 for (ulint size = 0; size < MB100; size += MB10) {
382 basic_insert_test(size);
383 }
384 }
385
386 TEST(z_insert_middle, InsertFront) {
387 const ulint offset = 0;
388 ulint lob_size;
389 ulint insert_length;
390
391 lob_size = KB1;
392 insert_length = MB1;
393 z_insert_middle_generic(lob_size, offset, insert_length);
394
395 lob_size = 10;
396 insert_length = 10;
397 z_insert_middle_generic(lob_size, offset, insert_length);
398
399 lob_size = 10;
400 insert_length = MB5;
401 z_insert_middle_generic(lob_size, offset, insert_length);
402
403 lob_size = MB10;
404 insert_length = MB10;
405 z_insert_middle_generic(lob_size, offset, insert_length);
406 }
407
408 TEST(z_insert_middle, Append) {
409 ulint offset = 0;
410 ulint lob_size;
411 ulint insert_length;
412
413 lob_size = KB1;
414 offset = lob_size;
415 insert_length = MB1;
416 z_insert_middle_generic(lob_size, offset, insert_length);
417
418 lob_size = 10;
419 offset = lob_size;
420 insert_length = 10;
421 z_insert_middle_generic(lob_size, offset, insert_length);
422
423 lob_size = 10;
424 offset = lob_size;
425 insert_length = MB5;
426 z_insert_middle_generic(lob_size, offset, insert_length);
427
428 lob_size = MB10;
429 offset = lob_size;
430 insert_length = MB10;
431 z_insert_middle_generic(lob_size, offset, insert_length);
432 }
433
434 TEST(z_insert_middle, StressUptoKB300) {
435 const ulint lob_size = MB1;
436 const ulint insert_length = KB128;
437
438 for (ulint offset = 1; offset <= lob_size; offset += KB1) {
439 z_insert_middle_generic(lob_size, offset, insert_length);
440 }
441 }
442
443 TEST(z_remove_middle, reallymiddle) { z_remove_middle_generic(MB5, MB1, MB1); }
444
445 TEST(z_remove_middle, PopFront) {
446 const ulint offset = 0;
447 ulint lob_size = MB5;
448 ulint remove_len = MB1;
449 z_remove_middle_generic(lob_size, offset, remove_len);
450
451 lob_size = MB4;
452 remove_len = MB2;
453 z_remove_middle_generic(lob_size, offset, remove_len);
454
455 lob_size = MB3;
456 remove_len = MB3;
457 z_remove_middle_generic(lob_size, offset, remove_len);
458 }
459
460 TEST(z_remove_middle, PopBack) {
461 ulint lob_size = MB5;
462 ulint remove_len = MB1;
463 ulint offset = (lob_size - remove_len);
464 z_remove_middle_generic(lob_size, offset, remove_len);
465
466 lob_size = MB4;
467 offset = (lob_size - remove_len);
468 remove_len = MB2;
469 z_remove_middle_generic(lob_size, offset, remove_len);
470
471 lob_size = MB3;
472 offset = (lob_size - remove_len);
473 remove_len = MB3;
474 z_remove_middle_generic(lob_size, offset, remove_len);
475 }
476
477 TEST(z_remove_middle, InvalidOffset) {
478 ulint lob_size = MB5;
479 ulint remove_len = MB1;
480 ulint offset = lob_size;
481 z_remove_middle_generic(lob_size, offset, remove_len);
482
483 lob_size = MB4;
484 offset = lob_size;
485 remove_len = MB2;
486 z_remove_middle_generic(lob_size, offset, remove_len);
487
488 lob_size = MB3;
489 offset = lob_size;
490 remove_len = MB3;
491 z_remove_middle_generic(lob_size, offset, remove_len);
492 }
493
494 TEST(z_remove_middle, FullRemoval) {
495 const ulint offset = 0;
496
497 ulint lob_size = MB5;
498 ulint remove_len = MB5;
499 z_remove_middle_generic(lob_size, offset, remove_len);
500
501 lob_size = MB10;
502 remove_len = MB10;
503 z_remove_middle_generic(lob_size, offset, remove_len);
504
505 lob_size = MB1;
506 remove_len = MB1;
507 z_remove_middle_generic(lob_size, offset, remove_len);
508 }
509
510 TEST(z_remove_middle, StressUpto300KB) {
511 const ulint lob_size = KB300;
512 const ulint remove_len = KB1;
513
514 for (ulint offset = 1; offset <= lob_size; offset += KB1) {
515 z_remove_middle_generic(lob_size, offset, remove_len);
516 }
517 }
518
519 TEST(z_replace, ReplaceBegin) {
520 ulint size;
521 ulint offset;
522 ulint replace_len;
523
524 size = MB10;
525 offset = 0;
526 replace_len = MB5;
527 ;
528 z_replace_generic(size, offset, replace_len);
529 }
530
531 TEST(z_replace, ReplaceStressKB500) {
532 ulint lob_size = KB500;
533 ulint replace_len = 1000;
534
535 for (ulint offset = 0; offset <= lob_size; offset += 500) {
536 z_replace_generic(lob_size, offset, replace_len);
537 }
538 }