Skip to content

Commit c7eeff9

Browse files
authored
Merge pull request #193 from ekexium/move-store
Refactor: move some modules to main crate
2 parents 1c490af + 87f2e59 commit c7eeff9

25 files changed

+68
-77
lines changed

Cargo.lock

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mock-tikv/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ derive-new = "0.5.8"
88
futures = "0.3"
99
grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false }
1010
log = "0.4"
11-
tikv-client-common = { path = "../tikv-client-common"}
1211
tikv-client-proto = { path = "../tikv-client-proto"}

tikv-client-common/src/kv/bound_range.rs renamed to src/kv/bound_range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use tikv_client_proto::kvrpcpb;
2727
/// ```rust
2828
/// # use std::ops::{Range, RangeInclusive, RangeTo, RangeToInclusive, RangeFrom, RangeFull, Bound};
2929
/// # use std::convert::TryInto;
30-
/// # use tikv_client_common::{Key, BoundRange};
30+
/// # use tikv_client::{Key, BoundRange};
3131
///
3232
/// let explict_range: Range<Key> = Range { start: Key::from("Rust".to_owned()), end: Key::from("TiKV".to_owned()) };
3333
/// let from_explict_range: BoundRange = explict_range.into();
@@ -78,7 +78,7 @@ impl BoundRange {
7878
/// The **end** of a scan is exclusive, unless appended with an '\0', then it is inclusive.
7979
///
8080
/// ```rust
81-
/// use tikv_client_common::{BoundRange, Key, ToOwnedRange};
81+
/// use tikv_client::{BoundRange, Key, ToOwnedRange};
8282
/// // Exclusive
8383
/// let range = "a".."z";
8484
/// assert_eq!(

tikv-client-common/src/kv/codec.rs renamed to src/kv/codec.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use crate::{errors::Result, Error};
21
use std::{io::Write, ptr};
2+
use tikv_client_common::Error;
3+
4+
use crate::Result;
35

46
const ENC_GROUP_SIZE: usize = 8;
57
const ENC_MARKER: u8 = 0xff;

tikv-client-common/src/kv/key.rs renamed to src/kv/key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const _PROPTEST_KEY_MAX: usize = 1024 * 2; // 2 KB
2121
/// This type wraps around an owned value, so it should be treated it like `String` or `Vec<u8>`.
2222
///
2323
/// ```rust
24-
/// use tikv_client_common::Key;
24+
/// use tikv_client::Key;
2525
///
2626
/// let static_str: &'static str = "TiKV";
2727
/// let from_static_str = Key::from(static_str.to_owned());
@@ -44,7 +44,7 @@ const _PROPTEST_KEY_MAX: usize = 1024 * 2; // 2 KB
4444
/// these cases using the fully-qualified-syntax is useful:
4545
///
4646
/// ```rust
47-
/// use tikv_client_common::Key;
47+
/// use tikv_client::Key;
4848
///
4949
/// let buf = "TiKV".as_bytes().to_owned();
5050
/// let key = Key::from(buf.clone());

tikv-client-common/src/kv/kvpair.rs renamed to src/kv/kvpair.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tikv_client_proto::kvrpcpb;
99
/// A key/value pair.
1010
///
1111
/// ```rust
12-
/// # use tikv_client_common::{Key, Value, KvPair};
12+
/// # use tikv_client::{Key, Value, KvPair};
1313
/// let key = "key".to_owned();
1414
/// let value = "value".to_owned();
1515
/// let constructed = KvPair::new(key.clone(), value.clone());
File renamed without changes.

tikv-client-common/src/kv/value.rs renamed to src/kv/value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const _PROPTEST_VALUE_MAX: usize = 1024 * 16; // 16 KB
1313
/// This type wraps around an owned value, so it should be treated it like `String` or `Vec<u8>`.
1414
///
1515
/// ```rust
16-
/// use tikv_client_common::Value;
16+
/// use tikv_client::Value;
1717
///
1818
/// let static_str: &'static str = "TiKV";
1919
/// let from_static_str = Value::from(static_str.to_owned());
@@ -36,7 +36,7 @@ const _PROPTEST_VALUE_MAX: usize = 1024 * 16; // 16 KB
3636
/// these cases using the fully-qualified-syntax is useful:
3737
///
3838
/// ```rust
39-
/// use tikv_client_common::Value;
39+
/// use tikv_client::Value;
4040
///
4141
/// let buf = "TiKV".as_bytes().to_owned();
4242
/// let value = Value::from(buf.clone());

src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ mod transaction;
7979
mod backoff;
8080
mod compat;
8181
mod config;
82+
mod kv;
8283
mod pd;
8384
mod raw;
85+
mod region;
8486
mod request;
8587
mod stats;
88+
mod store;
89+
mod timestamp;
8690

8791
#[cfg(test)]
8892
mod mock;
@@ -92,15 +96,16 @@ mod proptests;
9296
#[macro_use]
9397
extern crate log;
9498

99+
#[doc(inline)]
100+
pub use crate::kv::{BoundRange, Key, KvPair, ToOwnedRange, Value};
95101
#[doc(inline)]
96102
pub use crate::raw::{Client as RawClient, ColumnFamily};
97103
#[doc(inline)]
104+
pub use crate::timestamp::Timestamp;
105+
#[doc(inline)]
98106
pub use crate::transaction::{Client as TransactionClient, Snapshot, Transaction};
99107
pub use config::Config;
100108
#[doc(inline)]
101-
pub use tikv_client_common::{
102-
security::SecurityManager, BoundRange, Error, ErrorKind, Key, KvPair, Result, Timestamp,
103-
ToOwnedRange, Value,
104-
};
109+
pub use region::{Region, RegionId, RegionVerId, StoreId};
105110
#[doc(inline)]
106-
pub use tikv_client_store::region::{Region, RegionId, RegionVerId, StoreId};
111+
pub use tikv_client_common::{security::SecurityManager, Error, ErrorKind, Result};

src/mock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
88
use crate::{
99
pd::{PdClient, PdRpcClient, RetryClient},
10-
Config, Error, Key, Result, Timestamp,
10+
store::Store,
11+
Config, Error, Key, Region, RegionId, Result, Timestamp,
1112
};
1213
use async_trait::async_trait;
1314
use derive_new::new;
1415
use std::{any::Any, sync::Arc};
1516
use tikv_client_proto::metapb;
16-
use tikv_client_store::{KvClient, KvConnect, Region, RegionId, Request, Store};
17+
use tikv_client_store::{KvClient, KvConnect, Request};
1718

1819
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
1920
/// client can be tested without doing any RPC calls.

src/pd/client.rs

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

33
use crate::{
4-
compat::stream_fn, pd::RetryClient, BoundRange, Config, Key, Region, RegionId, Result,
5-
SecurityManager,
4+
compat::stream_fn, kv::codec, pd::RetryClient, store::Store, BoundRange, Config, Key, Region,
5+
RegionId, Result, SecurityManager, Timestamp,
66
};
77
use async_trait::async_trait;
88
use futures::{prelude::*, stream::BoxStream};
@@ -12,9 +12,8 @@ use std::{
1212
sync::{Arc, RwLock},
1313
thread,
1414
};
15-
use tikv_client_common::{codec, Timestamp};
1615
use tikv_client_pd::Cluster;
17-
use tikv_client_store::{KvClient, KvConnect, Store, TikvConnect};
16+
use tikv_client_store::{KvClient, KvConnect, TikvConnect};
1817

1918
const CQ_COUNT: usize = 1;
2019
const CLIENT_PREFIX: &str = "tikv-client";
@@ -315,7 +314,6 @@ pub mod test {
315314
use crate::mock::*;
316315

317316
use futures::{executor, executor::block_on};
318-
use tikv_client_common::BoundRange;
319317

320318
#[test]
321319
fn test_kv_client_caching() {

src/raw/requests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use crate::{
77
store_stream_for_key, store_stream_for_keys, store_stream_for_range,
88
store_stream_for_ranges, KvRequest,
99
},
10+
store::Store,
1011
transaction::HasLocks,
1112
BoundRange, ColumnFamily, Key, KvPair, Result, Value,
1213
};
1314
use async_trait::async_trait;
1415
use futures::{prelude::*, stream::BoxStream};
1516
use std::{mem, sync::Arc};
1617
use tikv_client_proto::kvrpcpb;
17-
use tikv_client_store::Store;
1818

1919
#[async_trait]
2020
impl KvRequest for kvrpcpb::RawGetRequest {
File renamed without changes.

src/request.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
backoff::{Backoff, NoBackoff, NoJitterBackoff},
55
pd::PdClient,
66
stats::tikv_stats,
7+
store::Store,
78
transaction::{resolve_locks, HasLocks},
89
BoundRange, Error, ErrorKind, Key, Result,
910
};
@@ -13,7 +14,7 @@ use std::{
1314
cmp::{max, min},
1415
sync::Arc,
1516
};
16-
use tikv_client_store::{HasError, HasRegionError, Request, Store};
17+
use tikv_client_store::{HasError, HasRegionError, Request};
1718

1819
const DEFAULT_REGION_BACKOFF: NoJitterBackoff = NoJitterBackoff::new(2, 500, 10);
1920
pub const OPTIMISTIC_BACKOFF: NoJitterBackoff = NoJitterBackoff::new(2, 500, 10);

src/store.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::{Region, Result};
2+
use derive_new::new;
3+
use std::any::Any;
4+
use tikv_client_store::{KvClient, KvConnect, Request, TikvConnect};
5+
6+
#[derive(new)]
7+
pub struct Store {
8+
pub region: Region,
9+
pub client: Box<dyn KvClient + Send + Sync>,
10+
}
11+
12+
impl Store {
13+
pub async fn dispatch<Req: Request, Resp: Any>(&self, request: &Req) -> Result<Box<Resp>> {
14+
Ok(self
15+
.client
16+
.dispatch(request)
17+
.await?
18+
.downcast()
19+
.expect("Downcast failed"))
20+
}
21+
}
22+
23+
pub trait KvConnectStore: KvConnect {
24+
fn connect_to_store(&self, region: Region, address: String) -> Result<Store> {
25+
info!("connect to tikv endpoint: {:?}", &address);
26+
let client = self.connect(address.as_str())?;
27+
Ok(Store::new(region, Box::new(client)))
28+
}
29+
}
30+
31+
impl KvConnectStore for TikvConnect {}
File renamed without changes.

src/transaction/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::{
55
config::Config,
66
pd::{PdClient, PdRpcClient},
77
request::{KvRequest, OPTIMISTIC_BACKOFF},
8+
timestamp::TimestampExt,
89
transaction::{Snapshot, Transaction},
910
Result,
1011
};
1112
use futures::executor::ThreadPool;
1213
use std::{mem, sync::Arc};
13-
use tikv_client_common::TimestampExt;
1414
use tikv_client_proto::{kvrpcpb, pdpb::Timestamp};
1515

1616
const SCAN_LOCK_BATCH_SIZE: u32 = 1024; // TODO: cargo-culted value

src/transaction/lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::{
22
pd::PdClient,
33
request::{KvRequest, OPTIMISTIC_BACKOFF},
4+
timestamp::TimestampExt,
45
transaction::requests,
56
ErrorKind, Key, RegionVerId, Result,
67
};
78
use std::{
89
collections::{HashMap, HashSet},
910
sync::Arc,
1011
};
11-
use tikv_client_common::TimestampExt;
1212
use tikv_client_proto::{kvrpcpb, pdpb::Timestamp};
1313

1414
const RESOLVE_LOCK_RETRY_LIMIT: usize = 10;

src/transaction/requests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use crate::{
44
backoff::Backoff,
55
pd::PdClient,
66
request::{store_stream_for_key, store_stream_for_keys, store_stream_for_range, KvRequest},
7+
store::Store,
8+
timestamp::TimestampExt,
79
transaction::HasLocks,
810
BoundRange, Error, Key, KvPair, Result, Value,
911
};
1012
use async_trait::async_trait;
1113
use futures::{prelude::*, stream::BoxStream};
1214
use std::{iter, mem, sync::Arc};
13-
use tikv_client_common::TimestampExt;
1415
use tikv_client_proto::{kvrpcpb, pdpb::Timestamp};
15-
use tikv_client_store::Store;
1616

1717
#[async_trait]
1818
impl KvRequest for kvrpcpb::GetRequest {

src/transaction/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use crate::{
44
pd::{PdClient, PdRpcClient},
55
request::{KvRequest, OPTIMISTIC_BACKOFF, PESSIMISTIC_BACKOFF},
6+
timestamp::TimestampExt,
67
transaction::{buffer::Buffer, requests::*},
78
BoundRange, Error, ErrorKind, Key, KvPair, Result, Value,
89
};
910
use derive_new::new;
1011
use futures::{executor::ThreadPool, prelude::*, stream::BoxStream};
1112
use std::{iter, mem, ops::RangeBounds, sync::Arc};
12-
use tikv_client_common::TimestampExt;
1313
use tikv_client_proto::{kvrpcpb, pdpb::Timestamp};
1414

1515
/// A undo-able set of actions on the dataset.

tikv-client-common/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ edition = "2018"
55

66

77
[dependencies]
8-
derive-new = "0.5"
98
failure = "0.1"
109
futures = { version = "0.3.5", features = ["compat", "async-await", "thread-pool"] }
1110
grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false }
1211
lazy_static = "1"
1312
log = "0.4"
1413
regex = "1"
15-
serde = "1.0"
16-
serde_derive = "1.0"
1714
tikv-client-proto = { path = "../tikv-client-proto" }
1815

1916
[dev-dependencies]

tikv-client-common/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#[macro_use]
22
mod errors;
3-
mod kv;
43
pub mod security;
5-
mod timestamp;
64

75
#[macro_use]
86
extern crate log;
97

108
#[doc(inline)]
119
pub use crate::errors::{Error, ErrorKind, Result};
12-
#[doc(inline)]
13-
pub use crate::kv::{codec, BoundRange, Key, KvPair, ToOwnedRange, Value};
14-
#[doc(inline)]
15-
pub use crate::timestamp::{Timestamp, TimestampExt};

tikv-client-pd/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ edition = "2018"
55

66
[dependencies]
77
async-trait = "0.1"
8-
derive-new = "0.5"
98
futures = { version = "0.3.5", features = ["compat", "async-await", "thread-pool"] }
109
grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false }
1110
log = "0.4"
1211
tikv-client-common = { path = "../tikv-client-common" }
1312
tikv-client-proto = { path = "../tikv-client-proto" }
14-
tokio = { version = "0.2", features = ["sync"] }
1513

1614
[dev-dependencies]
1715
clap = "2.32"
1816
fail = { version = "0.3", features = [ "failpoints" ] }
1917
proptest = "0.9"
2018
proptest-derive = "0.1.0"
2119
tempdir = "0.3"
22-
tokio = { version = "0.2", features = ["rt-threaded", "macros"] }

0 commit comments

Comments
 (0)