22
22
#include < Storages/DeltaMerge/DeltaMergeHelpers.h>
23
23
#include < Storages/DeltaMerge/dtpb/column_file.pb.h>
24
24
#include < Storages/KVStore/Decode/DecodedTiKVKeyValue.h>
25
- #include < Storages/KVStore/MultiRaft/RegionRangeKeys.h>
26
25
#include < Storages/KVStore/TiKVHelpers/TiKVRecordFormat.h>
27
26
#include < Storages/KVStore/Types.h>
28
27
#include < TiDB/Decode/DatumCodec.h>
29
28
29
+ namespace DB
30
+ {
31
+ class RegionRangeKeys ;
32
+ }
30
33
namespace DB ::DM
31
34
{
32
35
using HandleValuePtr = std::shared_ptr<String>;
@@ -86,62 +89,9 @@ struct RowKeyValue
86
89
, int_value(int_value_)
87
90
{}
88
91
89
- RowKeyValue (bool is_common_handle_, HandleValuePtr value_)
90
- : is_common_handle(is_common_handle_)
91
- , value(value_)
92
- {
93
- if (is_common_handle)
94
- int_value = 0 ;
95
- else
96
- {
97
- size_t cursor = 0 ;
98
- int_value = DB::DecodeInt64 (cursor, *value);
99
- if (unlikely (value->size () != sizeof (Int64)))
100
- {
101
- // For int type handle, the standard key enconding format should be t{table_id}_r{handle_value}.
102
- // But TiKV may generate region range keys which are not strictly following the standard format.
103
- // More concretely, the key may be t{table_id}_r{handle_value} + some other bytes.
104
- // We need to adapt the key to the standard format.
105
- // For example, the key may be t100_r1000 + 0x00, we need to adapt it to t100_r1001.
106
- // This is ok, because
107
- // 1) if the key is the start range, then [t100_r1000 + 0x00, xxx) has the same semantics with [t100_r1001, xxx)
108
- // 2) if the key is the end range, then [xxx, t100_r1000 + 0x00) also has the same semantics with [xxx, t100_r1001)
109
- //
110
- // Note if the `int_value` is Int64::max_value,
111
- // it is a value generated by tiflash itself to means +inf()(which is RowKeyValue::INT_HANDLE_MAX_KEY).
112
- // So we can just ignore it.
113
- if (value->size () != sizeof (UInt64) + 1 || value->back () != 0x00 )
114
- {
115
- LOG_WARNING (
116
- Logger::get (),
117
- " Meet rowkey {} with unexpected encoding format" ,
118
- Redact::keyToDebugString (value->data (), value->size ()));
119
- }
120
- else
121
- {
122
- if (int_value < std::numeric_limits<Int64>::max ())
123
- {
124
- LOG_WARNING (
125
- Logger::get (),
126
- " Meet rowkey {} which has an extra zero suffix" ,
127
- Redact::keyToDebugString (value->data (), value->size ()));
128
- int_value = int_value + 1 ;
129
- WriteBufferFromOwnString ss;
130
- DB::EncodeInt64 (int_value, ss);
131
- value = std::make_shared<String>(ss.releaseStr ());
132
- }
133
- else
134
- {
135
- // ignore RowKeyValue::INT_HANDLE_MAX_KEY
136
- }
137
- }
138
- }
139
- }
140
- }
141
-
142
92
explicit RowKeyValue (const RowKeyValueRef & rowkey_value)
93
+ : is_common_handle(rowkey_value.is_common_handle)
143
94
{
144
- is_common_handle = rowkey_value.is_common_handle ;
145
95
if (is_common_handle)
146
96
value = std::make_shared<String>(rowkey_value.data , rowkey_value.size );
147
97
else
@@ -153,12 +103,32 @@ struct RowKeyValue
153
103
int_value = rowkey_value.int_value ;
154
104
}
155
105
156
- static RowKeyValue fromHandle (Handle value)
106
+ #ifndef NDEBUG
107
+ // Generate from int handle
108
+ static RowKeyValue fromIntHandle (Handle value)
157
109
{
158
110
WriteBufferFromOwnString ss;
159
111
DB::EncodeInt64 (value, ss);
160
112
return RowKeyValue (false , std::make_shared<String>(ss.releaseStr ()), value);
161
113
}
114
+ #endif
115
+
116
+ static RowKeyValue fromHandle (bool is_common_handle_, const HandleValuePtr value_)
117
+ {
118
+ return fromHandleWithSuffix (is_common_handle_, value_).first ;
119
+ }
120
+
121
+ /* *
122
+ * Parse the handle from `value_`.
123
+ * If the handle is a common handle, the return RowKeyValue is the same with `value_`.
124
+ * If the handle is an int handle, the `int_value` will be the int value.
125
+ * Specially, for int handle, if there is any suffix rather than the regular format,
126
+ * a.k.a "t${tableID}_r{handleID}{AnySuffix}" `AnySuffix` is not empty, then
127
+ * the `int_value` will be the next int value, and the `AnySuffix` will be returned.
128
+ *
129
+ * Note that the `AnySuffix` rely on the lifetime of `value_`
130
+ */
131
+ static std::pair<RowKeyValue, std::string_view> fromHandleWithSuffix (bool is_common_handle_, HandleValuePtr value_);
162
132
163
133
// Format as a string
164
134
String toString () const ;
@@ -171,18 +141,6 @@ struct RowKeyValue
171
141
return RowKeyValueRef{is_common_handle, value->data (), value->size (), int_value};
172
142
}
173
143
174
- DecodedTiKVKeyPtr toRegionKey (TableID table_id) const
175
- {
176
- // FIXME: move this to TiKVRecordFormat.h
177
- WriteBufferFromOwnString ss;
178
- ss.write (' t' );
179
- EncodeInt64 (table_id, ss);
180
- ss.write (' _' );
181
- ss.write (' r' );
182
- String prefix = ss.releaseStr ();
183
- return std::make_shared<DecodedTiKVKey>(prefix + *value);
184
- }
185
-
186
144
bool operator ==(const RowKeyValue & v) const
187
145
{
188
146
return is_common_handle == v.is_common_handle && (*value) == (*v.value ) && int_value == v.int_value ;
@@ -249,7 +207,7 @@ struct RowKeyValue
249
207
readBoolText (is_common_handle, buf);
250
208
readStringBinary (value, buf);
251
209
HandleValuePtr start_ptr = std::make_shared<String>(value);
252
- return RowKeyValue (is_common_handle, start_ptr);
210
+ return RowKeyValue::fromHandle (is_common_handle, start_ptr);
253
211
}
254
212
255
213
bool is_common_handle = false ;
@@ -577,8 +535,8 @@ struct RowKeyRange
577
535
end_ptr = RowKeyValue::COMMON_HANDLE_MAX_KEY.value ;
578
536
}
579
537
return RowKeyRange (
580
- RowKeyValue (is_common_handle, start_ptr),
581
- RowKeyValue (is_common_handle, end_ptr),
538
+ RowKeyValue::fromHandle (is_common_handle, start_ptr),
539
+ RowKeyValue::fromHandle (is_common_handle, end_ptr),
582
540
is_common_handle,
583
541
rowkey_column_size);
584
542
}
@@ -598,8 +556,8 @@ struct RowKeyRange
598
556
end_ptr = RowKeyValue::COMMON_HANDLE_MAX_KEY.value ;
599
557
}
600
558
return RowKeyRange (
601
- RowKeyValue (is_common_handle, start_ptr),
602
- RowKeyValue (is_common_handle, end_ptr),
559
+ RowKeyValue::fromHandle (is_common_handle, start_ptr),
560
+ RowKeyValue::fromHandle (is_common_handle, end_ptr),
603
561
is_common_handle,
604
562
rowkey_column_size);
605
563
}
@@ -758,9 +716,10 @@ struct RowKeyRange
758
716
String end = ss.releaseStr ();
759
717
// / when handle_range.end == HandleRange::MAX, according to previous implementation, it should be +Inf
760
718
return RowKeyRange (
761
- RowKeyValue (is_common_handle, std::make_shared<String>(start)),
762
- handle_range.end == HandleRange::MAX ? RowKeyValue::COMMON_HANDLE_MAX_KEY
763
- : RowKeyValue (is_common_handle, std::make_shared<String>(end)),
719
+ RowKeyValue::fromHandle (is_common_handle, std::make_shared<String>(start)),
720
+ handle_range.end == HandleRange::MAX
721
+ ? RowKeyValue::COMMON_HANDLE_MAX_KEY
722
+ : RowKeyValue::fromHandle (is_common_handle, std::make_shared<String>(end)),
764
723
/* is_common_handle=*/ is_common_handle,
765
724
1 );
766
725
}
@@ -794,64 +753,17 @@ struct RowKeyRange
794
753
795
754
static RowKeyRange fromRegionRange (
796
755
const std::shared_ptr<const RegionRangeKeys> & region_range,
797
- const TableID table_id,
756
+ TableID table_id,
798
757
bool is_common_handle,
799
- size_t rowkey_column_size)
800
- {
801
- return fromRegionRange (
802
- region_range->rawKeys (),
803
- region_range->getMappedTableID (),
804
- table_id,
805
- is_common_handle,
806
- rowkey_column_size);
807
- }
758
+ size_t rowkey_column_size,
759
+ const String & tracing_msg = " " );
808
760
static RowKeyRange fromRegionRange (
809
761
const std::pair<DecodedTiKVKeyPtr, DecodedTiKVKeyPtr> & raw_keys,
810
- const TableID table_id_in_raw_key,
811
- const TableID table_id,
762
+ TableID table_id_in_raw_key,
763
+ TableID table_id,
812
764
bool is_common_handle,
813
- size_t rowkey_column_size)
814
- {
815
- if (likely (table_id_in_raw_key == table_id))
816
- {
817
- auto & start_key = *raw_keys.first ;
818
- auto & end_key = *raw_keys.second ;
819
- auto keyspace_id = start_key.getKeyspaceID ();
820
- const auto & table_range_min_max = getTableMinMaxData (keyspace_id, table_id, is_common_handle);
821
- RowKeyValue start_value, end_value;
822
- if (start_key <= *table_range_min_max.min )
823
- {
824
- if (is_common_handle)
825
- start_value = RowKeyValue::COMMON_HANDLE_MIN_KEY;
826
- else
827
- start_value = RowKeyValue::INT_HANDLE_MIN_KEY;
828
- }
829
- else
830
- {
831
- start_value = RowKeyValue (
832
- is_common_handle,
833
- std::make_shared<std::string>(RecordKVFormat::getRawTiDBPKView (start_key)));
834
- }
835
- if (end_key >= *table_range_min_max.max )
836
- {
837
- if (is_common_handle)
838
- end_value = RowKeyValue::COMMON_HANDLE_MAX_KEY;
839
- else
840
- end_value = RowKeyValue::INT_HANDLE_MAX_KEY;
841
- }
842
- else
843
- end_value = RowKeyValue (
844
- is_common_handle,
845
- std::make_shared<std::string>(RecordKVFormat::getRawTiDBPKView (end_key)));
846
- return RowKeyRange (start_value, end_value, is_common_handle, rowkey_column_size);
847
- }
848
- else
849
- {
850
- // / if table id is not the same, just return none range
851
- // / maybe should throw exception since it should not happen
852
- return newNone (is_common_handle, rowkey_column_size);
853
- }
854
- }
765
+ size_t rowkey_column_size,
766
+ const String & tracing_msg);
855
767
856
768
// Format as a string
857
769
String toString () const ;
0 commit comments