Skip to content

Commit 81cbddd

Browse files
committed
move request codec into raw/txn owned module
Signed-off-by: iosmanthus <[email protected]>
1 parent 7d68247 commit 81cbddd

File tree

15 files changed

+190
-166
lines changed

15 files changed

+190
-166
lines changed

examples/pessimistic.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
mod common;
4-
5-
use crate::common::parse_args;
63
use tikv_client::{
7-
request::codec::TxnApiV1, Config, Key, TransactionClient as Client, TransactionOptions, Value,
4+
transaction::ApiV1, Config, Key, TransactionClient as Client, TransactionOptions, Value,
85
};
96

7+
use crate::common::parse_args;
8+
9+
mod common;
10+
1011
#[tokio::main]
1112
async fn main() {
1213
// You can try running this example by passing your pd endpoints
@@ -22,7 +23,7 @@ async fn main() {
2223
};
2324

2425
// init
25-
let client = Client::new_with_config(args.pd, config, TxnApiV1, None)
26+
let client = Client::new_with_config(args.pd, config, ApiV1, None)
2627
.await
2728
.expect("Could not connect to tikv");
2829

examples/raw.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ mod common;
66

77
use crate::common::parse_args;
88
use tikv_client::{
9-
request::codec::RawApiV1, Config, IntoOwnedRange, Key, KvPair, RawClient as Client, Result,
10-
Value,
9+
raw::ApiV1, Config, IntoOwnedRange, Key, KvPair, RawClient as Client, Result, Value,
1110
};
1211

1312
const KEY: &str = "TiKV";
@@ -29,7 +28,7 @@ async fn main() -> Result<()> {
2928

3029
// When we first create a client we receive a `Connect` structure which must be resolved before
3130
// the client is actually connected and usable.
32-
let client = Client::new_with_config(args.pd, config, RawApiV1, None).await?;
31+
let client = Client::new_with_config(args.pd, config, ApiV1, None).await?;
3332

3433
// Requests are created from the connected client. These calls return structures which
3534
// implement `Future`. This means the `Future` must be resolved before the action ever takes

examples/transaction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod common;
44

55
use crate::common::parse_args;
66
use tikv_client::{
7-
request::codec::{TxnApiV1, TxnCodec},
8-
BoundRange, Config, Key, KvPair, TransactionClient as Client, Value,
7+
request::codec::TxnCodec, transaction::ApiV1, BoundRange, Config, Key, KvPair,
8+
TransactionClient as Client, Value,
99
};
1010

1111
async fn puts<C: TxnCodec>(client: &Client<C>, pairs: impl IntoIterator<Item = impl Into<KvPair>>) {
@@ -84,7 +84,7 @@ async fn main() {
8484
Config::default()
8585
};
8686

87-
let txn = Client::new_with_config(args.pd, config, TxnApiV1, None)
87+
let txn = Client::new_with_config(args.pd, config, ApiV1, None)
8888
.await
8989
.expect("Could not connect to tikv");
9090

src/mock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
88
use crate::{
99
pd::{PdClient, PdRpcClient, RetryClient},
10+
raw::ApiV1,
1011
region::{RegionId, RegionWithLeader},
11-
request::codec::RawApiV1,
1212
store::RegionStore,
1313
Config, Error, Key, Result, Timestamp,
1414
};
@@ -21,7 +21,7 @@ use tikv_client_store::{KvClient, KvConnect, Request};
2121

2222
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
2323
/// client can be tested without doing any RPC calls.
24-
pub async fn pd_rpc_client() -> PdRpcClient<RawApiV1, MockKvConnect, MockCluster> {
24+
pub async fn pd_rpc_client() -> PdRpcClient<ApiV1, MockKvConnect, MockCluster> {
2525
let config = Config::default();
2626
let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
2727
let logger = Logger::root(
@@ -42,7 +42,7 @@ pub async fn pd_rpc_client() -> PdRpcClient<RawApiV1, MockKvConnect, MockCluster
4242
MockCluster,
4343
))
4444
},
45-
RawApiV1,
45+
ApiV1,
4646
logger,
4747
)
4848
.await
@@ -154,7 +154,7 @@ impl MockPdClient {
154154
#[async_trait]
155155
impl PdClient for MockPdClient {
156156
type KvClient = MockKvClient;
157-
type RequestCodec = RawApiV1;
157+
type RequestCodec = ApiV1;
158158

159159
async fn map_region_to_store(self: Arc<Self>, region: RegionWithLeader) -> Result<RegionStore> {
160160
Ok(RegionStore::new(region, Arc::new(self.client.clone())))
@@ -201,6 +201,6 @@ impl PdClient for MockPdClient {
201201
async fn invalidate_region_cache(&self, _ver_id: crate::region::RegionVerId) {}
202202

203203
fn get_request_codec(&self) -> Self::RequestCodec {
204-
RawApiV1
204+
ApiV1
205205
}
206206
}

src/raw/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ mod tests {
792792

793793
use crate::{
794794
mock::{MockKvClient, MockPdClient},
795-
request::codec::RawApiV1,
795+
raw::ApiV1,
796796
Result,
797797
};
798798

@@ -828,7 +828,7 @@ mod tests {
828828
cf: Some(ColumnFamily::Default),
829829
atomic: false,
830830
logger,
831-
_phantom: PhantomData::<RawApiV1>,
831+
_phantom: PhantomData::<ApiV1>,
832832
};
833833
let resps = client
834834
.coprocessor(

src/raw/codec.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use tikv_client_proto::{kvrpcpb, metapb::Region};
2+
3+
use crate::{
4+
impl_request_codec_for_new_type,
5+
request::codec::{KeyMode, KeySpaceCodec, KeySpaceId, RawCodec, RequestCodec, TxnCodec},
6+
Result,
7+
};
8+
9+
#[derive(Clone)]
10+
pub struct ApiV1;
11+
12+
impl RequestCodec for ApiV1 {}
13+
14+
impl RawCodec for ApiV1 {}
15+
16+
#[derive(Clone)]
17+
pub struct Keyspace(KeySpaceCodec);
18+
19+
impl Keyspace {
20+
pub fn new(id: KeySpaceId) -> Self {
21+
Keyspace(KeySpaceCodec::new(KeyMode::Raw, id))
22+
}
23+
}
24+
25+
impl_request_codec_for_new_type!(Keyspace);
26+
27+
impl TxnCodec for Keyspace {}

src/raw/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
//!
1010
//! **Warning:** It is not advisable to use both raw and transactional functionality in the same keyspace.
1111
12-
pub use self::client::Client;
12+
pub use self::{
13+
client::Client,
14+
codec::{ApiV1, Keyspace},
15+
};
1316
use crate::Error;
1417
use std::{convert::TryFrom, fmt};
1518

1619
mod client;
20+
mod codec;
1721
pub mod lowering;
1822
mod requests;
1923

src/region_cache.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ mod test {
262262

263263
use crate::{
264264
pd::RetryClientTrait,
265+
raw::ApiV1,
265266
region::{RegionId, RegionWithLeader},
266-
request::codec::{RawApiV1, RequestCodec},
267+
request::codec::RequestCodec,
267268
Key, Result,
268269
};
269270

@@ -330,7 +331,7 @@ mod test {
330331
#[tokio::test]
331332
async fn cache_is_used() -> Result<()> {
332333
let retry_client = Arc::new(MockRetryClient::default());
333-
let cache = RegionCache::new(RawApiV1, retry_client.clone());
334+
let cache = RegionCache::new(ApiV1, retry_client.clone());
334335
retry_client.regions.lock().await.insert(
335336
1,
336337
RegionWithLeader {
@@ -400,7 +401,7 @@ mod test {
400401
#[tokio::test]
401402
async fn test_add_disjoint_regions() {
402403
let retry_client = Arc::new(MockRetryClient::default());
403-
let cache = RegionCache::new(RawApiV1, retry_client.clone());
404+
let cache = RegionCache::new(ApiV1, retry_client.clone());
404405
let region1 = region(1, vec![], vec![10]);
405406
let region2 = region(2, vec![10], vec![20]);
406407
let region3 = region(3, vec![30], vec![]);
@@ -419,7 +420,7 @@ mod test {
419420
#[tokio::test]
420421
async fn test_add_intersecting_regions() {
421422
let retry_client = Arc::new(MockRetryClient::default());
422-
let cache = RegionCache::new(RawApiV1, retry_client.clone());
423+
let cache = RegionCache::new(ApiV1, retry_client.clone());
423424

424425
cache.add_region(region(1, vec![], vec![10])).await;
425426
cache.add_region(region(2, vec![10], vec![20])).await;
@@ -457,7 +458,7 @@ mod test {
457458
#[tokio::test]
458459
async fn test_get_region_by_key() -> Result<()> {
459460
let retry_client = Arc::new(MockRetryClient::default());
460-
let cache = RegionCache::new(RawApiV1, retry_client.clone());
461+
let cache = RegionCache::new(ApiV1, retry_client.clone());
461462

462463
let region1 = region(1, vec![], vec![10]);
463464
let region2 = region(2, vec![10], vec![20]);

src/request/codec.rs

Lines changed: 34 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use core::intrinsics::copy;
22
use std::ops::{Deref, DerefMut};
33

4+
use derive_new::new;
5+
46
use tikv_client_common::Error;
57
use tikv_client_proto::{errorpb, kvrpcpb, metapb::Region};
68

@@ -185,36 +187,12 @@ pub trait RequestCodecExt: RequestCodec {
185187

186188
impl<T: RequestCodec> RequestCodecExt for T {}
187189

188-
#[derive(Clone)]
189-
pub struct TxnApiV1;
190-
191-
#[derive(Clone)]
192-
pub struct RawApiV1;
193-
194190
pub trait RawCodec: RequestCodec {}
195191

196192
pub trait TxnCodec: RequestCodec {}
197193

198-
impl RequestCodec for RawApiV1 {}
199-
200-
impl RequestCodec for TxnApiV1 {
201-
fn encode_pd_query(&self, key: Vec<u8>) -> Vec<u8> {
202-
Key::from(key).to_encoded().into()
203-
}
204-
205-
fn decode_region(&self, region: &mut Region) -> Result<()> {
206-
decode_bytes_in_place(region.mut_start_key(), false)?;
207-
decode_bytes_in_place(region.mut_end_key(), false)?;
208-
Ok(())
209-
}
210-
}
211-
212-
impl RawCodec for RawApiV1 {}
213-
214-
impl TxnCodec for TxnApiV1 {}
215-
216194
#[derive(Copy, Clone)]
217-
enum KeyMode {
195+
pub(crate) enum KeyMode {
218196
Raw,
219197
Txn,
220198
}
@@ -263,8 +241,8 @@ impl DerefMut for KeySpaceId {
263241
}
264242
}
265243

266-
#[derive(Copy, Clone)]
267-
struct KeySpaceCodec {
244+
#[derive(new, Copy, Clone)]
245+
pub(crate) struct KeySpaceCodec {
268246
mode: KeyMode,
269247
id: KeySpaceId,
270248
}
@@ -346,82 +324,33 @@ impl RequestCodec for KeySpaceCodec {
346324
}
347325
}
348326

349-
#[derive(Clone)]
350-
pub struct RawKeyspaceCodec(KeySpaceCodec);
351-
352-
impl RawKeyspaceCodec {
353-
pub fn new(id: KeySpaceId) -> Self {
354-
RawKeyspaceCodec(KeySpaceCodec {
355-
mode: KeyMode::Raw,
356-
id,
357-
})
358-
}
359-
}
360-
361-
impl RequestCodec for RawKeyspaceCodec {
362-
fn encode_key(&self, key: Vec<u8>) -> Vec<u8> {
363-
self.0.encode_key(key)
364-
}
365-
366-
fn decode_key(&self, key: &mut Vec<u8>) -> Result<()> {
367-
self.0.decode_key(key)
368-
}
369-
370-
fn encode_range(&self, start: Vec<u8>, end: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
371-
self.0.encode_range(start, end)
372-
}
373-
374-
fn encode_pd_query(&self, key: Vec<u8>) -> Vec<u8> {
375-
self.0.encode_pd_query(key)
376-
}
377-
378-
fn decode_region(&self, region: &mut Region) -> Result<()> {
379-
self.0.decode_region(region)
380-
}
381-
382-
fn version(&self) -> kvrpcpb::ApiVersion {
383-
self.0.version()
384-
}
385-
}
386-
387-
impl RawCodec for RawKeyspaceCodec {}
388-
389-
#[derive(Clone)]
390-
pub struct TxnKeyspaceCodec(KeySpaceCodec);
391-
392-
impl TxnKeyspaceCodec {
393-
pub fn new(id: KeySpaceId) -> Self {
394-
TxnKeyspaceCodec(KeySpaceCodec {
395-
mode: KeyMode::Txn,
396-
id,
397-
})
398-
}
399-
}
400-
401-
impl RequestCodec for TxnKeyspaceCodec {
402-
fn encode_key(&self, key: Vec<u8>) -> Vec<u8> {
403-
self.0.encode_key(key)
404-
}
405-
406-
fn decode_key(&self, key: &mut Vec<u8>) -> Result<()> {
407-
self.0.decode_key(key)
408-
}
409-
410-
fn encode_range(&self, start: Vec<u8>, end: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
411-
self.0.encode_range(start, end)
412-
}
413-
414-
fn encode_pd_query(&self, key: Vec<u8>) -> Vec<u8> {
415-
self.0.encode_pd_query(key)
416-
}
417-
418-
fn decode_region(&self, region: &mut Region) -> Result<()> {
419-
self.0.decode_region(region)
420-
}
421-
422-
fn version(&self) -> kvrpcpb::ApiVersion {
423-
self.0.version()
424-
}
327+
#[macro_export]
328+
macro_rules! impl_request_codec_for_new_type {
329+
($t:ty) => {
330+
impl RequestCodec for $t {
331+
fn encode_key(&self, key: Vec<u8>) -> Vec<u8> {
332+
self.0.encode_key(key)
333+
}
334+
335+
fn decode_key(&self, key: &mut Vec<u8>) -> Result<()> {
336+
self.0.decode_key(key)
337+
}
338+
339+
fn encode_range(&self, start: Vec<u8>, end: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
340+
self.0.encode_range(start, end)
341+
}
342+
343+
fn encode_pd_query(&self, key: Vec<u8>) -> Vec<u8> {
344+
self.0.encode_pd_query(key)
345+
}
346+
347+
fn decode_region(&self, region: &mut Region) -> Result<()> {
348+
self.0.decode_region(region)
349+
}
350+
351+
fn version(&self) -> kvrpcpb::ApiVersion {
352+
self.0.version()
353+
}
354+
}
355+
};
425356
}
426-
427-
impl TxnCodec for TxnKeyspaceCodec {}

0 commit comments

Comments
 (0)