Debian Codegraph @

unittest/gunit/innodb/lob/lot0lot.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 #include <cassert>
28 #include <iostream>
29 #include <map>
30 #include <memory>
31
32 #include "my_config.h"
33
34 #include <gtest/gtest.h>
35
36 #include "lot0buf.h"
37 #include "lot0dat.h"
38 #include "lot0lob.h"
39 #include "lot0lot.h"
40
41 #if 1
42 #define Fname(x) const char *fname = x;
43 #define LOG(x) \
44 { std::cout << fname << ":" << x << std::endl; }
45 #else
46 #define Fname(x)
47 #define LOG(x)
48 #endif
49
50 void lob_tester_t::print_info() {
51 const ulint SIZE = MB10;
52
53 ut_ad(get_page_size() == KB16);
54
55 lobid_t lobid;
56 byte *lob = lob_data::generate_lob(&lobid, SIZE);
57
58 trx_id_t trxid = 888;
59
60 lob::ref_t ref;
61 ref.m_id = lobid;
62 lob::insert(trxid, ref, lob, SIZE);
63
64 lob::print(std::cout, ref);
65 }
66
67 /** Insert 10MB of data and fetch it. This LOB will have the FIRST page, more
68 than one DATA page and also more than one INDEX page. This test case assumes
69 a page size of 16K. */
70 void lob_tester_t::fetch_full() {
71 Fname("test_1");
72 const ulint SIZE = MB10;
73
74 ut_ad(get_page_size() == KB16);
75
76 LOG("Inserting LOB of size " << SIZE << " bytes");
77 lobid_t lobid;
78 byte *lob = lob_data::generate_lob(&lobid, SIZE);
79 LOG("Inserted lobid=" << lobid);
80
81 trx_id_t trxid = 28;
82
83 lob::ref_t ref;
84 ref.m_id = lobid;
85 lob::insert(trxid, ref, lob, SIZE);
86
87 ulint offset = 0;
88 ulint fetch_bytes = SIZE;
89 byte *buf = new byte[fetch_bytes];
90 lob::read(trxid, ref, offset, fetch_bytes, buf);
91 LOG("Comparing bytes=" << fetch_bytes);
92 if (memcmp(lob + offset, buf, fetch_bytes) == 0) {
93 LOG("PASS");
94 } else {
95 LOG("FAIL");
96 }
97 }
98
99 void test_2() {
100 Fname("test_2");
101 const ulint SIZE = 5000;
102
103 LOG("Inserting LOB of size " << SIZE << " bytes");
104 lobid_t lobid;
105 byte *lob = lob_data::generate_lob(&lobid, SIZE);
106
107 lob::ref_t ref;
108 ref.m_id = lobid;
109 lob::insert(10, ref, lob, SIZE);
110
111 ulint replace_offset = 5;
112 ulint replace_bytes = 10;
113 byte *replace_buf = new byte[replace_bytes + 1];
114 memset(replace_buf, '\0', replace_bytes + 1);
115 strcpy((char *)replace_buf, "1234567890");
116 lob::replace(12, ref, replace_offset, replace_bytes, replace_buf);
117
118 ulint offset = 0;
119 ulint fetch_bytes = 40;
120 byte *buf = new byte[fetch_bytes];
121 memset(buf, '\0', fetch_bytes);
122 lob::read(11, ref, offset, fetch_bytes - 1, buf);
123 LOG("buf=" << buf);
124 lob::read(12, ref, offset, fetch_bytes - 1, buf);
125 LOG("buf=" << buf);
126 }
127
128 void insert_generic(ulint size) {
129 Fname("insert_generic");
130
131 LOG("LOB size=" << size << " bytes");
132 lobid_t lobid;
133 byte *lob = lob_data::generate_lob(&lobid, size);
134
135 trx_id_t trxid = 10;
136
137 /* Insert the LOB */
138 lob::ref_t ref;
139 ref.m_id = lobid;
140 lob::insert(trxid, ref, lob, size);
141
142 /* Fetch the LOB that has been inserted. */
143 std::unique_ptr<byte[]> buf(new byte[size]);
144 lob::read(trxid, ref, 0, size, buf.get());
145
146 ASSERT_TRUE(memcmp(buf.get(), lob, size) == 0);
147
148 lob_data::remove_lob(lobid);
149 buf_pool_reset();
150 }
151
152 /** An LOB of only one page long. The LOB has only the first page. Do an
153 insert_middle() which doesn't spill on other pages. This is done by same
154 transaction. This test case assumes a page size of 16K. */
155 void test_3() {
156 Fname("test_3");
157 const ulint SIZE = 100;
158
159 LOG("Inserting LOB of size " << SIZE << " bytes");
160 lobid_t lobid;
161 byte *lob = lob_data::generate_lob(&lobid, SIZE);
162
163 trx_id_t trxid = 10;
164
165 /* Insert the LOB */
166 lob::ref_t ref;
167 ref.m_id = lobid;
168 lob::insert(trxid, ref, lob, SIZE);
169 LOG("Inserted LOB: " << lob);
170
171 /* Fetch the LOB that has been inserted. */
172 ulint offset = 0;
173 ulint fetch_bytes = SIZE;
174 byte *buf = new byte[fetch_bytes];
175 lob::read(trxid, ref, offset, fetch_bytes, buf);
176 LOG("Fetched LOB: " << buf);
177
178 /* Insert more data in middle of LOB. */
179 ulint ins_offset = 20;
180 ulint ins_bytes = 10;
181 byte *ins_data = new byte[ins_bytes + 1];
182 strcpy((char *)ins_data, "1234567890");
183 LOG("Inserting data: " << ins_data);
184 lob::insert_middle(trxid, ref, ins_offset, ins_data, ins_bytes);
185
186 /* Fetch the LOB that has been inserted. */
187 fetch_bytes = SIZE + ins_bytes;
188 byte *buf2 = new byte[fetch_bytes + 1];
189 memset(buf2, '\0', fetch_bytes + 1);
190 lob::read(trxid, ref, offset, fetch_bytes, buf2);
191 LOG("Fetched LOB: " << buf2);
192
193 delete[] buf;
194 delete[] buf2;
195 delete[] ins_data;
196 }
197
198 /** An LOB that will have one FIRST page and one DATA page. Do an
199 insert_middle() on the second page, which doesn't spill on other pages. This is
200 done by same transaction. This test case assumes a page size of 16K. */
201 void lob_tester_t::test_4() {
202 Fname("lob_tester_t::test_4");
203 const ulint SIZE = KB16;
204
205 LOG("Inserting LOB of size " << SIZE << " bytes");
206 lobid_t lobid;
207 byte *lob = lob_data::generate_lob(&lobid, SIZE);
208
209 trx_id_t trxid = 182;
210
211 /* Insert the LOB */
212 lob::ref_t ref;
213 ref.m_id = lobid;
214 lob::insert(trxid, ref, lob, SIZE);
215 LOG("Inserted lobid=" << lobid);
216
217 /* Fetch the LOB that has been inserted. */
218 ulint offset = 0;
219 ulint fetch_bytes = SIZE;
220 byte *buf = new byte[fetch_bytes];
221 lob::read(trxid, ref, offset, fetch_bytes, buf);
222 if (memcmp(buf, lob, SIZE) == 0) {
223 LOG("Fetch PASS");
224 } else {
225 LOG("Fetch FAIL");
226 }
227
228 /* Insert more data in middle of LOB. */
229 ulint ins_offset = 11000;
230 ulint ins_bytes = 10;
231 byte *ins_data = new byte[ins_bytes + 1];
232 strcpy((char *)ins_data, "1234567890");
233 LOG("Inserting data: " << ins_data);
234 lob::insert_middle(trxid, ref, ins_offset, ins_data, ins_bytes);
235
236 /* Fetch the LOB that has been inserted. */
237 fetch_bytes = SIZE + ins_bytes;
238 byte *buf2 = new byte[fetch_bytes + 1];
239 memset(buf2, '\0', fetch_bytes + 1);
240 lob::read(trxid, ref, offset, fetch_bytes, buf2);
241
242 LOG("Comparing the initial bytes=" << ins_offset);
243 if (memcmp(buf, buf2, ins_offset) == 0) {
244 LOG("PASS");
245 } else {
246 LOG("FAIL");
247 }
248
249 ulint trail = SIZE - ins_offset;
250 LOG("Comparing the remaining bytes=" << trail);
251 if (memcmp(buf + ins_offset, buf2 + ins_offset + ins_bytes, trail) == 0) {
252 LOG("PASS");
253 } else {
254 LOG("FAIL");
255 }
256
257 if (memcmp(buf2 + ins_offset, ins_data, ins_bytes) == 0) {
258 LOG("PASS");
259 } else {
260 LOG("FAIL");
261 }
262
263 delete[] buf;
264 delete[] buf2;
265 delete[] ins_data;
266 }
267
268 /** An LOB that will have only one FIRST page. Do an
269 remove_middle() on the first page, which doesn't spill on other pages. This
270 test case assumes a page size of 16K. */
271 void lob_tester_t::remove_middle_1() {
272 Fname("lob_tester_t::test_4");
273 const ulint SIZE = KB5;
274
275 LOG("Inserting LOB of size " << SIZE << " bytes");
276 lobid_t lobid;
277 byte *lob = lob_data::generate_lob(&lobid, SIZE);
278
279 trx_id_t trxid = 200;
280
281 /* Insert the LOB */
282 lob::ref_t ref;
283 ref.m_id = lobid;
284 lob::insert(trxid, ref, lob, SIZE);
285 LOG("Inserted lobid=" << lobid);
286
287 /* Fetch the LOB that has been inserted. */
288 ulint offset = 0;
289 ulint fetch_bytes = SIZE;
290 byte *buf = new byte[fetch_bytes];
291 lob::read(trxid, ref, offset, fetch_bytes, buf);
292 if (memcmp(buf, lob, SIZE) == 0) {
293 LOG("Fetch PASS");
294 } else {
295 LOG("Fetch FAIL");
296 }
297
298 /* Remove 5 bytes data in middle of LOB. */
299 ulint rm_offset = 15;
300 ulint rm_bytes = 5;
301 LOG("Removing " << rm_bytes << " bytes of data");
302 lob::remove_middle(trxid, ref, rm_offset, rm_bytes);
303
304 /* Fetch the LOB that has been modified. */
305 fetch_bytes = SIZE - rm_bytes;
306 byte *buf2 = new byte[fetch_bytes + 1];
307 memset(buf2, '\0', fetch_bytes + 1);
308 lob::read(trxid, ref, offset, fetch_bytes, buf2);
309
310 LOG("Comparing the initial bytes=" << rm_offset);
311 if (memcmp(buf, buf2, rm_offset) == 0) {
312 LOG("PASS");
313 } else {
314 LOG("FAIL");
315 }
316
317 ulint trail = SIZE - rm_offset - rm_bytes;
318 LOG("Comparing the remaining bytes=" << trail);
319 if (memcmp(buf + rm_offset + rm_bytes, buf2 + rm_offset, trail) == 0) {
320 LOG("PASS");
321 } else {
322 LOG("FAIL");
323 }
324
325 delete[] buf;
326 delete[] buf2;
327 }
328
329 /** A stress tester for remove_middle(). Fixed removal length, with
330 a variable offset. */
331 void lob_tester_t::remove_middle_stress_1() {
332 const ulint SIZE = MB10;
333 ulint rm_len = 100;
334
335 for (ulint rm_offset = 0; rm_offset < SIZE; ++rm_offset) {
336 std::cerr << "size=" << SIZE << ", len=" << rm_len << ", off=" << rm_offset
337 << std::endl;
338 remove_middle_gen(SIZE, rm_offset, rm_len);
339 }
340 }
341
342 /** A stress tester for replace(). Fixed replace length, with
343 a variable offset. */
344 void lob_tester_t::replace_stress() {
345 const ulint SIZE = MB10;
346 ulint len = 2000;
347
348 for (ulint offset = 0; offset < SIZE; offset += 200) {
349 std::cerr << "size=" << SIZE << ", len=" << len << ", off=" << offset
350 << std::endl;
351 replace_generic(SIZE, offset, len);
352 }
353 }
354
355 /** A generic replace() tester. */
356 void lob_tester_t::replace_generic(ulint lob_size, ulint offset,
357 ulint replace_len) {
358 Fname("lob_tester_t::replace_generic");
359
360 const ulint SIZE = lob_size;
361
362 LOG("size=" << SIZE << ", len=" << replace_len << ", off=" << offset);
363
364 /* Ensure that the requested replace length is within limits. */
365 ulint can_be_replaced = lob_size - offset;
366 if (replace_len > can_be_replaced) {
367 replace_len = can_be_replaced;
368 }
369
370 lobid_t lobid;
371 byte *lob = lob_data::generate_lob(&lobid, SIZE);
372 trx_id_t trx1 = 100;
373
374 /* Insert the LOB */
375 lob::ref_t ref;
376 ref.m_id = lobid;
377 lob::insert(trx1, ref, lob, SIZE);
378
379 /* Fetch the LOB that has been inserted. */
380 ulint fetch_offset = 0;
381 ulint fetch_bytes = SIZE;
382 std::unique_ptr<byte[]> buf(new byte[fetch_bytes]);
383 lob::read(trx1, ref, fetch_offset, fetch_bytes, buf.get());
384 ut_ad(memcmp(buf.get(), lob, SIZE) == 0);
385
386 /* Replace */
387 lobid_t lobid2;
388 trx_id_t trx2 = 300;
389 ulint replace_offset = offset;
390 byte *replace_lob = lob_data::generate_lob(&lobid2, '|', replace_len);
391 lob::replace(trx2, ref, replace_offset, replace_len, replace_lob);
392
393 /* Fetch the older LOB that has been originally inserted. */
394 trx_id_t trx3 = 250;
395 std::unique_ptr<byte[]> buf2(new byte[SIZE]);
396 lob::read(trx3, ref, fetch_offset, fetch_bytes, buf2.get());
397 ut_ad(memcmp(buf2.get(), lob, SIZE) == 0);
398
399 /* Fetch the newer LOB that has been replaced. */
400 trx_id_t trx4 = 350;
401 memset(buf2.get(), '\0', SIZE);
402 ulint len = lob::read(trx4, ref, fetch_offset, fetch_bytes, buf2.get());
403
404 ut_ad(len == fetch_bytes);
405
406 // Compare the initial bytes.
407 ut_ad(memcmp(buf2.get(), lob, replace_offset) == 0);
408
409 // Compare the replaced bytes.
410 ut_ad(memcmp(buf2.get() + replace_offset, replace_lob, replace_len) == 0);
411
412 // Compare the trailer
413 ulint trailer_len = SIZE - replace_offset - replace_len;
414 ut_ad(memcmp(buf2.get() + replace_offset + replace_len,
415 lob + replace_offset + replace_len, trailer_len) == 0);
416
417 trx_rollback(trx2, ref);
418
419 /* Fetch the latest LOB. */
420 trx_id_t trx5 = 400;
421 memset(buf2.get(), '\0', SIZE);
422 lob::read(trx5, ref, fetch_offset, fetch_bytes, buf2.get());
423 ut_ad(memcmp(buf2.get(), lob, SIZE) == 0);
424
425 lob_data::remove_lob(lobid);
426 lob_data::remove_lob(lobid2);
427 buf_pool_reset();
428 }
429
430 /** A generic remove_middle() tester. */
431 void lob_tester_t::remove_middle_gen(ulint lob_size, ulint offset,
432 ulint rm_len) {
433 // Fname("lob_tester_t::remove_middle_gen");
434 const ulint SIZE = lob_size;
435
436 /* Ensure that the requested removal length is within limits. */
437 ulint can_be_removed = lob_size - offset;
438 if (rm_len > can_be_removed) {
439 rm_len = can_be_removed;
440 }
441
442 lobid_t lobid;
443 byte *lob = lob_data::generate_lob(&lobid, SIZE);
444
445 trx_id_t trxid = 200;
446
447 /* Insert the LOB */
448 lob::ref_t ref;
449 ref.m_id = lobid;
450 lob::insert(trxid, ref, lob, SIZE);
451
452 /* Fetch the LOB that has been inserted. */
453 ulint fetch_offset = 0;
454 ulint fetch_bytes = SIZE;
455 byte *buf = new byte[fetch_bytes];
456 lob::read(trxid, ref, fetch_offset, fetch_bytes, buf);
457 ut_ad(memcmp(buf, lob, SIZE) == 0);
458
459 trx_id_t rm_trxid = 300;
460
461 ulint rm_offset = offset;
462 ulint rm_bytes = rm_len;
463 lob::remove_middle(rm_trxid, ref, rm_offset, rm_bytes);
464
465 trx_id_t trx3 = 400;
466
467 /* Fetch the LOB that has been modified. */
468 fetch_bytes = SIZE - rm_bytes;
469 byte *buf2 = new byte[SIZE];
470 memset(buf2, '\0', fetch_bytes + 1);
471 ulint len = lob::read(trx3, ref, fetch_offset, fetch_bytes, buf2);
472
473 std::cout << "length=" << len << std::endl;
474 /* Comparing the initial bytes. */
475 ut_ad(memcmp(buf, buf2, rm_offset) == 0);
476
477 /* Comparing the remaining bytes. */
478 ulint trail = SIZE - rm_offset - rm_bytes;
479 ut_ad(memcmp(buf + rm_offset + rm_bytes, buf2 + rm_offset, trail) == 0);
480
481 trx_id_t trx4 = 250;
482
483 /* Fetch the older LOB. */
484 fetch_offset = 0;
485 fetch_bytes = SIZE;
486 memset(buf, 0x00, SIZE);
487 lob::read(trx4, ref, fetch_offset, fetch_bytes, buf);
488 ut_ad(memcmp(buf, lob, SIZE) == 0);
489
490 /* Rollback the remove_middle() operation. */
491 trx_rollback(rm_trxid, ref);
492
493 trx_id_t trx5 = 500;
494
495 /* Fetch the latest LOB. */
496 fetch_offset = 0;
497 fetch_bytes = SIZE;
498 memset(buf, 0x00, SIZE);
499 len = lob::read(trx5, ref, fetch_offset, fetch_bytes, buf);
500 ut_ad(memcmp(buf, lob, SIZE) == 0);
501
502 delete[] buf;
503 delete[] buf2;
504
505 lob_data::remove_lob(lobid);
506 buf_pool_reset();
507 }
508
509 void lob_tester_t::insert_rollback() {
510 const ulint SIZE = MB5;
511 lobid_t lobid;
512 byte *lob = lob_data::generate_lob(&lobid, SIZE);
513
514 trx_id_t trxid = 300;
515
516 /* Insert the LOB */
517 lob::ref_t ref;
518 ref.m_id = lobid;
519 lob::insert(trxid, ref, lob, SIZE);
520
521 /* Fetch the LOB that has been inserted. */
522 ulint fetch_offset = 0;
523 ulint fetch_bytes = SIZE;
524 byte *buf = new byte[fetch_bytes];
525 lob::read(trxid, ref, fetch_offset, fetch_bytes, buf);
526 ut_ad(memcmp(buf, lob, SIZE) == 0);
527
528 trx_rollback(trxid, ref);
529
530 ulint len = lob::read(trxid, ref, fetch_offset, fetch_bytes, buf);
531 ut_ad(len == 0);
532
533 lob_data::remove_lob(lobid);
534 buf_pool_reset();
535 }
536
537 /** A stress tester for insert_middle(). Fixed insert length, with
538 a variable offset. */
539 void lob_tester_t::insert_middle_stress() {
540 const ulint size = MB7;
541 ulint len = 2000;
542
543 for (ulint offset = 0; offset < size; offset += 200) {
544 std::cerr << "size=" << size << ", len=" << len << ", off=" << offset
545 << std::endl;
546 insert_middle_generic(size, offset, len);
547 }
548 }
549
550 /** A generic insert_middle() tester.
551 @param[in] lob_size the size of the LOB.
552 @param[in] offset The offset into LOB where new data inserted.
553 @param[in] len The length of new data inserted.*/
554 void lob_tester_t::insert_middle_generic(ulint lob_size, ulint offset,
555 ulint len) {
556 const ulint SIZE = lob_size;
557 const ulint size2 = lob_size + len;
558 ulint ret_len = 0;
559
560 std::unique_ptr<byte[]> buf2(new byte[size2]);
561
562 lobid_t lobid;
563 byte *lob = lob_data::generate_lob(&lobid, SIZE);
564 lobid_t lobid2;
565 byte *lob2 = lob_data::generate_lob(&lobid2, '|', len);
566
567 trx_id_t trx1 = 100;
568
569 /* Insert the LOB */
570 lob::ref_t ref;
571 ref.m_id = lobid;
572 lob::insert(trx1, ref, lob, SIZE);
573
574 /* Fetch the LOB that has been inserted. */
575 ulint fetch_offset = 0;
576 ulint fetch_bytes = SIZE;
577 std::unique_ptr<byte[]> buf(new byte[fetch_bytes]);
578 lob::read(trx1, ref, fetch_offset, fetch_bytes, buf.get());
579 ut_ad(memcmp(buf.get(), lob, SIZE) == 0);
580
581 /* Insert Middle */
582 trx_id_t trx2 = 300;
583 lob::insert_middle(trx2, ref, offset, lob2, len);
584
585 /* Fetch the older LOB that has been originally inserted. */
586 trx_id_t trx3 = 250;
587 memset(buf2.get(), '\0', size2);
588 ret_len = lob::read(trx3, ref, 0, SIZE, buf2.get());
589 ut_ad(memcmp(buf2.get(), lob, SIZE) == 0);
590
591 /* Fetch the newer LOB that has been modified. */
592 trx_id_t trx4 = 350;
593 memset(buf2.get(), '\0', size2);
594 ret_len = lob::read(trx4, ref, 0, size2, buf2.get());
595
596 ut_ad(ret_len == size2);
597
598 // Compare the initial bytes.
599 ut_ad(memcmp(buf2.get(), lob, offset) == 0);
600
601 // Compare the replaced bytes.
602 ut_ad(memcmp(buf2.get() + offset, lob2, len) == 0);
603
604 // Compare the trailer
605 ulint trailer_len = SIZE - offset;
606 ut_ad(memcmp(buf2.get() + offset + len, lob + offset, trailer_len) == 0);
607
608 trx_rollback(trx2, ref);
609
610 /* Fetch the latest LOB. */
611 trx_id_t trx5 = 400;
612 memset(buf2.get(), '\0', size2);
613 lob::read(trx5, ref, 0, SIZE, buf2.get());
614 ut_ad(memcmp(buf2.get(), lob, SIZE) == 0);
615
616 lob_data::remove_lob(lobid);
617 lob_data::remove_lob(lobid2);
618 buf_pool_reset();
619 }
620
621 TEST(insert, InsertStressMB1) {
622 for (ulint size = 0; size < MB1; size += KB1) {
623 insert_generic(size);
624 }
625 }
626
627 TEST(insert, InsertStressMB5) {
628 for (ulint size = MB1; size < MB5; size += KB2) {
629 insert_generic(size);
630 }
631 }
632
633 TEST(replace, ReplaceStressMB3) {
634 const ulint SIZE = MB3;
635 ulint len = 2000;
636 lob_tester_t tester;
637
638 for (ulint offset = 0; offset < SIZE; offset += 1000) {
639 tester.replace_generic(SIZE, offset, len);
640 }
641 }