Skip to content

Commit 9164cf0

Browse files
committed
fix all tests for API v1
Signed-off-by: iosmanthus <[email protected]>
1 parent 605aed8 commit 9164cf0

File tree

13 files changed

+148
-77
lines changed

13 files changed

+148
-77
lines changed

examples/pessimistic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
mod common;
44

55
use crate::common::parse_args;
6-
use tikv_client::{Config, Key, TransactionClient as Client, TransactionOptions, Value};
6+
use tikv_client::{
7+
request::request_codec::TxnApiV1, Config, Key, TransactionClient as Client, TransactionOptions,
8+
Value,
9+
};
710

811
#[tokio::main]
912
async fn main() {
@@ -20,7 +23,7 @@ async fn main() {
2023
};
2124

2225
// init
23-
let client = Client::new_with_config(args.pd, config, None)
26+
let client = Client::new_with_config(args.pd, config, TxnApiV1, None)
2427
.await
2528
.expect("Could not connect to tikv");
2629

examples/raw.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
mod common;
66

77
use crate::common::parse_args;
8-
use tikv_client::{Config, IntoOwnedRange, Key, KvPair, RawClient as Client, Result, Value};
8+
use tikv_client::{
9+
request::request_codec::RawApiV1, Config, IntoOwnedRange, Key, KvPair, RawClient as Client,
10+
Result, Value,
11+
};
912

1013
const KEY: &str = "TiKV";
1114
const VALUE: &str = "Rust";
@@ -26,7 +29,7 @@ async fn main() -> Result<()> {
2629

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

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

examples/transaction.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ mod common;
44

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

11-
async fn puts<C: RequestCodec>(
12-
client: &Client<C>,
13-
pairs: impl IntoIterator<Item = impl Into<KvPair>>,
14-
) {
11+
async fn puts<C: TxnCodec>(client: &Client<C>, pairs: impl IntoIterator<Item = impl Into<KvPair>>) {
1512
let mut txn = client
1613
.begin_optimistic()
1714
.await
@@ -23,7 +20,7 @@ async fn puts<C: RequestCodec>(
2320
txn.commit().await.expect("Could not commit transaction");
2421
}
2522

26-
async fn get<C: RequestCodec>(client: &Client<C>, key: Key) -> Option<Value> {
23+
async fn get<C: TxnCodec>(client: &Client<C>, key: Key) -> Option<Value> {
2724
let mut txn = client
2825
.begin_optimistic()
2926
.await
@@ -35,7 +32,7 @@ async fn get<C: RequestCodec>(client: &Client<C>, key: Key) -> Option<Value> {
3532
res
3633
}
3734

38-
async fn key_exists<C: RequestCodec>(client: &Client<C>, key: Key) -> bool {
35+
async fn key_exists<C: TxnCodec>(client: &Client<C>, key: Key) -> bool {
3936
let mut txn = client
4037
.begin_optimistic()
4138
.await
@@ -50,7 +47,7 @@ async fn key_exists<C: RequestCodec>(client: &Client<C>, key: Key) -> bool {
5047
res
5148
}
5249

53-
async fn scan<C: RequestCodec>(client: &Client<C>, range: impl Into<BoundRange>, limit: u32) {
50+
async fn scan<C: TxnCodec>(client: &Client<C>, range: impl Into<BoundRange>, limit: u32) {
5451
let mut txn = client
5552
.begin_optimistic()
5653
.await
@@ -62,7 +59,7 @@ async fn scan<C: RequestCodec>(client: &Client<C>, range: impl Into<BoundRange>,
6259
txn.commit().await.expect("Could not commit transaction");
6360
}
6461

65-
async fn dels<C: RequestCodec>(client: &Client<C>, keys: impl IntoIterator<Item = Key>) {
62+
async fn dels<C: TxnCodec>(client: &Client<C>, keys: impl IntoIterator<Item = Key>) {
6663
let mut txn = client
6764
.begin_optimistic()
6865
.await

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@
6666
//! # use tikv_client::{RawClient, Result};
6767
//! # use futures::prelude::*;
6868
//! # fn main() -> Result<()> {
69-
//! # futures::executor::block_on(async {
70-
//! let client = RawClient::new(vec!["127.0.0.1:2379"], None).await?;
69+
//! # use tikv_client::request::request_codec::RawApiV1;
70+
//! futures::executor::block_on(async {
71+
//! let client = RawClient::new(vec!["127.0.0.1:2379"], RawApiV1, None).await?;
7172
//! client.put("key".to_owned(), "value".to_owned()).await?;
7273
//! let value = client.get("key".to_owned()).await?;
7374
//! # Ok(())
@@ -80,8 +81,9 @@
8081
//! # use tikv_client::{TransactionClient, Result};
8182
//! # use futures::prelude::*;
8283
//! # fn main() -> Result<()> {
83-
//! # futures::executor::block_on(async {
84-
//! let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"], None).await?;
84+
//! # use tikv_client::request::request_codec::TxnApiV1;
85+
//! futures::executor::block_on(async {
86+
//! let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"], TxnApiV1, None).await?;
8587
//! let mut txn = txn_client.begin_optimistic().await?;
8688
//! txn.put("key".to_owned(), "value".to_owned()).await?;
8789
//! let value = txn.get("key".to_owned()).await?;

src/mock.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use crate::{
99
pd::{PdClient, PdRpcClient, RetryClient},
1010
region::{RegionId, RegionWithLeader},
11+
request::request_codec::RawApiV1,
1112
store::RegionStore,
1213
Config, Error, Key, Result, Timestamp,
1314
};
@@ -20,7 +21,7 @@ use tikv_client_store::{KvClient, KvConnect, Request};
2021

2122
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
2223
/// client can be tested without doing any RPC calls.
23-
pub async fn pd_rpc_client() -> PdRpcClient<MockKvConnect, MockCluster> {
24+
pub async fn pd_rpc_client() -> PdRpcClient<RawApiV1, MockKvConnect, MockCluster> {
2425
let config = Config::default();
2526
let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
2627
let logger = Logger::root(
@@ -41,7 +42,7 @@ pub async fn pd_rpc_client() -> PdRpcClient<MockKvConnect, MockCluster> {
4142
MockCluster,
4243
))
4344
},
44-
false,
45+
RawApiV1,
4546
logger,
4647
)
4748
.await
@@ -153,6 +154,7 @@ impl MockPdClient {
153154
#[async_trait]
154155
impl PdClient for MockPdClient {
155156
type KvClient = MockKvClient;
157+
type RequestCodec = RawApiV1;
156158

157159
async fn map_region_to_store(self: Arc<Self>, region: RegionWithLeader) -> Result<RegionStore> {
158160
Ok(RegionStore::new(region, Arc::new(self.client.clone())))
@@ -197,4 +199,8 @@ impl PdClient for MockPdClient {
197199
}
198200

199201
async fn invalidate_region_cache(&self, _ver_id: crate::region::RegionVerId) {}
202+
203+
fn get_request_codec(&self) -> Self::RequestCodec {
204+
RawApiV1
205+
}
200206
}

src/raw/client.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
config::Config,
1414
pd::{PdClient, PdRpcClient},
1515
raw::lowering::*,
16-
request::{request_codec::RequestCodec, Collect, CollectSingle, Plan},
16+
request::{request_codec::RawCodec, Collect, CollectSingle, Plan},
1717
Backoff, BoundRange, ColumnFamily, Key, KvPair, Result, Value,
1818
};
1919

@@ -33,10 +33,10 @@ pub struct Client<C, PdC: PdClient = PdRpcClient<C>> {
3333
/// Whether to use the [`atomic mode`](Client::with_atomic_for_cas).
3434
atomic: bool,
3535
logger: Logger,
36-
_phantom: std::marker::PhantomData<C>,
36+
_phantom: PhantomData<C>,
3737
}
3838

39-
impl<C: RequestCodec> Client<C, PdRpcClient<C>> {
39+
impl<C: RawCodec> Client<C, PdRpcClient<C>> {
4040
/// Create a raw [`Client`] and connect to the TiKV cluster.
4141
///
4242
/// Because TiKV is managed by a [PD](https://github.com/pingcap/pd/) cluster, the endpoints for
@@ -48,8 +48,11 @@ impl<C: RequestCodec> Client<C, PdRpcClient<C>> {
4848
/// ```rust,no_run
4949
/// # use tikv_client::RawClient;
5050
/// # use futures::prelude::*;
51+
/// use tikv_client::request::request_codec::RawApiV1;
5152
/// # futures::executor::block_on(async {
52-
/// let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
53+
/// let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None)
54+
/// .await
55+
/// .unwrap();
5356
/// # });
5457
/// ```
5558
pub async fn new<S: Into<String>>(
@@ -72,10 +75,12 @@ impl<C: RequestCodec> Client<C, PdRpcClient<C>> {
7275
/// # use tikv_client::{Config, RawClient};
7376
/// # use futures::prelude::*;
7477
/// # use std::time::Duration;
78+
/// use tikv_client::request::request_codec::RawApiV1;
7579
/// # futures::executor::block_on(async {
7680
/// let client = RawClient::new_with_config(
7781
/// vec!["192.168.0.100"],
7882
/// Config::default().with_timeout(Duration::from_secs(60)),
83+
/// RawApiV1,
7984
/// None,
8085
/// )
8186
/// .await
@@ -126,8 +131,9 @@ impl<C: RequestCodec> Client<C, PdRpcClient<C>> {
126131
/// # use tikv_client::{Config, RawClient, ColumnFamily};
127132
/// # use futures::prelude::*;
128133
/// # use std::convert::TryInto;
134+
/// use tikv_client::request::request_codec::RawApiV1;
129135
/// # futures::executor::block_on(async {
130-
/// let client = RawClient::new(vec!["192.168.0.100"], None)
136+
/// let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None)
131137
/// .await
132138
/// .unwrap()
133139
/// .with_cf(ColumnFamily::Write);
@@ -165,7 +171,7 @@ impl<C: RequestCodec> Client<C, PdRpcClient<C>> {
165171
}
166172
}
167173

168-
impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
174+
impl<C: RawCodec, PdC: PdClient> Client<C, PdC> {
169175
/// Create a new 'get' request.
170176
///
171177
/// Once resolved this request will result in the fetching of the value associated with the
@@ -177,8 +183,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
177183
/// ```rust,no_run
178184
/// # use tikv_client::{Value, Config, RawClient};
179185
/// # use futures::prelude::*;
186+
/// use tikv_client::request::request_codec::RawApiV1;
180187
/// # futures::executor::block_on(async {
181-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
188+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
182189
/// let key = "TiKV".to_owned();
183190
/// let req = client.get(key);
184191
/// let result: Option<Value> = req.await.unwrap();
@@ -206,8 +213,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
206213
/// ```rust,no_run
207214
/// # use tikv_client::{KvPair, Config, RawClient};
208215
/// # use futures::prelude::*;
216+
/// use tikv_client::request::request_codec::RawApiV1;
209217
/// # futures::executor::block_on(async {
210-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
218+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
211219
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
212220
/// let req = client.batch_get(keys);
213221
/// let result: Vec<KvPair> = req.await.unwrap();
@@ -237,8 +245,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
237245
/// ```rust,no_run
238246
/// # use tikv_client::{Key, Value, Config, RawClient};
239247
/// # use futures::prelude::*;
248+
/// use tikv_client::request::request_codec::RawApiV1;
240249
/// # futures::executor::block_on(async {
241-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
250+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
242251
/// let key = "TiKV".to_owned();
243252
/// let val = "TiKV".to_owned();
244253
/// let req = client.put(key, val);
@@ -266,8 +275,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
266275
/// ```rust,no_run
267276
/// # use tikv_client::{Result, KvPair, Key, Value, Config, RawClient, IntoOwnedRange};
268277
/// # use futures::prelude::*;
278+
/// use tikv_client::request::request_codec::RawApiV1;
269279
/// # futures::executor::block_on(async {
270-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
280+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
271281
/// let kvpair1 = ("PD".to_owned(), "Go".to_owned());
272282
/// let kvpair2 = ("TiKV".to_owned(), "Rust".to_owned());
273283
/// let iterable = vec![kvpair1, kvpair2];
@@ -303,8 +313,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
303313
/// ```rust,no_run
304314
/// # use tikv_client::{Key, Config, RawClient};
305315
/// # use futures::prelude::*;
316+
/// use tikv_client::request::request_codec::RawApiV1;
306317
/// # futures::executor::block_on(async {
307-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
318+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
308319
/// let key = "TiKV".to_owned();
309320
/// let req = client.delete(key);
310321
/// let result: () = req.await.unwrap();
@@ -332,8 +343,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
332343
/// ```rust,no_run
333344
/// # use tikv_client::{Config, RawClient};
334345
/// # use futures::prelude::*;
346+
/// use tikv_client::request::request_codec::RawApiV1;
335347
/// # futures::executor::block_on(async {
336-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
348+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
337349
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
338350
/// let req = client.batch_delete(keys);
339351
/// let result: () = req.await.unwrap();
@@ -360,8 +372,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
360372
/// ```rust,no_run
361373
/// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
362374
/// # use futures::prelude::*;
375+
/// use tikv_client::request::request_codec::RawApiV1;
363376
/// # futures::executor::block_on(async {
364-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
377+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
365378
/// let inclusive_range = "TiKV"..="TiDB";
366379
/// let req = client.delete_range(inclusive_range.into_owned());
367380
/// let result: () = req.await.unwrap();
@@ -399,8 +412,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
399412
/// ```rust,no_run
400413
/// # use tikv_client::{KvPair, Config, RawClient, IntoOwnedRange};
401414
/// # use futures::prelude::*;
415+
/// use tikv_client::request::request_codec::RawApiV1;
402416
/// # futures::executor::block_on(async {
403-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
417+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
404418
/// let inclusive_range = "TiKV"..="TiDB";
405419
/// let req = client.scan(inclusive_range.into_owned(), 2);
406420
/// let result: Vec<KvPair> = req.await.unwrap();
@@ -423,8 +437,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
423437
/// ```rust,no_run
424438
/// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
425439
/// # use futures::prelude::*;
440+
/// use tikv_client::request::request_codec::RawApiV1;
426441
/// # futures::executor::block_on(async {
427-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
442+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
428443
/// let inclusive_range = "TiKV"..="TiDB";
429444
/// let req = client.scan_keys(inclusive_range.into_owned(), 2);
430445
/// let result: Vec<Key> = req.await.unwrap();
@@ -454,8 +469,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
454469
/// ```rust,no_run
455470
/// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
456471
/// # use futures::prelude::*;
472+
/// use tikv_client::request::request_codec::RawApiV1;
457473
/// # futures::executor::block_on(async {
458-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
474+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
459475
/// let inclusive_range1 = "TiDB"..="TiKV";
460476
/// let inclusive_range2 = "TiKV"..="TiSpark";
461477
/// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
@@ -486,8 +502,9 @@ impl<C: RequestCodec, PdC: PdClient> Client<C, PdC> {
486502
/// ```rust,no_run
487503
/// # use tikv_client::{Key, Config, RawClient, IntoOwnedRange};
488504
/// # use futures::prelude::*;
505+
/// use tikv_client::request::request_codec::RawApiV1;
489506
/// # futures::executor::block_on(async {
490-
/// # let client = RawClient::new(vec!["192.168.0.100"], None).await.unwrap();
507+
/// # let client = RawClient::new(vec!["192.168.0.100"], RawApiV1, None).await.unwrap();
491508
/// let inclusive_range1 = "TiDB"..="TiKV";
492509
/// let inclusive_range2 = "TiKV"..="TiSpark";
493510
/// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
@@ -634,6 +651,7 @@ mod tests {
634651

635652
use crate::{
636653
mock::{MockKvClient, MockPdClient},
654+
request::request_codec::RawApiV1,
637655
Result,
638656
};
639657

@@ -669,6 +687,7 @@ mod tests {
669687
cf: Some(ColumnFamily::Default),
670688
atomic: false,
671689
logger,
690+
_phantom: PhantomData::<RawApiV1>,
672691
};
673692
let resps = client
674693
.coprocessor(

src/raw/requests.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,6 @@ impl Request for RawCoprocessorRequest {
496496

497497
impl<C: RequestCodec> KvRequest<C> for RawCoprocessorRequest {
498498
type Response = kvrpcpb::RawCoprocessorResponse;
499-
500-
fn encode_request(&self, _codec: &C) -> Cow<Self> {
501-
todo!()
502-
}
503-
504-
fn decode_response(&self, _codec: &C, _resp: Self::Response) -> Result<Self::Response> {
505-
todo!()
506-
}
507499
}
508500

509501
impl Shardable for RawCoprocessorRequest {

0 commit comments

Comments
 (0)