diff --git a/Cargo.lock b/Cargo.lock index f84ef2a4..57354fb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -884,7 +884,6 @@ dependencies = [ "futures 0.3.5", "grpcio", "log", - "tikv-client-common", "tikv-client-proto", ] @@ -1854,7 +1853,6 @@ name = "tikv-client-common" version = "0.0.0" dependencies = [ "clap", - "derive-new", "fail", "failure", "futures 0.3.5", @@ -1864,8 +1862,6 @@ dependencies = [ "proptest", "proptest-derive", "regex", - "serde", - "serde_derive", "tempdir", "tikv-client-proto", "tokio", @@ -1877,7 +1873,6 @@ version = "0.0.0" dependencies = [ "async-trait", "clap", - "derive-new", "fail", "futures 0.3.5", "grpcio", @@ -1887,7 +1882,6 @@ dependencies = [ "tempdir", "tikv-client-common", "tikv-client-proto", - "tokio", ] [[package]] diff --git a/mock-tikv/Cargo.toml b/mock-tikv/Cargo.toml index 39dfcdce..d2c3ab39 100644 --- a/mock-tikv/Cargo.toml +++ b/mock-tikv/Cargo.toml @@ -8,5 +8,4 @@ derive-new = "0.5.8" futures = "0.3" grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false } log = "0.4" -tikv-client-common = { path = "../tikv-client-common"} tikv-client-proto = { path = "../tikv-client-proto"} diff --git a/tikv-client-common/src/kv/bound_range.rs b/src/kv/bound_range.rs similarity index 98% rename from tikv-client-common/src/kv/bound_range.rs rename to src/kv/bound_range.rs index 5b6008ef..514cbfde 100644 --- a/tikv-client-common/src/kv/bound_range.rs +++ b/src/kv/bound_range.rs @@ -27,7 +27,7 @@ use tikv_client_proto::kvrpcpb; /// ```rust /// # use std::ops::{Range, RangeInclusive, RangeTo, RangeToInclusive, RangeFrom, RangeFull, Bound}; /// # use std::convert::TryInto; -/// # use tikv_client_common::{Key, BoundRange}; +/// # use tikv_client::{Key, BoundRange}; /// /// let explict_range: Range = Range { start: Key::from("Rust".to_owned()), end: Key::from("TiKV".to_owned()) }; /// let from_explict_range: BoundRange = explict_range.into(); @@ -78,7 +78,7 @@ impl BoundRange { /// The **end** of a scan is exclusive, unless appended with an '\0', then it is inclusive. /// /// ```rust - /// use tikv_client_common::{BoundRange, Key, ToOwnedRange}; + /// use tikv_client::{BoundRange, Key, ToOwnedRange}; /// // Exclusive /// let range = "a".."z"; /// assert_eq!( diff --git a/tikv-client-common/src/kv/codec.rs b/src/kv/codec.rs similarity index 99% rename from tikv-client-common/src/kv/codec.rs rename to src/kv/codec.rs index dd9b9b41..64231d78 100644 --- a/tikv-client-common/src/kv/codec.rs +++ b/src/kv/codec.rs @@ -1,5 +1,7 @@ -use crate::{errors::Result, Error}; use std::{io::Write, ptr}; +use tikv_client_common::Error; + +use crate::Result; const ENC_GROUP_SIZE: usize = 8; const ENC_MARKER: u8 = 0xff; diff --git a/tikv-client-common/src/kv/key.rs b/src/kv/key.rs similarity index 98% rename from tikv-client-common/src/kv/key.rs rename to src/kv/key.rs index 424bc78e..175ce2a1 100644 --- a/tikv-client-common/src/kv/key.rs +++ b/src/kv/key.rs @@ -21,7 +21,7 @@ const _PROPTEST_KEY_MAX: usize = 1024 * 2; // 2 KB /// This type wraps around an owned value, so it should be treated it like `String` or `Vec`. /// /// ```rust -/// use tikv_client_common::Key; +/// use tikv_client::Key; /// /// let static_str: &'static str = "TiKV"; /// let from_static_str = Key::from(static_str.to_owned()); @@ -44,7 +44,7 @@ const _PROPTEST_KEY_MAX: usize = 1024 * 2; // 2 KB /// these cases using the fully-qualified-syntax is useful: /// /// ```rust -/// use tikv_client_common::Key; +/// use tikv_client::Key; /// /// let buf = "TiKV".as_bytes().to_owned(); /// let key = Key::from(buf.clone()); diff --git a/tikv-client-common/src/kv/kvpair.rs b/src/kv/kvpair.rs similarity index 98% rename from tikv-client-common/src/kv/kvpair.rs rename to src/kv/kvpair.rs index 2dca90a6..54bff9fe 100644 --- a/tikv-client-common/src/kv/kvpair.rs +++ b/src/kv/kvpair.rs @@ -9,7 +9,7 @@ use tikv_client_proto::kvrpcpb; /// A key/value pair. /// /// ```rust -/// # use tikv_client_common::{Key, Value, KvPair}; +/// # use tikv_client::{Key, Value, KvPair}; /// let key = "key".to_owned(); /// let value = "value".to_owned(); /// let constructed = KvPair::new(key.clone(), value.clone()); diff --git a/tikv-client-common/src/kv/mod.rs b/src/kv/mod.rs similarity index 100% rename from tikv-client-common/src/kv/mod.rs rename to src/kv/mod.rs diff --git a/tikv-client-common/src/kv/value.rs b/src/kv/value.rs similarity index 96% rename from tikv-client-common/src/kv/value.rs rename to src/kv/value.rs index 4ee535cb..9bef87e8 100644 --- a/tikv-client-common/src/kv/value.rs +++ b/src/kv/value.rs @@ -13,7 +13,7 @@ const _PROPTEST_VALUE_MAX: usize = 1024 * 16; // 16 KB /// This type wraps around an owned value, so it should be treated it like `String` or `Vec`. /// /// ```rust -/// use tikv_client_common::Value; +/// use tikv_client::Value; /// /// let static_str: &'static str = "TiKV"; /// let from_static_str = Value::from(static_str.to_owned()); @@ -36,7 +36,7 @@ const _PROPTEST_VALUE_MAX: usize = 1024 * 16; // 16 KB /// these cases using the fully-qualified-syntax is useful: /// /// ```rust -/// use tikv_client_common::Value; +/// use tikv_client::Value; /// /// let buf = "TiKV".as_bytes().to_owned(); /// let value = Value::from(buf.clone()); diff --git a/src/lib.rs b/src/lib.rs index 6edf34d8..9879758e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,10 +79,14 @@ mod transaction; mod backoff; mod compat; mod config; +mod kv; mod pd; mod raw; +mod region; mod request; mod stats; +mod store; +mod timestamp; #[cfg(test)] mod mock; @@ -92,15 +96,16 @@ mod proptests; #[macro_use] extern crate log; +#[doc(inline)] +pub use crate::kv::{BoundRange, Key, KvPair, ToOwnedRange, Value}; #[doc(inline)] pub use crate::raw::{Client as RawClient, ColumnFamily}; #[doc(inline)] +pub use crate::timestamp::Timestamp; +#[doc(inline)] pub use crate::transaction::{Client as TransactionClient, Snapshot, Transaction}; pub use config::Config; #[doc(inline)] -pub use tikv_client_common::{ - security::SecurityManager, BoundRange, Error, ErrorKind, Key, KvPair, Result, Timestamp, - ToOwnedRange, Value, -}; +pub use region::{Region, RegionId, RegionVerId, StoreId}; #[doc(inline)] -pub use tikv_client_store::region::{Region, RegionId, RegionVerId, StoreId}; +pub use tikv_client_common::{security::SecurityManager, Error, ErrorKind, Result}; diff --git a/src/mock.rs b/src/mock.rs index 834698d0..2c7ff6e6 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -7,13 +7,14 @@ use crate::{ pd::{PdClient, PdRpcClient, RetryClient}, - Config, Error, Key, Result, Timestamp, + store::Store, + Config, Error, Key, Region, RegionId, Result, Timestamp, }; use async_trait::async_trait; use derive_new::new; use std::{any::Any, sync::Arc}; use tikv_client_proto::metapb; -use tikv_client_store::{KvClient, KvConnect, Region, RegionId, Request, Store}; +use tikv_client_store::{KvClient, KvConnect, Request}; /// Create a `PdRpcClient` with it's internals replaced with mocks so that the /// client can be tested without doing any RPC calls. diff --git a/src/pd/client.rs b/src/pd/client.rs index 75e8a6c8..2c7104f6 100644 --- a/src/pd/client.rs +++ b/src/pd/client.rs @@ -1,8 +1,8 @@ // Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0. use crate::{ - compat::stream_fn, pd::RetryClient, BoundRange, Config, Key, Region, RegionId, Result, - SecurityManager, + compat::stream_fn, kv::codec, pd::RetryClient, store::Store, BoundRange, Config, Key, Region, + RegionId, Result, SecurityManager, Timestamp, }; use async_trait::async_trait; use futures::{prelude::*, stream::BoxStream}; @@ -12,9 +12,8 @@ use std::{ sync::{Arc, RwLock}, thread, }; -use tikv_client_common::{codec, Timestamp}; use tikv_client_pd::Cluster; -use tikv_client_store::{KvClient, KvConnect, Store, TikvConnect}; +use tikv_client_store::{KvClient, KvConnect, TikvConnect}; const CQ_COUNT: usize = 1; const CLIENT_PREFIX: &str = "tikv-client"; @@ -315,7 +314,6 @@ pub mod test { use crate::mock::*; use futures::{executor, executor::block_on}; - use tikv_client_common::BoundRange; #[test] fn test_kv_client_caching() { diff --git a/src/raw/requests.rs b/src/raw/requests.rs index 694d0031..3b40d65e 100644 --- a/src/raw/requests.rs +++ b/src/raw/requests.rs @@ -7,6 +7,7 @@ use crate::{ store_stream_for_key, store_stream_for_keys, store_stream_for_range, store_stream_for_ranges, KvRequest, }, + store::Store, transaction::HasLocks, BoundRange, ColumnFamily, Key, KvPair, Result, Value, }; @@ -14,7 +15,6 @@ use async_trait::async_trait; use futures::{prelude::*, stream::BoxStream}; use std::{mem, sync::Arc}; use tikv_client_proto::kvrpcpb; -use tikv_client_store::Store; #[async_trait] impl KvRequest for kvrpcpb::RawGetRequest { diff --git a/tikv-client-store/src/region.rs b/src/region.rs similarity index 100% rename from tikv-client-store/src/region.rs rename to src/region.rs diff --git a/src/request.rs b/src/request.rs index b31a74cf..f7810856 100644 --- a/src/request.rs +++ b/src/request.rs @@ -4,6 +4,7 @@ use crate::{ backoff::{Backoff, NoBackoff, NoJitterBackoff}, pd::PdClient, stats::tikv_stats, + store::Store, transaction::{resolve_locks, HasLocks}, BoundRange, Error, ErrorKind, Key, Result, }; @@ -13,7 +14,7 @@ use std::{ cmp::{max, min}, sync::Arc, }; -use tikv_client_store::{HasError, HasRegionError, Request, Store}; +use tikv_client_store::{HasError, HasRegionError, Request}; const DEFAULT_REGION_BACKOFF: NoJitterBackoff = NoJitterBackoff::new(2, 500, 10); pub const OPTIMISTIC_BACKOFF: NoJitterBackoff = NoJitterBackoff::new(2, 500, 10); diff --git a/src/store.rs b/src/store.rs new file mode 100644 index 00000000..d6d395e5 --- /dev/null +++ b/src/store.rs @@ -0,0 +1,31 @@ +use crate::{Region, Result}; +use derive_new::new; +use std::any::Any; +use tikv_client_store::{KvClient, KvConnect, Request, TikvConnect}; + +#[derive(new)] +pub struct Store { + pub region: Region, + pub client: Box, +} + +impl Store { + pub async fn dispatch(&self, request: &Req) -> Result> { + Ok(self + .client + .dispatch(request) + .await? + .downcast() + .expect("Downcast failed")) + } +} + +pub trait KvConnectStore: KvConnect { + fn connect_to_store(&self, region: Region, address: String) -> Result { + info!("connect to tikv endpoint: {:?}", &address); + let client = self.connect(address.as_str())?; + Ok(Store::new(region, Box::new(client))) + } +} + +impl KvConnectStore for TikvConnect {} diff --git a/tikv-client-common/src/timestamp.rs b/src/timestamp.rs similarity index 100% rename from tikv-client-common/src/timestamp.rs rename to src/timestamp.rs diff --git a/src/transaction/client.rs b/src/transaction/client.rs index f9033035..ba11835c 100644 --- a/src/transaction/client.rs +++ b/src/transaction/client.rs @@ -5,12 +5,12 @@ use crate::{ config::Config, pd::{PdClient, PdRpcClient}, request::{KvRequest, OPTIMISTIC_BACKOFF}, + timestamp::TimestampExt, transaction::{Snapshot, Transaction}, Result, }; use futures::executor::ThreadPool; use std::{mem, sync::Arc}; -use tikv_client_common::TimestampExt; use tikv_client_proto::{kvrpcpb, pdpb::Timestamp}; const SCAN_LOCK_BATCH_SIZE: u32 = 1024; // TODO: cargo-culted value diff --git a/src/transaction/lock.rs b/src/transaction/lock.rs index 8da6b8d0..b44ab5bf 100644 --- a/src/transaction/lock.rs +++ b/src/transaction/lock.rs @@ -1,6 +1,7 @@ use crate::{ pd::PdClient, request::{KvRequest, OPTIMISTIC_BACKOFF}, + timestamp::TimestampExt, transaction::requests, ErrorKind, Key, RegionVerId, Result, }; @@ -8,7 +9,6 @@ use std::{ collections::{HashMap, HashSet}, sync::Arc, }; -use tikv_client_common::TimestampExt; use tikv_client_proto::{kvrpcpb, pdpb::Timestamp}; const RESOLVE_LOCK_RETRY_LIMIT: usize = 10; diff --git a/src/transaction/requests.rs b/src/transaction/requests.rs index 18914d3e..b5ec27c0 100644 --- a/src/transaction/requests.rs +++ b/src/transaction/requests.rs @@ -4,15 +4,15 @@ use crate::{ backoff::Backoff, pd::PdClient, request::{store_stream_for_key, store_stream_for_keys, store_stream_for_range, KvRequest}, + store::Store, + timestamp::TimestampExt, transaction::HasLocks, BoundRange, Error, Key, KvPair, Result, Value, }; use async_trait::async_trait; use futures::{prelude::*, stream::BoxStream}; use std::{iter, mem, sync::Arc}; -use tikv_client_common::TimestampExt; use tikv_client_proto::{kvrpcpb, pdpb::Timestamp}; -use tikv_client_store::Store; #[async_trait] impl KvRequest for kvrpcpb::GetRequest { diff --git a/src/transaction/transaction.rs b/src/transaction/transaction.rs index ea6e5364..916ca257 100644 --- a/src/transaction/transaction.rs +++ b/src/transaction/transaction.rs @@ -3,13 +3,13 @@ use crate::{ pd::{PdClient, PdRpcClient}, request::{KvRequest, OPTIMISTIC_BACKOFF, PESSIMISTIC_BACKOFF}, + timestamp::TimestampExt, transaction::{buffer::Buffer, requests::*}, BoundRange, Error, ErrorKind, Key, KvPair, Result, Value, }; use derive_new::new; use futures::{executor::ThreadPool, prelude::*, stream::BoxStream}; use std::{iter, mem, ops::RangeBounds, sync::Arc}; -use tikv_client_common::TimestampExt; use tikv_client_proto::{kvrpcpb, pdpb::Timestamp}; /// A undo-able set of actions on the dataset. diff --git a/tikv-client-common/Cargo.toml b/tikv-client-common/Cargo.toml index 0ab3dc28..e9575c19 100644 --- a/tikv-client-common/Cargo.toml +++ b/tikv-client-common/Cargo.toml @@ -5,15 +5,12 @@ edition = "2018" [dependencies] -derive-new = "0.5" failure = "0.1" futures = { version = "0.3.5", features = ["compat", "async-await", "thread-pool"] } grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false } lazy_static = "1" log = "0.4" regex = "1" -serde = "1.0" -serde_derive = "1.0" tikv-client-proto = { path = "../tikv-client-proto" } [dev-dependencies] diff --git a/tikv-client-common/src/lib.rs b/tikv-client-common/src/lib.rs index 066b5239..53c7fdda 100644 --- a/tikv-client-common/src/lib.rs +++ b/tikv-client-common/src/lib.rs @@ -1,15 +1,9 @@ #[macro_use] mod errors; -mod kv; pub mod security; -mod timestamp; #[macro_use] extern crate log; #[doc(inline)] pub use crate::errors::{Error, ErrorKind, Result}; -#[doc(inline)] -pub use crate::kv::{codec, BoundRange, Key, KvPair, ToOwnedRange, Value}; -#[doc(inline)] -pub use crate::timestamp::{Timestamp, TimestampExt}; diff --git a/tikv-client-pd/Cargo.toml b/tikv-client-pd/Cargo.toml index 6354baf1..1cb91843 100644 --- a/tikv-client-pd/Cargo.toml +++ b/tikv-client-pd/Cargo.toml @@ -5,13 +5,11 @@ edition = "2018" [dependencies] async-trait = "0.1" -derive-new = "0.5" futures = { version = "0.3.5", features = ["compat", "async-await", "thread-pool"] } grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false } log = "0.4" tikv-client-common = { path = "../tikv-client-common" } tikv-client-proto = { path = "../tikv-client-proto" } -tokio = { version = "0.2", features = ["sync"] } [dev-dependencies] clap = "2.32" @@ -19,4 +17,3 @@ fail = { version = "0.3", features = [ "failpoints" ] } proptest = "0.9" proptest-derive = "0.1.0" tempdir = "0.3" -tokio = { version = "0.2", features = ["rt-threaded", "macros"] } diff --git a/tikv-client-store/src/client.rs b/tikv-client-store/src/client.rs index 8c120a96..5e3534c5 100644 --- a/tikv-client-store/src/client.rs +++ b/tikv-client-store/src/client.rs @@ -1,6 +1,6 @@ // Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0. -use crate::{request::Request, Region, Result, SecurityManager}; +use crate::{request::Request, Result, SecurityManager}; use async_trait::async_trait; use derive_new::new; use grpcio::{CallOption, Environment}; @@ -12,12 +12,6 @@ pub trait KvConnect: Sized + Send + Sync + 'static { type KvClient: KvClient + Clone + Send + Sync + 'static; fn connect(&self, address: &str) -> Result; - - fn connect_to_store(&self, region: Region, address: String) -> Result { - info!("connect to tikv endpoint: {:?}", &address); - let client = self.connect(address.as_str())?; - Ok(Store::new(region, Box::new(client))) - } } #[derive(new, Clone)] @@ -61,20 +55,3 @@ impl KvClient for KvRpcClient { .await } } - -#[derive(new)] -pub struct Store { - pub region: Region, - pub client: Box, -} - -impl Store { - pub async fn dispatch(&self, request: &Req) -> Result> { - Ok(self - .client - .dispatch(request) - .await? - .downcast() - .expect("Downcast failed")) - } -} diff --git a/tikv-client-store/src/lib.rs b/tikv-client-store/src/lib.rs index 91ece20e..3ceeb959 100644 --- a/tikv-client-store/src/lib.rs +++ b/tikv-client-store/src/lib.rs @@ -1,18 +1,13 @@ // Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0. -#[macro_use] -extern crate log; - mod client; mod errors; -pub mod region; mod request; #[doc(inline)] pub use crate::{ - client::{KvClient, KvConnect, Store, TikvConnect}, + client::{KvClient, KvConnect, TikvConnect}, errors::{HasError, HasRegionError}, - region::{Region, RegionId, RegionVerId, StoreId}, request::Request, }; -pub use tikv_client_common::{security::SecurityManager, Error, ErrorKind, Key, Result}; +pub use tikv_client_common::{security::SecurityManager, Error, ErrorKind, Result};