/
rdb_datadic.cc
5386 lines (4593 loc) · 178 KB
/
rdb_datadic.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) 2012,2013 Monty Program Ab
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, Fifth Floor, Boston, MA 02111-1301 USA */
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
/* For use of 'PRIu64': */
#define __STDC_FORMAT_MACROS
#include <my_config.h>
/* This C++ file's header file */
#include "./rdb_datadic.h"
#include <inttypes.h>
/* C++ standard header files */
#include <algorithm>
#include <array>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
/* MySQL header files */
#include "./field.h"
#include "./key.h"
#include "./m_ctype.h"
#include "./my_bit.h"
#include "./my_bitmap.h"
#include "./sql_table.h"
/* MyRocks header files */
#include "./ha_rocksdb.h"
#include "./ha_rocksdb_proto.h"
#include "./my_stacktrace.h"
#include "./rdb_cf_manager.h"
#include "./rdb_psi.h"
#include "./rdb_utils.h"
namespace myrocks {
void get_mem_comparable_space(const CHARSET_INFO *cs,
const std::vector<uchar> **xfrm, size_t *xfrm_len,
size_t *mb_len);
/*
MariaDB's replacement for FB/MySQL Field::check_field_name_match :
*/
inline bool field_check_field_name_match(Field *field, const char *name)
{
return (0 == my_strcasecmp(system_charset_info,
field->field_name,
name));
}
/*
Decode current key field
@param fpi IN data structure contains field metadata
@param field IN current field
@param reader IN key slice reader
@param unp_reader IN unpack information reader
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code
*/
int Rdb_convert_to_record_key_decoder::decode_field(
Rdb_field_packing *fpi, Field *field, Rdb_string_reader *reader,
const uchar *const default_value, Rdb_string_reader *unpack_reader) {
if (fpi->m_maybe_null) {
const char *nullp;
if (!(nullp = reader->read(1))) {
return HA_EXIT_FAILURE;
}
if (*nullp == 0) {
/* Set the NULL-bit of this field */
field->set_null();
/* Also set the field to its default value */
memcpy(field->ptr, default_value, field->pack_length());
return HA_EXIT_SUCCESS;
} else if (*nullp == 1) {
field->set_notnull();
} else {
return HA_EXIT_FAILURE;
}
}
return (fpi->m_unpack_func)(fpi, field, field->ptr, reader, unpack_reader);
}
/*
Decode current key field
@param buf OUT the buf starting address
@param offset OUT the bytes offset when data is written
@param fpi IN data structure contains field metadata
@param table IN current table
@param field IN current field
@param has_unpack_inf IN whether contains unpack inf
@param reader IN key slice reader
@param unp_reader IN unpack information reader
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code
*/
int Rdb_convert_to_record_key_decoder::decode(
uchar *const buf, uint *offset, Rdb_field_packing *fpi, TABLE *table,
Field *field, bool has_unpack_info, Rdb_string_reader *reader,
Rdb_string_reader *unpack_reader) {
DBUG_ASSERT(buf != nullptr);
DBUG_ASSERT(offset != nullptr);
uint field_offset = field->ptr - table->record[0];
*offset = field_offset;
uint null_offset = field->null_offset();
bool maybe_null = field->real_maybe_null();
field->move_field(buf + field_offset,
maybe_null ? buf + null_offset : nullptr, field->null_bit);
// If we need unpack info, but there is none, tell the unpack function
// this by passing unp_reader as nullptr. If we never read unpack_info
// during unpacking anyway, then there won't an error.
bool maybe_missing_unpack = !has_unpack_info && fpi->uses_unpack_info();
int res =
decode_field(fpi, field, reader, table->s->default_values + field_offset,
maybe_missing_unpack ? nullptr : unpack_reader);
// Restore field->ptr and field->null_ptr
field->move_field(table->record[0] + field_offset,
maybe_null ? table->record[0] + null_offset : nullptr,
field->null_bit);
if (res != UNPACK_SUCCESS) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
return HA_EXIT_SUCCESS;
}
/*
Skip current key field
@param fpi IN data structure contains field metadata
@param field IN current field
@param reader IN key slice reader
@param unp_reader IN unpack information reader
@return
HA_EXIT_SUCCESS OK
other HA_ERR error code
*/
int Rdb_convert_to_record_key_decoder::skip(const Rdb_field_packing *fpi,
const Field *field,
Rdb_string_reader *reader,
Rdb_string_reader *unp_reader) {
/* It is impossible to unpack the column. Skip it. */
if (fpi->m_maybe_null) {
const char *nullp;
if (!(nullp = reader->read(1))) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
if (*nullp == 0) {
/* This is a NULL value */
return HA_EXIT_SUCCESS;
}
/* If NULL marker is not '0', it can be only '1' */
if (*nullp != 1) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
}
if ((fpi->m_skip_func)(fpi, field, reader)) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
// If this is a space padded varchar, we need to skip the indicator
// bytes for trailing bytes. They're useless since we can't restore the
// field anyway.
//
// There is a special case for prefixed varchars where we do not
// generate unpack info, because we know prefixed varchars cannot be
// unpacked. In this case, it is not necessary to skip.
if (fpi->m_skip_func == &Rdb_key_def::skip_variable_space_pad &&
!fpi->m_unpack_info_stores_value) {
unp_reader->read(fpi->m_unpack_info_uses_two_bytes ? 2 : 1);
}
return HA_EXIT_SUCCESS;
}
Rdb_key_field_iterator::Rdb_key_field_iterator(
const Rdb_key_def *key_def, Rdb_field_packing *pack_info,
Rdb_string_reader *reader, Rdb_string_reader *unp_reader, TABLE *table,
bool has_unpack_info, const MY_BITMAP *covered_bitmap, uchar *const buf) {
m_key_def = key_def;
m_pack_info = pack_info;
m_iter_index = 0;
m_iter_end = key_def->get_key_parts();
m_reader = reader;
m_unp_reader = unp_reader;
m_table = table;
m_has_unpack_info = has_unpack_info;
m_covered_bitmap = covered_bitmap;
m_buf = buf;
m_secondary_key =
(key_def->m_index_type == Rdb_key_def::INDEX_TYPE_SECONDARY);
m_hidden_pk_exists = Rdb_key_def::table_has_hidden_pk(table);
m_is_hidden_pk =
(key_def->m_index_type == Rdb_key_def::INDEX_TYPE_HIDDEN_PRIMARY);
m_curr_bitmap_pos = 0;
m_offset = 0;
}
void *Rdb_key_field_iterator::get_dst() const { return m_buf + m_offset; }
int Rdb_key_field_iterator::get_field_index() const {
DBUG_ASSERT(m_field != nullptr);
return m_field->field_index;
}
bool Rdb_key_field_iterator::get_is_null() const { return m_is_null; }
Field *Rdb_key_field_iterator::get_field() const {
DBUG_ASSERT(m_field != nullptr);
return m_field;
}
bool Rdb_key_field_iterator::has_next() { return m_iter_index < m_iter_end; }
/**
Iterate each field in the key and decode/skip one by one
*/
int Rdb_key_field_iterator::next() {
int status = HA_EXIT_SUCCESS;
while (m_iter_index < m_iter_end) {
int curr_index = m_iter_index++;
m_fpi = &m_pack_info[curr_index];
/*
Hidden pk field is packed at the end of the secondary keys, but the SQL
layer does not know about it. Skip retrieving field if hidden pk.
*/
if ((m_secondary_key && m_hidden_pk_exists &&
curr_index + 1 == m_iter_end) ||
m_is_hidden_pk) {
DBUG_ASSERT(m_fpi->m_unpack_func);
if ((m_fpi->m_skip_func)(m_fpi, nullptr, m_reader)) {
return HA_ERR_ROCKSDB_CORRUPT_DATA;
}
return HA_EXIT_SUCCESS;
}
m_field = m_fpi->get_field_in_table(m_table);
bool covered_column = true;
if (m_covered_bitmap != nullptr &&
m_field->real_type() == MYSQL_TYPE_VARCHAR && !m_fpi->m_covered) {
covered_column = m_curr_bitmap_pos < MAX_REF_PARTS &&
bitmap_is_set(m_covered_bitmap, m_curr_bitmap_pos++);
}
if (m_fpi->m_unpack_func && covered_column) {
/* It is possible to unpack this column. Do it. */
status = Rdb_convert_to_record_key_decoder::decode(
m_buf, &m_offset, m_fpi, m_table, m_field, m_has_unpack_info,
m_reader, m_unp_reader);
if (status) {
return status;
}
break;
} else {
status = Rdb_convert_to_record_key_decoder::skip(m_fpi, m_field, m_reader,
m_unp_reader);
if (status) {
return status;
}
}
}
return HA_EXIT_SUCCESS;
}
/*
Rdb_key_def class implementation
*/
Rdb_key_def::Rdb_key_def(uint indexnr_arg, uint keyno_arg,
rocksdb::ColumnFamilyHandle *cf_handle_arg,
uint16_t index_dict_version_arg, uchar index_type_arg,
uint16_t kv_format_version_arg, bool is_reverse_cf_arg,
bool is_per_partition_cf_arg, const char *_name,
Rdb_index_stats _stats, uint32 index_flags_bitmap,
uint32 ttl_rec_offset, uint64 ttl_duration)
: m_index_number(indexnr_arg),
m_cf_handle(cf_handle_arg),
m_index_dict_version(index_dict_version_arg),
m_index_type(index_type_arg),
m_kv_format_version(kv_format_version_arg),
m_is_reverse_cf(is_reverse_cf_arg),
m_is_per_partition_cf(is_per_partition_cf_arg),
m_name(_name),
m_stats(_stats),
m_index_flags_bitmap(index_flags_bitmap),
m_ttl_rec_offset(ttl_rec_offset),
m_ttl_duration(ttl_duration),
m_ttl_column(""),
m_pk_part_no(nullptr),
m_pack_info(nullptr),
m_keyno(keyno_arg),
m_key_parts(0),
m_ttl_pk_key_part_offset(UINT_MAX),
m_ttl_field_index(UINT_MAX),
m_prefix_extractor(nullptr),
m_maxlength(0) // means 'not intialized'
{
mysql_mutex_init(0, &m_mutex, MY_MUTEX_INIT_FAST);
rdb_netbuf_store_index(m_index_number_storage_form, m_index_number);
m_total_index_flags_length =
calculate_index_flag_offset(m_index_flags_bitmap, MAX_FLAG);
DBUG_ASSERT_IMP(m_index_type == INDEX_TYPE_SECONDARY &&
m_kv_format_version <= SECONDARY_FORMAT_VERSION_UPDATE2,
m_total_index_flags_length == 0);
DBUG_ASSERT_IMP(m_index_type == INDEX_TYPE_PRIMARY &&
m_kv_format_version <= PRIMARY_FORMAT_VERSION_UPDATE2,
m_total_index_flags_length == 0);
DBUG_ASSERT(m_cf_handle != nullptr);
}
Rdb_key_def::Rdb_key_def(const Rdb_key_def &k)
: m_index_number(k.m_index_number),
m_cf_handle(k.m_cf_handle),
m_is_reverse_cf(k.m_is_reverse_cf),
m_is_per_partition_cf(k.m_is_per_partition_cf),
m_name(k.m_name),
m_stats(k.m_stats),
m_index_flags_bitmap(k.m_index_flags_bitmap),
m_ttl_rec_offset(k.m_ttl_rec_offset),
m_ttl_duration(k.m_ttl_duration),
m_ttl_column(k.m_ttl_column),
m_pk_part_no(k.m_pk_part_no),
m_pack_info(k.m_pack_info),
m_keyno(k.m_keyno),
m_key_parts(k.m_key_parts),
m_ttl_pk_key_part_offset(k.m_ttl_pk_key_part_offset),
m_ttl_field_index(UINT_MAX),
m_prefix_extractor(k.m_prefix_extractor),
m_maxlength(k.m_maxlength) {
mysql_mutex_init(0, &m_mutex, MY_MUTEX_INIT_FAST);
rdb_netbuf_store_index(m_index_number_storage_form, m_index_number);
m_total_index_flags_length =
calculate_index_flag_offset(m_index_flags_bitmap, MAX_FLAG);
DBUG_ASSERT_IMP(m_index_type == INDEX_TYPE_SECONDARY &&
m_kv_format_version <= SECONDARY_FORMAT_VERSION_UPDATE2,
m_total_index_flags_length == 0);
DBUG_ASSERT_IMP(m_index_type == INDEX_TYPE_PRIMARY &&
m_kv_format_version <= PRIMARY_FORMAT_VERSION_UPDATE2,
m_total_index_flags_length == 0);
if (k.m_pack_info) {
const size_t size = sizeof(Rdb_field_packing) * k.m_key_parts;
void *pack_info= my_malloc(size, MYF(0));
memcpy(pack_info, k.m_pack_info, size);
m_pack_info = reinterpret_cast<Rdb_field_packing *>(pack_info);
}
if (k.m_pk_part_no) {
const size_t size = sizeof(uint) * m_key_parts;
m_pk_part_no = reinterpret_cast<uint *>(my_malloc(size, MYF(0)));
memcpy(m_pk_part_no, k.m_pk_part_no, size);
}
}
Rdb_key_def::~Rdb_key_def() {
mysql_mutex_destroy(&m_mutex);
my_free(m_pk_part_no);
m_pk_part_no = nullptr;
my_free(m_pack_info);
m_pack_info = nullptr;
}
void Rdb_key_def::setup(const TABLE *const tbl,
const Rdb_tbl_def *const tbl_def) {
DBUG_ASSERT(tbl != nullptr);
DBUG_ASSERT(tbl_def != nullptr);
/*
Set max_length based on the table. This can be called concurrently from
multiple threads, so there is a mutex to protect this code.
*/
const bool is_hidden_pk = (m_index_type == INDEX_TYPE_HIDDEN_PRIMARY);
const bool hidden_pk_exists = table_has_hidden_pk(tbl);
const bool secondary_key = (m_index_type == INDEX_TYPE_SECONDARY);
if (!m_maxlength) {
RDB_MUTEX_LOCK_CHECK(m_mutex);
if (m_maxlength != 0) {
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
return;
}
KEY *key_info = nullptr;
KEY *pk_info = nullptr;
if (!is_hidden_pk) {
key_info = &tbl->key_info[m_keyno];
if (!hidden_pk_exists) pk_info = &tbl->key_info[tbl->s->primary_key];
m_name = std::string(key_info->name);
} else {
m_name = HIDDEN_PK_NAME;
}
if (secondary_key) {
m_pk_key_parts= hidden_pk_exists ? 1 : pk_info->ext_key_parts;
} else {
pk_info = nullptr;
m_pk_key_parts = 0;
}
// "unique" secondary keys support:
m_key_parts= is_hidden_pk ? 1 : key_info->ext_key_parts;
if (secondary_key) {
/*
In most cases, SQL layer puts PK columns as invisible suffix at the
end of secondary key. There are cases where this doesn't happen:
- unique secondary indexes.
- partitioned tables.
Internally, we always need PK columns as suffix (and InnoDB does,
too, if you were wondering).
The loop below will attempt to put all PK columns at the end of key
definition. Columns that are already included in the index (either
by the user or by "extended keys" feature) are not included for the
second time.
*/
m_key_parts += m_pk_key_parts;
}
if (secondary_key) {
m_pk_part_no = reinterpret_cast<uint *>(
my_malloc(sizeof(uint) * m_key_parts, MYF(0)));
} else {
m_pk_part_no = nullptr;
}
const size_t size = sizeof(Rdb_field_packing) * m_key_parts;
m_pack_info =
reinterpret_cast<Rdb_field_packing *>(my_malloc(size, MYF(0)));
/*
Guaranteed not to error here as checks have been made already during
table creation.
*/
Rdb_key_def::extract_ttl_col(tbl, tbl_def, &m_ttl_column,
&m_ttl_field_index, true);
size_t max_len = INDEX_NUMBER_SIZE;
int unpack_len = 0;
int max_part_len = 0;
bool simulating_extkey = false;
uint dst_i = 0;
uint keyno_to_set = m_keyno;
uint keypart_to_set = 0;
if (is_hidden_pk) {
Field *field = nullptr;
m_pack_info[dst_i].setup(this, field, keyno_to_set, 0, 0);
m_pack_info[dst_i].m_unpack_data_offset = unpack_len;
max_len += m_pack_info[dst_i].m_max_image_len;
max_part_len = std::max(max_part_len, m_pack_info[dst_i].m_max_image_len);
dst_i++;
} else {
KEY_PART_INFO *key_part = key_info->key_part;
/* this loop also loops over the 'extended key' tail */
for (uint src_i = 0; src_i < m_key_parts; src_i++, keypart_to_set++) {
Field *const field = key_part ? key_part->field : nullptr;
if (simulating_extkey && !hidden_pk_exists) {
DBUG_ASSERT(secondary_key);
/* Check if this field is already present in the key definition */
bool found = false;
for (uint j= 0; j < key_info->ext_key_parts; j++) {
if (field->field_index ==
key_info->key_part[j].field->field_index &&
key_part->length == key_info->key_part[j].length) {
found = true;
break;
}
}
if (found) {
key_part++;
continue;
}
}
if (field && field->real_maybe_null()) max_len += 1; // NULL-byte
m_pack_info[dst_i].setup(this, field, keyno_to_set, keypart_to_set,
key_part ? key_part->length : 0);
m_pack_info[dst_i].m_unpack_data_offset = unpack_len;
if (pk_info) {
m_pk_part_no[dst_i] = -1;
for (uint j = 0; j < m_pk_key_parts; j++) {
if (field->field_index == pk_info->key_part[j].field->field_index) {
m_pk_part_no[dst_i] = j;
break;
}
}
} else if (secondary_key && hidden_pk_exists) {
/*
The hidden pk can never be part of the sk. So it is always
appended to the end of the sk.
*/
m_pk_part_no[dst_i] = -1;
if (simulating_extkey) m_pk_part_no[dst_i] = 0;
}
max_len += m_pack_info[dst_i].m_max_image_len;
max_part_len =
std::max(max_part_len, m_pack_info[dst_i].m_max_image_len);
/*
Check key part name here, if it matches the TTL column then we store
the offset of the TTL key part here.
*/
if (!m_ttl_column.empty() &&
field_check_field_name_match(field, m_ttl_column.c_str())) {
DBUG_ASSERT(field->real_type() == MYSQL_TYPE_LONGLONG);
DBUG_ASSERT(field->key_type() == HA_KEYTYPE_ULONGLONG);
DBUG_ASSERT(!field->real_maybe_null());
m_ttl_pk_key_part_offset = dst_i;
}
key_part++;
/*
For "unique" secondary indexes, pretend they have
"index extensions".
MariaDB also has this property: if an index has a partially-covered
column like KEY(varchar_col(N)), then the SQL layer will think it is
not "extended" with PK columns. The code below handles this case,
also.
*/
if (secondary_key && src_i+1 == key_info->ext_key_parts) {
simulating_extkey = true;
if (!hidden_pk_exists) {
keyno_to_set = tbl->s->primary_key;
key_part = pk_info->key_part;
keypart_to_set = (uint)-1;
} else {
keyno_to_set = tbl_def->m_key_count - 1;
key_part = nullptr;
keypart_to_set = 0;
}
}
dst_i++;
}
}
m_key_parts = dst_i;
/* Initialize the memory needed by the stats structure */
m_stats.m_distinct_keys_per_prefix.resize(get_key_parts());
/* Cache prefix extractor for bloom filter usage later */
rocksdb::Options opt = rdb_get_rocksdb_db()->GetOptions(get_cf());
m_prefix_extractor = opt.prefix_extractor;
/*
This should be the last member variable set before releasing the mutex
so that other threads can't see the object partially set up.
*/
m_maxlength = max_len;
RDB_MUTEX_UNLOCK_CHECK(m_mutex);
}
}
/*
Determine if the table has TTL enabled by parsing the table comment.
@param[IN] table_arg
@param[IN] tbl_def_arg
@param[OUT] ttl_duration Default TTL value parsed from table comment
*/
uint Rdb_key_def::extract_ttl_duration(const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg,
uint64 *ttl_duration) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
DBUG_ASSERT(ttl_duration != nullptr);
std::string table_comment(table_arg->s->comment.str,
table_arg->s->comment.length);
bool ttl_duration_per_part_match_found = false;
std::string ttl_duration_str = Rdb_key_def::parse_comment_for_qualifier(
table_comment, table_arg, tbl_def_arg, &ttl_duration_per_part_match_found,
RDB_TTL_DURATION_QUALIFIER);
/* If we don't have a ttl duration, nothing to do here. */
if (ttl_duration_str.empty()) {
return HA_EXIT_SUCCESS;
}
/*
Catch errors where a non-integral value was used as ttl duration, strtoull
will return 0.
*/
*ttl_duration = std::strtoull(ttl_duration_str.c_str(), nullptr, 0);
if (!*ttl_duration) {
my_error(ER_RDB_TTL_DURATION_FORMAT, MYF(0), ttl_duration_str.c_str());
return HA_EXIT_FAILURE;
}
return HA_EXIT_SUCCESS;
}
/*
Determine if the table has TTL enabled by parsing the table comment.
@param[IN] table_arg
@param[IN] tbl_def_arg
@param[OUT] ttl_column TTL column in the table
@param[IN] skip_checks Skip validation checks (when called in
setup())
*/
uint Rdb_key_def::extract_ttl_col(const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg,
std::string *ttl_column,
uint *ttl_field_index, bool skip_checks) {
std::string table_comment(table_arg->s->comment.str,
table_arg->s->comment.length);
/*
Check if there is a TTL column specified. Note that this is not required
and if omitted, an 8-byte ttl field will be prepended to each record
implicitly.
*/
bool ttl_col_per_part_match_found = false;
std::string ttl_col_str = Rdb_key_def::parse_comment_for_qualifier(
table_comment, table_arg, tbl_def_arg, &ttl_col_per_part_match_found,
RDB_TTL_COL_QUALIFIER);
if (skip_checks) {
for (uint i = 0; i < table_arg->s->fields; i++) {
Field *const field = table_arg->field[i];
if (field_check_field_name_match(field, ttl_col_str.c_str())) {
*ttl_column = ttl_col_str;
*ttl_field_index = i;
}
}
return HA_EXIT_SUCCESS;
}
/* Check if TTL column exists in table */
if (!ttl_col_str.empty()) {
bool found = false;
for (uint i = 0; i < table_arg->s->fields; i++) {
Field *const field = table_arg->field[i];
if (field_check_field_name_match(field, ttl_col_str.c_str()) &&
field->real_type() == MYSQL_TYPE_LONGLONG &&
field->key_type() == HA_KEYTYPE_ULONGLONG &&
!field->real_maybe_null()) {
*ttl_column = ttl_col_str;
*ttl_field_index = i;
found = true;
break;
}
}
if (!found) {
my_error(ER_RDB_TTL_COL_FORMAT, MYF(0), ttl_col_str.c_str());
return HA_EXIT_FAILURE;
}
}
return HA_EXIT_SUCCESS;
}
const std::string Rdb_key_def::gen_qualifier_for_table(
const char *const qualifier, const std::string &partition_name) {
bool has_partition = !partition_name.empty();
std::string qualifier_str = "";
if (!strcmp(qualifier, RDB_CF_NAME_QUALIFIER)) {
return has_partition ? gen_cf_name_qualifier_for_partition(partition_name)
: qualifier_str + RDB_CF_NAME_QUALIFIER +
RDB_QUALIFIER_VALUE_SEP;
} else if (!strcmp(qualifier, RDB_TTL_DURATION_QUALIFIER)) {
return has_partition
? gen_ttl_duration_qualifier_for_partition(partition_name)
: qualifier_str + RDB_TTL_DURATION_QUALIFIER +
RDB_QUALIFIER_VALUE_SEP;
} else if (!strcmp(qualifier, RDB_TTL_COL_QUALIFIER)) {
return has_partition ? gen_ttl_col_qualifier_for_partition(partition_name)
: qualifier_str + RDB_TTL_COL_QUALIFIER +
RDB_QUALIFIER_VALUE_SEP;
} else {
DBUG_ASSERT(0);
}
return qualifier_str;
}
/*
Formats the string and returns the column family name assignment part for a
specific partition.
*/
const std::string Rdb_key_def::gen_cf_name_qualifier_for_partition(
const std::string &prefix) {
DBUG_ASSERT(!prefix.empty());
return prefix + RDB_PER_PARTITION_QUALIFIER_NAME_SEP + RDB_CF_NAME_QUALIFIER +
RDB_QUALIFIER_VALUE_SEP;
}
const std::string Rdb_key_def::gen_ttl_duration_qualifier_for_partition(
const std::string &prefix) {
DBUG_ASSERT(!prefix.empty());
return prefix + RDB_PER_PARTITION_QUALIFIER_NAME_SEP +
RDB_TTL_DURATION_QUALIFIER + RDB_QUALIFIER_VALUE_SEP;
}
const std::string Rdb_key_def::gen_ttl_col_qualifier_for_partition(
const std::string &prefix) {
DBUG_ASSERT(!prefix.empty());
return prefix + RDB_PER_PARTITION_QUALIFIER_NAME_SEP + RDB_TTL_COL_QUALIFIER +
RDB_QUALIFIER_VALUE_SEP;
}
const std::string Rdb_key_def::parse_comment_for_qualifier(
const std::string &comment, const TABLE *const table_arg,
const Rdb_tbl_def *const tbl_def_arg, bool *per_part_match_found,
const char *const qualifier) {
DBUG_ASSERT(table_arg != nullptr);
DBUG_ASSERT(tbl_def_arg != nullptr);
DBUG_ASSERT(per_part_match_found != nullptr);
DBUG_ASSERT(qualifier != nullptr);
std::string empty_result;
// Flag which marks if partition specific options were found.
*per_part_match_found = false;
if (comment.empty()) {
return empty_result;
}
// Let's fetch the comment for a index and check if there's a custom key
// name specified for a partition we are handling.
std::vector<std::string> v =
myrocks::parse_into_tokens(comment, RDB_QUALIFIER_SEP);
std::string search_str = gen_qualifier_for_table(qualifier);
// If table has partitions then we need to check if user has requested
// qualifiers on a per partition basis.
//
// NOTE: this means if you specify a qualifier for a specific partition it
// will take precedence the 'table level' qualifier if one exists.
std::string search_str_part;
if (IF_PARTITIONING(table_arg->part_info,nullptr) != nullptr) {
std::string partition_name = tbl_def_arg->base_partition();
DBUG_ASSERT(!partition_name.empty());
search_str_part = gen_qualifier_for_table(qualifier, partition_name);
}
DBUG_ASSERT(!search_str.empty());
// Basic O(N) search for a matching assignment. At most we expect maybe
// ten or so elements here.
if (!search_str_part.empty()) {
for (const auto &it : v) {
if (it.substr(0, search_str_part.length()) == search_str_part) {
// We found a prefix match. Try to parse it as an assignment.
std::vector<std::string> tokens =
myrocks::parse_into_tokens(it, RDB_QUALIFIER_VALUE_SEP);
// We found a custom qualifier, it was in the form we expected it to be.
// Return that instead of whatever we initially wanted to return. In
// a case below the `foo` part will be returned to the caller.
//
// p3_cfname=foo
//
// If no value was specified then we'll return an empty string which
// later gets translated into using a default CF.
if (tokens.size() == 2) {
*per_part_match_found = true;
return tokens[1];
} else {
return empty_result;
}
}
}
}
// Do this loop again, this time searching for 'table level' qualifiers if we
// didn't find any partition level qualifiers above.
for (const auto &it : v) {
if (it.substr(0, search_str.length()) == search_str) {
std::vector<std::string> tokens =
myrocks::parse_into_tokens(it, RDB_QUALIFIER_VALUE_SEP);
if (tokens.size() == 2) {
return tokens[1];
} else {
return empty_result;
}
}
}
// If we didn't find any partitioned/non-partitioned qualifiers, return an
// empty string.
return empty_result;
}
/**
Read a memcmp key part from a slice using the passed in reader.
Returns -1 if field was null, 1 if error, 0 otherwise.
*/
int Rdb_key_def::read_memcmp_key_part(const TABLE *table_arg,
Rdb_string_reader *reader,
const uint part_num) const {
/* It is impossible to unpack the column. Skip it. */
if (m_pack_info[part_num].m_maybe_null) {
const char *nullp;
if (!(nullp = reader->read(1))) return 1;
if (*nullp == 0) {
/* This is a NULL value */
return -1;
} else {
/* If NULL marker is not '0', it can be only '1' */
if (*nullp != 1) return 1;
}
}
Rdb_field_packing *fpi = &m_pack_info[part_num];
DBUG_ASSERT(table_arg->s != nullptr);
bool is_hidden_pk_part = (part_num + 1 == m_key_parts) &&
(table_arg->s->primary_key == MAX_INDEXES);
Field *field = nullptr;
if (!is_hidden_pk_part) {
field = fpi->get_field_in_table(table_arg);
}
if ((fpi->m_skip_func)(fpi, field, reader)) {
return 1;
}
return 0;
}
/**
Get a mem-comparable form of Primary Key from mem-comparable form of this key
@param
pk_descr Primary Key descriptor
key Index tuple from this key in mem-comparable form
pk_buffer OUT Put here mem-comparable form of the Primary Key.
@note
It may or may not be possible to restore primary key columns to their
mem-comparable form. To handle all cases, this function copies mem-
comparable forms directly.
RocksDB SE supports "Extended keys". This means that PK columns are present
at the end of every key. If the key already includes PK columns, then
these columns are not present at the end of the key.
Because of the above, we copy each primary key column.
@todo
If we checked crc32 checksums in this function, we would catch some CRC
violations that we currently don't. On the other hand, there is a broader
set of queries for which we would check the checksum twice.
*/
uint Rdb_key_def::get_primary_key_tuple(const TABLE *const table,
const Rdb_key_def &pk_descr,
const rocksdb::Slice *const key,
uchar *const pk_buffer) const {
DBUG_ASSERT(table != nullptr);
DBUG_ASSERT(key != nullptr);
DBUG_ASSERT(m_index_type == Rdb_key_def::INDEX_TYPE_SECONDARY);
DBUG_ASSERT(pk_buffer);
uint size = 0;
uchar *buf = pk_buffer;
DBUG_ASSERT(m_pk_key_parts);
/* Put the PK number */
rdb_netbuf_store_index(buf, pk_descr.m_index_number);
buf += INDEX_NUMBER_SIZE;
size += INDEX_NUMBER_SIZE;
const char *start_offs[MAX_REF_PARTS];
const char *end_offs[MAX_REF_PARTS];
int pk_key_part;
uint i;
Rdb_string_reader reader(key);
// Skip the index number
if ((!reader.read(INDEX_NUMBER_SIZE))) return RDB_INVALID_KEY_LEN;
for (i = 0; i < m_key_parts; i++) {
if ((pk_key_part = m_pk_part_no[i]) != -1) {
start_offs[pk_key_part] = reader.get_current_ptr();
}
if (read_memcmp_key_part(table, &reader, i) > 0) {
return RDB_INVALID_KEY_LEN;
}
if (pk_key_part != -1) {
end_offs[pk_key_part] = reader.get_current_ptr();
}
}
for (i = 0; i < m_pk_key_parts; i++) {
const uint part_size = end_offs[i] - start_offs[i];
memcpy(buf, start_offs[i], end_offs[i] - start_offs[i]);
buf += part_size;
size += part_size;
}
return size;
}
/**
Get a mem-comparable form of Secondary Key from mem-comparable form of this
key, without the extended primary key tail.
@param
key Index tuple from this key in mem-comparable form
sk_buffer OUT Put here mem-comparable form of the Secondary Key.
n_null_fields OUT Put number of null fields contained within sk entry
*/
uint Rdb_key_def::get_memcmp_sk_parts(const TABLE *table,
const rocksdb::Slice &key,
uchar *sk_buffer,
uint *n_null_fields) const {
DBUG_ASSERT(table != nullptr);
DBUG_ASSERT(sk_buffer != nullptr);
DBUG_ASSERT(n_null_fields != nullptr);
DBUG_ASSERT(m_keyno != table->s->primary_key && !table_has_hidden_pk(table));
uchar *buf = sk_buffer;
int res;
Rdb_string_reader reader(&key);
const char *start = reader.get_current_ptr();
// Skip the index number
if ((!reader.read(INDEX_NUMBER_SIZE))) return RDB_INVALID_KEY_LEN;
for (uint i = 0; i < table->key_info[m_keyno].user_defined_key_parts; i++) {
if ((res = read_memcmp_key_part(table, &reader, i)) > 0) {
return RDB_INVALID_KEY_LEN;
} else if (res == -1) {
(*n_null_fields)++;
}
}
uint sk_memcmp_len = reader.get_current_ptr() - start;
memcpy(buf, start, sk_memcmp_len);
return sk_memcmp_len;
}
/**
Convert index tuple into storage (i.e. mem-comparable) format
@detail
Currently this is done by unpacking into table->record[0] and then
packing index columns into storage format.
@param pack_buffer Temporary area for packing varchar columns. Its
size is at least max_storage_fmt_length() bytes.
*/
uint Rdb_key_def::pack_index_tuple(TABLE *const tbl, uchar *const pack_buffer,
uchar *const packed_tuple,
const uchar *const key_tuple,
const key_part_map &keypart_map) const {
DBUG_ASSERT(tbl != nullptr);
DBUG_ASSERT(pack_buffer != nullptr);