Skip to content

Commit 4120b76

Browse files
committed
style: resolve comments
Signed-off-by: ekexium <[email protected]>
1 parent d789cc6 commit 4120b76

File tree

8 files changed

+91
-112
lines changed

8 files changed

+91
-112
lines changed

src/kv/key.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ impl Key {
128128
}
129129
}
130130

131-
pub trait HasKeys {
132-
fn get_keys(&self) -> Vec<Key>;
133-
}
134-
135131
impl From<Vec<u8>> for Key {
136132
fn from(v: Vec<u8>) -> Self {
137133
Key(v)

src/kv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod kvpair;
88
mod value;
99

1010
pub use bound_range::{BoundRange, IntoOwnedRange};
11-
pub use key::{HasKeys, Key};
11+
pub use key::Key;
1212
pub use kvpair::KvPair;
1313
pub use value::Value;
1414

src/request/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use tikv_client_store::{HasError, Request};
1010

1111
pub use self::{
1212
plan::{
13-
Collect, CollectAndMatchKey, CollectError, DefaultProcessor, Dispatch, ExtractError, Merge,
14-
MergeResponse, MultiRegion, Plan, Process, ProcessResponse, ResolveLock, RetryRegion,
13+
Collect, CollectAndMatchKey, CollectError, DefaultProcessor, Dispatch, ExtractError,
14+
HasKeys, Merge, MergeResponse, MultiRegion, Plan, PreserveKey, Process, ProcessResponse,
15+
ResolveLock, RetryRegion,
1516
},
1617
plan_builder::{PlanBuilder, SingleKey},
1718
shard::Shardable,

src/request/plan.rs

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use crate::{
44
backoff::Backoff,
5-
kv::HasKeys,
65
pd::PdClient,
76
request::{KvRequest, Shardable},
87
stats::tikv_stats,
@@ -340,67 +339,12 @@ where
340339
async fn execute(&self) -> Result<Self::Result> {
341340
let keys = self.inner.get_keys();
342341
let res = self.inner.execute().await?;
343-
// TODO: should we check they have the same length?
344342
Ok(ResponseAndKeys(res, keys))
345343
}
346344
}
347345

348-
#[cfg(test)]
349-
mod test {
350-
use super::*;
351-
use crate::mock::{mock_store, MockPdClient};
352-
use futures::stream::BoxStream;
353-
use tikv_client_proto::kvrpcpb::BatchGetResponse;
354-
355-
#[derive(Clone)]
356-
struct ErrPlan;
357-
358-
#[async_trait]
359-
impl Plan for ErrPlan {
360-
type Result = BatchGetResponse;
361-
362-
async fn execute(&self) -> Result<Self::Result> {
363-
Err(Error::Unimplemented)
364-
}
365-
}
366-
367-
impl Shardable for ErrPlan {
368-
type Shard = u8;
369-
370-
fn shards(
371-
&self,
372-
_: &Arc<impl crate::pd::PdClient>,
373-
) -> BoxStream<'static, crate::Result<(Self::Shard, crate::store::Store)>> {
374-
Box::pin(stream::iter(1..=3).map(|_| Err(Error::Unimplemented)))
375-
.map_ok(|_: u8| (42, mock_store()))
376-
.boxed()
377-
}
378-
379-
fn apply_shard(&mut self, _: Self::Shard, _: &crate::store::Store) -> Result<()> {
380-
Ok(())
381-
}
382-
}
383-
384-
#[tokio::test]
385-
async fn test_err() {
386-
let plan = RetryRegion {
387-
inner: MultiRegion {
388-
inner: ResolveLock {
389-
inner: ErrPlan,
390-
backoff: Backoff::no_backoff(),
391-
pd_client: Arc::new(MockPdClient::default()),
392-
},
393-
pd_client: Arc::new(MockPdClient::default()),
394-
},
395-
backoff: Backoff::no_backoff(),
396-
pd_client: Arc::new(MockPdClient::default()),
397-
};
398-
plan.execute()
399-
.await
400-
.unwrap()
401-
.iter()
402-
.for_each(|r| assert!(r.is_err()));
403-
}
346+
pub trait HasKeys {
347+
fn get_keys(&self) -> Vec<Key>;
404348
}
405349

406350
// contains a response and the corresponding keys
@@ -459,3 +403,61 @@ impl Merge<ResponseAndKeys<kvrpcpb::PessimisticLockResponse>> for CollectAndMatc
459403
.collect()
460404
}
461405
}
406+
407+
#[cfg(test)]
408+
mod test {
409+
use super::*;
410+
use crate::mock::{mock_store, MockPdClient};
411+
use futures::stream::BoxStream;
412+
use tikv_client_proto::kvrpcpb::BatchGetResponse;
413+
414+
#[derive(Clone)]
415+
struct ErrPlan;
416+
417+
#[async_trait]
418+
impl Plan for ErrPlan {
419+
type Result = BatchGetResponse;
420+
421+
async fn execute(&self) -> Result<Self::Result> {
422+
Err(Error::Unimplemented)
423+
}
424+
}
425+
426+
impl Shardable for ErrPlan {
427+
type Shard = u8;
428+
429+
fn shards(
430+
&self,
431+
_: &Arc<impl crate::pd::PdClient>,
432+
) -> BoxStream<'static, crate::Result<(Self::Shard, crate::store::Store)>> {
433+
Box::pin(stream::iter(1..=3).map(|_| Err(Error::Unimplemented)))
434+
.map_ok(|_: u8| (42, mock_store()))
435+
.boxed()
436+
}
437+
438+
fn apply_shard(&mut self, _: Self::Shard, _: &crate::store::Store) -> Result<()> {
439+
Ok(())
440+
}
441+
}
442+
443+
#[tokio::test]
444+
async fn test_err() {
445+
let plan = RetryRegion {
446+
inner: MultiRegion {
447+
inner: ResolveLock {
448+
inner: ErrPlan,
449+
backoff: Backoff::no_backoff(),
450+
pd_client: Arc::new(MockPdClient::default()),
451+
},
452+
pd_client: Arc::new(MockPdClient::default()),
453+
},
454+
backoff: Backoff::no_backoff(),
455+
pd_client: Arc::new(MockPdClient::default()),
456+
};
457+
plan.execute()
458+
.await
459+
.unwrap()
460+
.iter()
461+
.for_each(|r| assert!(r.is_err()));
462+
}
463+
}

src/request/plan_builder.rs

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

3+
use super::PreserveKey;
34
use crate::{
45
backoff::Backoff,
5-
kv::HasKeys,
66
pd::PdClient,
77
request::{
8-
DefaultProcessor, Dispatch, ExtractError, KvRequest, Merge, MergeResponse, MultiRegion,
9-
Plan, Process, ProcessResponse, ResolveLock, RetryRegion, Shardable,
8+
DefaultProcessor, Dispatch, ExtractError, HasKeys, KvRequest, Merge, MergeResponse,
9+
MultiRegion, Plan, Process, ProcessResponse, ResolveLock, RetryRegion, Shardable,
1010
},
1111
store::Store,
1212
transaction::HasLocks,
@@ -15,8 +15,6 @@ use crate::{
1515
use std::{marker::PhantomData, sync::Arc};
1616
use tikv_client_store::HasError;
1717

18-
use super::plan::PreserveKey;
19-
2018
/// Builder type for plans (see that module for more).
2119
pub struct PlanBuilder<PdC: PdClient, P: Plan, Ph: PlanBuilderPhase> {
2220
pd_client: Arc<PdC>,

src/request/shard.rs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use crate::{
4-
kv::HasKeys,
54
pd::PdClient,
6-
request::{Dispatch, KvRequest, Plan, ResolveLock, RetryRegion},
5+
request::{Dispatch, HasKeys, KvRequest, Plan, PreserveKey, ResolveLock, RetryRegion},
76
store::Store,
87
Result,
98
};
109
use futures::stream::BoxStream;
1110
use std::sync::Arc;
1211

13-
use super::plan::PreserveKey;
12+
macro_rules! impl_inner_shardable {
13+
() => {
14+
type Shard = P::Shard;
15+
16+
fn shards(
17+
&self,
18+
pd_client: &Arc<impl PdClient>,
19+
) -> BoxStream<'static, Result<(Self::Shard, Store)>> {
20+
self.inner.shards(pd_client)
21+
}
22+
23+
fn apply_shard(&mut self, shard: Self::Shard, store: &Store) -> Result<()> {
24+
self.inner.apply_shard(shard, store)
25+
}
26+
};
27+
}
1428

1529
pub trait Shardable {
1630
type Shard: Send;
@@ -40,48 +54,15 @@ impl<Req: KvRequest + Shardable> Shardable for Dispatch<Req> {
4054
}
4155

4256
impl<P: Plan + Shardable, PdC: PdClient> Shardable for ResolveLock<P, PdC> {
43-
type Shard = P::Shard;
44-
45-
fn shards(
46-
&self,
47-
pd_client: &Arc<impl PdClient>,
48-
) -> BoxStream<'static, Result<(Self::Shard, Store)>> {
49-
self.inner.shards(pd_client)
50-
}
51-
52-
fn apply_shard(&mut self, shard: Self::Shard, store: &Store) -> Result<()> {
53-
self.inner.apply_shard(shard, store)
54-
}
57+
impl_inner_shardable!();
5558
}
5659

5760
impl<P: Plan + HasKeys + Shardable> Shardable for PreserveKey<P> {
58-
type Shard = P::Shard;
59-
60-
fn shards(
61-
&self,
62-
pd_client: &Arc<impl PdClient>,
63-
) -> BoxStream<'static, Result<(Self::Shard, Store)>> {
64-
self.inner.shards(pd_client)
65-
}
66-
67-
fn apply_shard(&mut self, shard: Self::Shard, store: &Store) -> Result<()> {
68-
self.inner.apply_shard(shard, store)
69-
}
61+
impl_inner_shardable!();
7062
}
7163

7264
impl<P: Plan + Shardable, PdC: PdClient> Shardable for RetryRegion<P, PdC> {
73-
type Shard = P::Shard;
74-
75-
fn shards(
76-
&self,
77-
pd_client: &Arc<impl PdClient>,
78-
) -> BoxStream<'static, Result<(Self::Shard, Store)>> {
79-
self.inner.shards(pd_client)
80-
}
81-
82-
fn apply_shard(&mut self, shard: Self::Shard, store: &Store) -> Result<()> {
83-
self.inner.apply_shard(shard, store)
84-
}
65+
impl_inner_shardable!();
8566
}
8667

8768
#[macro_export]

src/transaction/requests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use crate::{
4-
kv::HasKeys,
54
pd::PdClient,
6-
request::{Collect, DefaultProcessor, KvRequest, Merge, Process, Shardable, SingleKey},
5+
request::{
6+
Collect, DefaultProcessor, HasKeys, KvRequest, Merge, Process, Shardable, SingleKey,
7+
},
78
store::{store_stream_for_keys, store_stream_for_range_by_start_key, Store},
89
timestamp::TimestampExt,
910
transaction::HasLocks,

tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ async fn txn_write_million() -> Result<()> {
290290
assert_eq!(sum, 2usize.pow(NUM_BITS_KEY_PER_TXN + NUM_BITS_TXN));
291291

292292
// test batch_get and batch_get_for_update
293-
const SKIP_BITS: u32 = 6; // do not retrive all because there's a limit of message size
293+
const SKIP_BITS: u32 = 7; // do not retrive all because there's a limit of message size
294294
let mut cur = 0u32;
295295
let keys = iter::repeat_with(|| {
296296
let v = cur;

0 commit comments

Comments
 (0)