1
+ use core:: intrinsics:: copy;
1
2
use std:: ops:: { Deref , DerefMut } ;
2
3
use tikv_client_common:: Error ;
3
- use tikv_client_proto:: { kvrpcpb, metapb:: Region } ;
4
+ use tikv_client_proto:: { errorpb , kvrpcpb, metapb:: Region } ;
4
5
5
6
use crate :: { kv:: codec:: decode_bytes_in_place, Key , Result } ;
6
7
@@ -45,22 +46,85 @@ pub trait RequestCodec: Sized + Clone + Sync + Send + 'static {
45
46
pairs
46
47
}
47
48
48
- fn decode_key ( & self , key : Vec < u8 > ) -> Result < Vec < u8 > > {
49
- Ok ( key )
49
+ fn decode_key ( & self , _key : & mut Vec < u8 > ) -> Result < ( ) > {
50
+ Ok ( ( ) )
50
51
}
51
52
52
- fn decode_keys ( & self , keys : Vec < Vec < u8 > > ) -> Result < Vec < Vec < u8 > > > {
53
- keys. into_iter ( )
54
- . map ( |key| self . decode_key ( key) )
55
- . collect :: < Result < Vec < Vec < u8 > > > > ( )
53
+ fn decode_keys ( & self , keys : & mut [ Vec < u8 > ] ) -> Result < ( ) > {
54
+ for key in keys. iter_mut ( ) {
55
+ self . decode_key ( key) ?;
56
+ }
57
+
58
+ Ok ( ( ) )
59
+ }
60
+
61
+ fn decode_key_error ( & self , err : & mut kvrpcpb:: KeyError ) -> Result < ( ) > {
62
+ if err. has_locked ( ) {
63
+ let locked = err. mut_locked ( ) ;
64
+ self . decode_lock ( locked) ?;
65
+ }
66
+
67
+ if err. has_conflict ( ) {
68
+ let conflict = err. mut_conflict ( ) ;
69
+ self . decode_key ( conflict. mut_key ( ) ) ?;
70
+ self . decode_key ( conflict. mut_primary ( ) ) ?;
71
+ }
72
+
73
+ if err. has_already_exist ( ) {
74
+ let already_exist = err. mut_already_exist ( ) ;
75
+ self . decode_key ( already_exist. mut_key ( ) ) ?;
76
+ }
77
+
78
+ // We do not decode key in `Deadlock` since there is no use for the key right now in client side.
79
+ // All we need is the key hash to detect deadlock.
80
+ // TODO: while we check the keys against the deadlock key hash, we need to encode the key.
81
+
82
+ if err. has_commit_ts_expired ( ) {
83
+ let commit_ts_expired = err. mut_commit_ts_expired ( ) ;
84
+ self . decode_key ( commit_ts_expired. mut_key ( ) ) ?;
85
+ }
86
+
87
+ if err. has_txn_not_found ( ) {
88
+ let txn_not_found = err. mut_txn_not_found ( ) ;
89
+ self . decode_key ( txn_not_found. mut_primary_key ( ) ) ?;
90
+ }
91
+
92
+ if err. has_assertion_failed ( ) {
93
+ let assertion_failed = err. mut_assertion_failed ( ) ;
94
+ self . decode_key ( assertion_failed. mut_key ( ) ) ?;
95
+ }
96
+
97
+ Ok ( ( ) )
98
+ }
99
+
100
+ fn decode_key_errors ( & self , errors : & mut [ kvrpcpb:: KeyError ] ) -> Result < ( ) > {
101
+ for err in errors. iter_mut ( ) {
102
+ self . decode_key_error ( err) ?;
103
+ }
104
+
105
+ Ok ( ( ) )
106
+ }
107
+
108
+ fn decode_lock ( & self , lock : & mut kvrpcpb:: LockInfo ) -> Result < ( ) > {
109
+ self . decode_key ( lock. mut_primary_lock ( ) ) ?;
110
+ self . decode_key ( lock. mut_key ( ) ) ?;
111
+ self . decode_keys ( lock. mut_secondaries ( ) )
112
+ }
113
+
114
+ fn decode_locks ( & self , locks : & mut [ kvrpcpb:: LockInfo ] ) -> Result < ( ) > {
115
+ for lock in locks. iter_mut ( ) {
116
+ self . decode_lock ( lock) ?;
117
+ }
118
+
119
+ Ok ( ( ) )
56
120
}
57
121
58
- fn decode_pairs ( & self , mut pairs : Vec < kvrpcpb:: KvPair > ) -> Result < Vec < kvrpcpb :: KvPair > > {
122
+ fn decode_pairs ( & self , pairs : & mut [ kvrpcpb:: KvPair ] ) -> Result < ( ) > {
59
123
for pair in pairs. iter_mut ( ) {
60
- * pair . mut_key ( ) = self . decode_key ( pair. take_key ( ) ) ?;
124
+ self . decode_key ( pair. mut_key ( ) ) ?;
61
125
}
62
126
63
- Ok ( pairs )
127
+ Ok ( ( ) )
64
128
}
65
129
66
130
fn encode_range ( & self , start : Vec < u8 > , end : Vec < u8 > ) -> ( Vec < u8 > , Vec < u8 > ) {
@@ -81,8 +145,22 @@ pub trait RequestCodec: Sized + Clone + Sync + Send + 'static {
81
145
key
82
146
}
83
147
84
- fn decode_region ( & self , region : Region ) -> Result < Region > {
85
- Ok ( region)
148
+ fn decode_region ( & self , _region : & mut Region ) -> Result < ( ) > {
149
+ Ok ( ( ) )
150
+ }
151
+
152
+ fn decode_regions ( & self , regions : & mut [ Region ] ) -> Result < ( ) > {
153
+ for region in regions. iter_mut ( ) {
154
+ self . decode_region ( region) ?;
155
+ }
156
+ Ok ( ( ) )
157
+ }
158
+
159
+ fn decode_region_error ( & self , err : & mut errorpb:: Error ) -> Result < ( ) > {
160
+ if err. has_epoch_not_match ( ) {
161
+ self . decode_regions ( err. mut_epoch_not_match ( ) . mut_current_regions ( ) ) ?;
162
+ }
163
+ Ok ( ( ) )
86
164
}
87
165
88
166
fn is_plain ( & self ) -> bool {
@@ -107,10 +185,10 @@ impl RequestCodec for TxnApiV1 {
107
185
Key :: from ( key) . to_encoded ( ) . into ( )
108
186
}
109
187
110
- fn decode_region ( & self , mut region : Region ) -> Result < Region > {
188
+ fn decode_region ( & self , region : & mut Region ) -> Result < ( ) > {
111
189
decode_bytes_in_place ( region. mut_start_key ( ) , false ) ?;
112
190
decode_bytes_in_place ( region. mut_end_key ( ) , false ) ?;
113
- Ok ( region )
191
+ Ok ( ( ) )
114
192
}
115
193
}
116
194
@@ -151,7 +229,7 @@ impl KeyMode {
151
229
152
230
type Prefix = [ u8 ; KEYSPACE_PREFIX_LEN ] ;
153
231
154
- #[ derive( Clone , Copy , Default , PartialEq ) ]
232
+ #[ derive( Clone , Copy , Default , PartialEq , Eq ) ]
155
233
pub struct KeySpaceId ( [ u8 ; 3 ] ) ;
156
234
157
235
impl Deref for KeySpaceId {
@@ -189,18 +267,27 @@ impl RequestCodec for KeySpaceCodec {
189
267
encoded
190
268
}
191
269
192
- fn decode_key ( & self , mut key : Vec < u8 > ) -> Result < Vec < u8 > > {
270
+ fn decode_key ( & self , key : & mut Vec < u8 > ) -> Result < ( ) > {
193
271
let prefix: Prefix = ( * self ) . into ( ) ;
194
272
195
273
if !key. starts_with ( & prefix) {
196
274
return Err ( Error :: CorruptedKeyspace {
197
275
expected : prefix. to_vec ( ) ,
198
276
actual : key[ ..KEYSPACE_PREFIX_LEN ] . to_vec ( ) ,
199
- key,
277
+ key : key . to_vec ( ) ,
200
278
} ) ;
201
279
}
202
280
203
- Ok ( key. split_off ( KEYSPACE_PREFIX_LEN ) )
281
+ unsafe {
282
+ let trimmed_len = key. len ( ) - KEYSPACE_PREFIX_LEN ;
283
+ let ptr = key. as_mut_ptr ( ) ;
284
+ let trimmed = key[ KEYSPACE_PREFIX_LEN ..] . as_mut_ptr ( ) ;
285
+
286
+ copy ( trimmed, ptr, trimmed_len) ;
287
+
288
+ key. set_len ( trimmed_len) ;
289
+ }
290
+ Ok ( ( ) )
204
291
}
205
292
206
293
fn encode_range ( & self , start : Vec < u8 > , end : Vec < u8 > ) -> ( Vec < u8 > , Vec < u8 > ) {
@@ -215,25 +302,25 @@ impl RequestCodec for KeySpaceCodec {
215
302
Key :: from ( self . encode_key ( key) ) . to_encoded ( ) . into ( )
216
303
}
217
304
218
- fn decode_region ( & self , mut region : Region ) -> Result < Region > {
305
+ fn decode_region ( & self , region : & mut Region ) -> Result < ( ) > {
219
306
decode_bytes_in_place ( region. mut_start_key ( ) , false ) ?;
220
307
decode_bytes_in_place ( region. mut_end_key ( ) , false ) ?;
221
308
222
309
// Map the region's start key to the keyspace's start key.
223
310
if region. get_start_key ( ) < self . mode . min_key ( ) . as_slice ( ) {
224
311
* region. mut_start_key ( ) = vec ! [ ] ;
225
312
} else {
226
- * region . mut_start_key ( ) = self . decode_key ( region. get_start_key ( ) . to_vec ( ) ) ?;
313
+ self . decode_key ( region. mut_start_key ( ) ) ?;
227
314
}
228
315
229
316
// Map the region's end key to the keyspace's end key.
230
317
if region. get_end_key ( ) > self . mode . max_key ( ) . as_slice ( ) {
231
318
* region. mut_end_key ( ) = vec ! [ ] ;
232
319
} else {
233
- * region . mut_end_key ( ) = self . decode_key ( region. get_end_key ( ) . to_vec ( ) ) ?;
320
+ self . decode_key ( region. mut_end_key ( ) ) ?;
234
321
}
235
322
236
- Ok ( region )
323
+ Ok ( ( ) )
237
324
}
238
325
239
326
fn is_plain ( & self ) -> bool {
0 commit comments