-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
page0page.cc
2659 lines (2181 loc) · 84.1 KB
/
page0page.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1994, 2024, Oracle and/or its affiliates.
Copyright (c) 2012, Facebook Inc.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/** @file page/page0page.cc
Index page routines
Created 2/2/1994 Heikki Tuuri
*******************************************************/
#include "my_dbug.h"
#include "btr0btr.h"
#include "buf0buf.h"
#include "ibuf0ibuf.h"
#include "page0cur.h"
#include "page0page.h"
#include "page0zip.h"
#ifndef UNIV_HOTBACKUP
#include "btr0sea.h"
#include "fut0lst.h"
#include "lock0lock.h"
#include "srv0srv.h"
#endif /* !UNIV_HOTBACKUP */
/* THE INDEX PAGE
==============
The index page consists of a page header which contains the page's
id and other information. On top of it are the index records
in a heap linked into a one way linear list according to alphabetic order.
Just below page end is an array of pointers which we call page directory,
to about every sixth record in the list. The pointers are placed in
the directory in the alphabetical order of the records pointed to,
enabling us to make binary search using the array. Each slot n:o I
in the directory points to a record, where a 4-bit field contains a count
of those records which are in the linear list between pointer I and
the pointer I - 1 in the directory, including the record
pointed to by pointer I and not including the record pointed to by I - 1.
We say that the record pointed to by slot I, or that slot I, owns
these records. The count is always kept in the range 4 to 8, with
the exception that it is 1 for the first slot, and 1--8 for the second slot.
An essentially binary search can be performed in the list of index
records, like we could do if we had pointer to every record in the
page directory. The data structure is, however, more efficient when
we are doing inserts, because most inserts are just pushed on a heap.
Only every 8th insert requires block move in the directory pointer
table, which itself is quite small. A record is deleted from the page
by just taking it off the linear list and updating the number of owned
records-field of the record which owns it, and updating the page directory,
if necessary. A special case is the one when the record owns itself.
Because the overhead of inserts is so small, we may also increase the
page size from the projected default of 8 kB to 64 kB without too
much loss of efficiency in inserts. Bigger page becomes actual
when the disk transfer rate compared to seek and latency time rises.
On the present system, the page size is set so that the page transfer
time (3 ms) is 20 % of the disk random access time (15 ms).
When the page is split, merged, or becomes full but contains deleted
records, we have to reorganize the page.
Assuming a page size of 8 kB, a typical index page of a secondary
index contains 300 index entries, and the size of the page directory
is 50 x 4 bytes = 200 bytes. */
/** Looks for the directory slot which owns the given record.
@return the directory slot number */
ulint page_dir_find_owner_slot(const rec_t *rec) /*!< in: the physical record */
{
const page_t *page;
uint16 rec_offs_bytes;
const page_dir_slot_t *slot;
const page_dir_slot_t *first_slot;
const rec_t *r = rec;
ut_ad(page_rec_check(rec));
page = page_align(rec);
first_slot = page_dir_get_nth_slot(page, 0);
slot = page_dir_get_nth_slot(page, page_dir_get_n_slots(page) - 1);
if (page_is_comp(page)) {
while (rec_get_n_owned_new(r) == 0) {
r = rec_get_next_ptr_const(r, true);
ut_ad(r >= page + PAGE_NEW_SUPREMUM);
ut_ad(r < page + (UNIV_PAGE_SIZE - PAGE_DIR));
}
} else {
while (rec_get_n_owned_old(r) == 0) {
r = rec_get_next_ptr_const(r, false);
ut_ad(r >= page + PAGE_OLD_SUPREMUM);
ut_ad(r < page + (UNIV_PAGE_SIZE - PAGE_DIR));
}
}
rec_offs_bytes = mach_encode_2(r - page);
while (UNIV_LIKELY(*(uint16 *)slot != rec_offs_bytes)) {
if (UNIV_UNLIKELY(slot == first_slot)) {
ib::error(ER_IB_MSG_860)
<< "Probable data corruption on page " << page_get_page_no(page)
<< ". Original record on that page;";
if (page_is_comp(page)) {
fputs("(compact record)", stderr);
} else {
rec_print_old(stderr, rec);
}
ib::error(ER_IB_MSG_861) << "Cannot find the dir slot for this"
" record on that page;";
if (page_is_comp(page)) {
fputs("(compact record)", stderr);
} else {
rec_print_old(stderr, page + mach_decode_2(rec_offs_bytes));
}
ut_error;
}
slot += PAGE_DIR_SLOT_SIZE;
}
return (((ulint)(first_slot - slot)) / PAGE_DIR_SLOT_SIZE);
}
/** Used to check the consistency of a directory slot.
@return true if succeed */
static bool page_dir_slot_check(const page_dir_slot_t *slot) /*!< in: slot */
{
const page_t *page;
ulint n_slots;
ulint n_owned;
ut_a(slot);
page = page_align(slot);
n_slots = page_dir_get_n_slots(page);
ut_a(slot <= page_dir_get_nth_slot(page, 0));
ut_a(slot >= page_dir_get_nth_slot(page, n_slots - 1));
ut_a(page_rec_check(page_dir_slot_get_rec(slot)));
if (page_is_comp(page)) {
n_owned = rec_get_n_owned_new(page_dir_slot_get_rec(slot));
} else {
n_owned = rec_get_n_owned_old(page_dir_slot_get_rec(slot));
}
if (slot == page_dir_get_nth_slot(page, 0)) {
ut_a(n_owned == 1);
} else if (slot == page_dir_get_nth_slot(page, n_slots - 1)) {
ut_a(n_owned >= 1);
ut_a(n_owned <= PAGE_DIR_SLOT_MAX_N_OWNED);
} else {
ut_a(n_owned >= PAGE_DIR_SLOT_MIN_N_OWNED);
ut_a(n_owned <= PAGE_DIR_SLOT_MAX_N_OWNED);
}
return true;
}
/** Sets the max trx id field value.
@param[in,out] block Page
@param[in,out] page_zip Compressed page, or NULL
@param[in] trx_id Transaction id
@param[in,out] mtr Mini-transaction, or NULL */
void page_set_max_trx_id(buf_block_t *block, page_zip_des_t *page_zip,
trx_id_t trx_id, mtr_t *mtr) {
page_t *page = buf_block_get_frame(block);
#ifndef UNIV_HOTBACKUP
ut_ad(!mtr || mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
#endif /* !UNIV_HOTBACKUP */
/* It is not necessary to write this change to the redo log, as
during a database recovery we assume that the max trx id of every
page is the maximum trx id assigned before the crash. */
if (page_zip) {
mach_write_to_8(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), trx_id);
page_zip_write_header(page_zip, page + (PAGE_HEADER + PAGE_MAX_TRX_ID), 8,
mtr);
#ifndef UNIV_HOTBACKUP
} else if (mtr) {
mlog_write_ull(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), trx_id, mtr);
#endif /* !UNIV_HOTBACKUP */
} else {
mach_write_to_8(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), trx_id);
}
}
/** Allocates a block of memory from the heap of an index page.
@return pointer to start of allocated buffer, or NULL if allocation fails */
byte *page_mem_alloc_heap(
page_t *page, /*!< in/out: index page */
page_zip_des_t *page_zip, /*!< in/out: compressed page with enough
space available for inserting the record,
or NULL */
ulint need, /*!< in: total number of bytes needed */
ulint *heap_no) /*!< out: this contains the heap number
of the allocated record
if allocation succeeds */
{
byte *block;
ulint avl_space;
ut_ad(page && heap_no);
avl_space = page_get_max_insert_size(page, 1);
if (avl_space >= need) {
block = page_header_get_ptr(page, PAGE_HEAP_TOP);
page_header_set_ptr(page, page_zip, PAGE_HEAP_TOP, block + need);
*heap_no = page_dir_get_n_heap(page);
page_dir_set_n_heap(page, page_zip, 1 + *heap_no);
return (block);
}
return (nullptr);
}
#ifndef UNIV_HOTBACKUP
/** Writes a log record of page creation
@param[in] frame A buffer frame where the page is created
@param[in] mtr Mini-transaction handle
@param[in] comp true=compact page format
@param[in] page_type Page type */
static inline void page_create_write_log(buf_frame_t *frame, mtr_t *mtr,
bool comp, page_type_t page_type) {
mlog_id_t type;
switch (page_type) {
case FIL_PAGE_INDEX:
type = comp ? MLOG_COMP_PAGE_CREATE : MLOG_PAGE_CREATE;
break;
case FIL_PAGE_RTREE:
type = comp ? MLOG_COMP_PAGE_CREATE_RTREE : MLOG_PAGE_CREATE_RTREE;
break;
case FIL_PAGE_SDI:
type = comp ? MLOG_COMP_PAGE_CREATE_SDI : MLOG_PAGE_CREATE_SDI;
break;
default:
ut_error;
}
mlog_write_initial_log_record(frame, type, mtr);
}
#else /* !UNIV_HOTBACKUP */
#define page_create_write_log(frame, mtr, comp, type) ((void)0)
#endif /* !UNIV_HOTBACKUP */
/** The page infimum and supremum of an empty page in ROW_FORMAT=REDUNDANT */
static const byte infimum_supremum_redundant[] = {
/* the infimum record */
0x08 /*end offset*/, 0x01 /*n_owned*/, 0x00, 0x00 /*heap_no=0*/,
0x03 /*n_fields=1, 1-byte offsets*/, 0x00, 0x74 /* pointer to supremum */,
'i', 'n', 'f', 'i', 'm', 'u', 'm', 0,
/* the supremum record */
0x09 /*end offset*/, 0x01 /*n_owned*/, 0x00, 0x08 /*heap_no=1*/,
0x03 /*n_fields=1, 1-byte offsets*/, 0x00, 0x00 /* end of record list */,
's', 'u', 'p', 'r', 'e', 'm', 'u', 'm', 0};
/** The page infimum and supremum of an empty page in ROW_FORMAT=COMPACT */
static const byte infimum_supremum_compact[] = {
/* the infimum record */
0x01 /*n_owned=1*/, 0x00, 0x02 /* heap_no=0, REC_STATUS_INFIMUM */, 0x00,
0x0d /* pointer to supremum */, 'i', 'n', 'f', 'i', 'm', 'u', 'm', 0,
/* the supremum record */
0x01 /*n_owned=1*/, 0x00, 0x0b /* heap_no=1, REC_STATUS_SUPREMUM */, 0x00,
0x00 /* end of record list */, 's', 'u', 'p', 'r', 'e', 'm', 'u', 'm'};
/** The index page creation function.
@param[in,out] block a buffer block where the page is created
@param[in] comp nonzero=compact page format
@param[in] page_type page type
@return pointer to the page */
static page_t *page_create_low(buf_block_t *block, ulint comp,
page_type_t page_type) {
page_t *page;
static_assert(PAGE_BTR_IBUF_FREE_LIST + FLST_BASE_NODE_SIZE <= PAGE_DATA,
"PAGE_BTR_IBUF_FREE_LIST + FLST_BASE_NODE_SIZE > PAGE_DATA");
static_assert(PAGE_BTR_IBUF_FREE_LIST_NODE + FLST_NODE_SIZE <= PAGE_DATA,
"PAGE_BTR_IBUF_FREE_LIST_NODE + FLST_NODE_SIZE > PAGE_DATA");
buf_block_modify_clock_inc(block);
page = buf_block_get_frame(block);
ut_ad(page_type == FIL_PAGE_INDEX || page_type == FIL_PAGE_RTREE ||
page_type == FIL_PAGE_SDI);
fil_page_set_type(page, page_type);
memset(page + PAGE_HEADER, 0, PAGE_HEADER_PRIV_END);
page[PAGE_HEADER + PAGE_N_DIR_SLOTS + 1] = 2;
page[PAGE_HEADER + PAGE_DIRECTION + 1] = PAGE_NO_DIRECTION;
if (comp) {
page[PAGE_HEADER + PAGE_N_HEAP] = 0x80; /*page_is_comp()*/
page[PAGE_HEADER + PAGE_N_HEAP + 1] = PAGE_HEAP_NO_USER_LOW;
page[PAGE_HEADER + PAGE_HEAP_TOP + 1] = PAGE_NEW_SUPREMUM_END;
memcpy(page + PAGE_DATA, infimum_supremum_compact,
sizeof infimum_supremum_compact);
memset(page + PAGE_NEW_SUPREMUM_END, 0,
UNIV_PAGE_SIZE - PAGE_DIR - PAGE_NEW_SUPREMUM_END);
page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE * 2 + 1] =
PAGE_NEW_SUPREMUM;
page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE + 1] = PAGE_NEW_INFIMUM;
} else {
page[PAGE_HEADER + PAGE_N_HEAP + 1] = PAGE_HEAP_NO_USER_LOW;
page[PAGE_HEADER + PAGE_HEAP_TOP + 1] = PAGE_OLD_SUPREMUM_END;
memcpy(page + PAGE_DATA, infimum_supremum_redundant,
sizeof infimum_supremum_redundant);
memset(page + PAGE_OLD_SUPREMUM_END, 0,
UNIV_PAGE_SIZE - PAGE_DIR - PAGE_OLD_SUPREMUM_END);
page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE * 2 + 1] =
PAGE_OLD_SUPREMUM;
page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE + 1] = PAGE_OLD_INFIMUM;
}
return (page);
}
/** Parses a redo log record of creating a page.
@param[in,out] block buffer block, or NULL
@param[in] comp nonzero=compact page format
@param[in] page_type page type (FIL_PAGE_INDEX, FIL_PAGE_RTREE
or FIL_PAGE_SDI) */
void page_parse_create(buf_block_t *block, ulint comp, page_type_t page_type) {
if (block != nullptr) {
page_create_low(block, comp, page_type);
}
}
/** Create an uncompressed B-tree or R-tree or SDI index page.
@param[in] block A buffer block where the page is created
@param[in] mtr Mini-transaction handle
@param[in] comp nonzero=compact page format
@param[in] page_type Page type
@return pointer to the page */
page_t *page_create(buf_block_t *block, mtr_t *mtr, ulint comp,
page_type_t page_type) {
page_create_write_log(buf_block_get_frame(block), mtr, comp, page_type);
return (page_create_low(block, comp, page_type));
}
/** Create a compressed B-tree index page.
@param[in,out] block Buffer frame where the page is created
@param[in] index Index of the page, or NULL when applying
TRUNCATE log record during recovery
@param[in] level The B-tree level of the page
@param[in] max_trx_id PAGE_MAX_TRX_ID
@param[in] mtr Mini-transaction handle
@param[in] page_type Page type to be created. Only FIL_PAGE_INDEX,
FIL_PAGE_RTREE, FIL_PAGE_SDI allowed
@return pointer to the page */
page_t *page_create_zip(buf_block_t *block, dict_index_t *index, ulint level,
trx_id_t max_trx_id, mtr_t *mtr,
page_type_t page_type) {
page_t *page;
page_zip_des_t *page_zip = buf_block_get_page_zip(block);
ut_ad(block);
ut_ad(page_zip);
ut_ad(dict_table_is_comp(index->table));
#ifdef UNIV_DEBUG
switch (page_type) {
case FIL_PAGE_INDEX:
case FIL_PAGE_RTREE:
case FIL_PAGE_SDI:
break;
default:
ut_d(ut_error);
}
#endif /* UNIV_DEBUG */
page = page_create_low(block, true, page_type);
mach_write_to_2(PAGE_HEADER + PAGE_LEVEL + page, level);
mach_write_to_8(PAGE_HEADER + PAGE_MAX_TRX_ID + page, max_trx_id);
if (!page_zip_compress(page_zip, page, index, page_zip_level, mtr)) {
/* The compression of a newly created
page should always succeed. */
ut_error;
}
return (page);
}
/** Empty a previously created B-tree index page.
@param[in,out] block B-tree block
@param[in] index The index of the page
@param[in,out] mtr Mini-transaction */
void page_create_empty(buf_block_t *block, dict_index_t *index, mtr_t *mtr) {
trx_id_t max_trx_id = 0;
page_t *page = buf_block_get_frame(block);
page_zip_des_t *page_zip = buf_block_get_page_zip(block);
ut_ad(fil_page_index_page_check(page));
/* Multiple transactions cannot simultaneously operate on the
same temp-table in parallel.
max_trx_id is ignored for temp tables because it not required
for MVCC. */
if (dict_index_is_sec_or_ibuf(index) && !index->table->is_temporary() &&
page_is_leaf(page)) {
max_trx_id = page_get_max_trx_id(page);
ut_ad(max_trx_id);
}
if (page_zip) {
ut_ad(!index->table->is_temporary());
page_create_zip(block, index, page_header_get_field(page, PAGE_LEVEL),
max_trx_id, mtr, fil_page_get_type(page));
} else {
page_create(block, mtr, page_is_comp(page), fil_page_get_type(page));
if (max_trx_id) {
page_update_max_trx_id(block, page_zip, max_trx_id, mtr);
}
}
}
/** Differs from page_copy_rec_list_end, because this function does not
touch the lock table and max trx id on page or compress the page.
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
if new_block is a compressed leaf page in a secondary index.
This has to be done either within the same mini-transaction,
or by invoking ibuf_reset_free_bits() before mtr_commit(). */
void page_copy_rec_list_end_no_locks(
buf_block_t *new_block, /*!< in: index page to copy to */
buf_block_t *block, /*!< in: index page of rec */
rec_t *rec, /*!< in: record on page */
dict_index_t *index, /*!< in: record descriptor */
mtr_t *mtr) /*!< in: mtr */
{
page_t *new_page = buf_block_get_frame(new_block);
page_cur_t cur1;
rec_t *cur2;
mem_heap_t *heap = nullptr;
ulint offsets_[REC_OFFS_NORMAL_SIZE];
ulint *offsets = offsets_;
rec_offs_init(offsets_);
page_cur_position(rec, block, &cur1);
if (page_cur_is_before_first(&cur1)) {
page_cur_move_to_next(&cur1);
}
btr_assert_not_corrupted(new_block, index);
ut_a(page_is_comp(new_page) == page_rec_is_comp(rec));
ut_a(mach_read_from_2(new_page + UNIV_PAGE_SIZE - 10) ==
(ulint)(page_is_comp(new_page) ? PAGE_NEW_INFIMUM : PAGE_OLD_INFIMUM));
cur2 = page_get_infimum_rec(buf_block_get_frame(new_block));
/* Copy records from the original page to the new page */
while (!page_cur_is_after_last(&cur1)) {
rec_t *cur1_rec = page_cur_get_rec(&cur1);
rec_t *ins_rec;
offsets = rec_get_offsets(cur1_rec, index, offsets, ULINT_UNDEFINED,
UT_LOCATION_HERE, &heap);
ins_rec = page_cur_insert_rec_low(cur2, index, cur1_rec, offsets, mtr);
if (UNIV_UNLIKELY(!ins_rec)) {
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_862)
<< "Rec offset " << page_offset(rec) << ", cur1 offset "
<< page_offset(page_cur_get_rec(&cur1)) << ", cur2 offset "
<< page_offset(cur2);
}
page_cur_move_to_next(&cur1);
cur2 = ins_rec;
}
if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
}
}
#ifndef UNIV_HOTBACKUP
/** Copies records from page to new_page, from a given record onward,
including that record. Infimum and supremum records are not copied.
The records are copied to the start of the record list on new_page.
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
if new_block is a compressed leaf page in a secondary index.
This has to be done either within the same mini-transaction,
or by invoking ibuf_reset_free_bits() before mtr_commit().
@return pointer to the original successor of the infimum record on
new_page, or NULL on zip overflow (new_block will be decompressed) */
rec_t *page_copy_rec_list_end(
buf_block_t *new_block, /*!< in/out: index page to copy to */
buf_block_t *block, /*!< in: index page containing rec */
rec_t *rec, /*!< in: record on page */
dict_index_t *index, /*!< in: record descriptor */
mtr_t *mtr) /*!< in: mtr */
{
page_t *new_page = buf_block_get_frame(new_block);
page_zip_des_t *new_page_zip = buf_block_get_page_zip(new_block);
page_t *page = page_align(rec);
rec_t *ret = page_rec_get_next(page_get_infimum_rec(new_page));
ulint num_moved = 0;
rtr_rec_move_t *rec_move = nullptr;
mem_heap_t *heap = nullptr;
#ifdef UNIV_ZIP_DEBUG
if (new_page_zip) {
page_zip_des_t *page_zip = buf_block_get_page_zip(block);
ut_a(page_zip);
/* Strict page_zip_validate() may fail here.
Furthermore, btr_compress() may set FIL_PAGE_PREV to
FIL_NULL on new_page while leaving it intact on
new_page_zip. So, we cannot validate new_page_zip. */
ut_a(page_zip_validate_low(page_zip, page, index, true));
}
#endif /* UNIV_ZIP_DEBUG */
ut_ad(buf_block_get_frame(block) == page);
ut_ad(page_is_leaf(page) == page_is_leaf(new_page));
ut_ad(page_is_comp(page) == page_is_comp(new_page));
/* Here, "ret" may be pointing to a user record or the
predefined supremum record. */
mtr_log_t log_mode = MTR_LOG_NONE;
if (new_page_zip) {
log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
}
if (page_dir_get_n_heap(new_page) == PAGE_HEAP_NO_USER_LOW) {
page_copy_rec_list_end_to_created_page(new_page, rec, index, mtr);
} else {
if (dict_index_is_spatial(index)) {
ulint max_to_move = page_get_n_recs(buf_block_get_frame(block));
heap = mem_heap_create(256, UT_LOCATION_HERE);
rec_move = static_cast<rtr_rec_move_t *>(
mem_heap_alloc(heap, sizeof(*rec_move) * max_to_move));
/* For spatial index, we need to insert recs one by one
to keep recs ordered. */
rtr_page_copy_rec_list_end_no_locks(new_block, block, rec, index, heap,
rec_move, max_to_move, &num_moved,
mtr);
} else {
page_copy_rec_list_end_no_locks(new_block, block, rec, index, mtr);
}
}
/* Update PAGE_MAX_TRX_ID on the uncompressed page.
Modifications will be redo logged and copied to the compressed
page in page_zip_compress() or page_zip_reorganize() below.
Multiple transactions cannot simultaneously operate on the
same temp-table in parallel.
max_trx_id is ignored for temp tables because it not required
for MVCC. */
if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page) &&
!index->table->is_temporary()) {
page_update_max_trx_id(new_block, nullptr, page_get_max_trx_id(page), mtr);
}
if (new_page_zip) {
mtr_set_log_mode(mtr, log_mode);
if (!page_zip_compress(new_page_zip, new_page, index, page_zip_level,
mtr)) {
/* Before trying to reorganize the page,
store the number of preceding records on the page. */
ulint ret_pos = page_rec_get_n_recs_before(ret);
/* Before copying, "ret" was the successor of
the predefined infimum record. It must still
have at least one predecessor (the predefined
infimum record, or a freshly copied record
that is smaller than "ret"). */
ut_a(ret_pos > 0);
if (!page_zip_reorganize(new_block, index, mtr)) {
if (!page_zip_decompress(new_page_zip, new_page, false)) {
ut_error;
}
ut_ad(page_validate(new_page, index));
if (heap) {
mem_heap_free(heap);
}
return (nullptr);
} else {
/* The page was reorganized:
Seek to ret_pos. */
ret = new_page + PAGE_NEW_INFIMUM;
do {
ret = rec_get_next_ptr(ret, true);
} while (--ret_pos);
}
}
}
/* Update the lock table and possible hash index */
if (dict_index_is_spatial(index) && rec_move) {
lock_rtr_move_rec_list(new_block, block, rec_move, num_moved);
} else if (!dict_table_is_locking_disabled(index->table)) {
lock_move_rec_list_end(new_block, block, rec);
}
if (heap) {
mem_heap_free(heap);
}
btr_search_update_hash_on_move(new_block, block, index);
return (ret);
}
/** Copies records from page to new_page, up to the given record,
NOT including that record. Infimum and supremum records are not copied.
The records are copied to the end of the record list on new_page.
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
if new_block is a compressed leaf page in a secondary index.
This has to be done either within the same mini-transaction,
or by invoking ibuf_reset_free_bits() before mtr_commit().
@return pointer to the original predecessor of the supremum record on
new_page, or NULL on zip overflow (new_block will be decompressed) */
rec_t *page_copy_rec_list_start(
buf_block_t *new_block, /*!< in/out: index page to copy to */
buf_block_t *block, /*!< in: index page containing rec */
rec_t *rec, /*!< in: record on page */
dict_index_t *index, /*!< in: record descriptor */
mtr_t *mtr) /*!< in: mtr */
{
page_t *new_page = buf_block_get_frame(new_block);
page_zip_des_t *new_page_zip = buf_block_get_page_zip(new_block);
page_cur_t cur1;
rec_t *cur2;
mem_heap_t *heap = nullptr;
ulint num_moved = 0;
rtr_rec_move_t *rec_move = nullptr;
rec_t *ret = page_rec_get_prev(page_get_supremum_rec(new_page));
ulint offsets_[REC_OFFS_NORMAL_SIZE];
ulint *offsets = offsets_;
rec_offs_init(offsets_);
/* Here, "ret" may be pointing to a user record or the
predefined infimum record. */
if (page_rec_is_infimum(rec)) {
return (ret);
}
mtr_log_t log_mode = MTR_LOG_NONE;
if (new_page_zip) {
log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
}
page_cur_set_before_first(block, &cur1);
page_cur_move_to_next(&cur1);
cur2 = ret;
/* Copy records from the original page to the new page */
if (dict_index_is_spatial(index)) {
ulint max_to_move = page_get_n_recs(buf_block_get_frame(block));
heap = mem_heap_create(256, UT_LOCATION_HERE);
rec_move = static_cast<rtr_rec_move_t *>(
mem_heap_alloc(heap, sizeof(*rec_move) * max_to_move));
/* For spatial index, we need to insert recs one by one
to keep recs ordered. */
rtr_page_copy_rec_list_start_no_locks(new_block, block, rec, index, heap,
rec_move, max_to_move, &num_moved,
mtr);
} else {
while (page_cur_get_rec(&cur1) != rec) {
rec_t *cur1_rec = page_cur_get_rec(&cur1);
offsets = rec_get_offsets(cur1_rec, index, offsets, ULINT_UNDEFINED,
UT_LOCATION_HERE, &heap);
cur2 = page_cur_insert_rec_low(cur2, index, cur1_rec, offsets, mtr);
ut_a(cur2);
page_cur_move_to_next(&cur1);
}
}
/* Update PAGE_MAX_TRX_ID on the uncompressed page.
Modifications will be redo logged and copied to the compressed
page in page_zip_compress() or page_zip_reorganize() below.
Multiple transactions cannot simultaneously operate on the
same temp-table in parallel.
max_trx_id is ignored for temp tables because it not required
for MVCC. */
if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page_align(rec)) &&
!index->table->is_temporary()) {
page_update_max_trx_id(new_block, nullptr,
page_get_max_trx_id(page_align(rec)), mtr);
}
if (new_page_zip) {
mtr_set_log_mode(mtr, log_mode);
DBUG_EXECUTE_IF("page_copy_rec_list_start_compress_fail",
goto zip_reorganize;);
if (!page_zip_compress(new_page_zip, new_page, index, page_zip_level,
mtr)) {
ulint ret_pos;
#ifdef UNIV_DEBUG
zip_reorganize:
#endif /* UNIV_DEBUG */
/* Before trying to reorganize the page,
store the number of preceding records on the page. */
ret_pos = page_rec_get_n_recs_before(ret);
/* Before copying, "ret" was the predecessor
of the predefined supremum record. If it was
the predefined infimum record, then it would
still be the infimum, and we would have
ret_pos == 0. */
if (UNIV_UNLIKELY(!page_zip_reorganize(new_block, index, mtr))) {
if (UNIV_UNLIKELY(
!page_zip_decompress(new_page_zip, new_page, false))) {
ut_error;
}
ut_ad(page_validate(new_page, index));
if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
}
return (nullptr);
}
/* The page was reorganized: Seek to ret_pos. */
ret = page_rec_get_nth(new_page, ret_pos);
}
}
/* Update the lock table and possible hash index */
if (dict_index_is_spatial(index)) {
lock_rtr_move_rec_list(new_block, block, rec_move, num_moved);
} else if (!dict_table_is_locking_disabled(index->table)) {
lock_move_rec_list_start(new_block, block, rec, ret);
}
if (heap) {
mem_heap_free(heap);
}
btr_search_update_hash_on_move(new_block, block, index);
return (ret);
}
/** Writes a log record of a record list end or start deletion. */
static inline void page_delete_rec_list_write_log(
rec_t *rec, /*!< in: record on page */
dict_index_t *index, /*!< in: record descriptor */
mlog_id_t type, /*!< in: operation type:
MLOG_LIST_END_DELETE, ... */
mtr_t *mtr) /*!< in: mtr */
{
byte *log_ptr = nullptr;
ut_ad(type == MLOG_LIST_END_DELETE || type == MLOG_LIST_START_DELETE);
if (!mlog_open_and_write_index(mtr, rec, index, type, 2, log_ptr)) {
return;
}
/* Write the parameter as a 2-byte ulint */
mach_write_to_2(log_ptr, page_offset(rec));
mlog_close(mtr, log_ptr + 2);
}
#else /* !UNIV_HOTBACKUP */
#define page_delete_rec_list_write_log(rec, index, type, mtr) ((void)0)
#endif /* !UNIV_HOTBACKUP */
/** Parses a log record of a record list end or start deletion.
@return end of log record or NULL */
byte *page_parse_delete_rec_list(
mlog_id_t type, /*!< in: MLOG_LIST_END_DELETE,
MLOG_LIST_START_DELETE,
MLOG_COMP_LIST_END_DELETE or
MLOG_COMP_LIST_START_DELETE */
byte *ptr, /*!< in: buffer */
byte *end_ptr, /*!< in: buffer end */
buf_block_t *block, /*!< in/out: buffer block or NULL */
dict_index_t *index, /*!< in: record descriptor */
mtr_t *mtr) /*!< in: mtr or NULL */
{
page_t *page;
ulint offset;
ut_ad(type == MLOG_LIST_END_DELETE_8027 ||
type == MLOG_LIST_START_DELETE_8027 ||
type == MLOG_COMP_LIST_END_DELETE_8027 ||
type == MLOG_COMP_LIST_START_DELETE_8027 ||
type == MLOG_LIST_END_DELETE || type == MLOG_LIST_START_DELETE);
/* Read the record offset as a 2-byte ulint */
if (end_ptr < ptr + 2) {
return (nullptr);
}
offset = mach_read_from_2(ptr);
ptr += 2;
if (!block) {
return (ptr);
}
page = buf_block_get_frame(block);
ut_ad(page_is_comp(page) == dict_table_is_comp(index->table));
if (type == MLOG_LIST_END_DELETE || type == MLOG_COMP_LIST_END_DELETE_8027 ||
type == MLOG_LIST_END_DELETE_8027) {
page_delete_rec_list_end(page + offset, block, index, ULINT_UNDEFINED,
ULINT_UNDEFINED, mtr);
} else {
page_delete_rec_list_start(page + offset, block, index, mtr);
}
return (ptr);
}
/** Deletes records from a page from a given record onward, including that
record. The infimum and supremum records are not deleted. */
void page_delete_rec_list_end(
rec_t *rec, /*!< in: pointer to record on page */
buf_block_t *block, /*!< in: buffer block of the page */
dict_index_t *index, /*!< in: record descriptor */
ulint n_recs, /*!< in: number of records to delete,
or ULINT_UNDEFINED if not known */
ulint size, /*!< in: the sum of the sizes of the
records in the end of the chain to
delete, or ULINT_UNDEFINED if not known */
mtr_t *mtr) /*!< in: mtr */
{
page_dir_slot_t *slot;
ulint slot_index;
rec_t *last_rec;
rec_t *prev_rec;
ulint n_owned;
page_zip_des_t *page_zip = buf_block_get_page_zip(block);
page_t *page = page_align(rec);
mem_heap_t *heap = nullptr;
ulint offsets_[REC_OFFS_NORMAL_SIZE];
ulint *offsets = offsets_;
rec_offs_init(offsets_);
ut_ad(size == ULINT_UNDEFINED || size < UNIV_PAGE_SIZE);
ut_ad(!page_zip || page_rec_is_comp(rec));
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip || page_zip_validate(page_zip, page, index));
#endif /* UNIV_ZIP_DEBUG */
if (page_rec_is_supremum(rec)) {
ut_ad(n_recs == 0 || n_recs == ULINT_UNDEFINED);
/* Nothing to do, there are no records bigger than the
page supremum. */
return;
}
if (recv_recovery_is_on()) {
/* If we are replaying a redo log record, we must
replay it exactly. Since MySQL 5.6.11, we should be
generating a redo log record for page creation if
the page would become empty. Thus, this branch should
only be executed when applying redo log that was
generated by an older version of MySQL. */
} else if (page_rec_is_infimum(rec) || n_recs == page_get_n_recs(page)) {
delete_all:
/* We are deleting all records. */
page_create_empty(block, index, mtr);
return;
} else if (page_is_comp(page)) {
if (page_rec_get_next_low(page + PAGE_NEW_INFIMUM, 1) == rec) {
/* We are deleting everything from the first
user record onwards. */
goto delete_all;
}
} else {
if (page_rec_get_next_low(page + PAGE_OLD_INFIMUM, 0) == rec) {
/* We are deleting everything from the first
user record onwards. */
goto delete_all;
}
}
/* Reset the last insert info in the page header and increment
the modify clock for the frame */
page_header_set_ptr(page, page_zip, PAGE_LAST_INSERT, nullptr);
/* The page gets invalid for optimistic searches: increment the
frame modify clock */
buf_block_modify_clock_inc(block);
page_delete_rec_list_write_log(rec, index, MLOG_LIST_END_DELETE, mtr);
if (page_zip) {
mtr_log_t log_mode;
ut_a(page_is_comp(page));
/* Individual deletes are not logged */
log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
do {
page_cur_t cur;
page_cur_position(rec, block, &cur);
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
UT_LOCATION_HERE, &heap);
rec = rec_get_next_ptr(rec, true);
#ifdef UNIV_ZIP_DEBUG
ut_a(page_zip_validate(page_zip, page, index));
#endif /* UNIV_ZIP_DEBUG */
page_cur_delete_rec(&cur, index, offsets, mtr);
} while (page_offset(rec) != PAGE_NEW_SUPREMUM);
if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
}
/* Restore log mode */
mtr_set_log_mode(mtr, log_mode);
return;
}
prev_rec = page_rec_get_prev(rec);
last_rec = page_rec_get_prev(page_get_supremum_rec(page));
if ((size == ULINT_UNDEFINED) || (n_recs == ULINT_UNDEFINED)) {
rec_t *rec2 = rec;
/* Calculate the sum of sizes and the number of records */
size = 0;
n_recs = 0;
do {
ulint s;
offsets = rec_get_offsets(rec2, index, offsets, ULINT_UNDEFINED,
UT_LOCATION_HERE, &heap);
s = rec_offs_size(offsets);
ut_ad(rec2 - page + s - rec_offs_extra_size(offsets) < UNIV_PAGE_SIZE);
ut_ad(size + s < UNIV_PAGE_SIZE);
size += s;
n_recs++;
rec2 = page_rec_get_next(rec2);
} while (!page_rec_is_supremum(rec2));