/
row0merge.cc
3871 lines (3248 loc) · 104 KB
/
row0merge.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) 2005, 2015, Oracle and/or its affiliates. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
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 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 Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file row/row0merge.cc
New index creation routines using a merge sort
Created 12/4/2005 Jan Lindstrom
Completed by Sunny Bains and Marko Makela
*******************************************************/
#include "row0merge.h"
#include "row0ext.h"
#include "row0log.h"
#include "row0ins.h"
#include "row0sel.h"
#include "dict0crea.h"
#include "trx0purge.h"
#include "lock0lock.h"
#include "pars0pars.h"
#include "ut0sort.h"
#include "row0ftsort.h"
#include "row0import.h"
#include "handler0alter.h"
#include "ha_prototypes.h"
/* Ignore posix_fadvise() on those platforms where it does not exist */
#if defined __WIN__
# define posix_fadvise(fd, offset, len, advice) /* nothing */
#endif /* __WIN__ */
#ifdef UNIV_DEBUG
/** Set these in order ot enable debug printout. */
/* @{ */
/** Log each record read from temporary file. */
static ibool row_merge_print_read;
/** Log each record write to temporary file. */
static ibool row_merge_print_write;
/** Log each row_merge_blocks() call, merging two blocks of records to
a bigger one. */
static ibool row_merge_print_block;
/** Log each block read from temporary file. */
static ibool row_merge_print_block_read;
/** Log each block read from temporary file. */
static ibool row_merge_print_block_write;
/* @} */
#endif /* UNIV_DEBUG */
/* Whether to disable file system cache */
UNIV_INTERN char srv_disable_sort_file_cache;
/* Maximum pending doc memory limit in bytes for a fts tokenization thread */
#define FTS_PENDING_DOC_MEMORY_LIMIT 1000000
#ifdef UNIV_DEBUG
/******************************************************//**
Display a merge tuple. */
static __attribute__((nonnull))
void
row_merge_tuple_print(
/*==================*/
FILE* f, /*!< in: output stream */
const mtuple_t* entry, /*!< in: tuple to print */
ulint n_fields)/*!< in: number of fields in the tuple */
{
ulint j;
for (j = 0; j < n_fields; j++) {
const dfield_t* field = &entry->fields[j];
if (dfield_is_null(field)) {
fputs("\n NULL;", f);
} else {
ulint field_len = dfield_get_len(field);
ulint len = ut_min(field_len, 20);
if (dfield_is_ext(field)) {
fputs("\nE", f);
} else {
fputs("\n ", f);
}
ut_print_buf(f, dfield_get_data(field), len);
if (len != field_len) {
fprintf(f, " (total %lu bytes)", field_len);
}
}
}
putc('\n', f);
}
#endif /* UNIV_DEBUG */
/******************************************************//**
Encode an index record. */
static __attribute__((nonnull))
void
row_merge_buf_encode(
/*=================*/
byte** b, /*!< in/out: pointer to
current end of output buffer */
const dict_index_t* index, /*!< in: index */
const mtuple_t* entry, /*!< in: index fields
of the record to encode */
ulint n_fields) /*!< in: number of fields
in the entry */
{
ulint size;
ulint extra_size;
size = rec_get_converted_size_temp(
index, entry->fields, n_fields, &extra_size);
ut_ad(size >= extra_size);
/* Encode extra_size + 1 */
if (extra_size + 1 < 0x80) {
*(*b)++ = (byte) (extra_size + 1);
} else {
ut_ad((extra_size + 1) < 0x8000);
*(*b)++ = (byte) (0x80 | ((extra_size + 1) >> 8));
*(*b)++ = (byte) (extra_size + 1);
}
rec_convert_dtuple_to_temp(*b + extra_size, index,
entry->fields, n_fields);
*b += size;
}
/******************************************************//**
Allocate a sort buffer.
@return own: sort buffer */
static __attribute__((malloc, nonnull))
row_merge_buf_t*
row_merge_buf_create_low(
/*=====================*/
mem_heap_t* heap, /*!< in: heap where allocated */
dict_index_t* index, /*!< in: secondary index */
ulint max_tuples, /*!< in: maximum number of
data tuples */
ulint buf_size) /*!< in: size of the buffer,
in bytes */
{
row_merge_buf_t* buf;
ut_ad(max_tuples > 0);
ut_ad(max_tuples <= srv_sort_buf_size);
buf = static_cast<row_merge_buf_t*>(mem_heap_zalloc(heap, buf_size));
buf->heap = heap;
buf->index = index;
buf->max_tuples = max_tuples;
buf->tuples = static_cast<mtuple_t*>(
ut_malloc(2 * max_tuples * sizeof *buf->tuples));
buf->tmp_tuples = buf->tuples + max_tuples;
return(buf);
}
/******************************************************//**
Allocate a sort buffer.
@return own: sort buffer */
UNIV_INTERN
row_merge_buf_t*
row_merge_buf_create(
/*=================*/
dict_index_t* index) /*!< in: secondary index */
{
row_merge_buf_t* buf;
ulint max_tuples;
ulint buf_size;
mem_heap_t* heap;
max_tuples = srv_sort_buf_size
/ ut_max(1, dict_index_get_min_size(index));
buf_size = (sizeof *buf);
heap = mem_heap_create(buf_size);
buf = row_merge_buf_create_low(heap, index, max_tuples, buf_size);
return(buf);
}
/******************************************************//**
Empty a sort buffer.
@return sort buffer */
UNIV_INTERN
row_merge_buf_t*
row_merge_buf_empty(
/*================*/
row_merge_buf_t* buf) /*!< in,own: sort buffer */
{
ulint buf_size = sizeof *buf;
ulint max_tuples = buf->max_tuples;
mem_heap_t* heap = buf->heap;
dict_index_t* index = buf->index;
mtuple_t* tuples = buf->tuples;
mem_heap_empty(heap);
buf = static_cast<row_merge_buf_t*>(mem_heap_zalloc(heap, buf_size));
buf->heap = heap;
buf->index = index;
buf->max_tuples = max_tuples;
buf->tuples = tuples;
buf->tmp_tuples = buf->tuples + max_tuples;
return(buf);
}
/******************************************************//**
Deallocate a sort buffer. */
UNIV_INTERN
void
row_merge_buf_free(
/*===============*/
row_merge_buf_t* buf) /*!< in,own: sort buffer to be freed */
{
ut_free(buf->tuples);
mem_heap_free(buf->heap);
}
/** Convert the field data from compact to redundant format.
@param[in] row_field field to copy from
@param[out] field field to copy to
@param[in] len length of the field data
@param[in] zip_size compressed BLOB page size,
zero for uncompressed BLOBs
@param[in,out] heap memory heap where to allocate data when
converting to ROW_FORMAT=REDUNDANT, or NULL
when not to invoke
row_merge_buf_redundant_convert(). */
static
void
row_merge_buf_redundant_convert(
const dfield_t* row_field,
dfield_t* field,
ulint len,
ulint zip_size,
mem_heap_t* heap)
{
ut_ad(DATA_MBMINLEN(field->type.mbminmaxlen) == 1);
ut_ad(DATA_MBMAXLEN(field->type.mbminmaxlen) > 1);
byte* buf = (byte*) mem_heap_alloc(heap, len);
ulint field_len = row_field->len;
ut_ad(field_len <= len);
if (row_field->ext) {
const byte* field_data = static_cast<byte*>(
dfield_get_data(row_field));
ulint ext_len;
ut_a(field_len >= BTR_EXTERN_FIELD_REF_SIZE);
ut_a(memcmp(field_data + field_len - BTR_EXTERN_FIELD_REF_SIZE,
field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));
byte* data = btr_copy_externally_stored_field(
&ext_len, field_data, zip_size, field_len, heap, NULL);
ut_ad(ext_len < len);
memcpy(buf, data, ext_len);
field_len = ext_len;
} else {
memcpy(buf, row_field->data, field_len);
}
memset(buf + field_len, 0x20, len - field_len);
dfield_set_data(field, buf, len);
}
/** Insert a data tuple into a sort buffer.
@param[in,out] buf sort buffer
@param[in] fts_index fts index to be created
@param[in] old_table original table
@param[in,out] psort_info parallel sort info
@param[in] row table row
@param[in] ext cache of externally stored
column prefixes, or NULL
@param[in,out] doc_id Doc ID if we are creating
FTS index
@param[in,out] conv_heap memory heap where to allocate data when
converting to ROW_FORMAT=REDUNDANT, or NULL
when not to invoke
row_merge_buf_redundant_convert()
@param[in,out] exceed_page set if the record size exceeds the page size
when converting to ROW_FORMAT=REDUNDANT
@return number of rows added, 0 if out of space */
static
ulint
row_merge_buf_add(
row_merge_buf_t* buf,
dict_index_t* fts_index,
const dict_table_t* old_table,
fts_psort_t* psort_info,
const dtuple_t* row,
const row_ext_t* ext,
doc_id_t* doc_id,
mem_heap_t* conv_heap,
bool* exceed_page)
{
ulint i;
const dict_index_t* index;
mtuple_t* entry;
dfield_t* field;
const dict_field_t* ifield;
ulint n_fields;
ulint data_size;
ulint extra_size;
ulint bucket = 0;
doc_id_t write_doc_id;
ulint n_row_added = 0;
DBUG_ENTER("row_merge_buf_add");
if (buf->n_tuples >= buf->max_tuples) {
DBUG_RETURN(0);
}
DBUG_EXECUTE_IF(
"ib_row_merge_buf_add_two",
if (buf->n_tuples >= 2) DBUG_RETURN(0););
UNIV_PREFETCH_R(row->fields);
/* If we are building FTS index, buf->index points to
the 'fts_sort_idx', and real FTS index is stored in
fts_index */
index = (buf->index->type & DICT_FTS) ? fts_index : buf->index;
n_fields = dict_index_get_n_fields(index);
entry = &buf->tuples[buf->n_tuples];
field = entry->fields = static_cast<dfield_t*>(
mem_heap_alloc(buf->heap, n_fields * sizeof *entry->fields));
data_size = 0;
extra_size = UT_BITS_IN_BYTES(index->n_nullable);
ifield = dict_index_get_nth_field(index, 0);
for (i = 0; i < n_fields; i++, field++, ifield++) {
ulint len;
const dict_col_t* col;
ulint col_no;
ulint fixed_len;
const dfield_t* row_field;
col = ifield->col;
col_no = dict_col_get_no(col);
/* Process the Doc ID column */
if (*doc_id > 0
&& col_no == index->table->fts->doc_col) {
fts_write_doc_id((byte*) &write_doc_id, *doc_id);
/* Note: field->data now points to a value on the
stack: &write_doc_id after dfield_set_data(). Because
there is only one doc_id per row, it shouldn't matter.
We allocate a new buffer before we leave the function
later below. */
dfield_set_data(
field, &write_doc_id, sizeof(write_doc_id));
field->type.mtype = ifield->col->mtype;
field->type.prtype = ifield->col->prtype;
field->type.mbminmaxlen = DATA_MBMINMAXLEN(0, 0);
field->type.len = ifield->col->len;
} else {
row_field = dtuple_get_nth_field(row, col_no);
dfield_copy(field, row_field);
/* Tokenize and process data for FTS */
if (index->type & DICT_FTS) {
fts_doc_item_t* doc_item;
byte* value;
void* ptr;
const ulint max_trial_count = 10000;
ulint trial_count = 0;
/* fetch Doc ID if it already exists
in the row, and not supplied by the
caller. Even if the value column is
NULL, we still need to get the Doc
ID so to maintain the correct max
Doc ID */
if (*doc_id == 0) {
const dfield_t* doc_field;
doc_field = dtuple_get_nth_field(
row,
index->table->fts->doc_col);
*doc_id = (doc_id_t) mach_read_from_8(
static_cast<byte*>(
dfield_get_data(doc_field)));
if (*doc_id == 0) {
ib_logf(IB_LOG_LEVEL_WARN,
"FTS Doc ID is zero. "
"Record Skipped");
DBUG_RETURN(0);
}
}
if (dfield_is_null(field)) {
n_row_added = 1;
continue;
}
ptr = ut_malloc(sizeof(*doc_item)
+ field->len);
doc_item = static_cast<fts_doc_item_t*>(ptr);
value = static_cast<byte*>(ptr)
+ sizeof(*doc_item);
memcpy(value, field->data, field->len);
field->data = value;
doc_item->field = field;
doc_item->doc_id = *doc_id;
bucket = *doc_id % fts_sort_pll_degree;
/* Add doc item to fts_doc_list */
mutex_enter(&psort_info[bucket].mutex);
if (psort_info[bucket].error == DB_SUCCESS) {
UT_LIST_ADD_LAST(
doc_list,
psort_info[bucket].fts_doc_list,
doc_item);
psort_info[bucket].memory_used +=
sizeof(*doc_item) + field->len;
} else {
ut_free(doc_item);
}
mutex_exit(&psort_info[bucket].mutex);
/* Sleep when memory used exceeds limit*/
while (psort_info[bucket].memory_used
> FTS_PENDING_DOC_MEMORY_LIMIT
&& trial_count++ < max_trial_count) {
os_thread_sleep(1000);
}
n_row_added = 1;
continue;
}
if (field->len != UNIV_SQL_NULL
&& col->mtype == DATA_MYSQL
&& col->len != field->len) {
if (conv_heap != NULL) {
row_merge_buf_redundant_convert(
row_field, field, col->len,
dict_table_zip_size(old_table),
conv_heap);
} else {
/* Field length mismatch should not
happen when rebuilding redundant row
format table. */
ut_ad(dict_table_is_comp(index->table));
}
}
}
len = dfield_get_len(field);
if (dfield_is_null(field)) {
ut_ad(!(col->prtype & DATA_NOT_NULL));
continue;
} else if (!ext) {
} else if (dict_index_is_clust(index)) {
/* Flag externally stored fields. */
const byte* buf = row_ext_lookup(ext, col_no,
&len);
if (UNIV_LIKELY_NULL(buf)) {
ut_a(buf != field_ref_zero);
if (i < dict_index_get_n_unique(index)) {
dfield_set_data(field, buf, len);
} else {
dfield_set_ext(field);
len = dfield_get_len(field);
}
}
} else {
const byte* buf = row_ext_lookup(ext, col_no,
&len);
if (UNIV_LIKELY_NULL(buf)) {
ut_a(buf != field_ref_zero);
dfield_set_data(field, buf, len);
}
}
/* If a column prefix index, take only the prefix */
if (ifield->prefix_len) {
len = dtype_get_at_most_n_mbchars(
col->prtype,
col->mbminmaxlen,
ifield->prefix_len,
len,
static_cast<char*>(dfield_get_data(field)));
dfield_set_len(field, len);
}
ut_ad(len <= col->len || col->mtype == DATA_BLOB);
fixed_len = ifield->fixed_len;
if (fixed_len && !dict_table_is_comp(index->table)
&& DATA_MBMINLEN(col->mbminmaxlen)
!= DATA_MBMAXLEN(col->mbminmaxlen)) {
/* CHAR in ROW_FORMAT=REDUNDANT is always
fixed-length, but in the temporary file it is
variable-length for variable-length character
sets. */
fixed_len = 0;
}
if (fixed_len) {
#ifdef UNIV_DEBUG
ulint mbminlen = DATA_MBMINLEN(col->mbminmaxlen);
ulint mbmaxlen = DATA_MBMAXLEN(col->mbminmaxlen);
/* len should be between size calcualted base on
mbmaxlen and mbminlen */
ut_ad(len <= fixed_len);
ut_ad(!mbmaxlen || len >= mbminlen
* (fixed_len / mbmaxlen));
ut_ad(!dfield_is_ext(field));
#endif /* UNIV_DEBUG */
} else if (dfield_is_ext(field)) {
extra_size += 2;
} else if (len < 128
|| (col->len < 256 && col->mtype != DATA_BLOB)) {
extra_size++;
} else {
/* For variable-length columns, we look up the
maximum length from the column itself. If this
is a prefix index column shorter than 256 bytes,
this will waste one byte. */
extra_size += 2;
}
data_size += len;
}
/* If this is FTS index, we already populated the sort buffer, return
here */
if (index->type & DICT_FTS) {
DBUG_RETURN(n_row_added);
}
#ifdef UNIV_DEBUG
{
ulint size;
ulint extra;
size = rec_get_converted_size_temp(
index, entry->fields, n_fields, &extra);
ut_ad(data_size + extra_size == size);
ut_ad(extra_size == extra);
}
#endif /* UNIV_DEBUG */
/* Add to the total size of the record in row_merge_block_t
the encoded length of extra_size and the extra bytes (extra_size).
See row_merge_buf_write() for the variable-length encoding
of extra_size. */
data_size += (extra_size + 1) + ((extra_size + 1) >= 0x80);
/* Record size can exceed page size while converting to
redundant row format. But there is assert
ut_ad(size < UNIV_PAGE_SIZE) in rec_offs_data_size().
It may hit the assert before attempting to insert the row. */
if (conv_heap != NULL && data_size > UNIV_PAGE_SIZE) {
*exceed_page = true;
}
ut_ad(data_size < srv_sort_buf_size);
/* Reserve one byte for the end marker of row_merge_block_t. */
if (buf->total_size + data_size >= srv_sort_buf_size - 1) {
DBUG_RETURN(0);
}
buf->total_size += data_size;
buf->n_tuples++;
n_row_added++;
field = entry->fields;
/* Copy the data fields. */
do {
dfield_dup(field++, buf->heap);
} while (--n_fields);
if (conv_heap != NULL) {
mem_heap_empty(conv_heap);
}
DBUG_RETURN(n_row_added);
}
/*************************************************************//**
Report a duplicate key. */
UNIV_INTERN
void
row_merge_dup_report(
/*=================*/
row_merge_dup_t* dup, /*!< in/out: for reporting duplicates */
const dfield_t* entry) /*!< in: duplicate index entry */
{
if (!dup->n_dup++) {
/* Only report the first duplicate record,
but count all duplicate records. */
innobase_fields_to_mysql(dup->table, dup->index, entry);
}
}
/*************************************************************//**
Compare two tuples.
@return 1, 0, -1 if a is greater, equal, less, respectively, than b */
static __attribute__((warn_unused_result))
int
row_merge_tuple_cmp(
/*================*/
ulint n_uniq, /*!< in: number of unique fields */
ulint n_field,/*!< in: number of fields */
const mtuple_t& a, /*!< in: first tuple to be compared */
const mtuple_t& b, /*!< in: second tuple to be compared */
row_merge_dup_t* dup) /*!< in/out: for reporting duplicates,
NULL if non-unique index */
{
int cmp;
const dfield_t* af = a.fields;
const dfield_t* bf = b.fields;
ulint n = n_uniq;
ut_ad(n_uniq > 0);
ut_ad(n_uniq <= n_field);
/* Compare the fields of the tuples until a difference is
found or we run out of fields to compare. If !cmp at the
end, the tuples are equal. */
do {
cmp = cmp_dfield_dfield(af++, bf++);
} while (!cmp && --n);
if (cmp) {
return(cmp);
}
if (dup) {
/* Report a duplicate value error if the tuples are
logically equal. NULL columns are logically inequal,
although they are equal in the sorting order. Find
out if any of the fields are NULL. */
for (const dfield_t* df = a.fields; df != af; df++) {
if (dfield_is_null(df)) {
goto no_report;
}
}
row_merge_dup_report(dup, a.fields);
}
no_report:
/* The n_uniq fields were equal, but we compare all fields so
that we will get the same (internal) order as in the B-tree. */
for (n = n_field - n_uniq + 1; --n; ) {
cmp = cmp_dfield_dfield(af++, bf++);
if (cmp) {
return(cmp);
}
}
/* This should never be reached, except in a secondary index
when creating a secondary index and a PRIMARY KEY, and there
is a duplicate in the PRIMARY KEY that has not been detected
yet. Internally, an index must never contain duplicates. */
return(cmp);
}
/** Wrapper for row_merge_tuple_sort() to inject some more context to
UT_SORT_FUNCTION_BODY().
@param tuples array of tuples that being sorted
@param aux work area, same size as tuples[]
@param low lower bound of the sorting area, inclusive
@param high upper bound of the sorting area, inclusive */
#define row_merge_tuple_sort_ctx(tuples, aux, low, high) \
row_merge_tuple_sort(n_uniq, n_field, dup, tuples, aux, low, high)
/** Wrapper for row_merge_tuple_cmp() to inject some more context to
UT_SORT_FUNCTION_BODY().
@param a first tuple to be compared
@param b second tuple to be compared
@return 1, 0, -1 if a is greater, equal, less, respectively, than b */
#define row_merge_tuple_cmp_ctx(a,b) \
row_merge_tuple_cmp(n_uniq, n_field, a, b, dup)
/**********************************************************************//**
Merge sort the tuple buffer in main memory. */
static __attribute__((nonnull(4,5)))
void
row_merge_tuple_sort(
/*=================*/
ulint n_uniq, /*!< in: number of unique fields */
ulint n_field,/*!< in: number of fields */
row_merge_dup_t* dup, /*!< in/out: reporter of duplicates
(NULL if non-unique index) */
mtuple_t* tuples, /*!< in/out: tuples */
mtuple_t* aux, /*!< in/out: work area */
ulint low, /*!< in: lower bound of the
sorting area, inclusive */
ulint high) /*!< in: upper bound of the
sorting area, exclusive */
{
ut_ad(n_field > 0);
ut_ad(n_uniq <= n_field);
UT_SORT_FUNCTION_BODY(row_merge_tuple_sort_ctx,
tuples, aux, low, high, row_merge_tuple_cmp_ctx);
}
/******************************************************//**
Sort a buffer. */
UNIV_INTERN
void
row_merge_buf_sort(
/*===============*/
row_merge_buf_t* buf, /*!< in/out: sort buffer */
row_merge_dup_t* dup) /*!< in/out: reporter of duplicates
(NULL if non-unique index) */
{
row_merge_tuple_sort(dict_index_get_n_unique(buf->index),
dict_index_get_n_fields(buf->index),
dup,
buf->tuples, buf->tmp_tuples, 0, buf->n_tuples);
}
/******************************************************//**
Write a buffer to a block. */
UNIV_INTERN
void
row_merge_buf_write(
/*================*/
const row_merge_buf_t* buf, /*!< in: sorted buffer */
const merge_file_t* of UNIV_UNUSED,
/*!< in: output file */
row_merge_block_t* block) /*!< out: buffer for writing to file */
{
const dict_index_t* index = buf->index;
ulint n_fields= dict_index_get_n_fields(index);
byte* b = &block[0];
for (ulint i = 0; i < buf->n_tuples; i++) {
const mtuple_t* entry = &buf->tuples[i];
row_merge_buf_encode(&b, index, entry, n_fields);
ut_ad(b < &block[srv_sort_buf_size]);
#ifdef UNIV_DEBUG
if (row_merge_print_write) {
fprintf(stderr, "row_merge_buf_write %p,%d,%lu %lu",
(void*) b, of->fd, (ulong) of->offset,
(ulong) i);
row_merge_tuple_print(stderr, entry, n_fields);
}
#endif /* UNIV_DEBUG */
}
/* Write an "end-of-chunk" marker. */
ut_a(b < &block[srv_sort_buf_size]);
ut_a(b == &block[0] + buf->total_size);
*b++ = 0;
#ifdef UNIV_DEBUG_VALGRIND
/* The rest of the block is uninitialized. Initialize it
to avoid bogus warnings. */
memset(b, 0xff, &block[srv_sort_buf_size] - b);
#endif /* UNIV_DEBUG_VALGRIND */
#ifdef UNIV_DEBUG
if (row_merge_print_write) {
fprintf(stderr, "row_merge_buf_write %p,%d,%lu EOF\n",
(void*) b, of->fd, (ulong) of->offset);
}
#endif /* UNIV_DEBUG */
}
/******************************************************//**
Create a memory heap and allocate space for row_merge_rec_offsets()
and mrec_buf_t[3].
@return memory heap */
static
mem_heap_t*
row_merge_heap_create(
/*==================*/
const dict_index_t* index, /*!< in: record descriptor */
mrec_buf_t** buf, /*!< out: 3 buffers */
ulint** offsets1, /*!< out: offsets */
ulint** offsets2) /*!< out: offsets */
{
ulint i = 1 + REC_OFFS_HEADER_SIZE
+ dict_index_get_n_fields(index);
mem_heap_t* heap = mem_heap_create(2 * i * sizeof **offsets1
+ 3 * sizeof **buf);
*buf = static_cast<mrec_buf_t*>(
mem_heap_alloc(heap, 3 * sizeof **buf));
*offsets1 = static_cast<ulint*>(
mem_heap_alloc(heap, i * sizeof **offsets1));
*offsets2 = static_cast<ulint*>(
mem_heap_alloc(heap, i * sizeof **offsets2));
(*offsets1)[0] = (*offsets2)[0] = i;
(*offsets1)[1] = (*offsets2)[1] = dict_index_get_n_fields(index);
return(heap);
}
/********************************************************************//**
Read a merge block from the file system.
@return TRUE if request was successful, FALSE if fail */
UNIV_INTERN
ibool
row_merge_read(
/*===========*/
int fd, /*!< in: file descriptor */
ulint offset, /*!< in: offset where to read
in number of row_merge_block_t
elements */
row_merge_block_t* buf) /*!< out: data */
{
os_offset_t ofs = ((os_offset_t) offset) * srv_sort_buf_size;
ibool success;
DBUG_EXECUTE_IF("row_merge_read_failure", return(FALSE););
#ifdef UNIV_DEBUG
if (row_merge_print_block_read) {
fprintf(stderr, "row_merge_read fd=%d ofs=%lu\n",
fd, (ulong) offset);
}
#endif /* UNIV_DEBUG */
#ifdef UNIV_DEBUG
if (row_merge_print_block_read) {
fprintf(stderr, "row_merge_read fd=%d ofs=%lu\n",
fd, (ulong) offset);
}
#endif /* UNIV_DEBUG */
success = os_file_read_no_error_handling(OS_FILE_FROM_FD(fd), buf,
ofs, srv_sort_buf_size);
#ifdef POSIX_FADV_DONTNEED
/* Each block is read exactly once. Free up the file cache. */
posix_fadvise(fd, ofs, srv_sort_buf_size, POSIX_FADV_DONTNEED);
#endif /* POSIX_FADV_DONTNEED */
if (UNIV_UNLIKELY(!success)) {
ut_print_timestamp(stderr);
fprintf(stderr,
" InnoDB: failed to read merge block at " UINT64PF "\n",
ofs);
}
return(UNIV_LIKELY(success));
}
/********************************************************************//**
Write a merge block to the file system.
@return TRUE if request was successful, FALSE if fail */
UNIV_INTERN
ibool
row_merge_write(
/*============*/
int fd, /*!< in: file descriptor */
ulint offset, /*!< in: offset where to write,
in number of row_merge_block_t elements */
const void* buf) /*!< in: data */
{
size_t buf_len = srv_sort_buf_size;
os_offset_t ofs = buf_len * (os_offset_t) offset;
ibool ret;
DBUG_EXECUTE_IF("row_merge_write_failure", return(FALSE););
ret = os_file_write("(merge)", OS_FILE_FROM_FD(fd), buf, ofs, buf_len);
#ifdef UNIV_DEBUG
if (row_merge_print_block_write) {
fprintf(stderr, "row_merge_write fd=%d ofs=%lu\n",
fd, (ulong) offset);
}
#endif /* UNIV_DEBUG */
#ifdef POSIX_FADV_DONTNEED
/* The block will be needed on the next merge pass,
but it can be evicted from the file cache meanwhile. */
posix_fadvise(fd, ofs, buf_len, POSIX_FADV_DONTNEED);
#endif /* POSIX_FADV_DONTNEED */
return(UNIV_LIKELY(ret));
}
/********************************************************************//**
Read a merge record.
@return pointer to next record, or NULL on I/O error or end of list */
UNIV_INTERN
const byte*
row_merge_read_rec(
/*===============*/
row_merge_block_t* block, /*!< in/out: file buffer */
mrec_buf_t* buf, /*!< in/out: secondary buffer */
const byte* b, /*!< in: pointer to record */
const dict_index_t* index, /*!< in: index of the record */
int fd, /*!< in: file descriptor */
ulint* foffs, /*!< in/out: file offset */
const mrec_t** mrec, /*!< out: pointer to merge record,
or NULL on end of list
(non-NULL on I/O error) */
ulint* offsets)/*!< out: offsets of mrec */
{
ulint extra_size;
ulint data_size;
ulint avail_size;
ut_ad(block);
ut_ad(buf);
ut_ad(b >= &block[0]);
ut_ad(b < &block[srv_sort_buf_size]);
ut_ad(index);
ut_ad(foffs);
ut_ad(mrec);
ut_ad(offsets);
ut_ad(*offsets == 1 + REC_OFFS_HEADER_SIZE
+ dict_index_get_n_fields(index));
extra_size = *b++;
if (UNIV_UNLIKELY(!extra_size)) {
/* End of list */
*mrec = NULL;
#ifdef UNIV_DEBUG
if (row_merge_print_read) {
fprintf(stderr, "row_merge_read %p,%p,%d,%lu EOF\n",
(const void*) b, (const void*) block,
fd, (ulong) *foffs);
}
#endif /* UNIV_DEBUG */
return(NULL);
}
if (extra_size >= 0x80) {
/* Read another byte of extra_size. */
if (UNIV_UNLIKELY(b >= &block[srv_sort_buf_size])) {
if (!row_merge_read(fd, ++(*foffs), block)) {
err_exit:
/* Signal I/O error. */
*mrec = b;
return(NULL);
}
/* Wrap around to the beginning of the buffer. */
b = &block[0];
}
extra_size = (extra_size & 0x7f) << 8;
extra_size |= *b++;
}
/* Normalize extra_size. Above, value 0 signals "end of list". */
extra_size--;
/* Read the extra bytes. */
if (UNIV_UNLIKELY(b + extra_size >= &block[srv_sort_buf_size])) {
/* The record spans two blocks. Copy the entire record
to the auxiliary buffer and handle this as a special