1
1
use tikv_client_common:: Error ;
2
2
use tikv_client_proto:: metapb:: Region ;
3
- use tikv_client_store:: Request ;
4
3
5
- use crate :: { Key , kv:: codec:: decode_bytes_in_place, Result } ;
4
+ use crate :: { kv:: codec:: decode_bytes_in_place, Key , Result } ;
6
5
7
- const KEYSPACE_PREFIX_LEN : usize = 4 ;
8
6
const RAW_MODE_PREFIX : u8 = b'r' ;
9
7
const TXN_MODE_PREFIX : u8 = b'x' ;
8
+
9
+ const KEYSPACE_PREFIX_LEN : usize = 4 ;
10
+
11
+ const RAW_MODE_MIN_KEY : Prefix = [ RAW_MODE_PREFIX , 0 , 0 , 0 ] ;
10
12
const RAW_MODE_MAX_KEY : Prefix = [ RAW_MODE_PREFIX + 1 , 0 , 0 , 0 ] ;
13
+
14
+ const TXN_MODE_MIN_KEY : Prefix = [ TXN_MODE_PREFIX , 0 , 0 , 0 ] ;
11
15
const TXN_MODE_MAX_KEY : Prefix = [ TXN_MODE_PREFIX + 1 , 0 , 0 , 0 ] ;
12
- const MAX_KEYSPACE_ID : KeySpaceId = [ 0xff , 0xff , 0xff ] ;
13
16
14
17
#[ macro_export]
15
18
macro_rules! plain_request {
@@ -50,6 +53,34 @@ pub trait RequestCodec: Sized + Clone + Sync + Send + 'static {
50
53
}
51
54
}
52
55
56
+ #[ derive( Clone ) ]
57
+ pub struct TxnApiV1 ;
58
+
59
+ #[ derive( Clone ) ]
60
+ pub struct RawApiV1 ;
61
+
62
+ pub trait RawCodec : RequestCodec { }
63
+
64
+ pub trait TxnCodec : RequestCodec { }
65
+
66
+ impl RequestCodec for RawApiV1 { }
67
+
68
+ impl RequestCodec for TxnApiV1 {
69
+ fn encode_pd_query ( & self , key : Key ) -> Key {
70
+ key. to_encoded ( )
71
+ }
72
+
73
+ fn decode_region ( & self , mut region : Region ) -> Result < Region > {
74
+ decode_bytes_in_place ( region. mut_start_key ( ) , false ) ?;
75
+ decode_bytes_in_place ( region. mut_end_key ( ) , false ) ?;
76
+ Ok ( region)
77
+ }
78
+ }
79
+
80
+ impl RawCodec for RawApiV1 { }
81
+
82
+ impl TxnCodec for TxnApiV1 { }
83
+
53
84
#[ derive( Copy , Clone ) ]
54
85
enum KeyMode {
55
86
Raw ,
@@ -65,6 +96,22 @@ impl From<KeyMode> for u8 {
65
96
}
66
97
}
67
98
99
+ impl KeyMode {
100
+ fn min_key ( self ) -> Key {
101
+ match self {
102
+ KeyMode :: Raw => Key :: from ( RAW_MODE_MIN_KEY . to_vec ( ) ) ,
103
+ KeyMode :: Txn => Key :: from ( TXN_MODE_MIN_KEY . to_vec ( ) ) ,
104
+ }
105
+ }
106
+
107
+ fn max_key ( self ) -> Key {
108
+ match self {
109
+ KeyMode :: Raw => Key :: from ( RAW_MODE_MAX_KEY . to_vec ( ) ) ,
110
+ KeyMode :: Txn => Key :: from ( TXN_MODE_MAX_KEY . to_vec ( ) ) ,
111
+ }
112
+ }
113
+ }
114
+
68
115
type Prefix = [ u8 ; KEYSPACE_PREFIX_LEN ] ;
69
116
70
117
type KeySpaceId = [ u8 ; 3 ] ;
@@ -110,43 +157,65 @@ impl RequestCodec for KeySpaceCodec {
110
157
}
111
158
112
159
fn encode_pd_query ( & self , key : Key ) -> Key {
113
- todo ! ( )
160
+ self . encode_key ( key ) . to_encoded ( )
114
161
}
115
162
116
- fn decode_region ( & self , region : Region ) -> Result < Region > {
117
- todo ! ( )
163
+ fn decode_region ( & self , mut region : Region ) -> Result < Region > {
164
+ decode_bytes_in_place ( region. mut_start_key ( ) , false ) ?;
165
+ decode_bytes_in_place ( region. mut_end_key ( ) , false ) ?;
166
+
167
+ if region. get_start_key ( ) < self . mode . min_key ( ) . as_slice ( ) {
168
+ * region. mut_start_key ( ) = vec ! [ ] ;
169
+ } else {
170
+ * region. mut_start_key ( ) = self
171
+ . decode_key ( region. get_start_key ( ) . to_vec ( ) . into ( ) ) ?
172
+ . into ( ) ;
173
+ }
174
+
175
+ if region. get_end_key ( ) > self . mode . max_key ( ) . as_slice ( ) {
176
+ * region. mut_end_key ( ) = vec ! [ ] ;
177
+ } else {
178
+ * region. mut_end_key ( ) = self
179
+ . decode_key ( region. get_end_key ( ) . to_vec ( ) . into ( ) ) ?
180
+ . into ( ) ;
181
+ }
182
+
183
+ Ok ( region)
118
184
}
119
185
120
186
fn is_plain ( & self ) -> bool {
121
- todo ! ( )
187
+ false
122
188
}
123
189
}
124
190
125
191
#[ derive( Clone ) ]
126
- pub struct TxnApiV1 ;
127
-
128
- #[ derive( Clone ) ]
129
- pub struct RawApiV1 ;
192
+ pub struct RawKeyspaceCodec ( KeySpaceCodec ) ;
130
193
131
- pub trait RawCodec : RequestCodec { }
194
+ impl RawKeyspaceCodec {
195
+ pub fn new ( id : KeySpaceId ) -> Self {
196
+ RawKeyspaceCodec ( KeySpaceCodec {
197
+ mode : KeyMode :: Raw ,
198
+ id,
199
+ } )
200
+ }
201
+ }
132
202
133
- pub trait TxnCodec : RequestCodec { }
203
+ impl RequestCodec for RawKeyspaceCodec { }
134
204
135
- impl RequestCodec for RawApiV1 { }
205
+ impl RawCodec for RawKeyspaceCodec { }
136
206
137
- impl RequestCodec for TxnApiV1 {
138
- fn encode_pd_query ( & self , key : Key ) -> Key {
139
- key. to_encoded ( )
140
- }
141
-
142
- fn decode_region ( & self , mut region : Region ) -> Result < Region > {
143
- decode_bytes_in_place ( region. mut_start_key ( ) , false ) ?;
144
- decode_bytes_in_place ( region. mut_end_key ( ) , false ) ?;
207
+ #[ derive( Clone ) ]
208
+ pub struct TxnKeyspaceCodec ( KeySpaceCodec ) ;
145
209
146
- Ok ( region)
210
+ impl TxnKeyspaceCodec {
211
+ pub fn new ( id : KeySpaceId ) -> Self {
212
+ TxnKeyspaceCodec ( KeySpaceCodec {
213
+ mode : KeyMode :: Txn ,
214
+ id,
215
+ } )
147
216
}
148
217
}
149
218
150
- impl RawCodec for RawApiV1 { }
219
+ impl RequestCodec for TxnKeyspaceCodec { }
151
220
152
- impl TxnCodec for TxnApiV1 { }
221
+ impl TxnCodec for TxnKeyspaceCodec { }
0 commit comments