From c46004de9f4e644bc24f711ea6d4c9ba46beed4f Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Sun, 14 Oct 2018 08:53:24 +0800 Subject: [PATCH 01/11] Initial public RawKV API for comments --- Cargo.toml | 11 ++ src/api/mod.rs | 2 + src/api/raw_kv.rs | 282 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 47 ++++++++ 4 files changed, 342 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/api/mod.rs create mode 100644 src/api/raw_kv.rs create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..5f721f98 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "tikv-client" +version = "0.0.0" +keywords = ["TiKV", "KV", "distributed-systems"] +license = "Apache-2.0" +authors = ["The TiKV Project Developers"] +repository = "https://github.com/tikv/client-rust" +description = "The rust language implementation of TiKV client." + +[lib] +name = "tikv_client" diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 00000000..5bd09feb --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,2 @@ +mod raw_kv; +pub use self::raw_kv::RawKV; diff --git a/src/api/raw_kv.rs b/src/api/raw_kv.rs new file mode 100644 index 00000000..e9ca6518 --- /dev/null +++ b/src/api/raw_kv.rs @@ -0,0 +1,282 @@ +use std::fmt::Debug; +use std::io::Result; + +use *; + +pub struct RawKV; + +macro_rules! define_cf_setter { + () => { + pub fn cf(mut self, cf: C) -> Self + where + C: Into, + { + self.cf = Some(cf.into()); + self + } + } +} + +trait RequestType: Default + Debug + Clone + Copy {} + +#[derive(Debug, Clone, Copy, Default)] +pub struct Get; +impl RequestType for Get {} +#[derive(Debug, Clone, Copy, Default)] +pub struct Put; +impl RequestType for Put {} +#[derive(Debug, Clone, Copy, Default)] +pub struct Delete; +impl RequestType for Delete {} +#[derive(Debug, Clone, Copy, Default)] +pub struct Scan { + limit: u32, + key_only: bool, +} +impl RequestType for Scan {} + +pub struct RawKVRequest +where + T: Default, +{ + t: T, + payload: P, + cf: Option, +} + +impl RawKVRequest +where + T: Default, +{ + pub fn new(payload: P) -> Self { + RawKVRequest { + t: T::default(), + payload, + cf: None, + } + } + + define_cf_setter!(); +} + +pub struct RawKVBatchRequest +where + T: Default, + P: Sized, +{ + t: T, + payloads: Vec

, + cf: Option, +} + +impl RawKVBatchRequest +where + T: Default, + P: Sized, +{ + pub fn new(payloads: I) -> Self + where + I: Iterator, + K: Into

, + { + RawKVBatchRequest { + t: T::default(), + payloads: payloads.map(Into::into).collect(), + cf: None, + } + } + + define_cf_setter!(); +} + +pub type RawKVKeyRequest = RawKVRequest; +pub type RawKVBatchKeyRequest = RawKVBatchRequest; +pub type RawKVKvPairRequest = RawKVRequest; +pub type RawKVBatchKvPairRequest = RawKVBatchRequest; +pub type RawKVRangeRequest = RawKVRequest; +pub type RawKVBatchRangeRequest = RawKVBatchRequest; + +pub type RawKVGetRequest = RawKVKeyRequest; +pub type RawKVBatchGetRequest = RawKVBatchKeyRequest; +pub type RawKVPutRequest = RawKVKvPairRequest; +pub type RawKVBatchPutRequest = RawKVBatchKvPairRequest; +pub type RawKVDeleteRequest = RawKVKeyRequest; +pub type RawKVBatchDeleteRequest = RawKVBatchKeyRequest; +pub type RawKVScanRequest = RawKVRangeRequest; +pub type RawKVBatchScanRequest = RawKVBatchRangeRequest; +pub type RawKVDeleteRangeRequest = RawKVRequest; + +pub type RawKVGetResponse = Result; +pub type RawKVBatchGetResponse = Result>; +pub type RawKVPutResponse = Result<()>; +pub type RawKVBatchPutResponse = Result<()>; +pub type RawKVDeleteResponse = Result<()>; +pub type RawKVBatchDeleteResponse = Result<()>; +pub type RawKVDeleteRangeResponse = Result<()>; +pub type RawKVScanResponse = Result>; +pub type RawKVBatchScanResponse = Result>; + +impl RawKV { + pub fn get(key: K) -> RawKVGetRequest + where + K: Into, + { + RawKVGetRequest::new(key.into()) + } + + pub fn batch_get(keys: I) -> RawKVBatchGetRequest + where + I: IntoIterator, + K: Into, + { + RawKVBatchGetRequest::new(keys.into_iter()) + } + + pub fn put

(pair: P) -> RawKVPutRequest + where + P: Into, + { + RawKVPutRequest::new(pair.into()) + } + + pub fn batch_put(pairs: I) -> RawKVBatchPutRequest + where + I: IntoIterator, + P: Into, + { + RawKVBatchPutRequest::new(pairs.into_iter()) + } + + pub fn delete(key: K) -> RawKVDeleteRequest + where + K: Into, + { + RawKVDeleteRequest::new(key.into()) + } + + pub fn batch_delete(keys: I) -> RawKVBatchDeleteRequest + where + I: IntoIterator, + K: Into, + { + RawKVBatchDeleteRequest::new(keys.into_iter()) + } + + pub fn scan(range: R, limit: u32) -> RawKVScanRequest + where + R: Into, + { + RawKVScanRequest::new(range.into()).limit(limit) + } + + pub fn batch_scan(ranges: I, each_limit: u32) -> RawKVBatchScanRequest + where + I: IntoIterator, + R: Into, + { + RawKVBatchScanRequest::new(ranges.into_iter()).each_limit(each_limit) + } + + pub fn delete_range(range: R) -> RawKVDeleteRangeRequest + where + R: Into, + { + RawKVDeleteRangeRequest::new(range.into()) + } +} + +impl RawKVScanRequest { + pub fn limit(mut self, limit: u32) -> Self { + self.t.limit = limit; + self + } + + pub fn key_only(mut self) -> Self { + self.t.key_only = true; + self + } +} + +impl RawKVBatchScanRequest { + pub fn each_limit(mut self, each_limit: u32) -> Self { + self.t.limit = each_limit; + self + } + + pub fn key_only(mut self) -> Self { + self.t.key_only = true; + self + } +} + +impl Request for RawKVGetRequest { + type Response = RawKVGetResponse; + + fn execute(self, _kv: &TiKV) -> RawKVGetResponse { + unimplemented!() + } +} + +impl Request for RawKVBatchGetRequest { + type Response = RawKVBatchGetResponse; + + fn execute(self, _kv: &TiKV) -> RawKVBatchGetResponse { + unimplemented!() + } +} + +impl Request for RawKVPutRequest { + type Response = RawKVPutResponse; + + fn execute(self, _kv: &TiKV) -> RawKVPutResponse { + unimplemented!() + } +} + +impl Request for RawKVBatchPutRequest { + type Response = RawKVBatchPutResponse; + + fn execute(self, _kv: &TiKV) -> RawKVBatchPutResponse { + unimplemented!() + } +} + +impl Request for RawKVDeleteRequest { + type Response = RawKVDeleteResponse; + + fn execute(self, _kv: &TiKV) -> RawKVDeleteResponse { + unimplemented!() + } +} + +impl Request for RawKVBatchDeleteRequest { + type Response = RawKVBatchDeleteResponse; + + fn execute(self, _kv: &TiKV) -> RawKVBatchDeleteResponse { + unimplemented!() + } +} + +impl Request for RawKVScanRequest { + type Response = RawKVScanResponse; + + fn execute(self, _kv: &TiKV) -> RawKVScanResponse { + unimplemented!() + } +} + +impl Request for RawKVBatchScanRequest { + type Response = RawKVBatchScanResponse; + + fn execute(self, _kv: &TiKV) -> RawKVBatchScanResponse { + unimplemented!() + } +} + +impl Request for RawKVDeleteRangeRequest { + type Response = RawKVDeleteRangeResponse; + + fn execute(self, _kv: &TiKV) -> RawKVDeleteRangeResponse { + unimplemented!() + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..4a142452 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,47 @@ +pub mod api; + +pub struct Key(Vec); +pub struct Value(Vec); +pub struct KvPair(Key, Value); +pub struct KeyRange(Key, Key); + +impl Into for Vec { + fn into(self) -> Key { + Key(self) + } +} + +impl Into for Vec { + fn into(self) -> Value { + Value(self) + } +} + +impl Into for (Key, Value) { + fn into(self) -> KvPair { + KvPair(self.0, self.1) + } +} + +impl Into for (Key, Key) { + fn into(self) -> KeyRange { + KeyRange(self.0, self.1) + } +} + +pub trait Request: Sized { + type Response: Sized; + fn execute(self, kv: &TiKV) -> Self::Response; +} + +pub struct TiKV {} + +impl TiKV { + pub fn execute(&self, request: E) -> R + where + E: Request, + R: Sized, + { + request.execute(self) + } +} From 91e20f5260c33483087646081ed0855f9f6fe117 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Sun, 14 Oct 2018 14:53:47 +0800 Subject: [PATCH 02/11] Change TiKV/RawKV to TiKv/RawKv --- src/api/mod.rs | 2 +- src/api/raw_kv.rs | 164 +++++++++++++++++++++++----------------------- src/lib.rs | 6 +- 3 files changed, 86 insertions(+), 86 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 5bd09feb..bfea31cf 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,2 +1,2 @@ mod raw_kv; -pub use self::raw_kv::RawKV; +pub use self::raw_kv::RawKv; diff --git a/src/api/raw_kv.rs b/src/api/raw_kv.rs index e9ca6518..5e6956eb 100644 --- a/src/api/raw_kv.rs +++ b/src/api/raw_kv.rs @@ -3,7 +3,7 @@ use std::io::Result; use *; -pub struct RawKV; +pub struct RawKv; macro_rules! define_cf_setter { () => { @@ -35,7 +35,7 @@ pub struct Scan { } impl RequestType for Scan {} -pub struct RawKVRequest +pub struct RawRequest where T: Default, { @@ -44,12 +44,12 @@ where cf: Option, } -impl RawKVRequest +impl RawRequest where T: Default, { pub fn new(payload: P) -> Self { - RawKVRequest { + RawRequest { t: T::default(), payload, cf: None, @@ -59,7 +59,7 @@ where define_cf_setter!(); } -pub struct RawKVBatchRequest +pub struct RawBatchRequest where T: Default, P: Sized, @@ -69,7 +69,7 @@ where cf: Option, } -impl RawKVBatchRequest +impl RawBatchRequest where T: Default, P: Sized, @@ -79,7 +79,7 @@ where I: Iterator, K: Into

, { - RawKVBatchRequest { + RawBatchRequest { t: T::default(), payloads: payloads.map(Into::into).collect(), cf: None, @@ -89,103 +89,103 @@ where define_cf_setter!(); } -pub type RawKVKeyRequest = RawKVRequest; -pub type RawKVBatchKeyRequest = RawKVBatchRequest; -pub type RawKVKvPairRequest = RawKVRequest; -pub type RawKVBatchKvPairRequest = RawKVBatchRequest; -pub type RawKVRangeRequest = RawKVRequest; -pub type RawKVBatchRangeRequest = RawKVBatchRequest; - -pub type RawKVGetRequest = RawKVKeyRequest; -pub type RawKVBatchGetRequest = RawKVBatchKeyRequest; -pub type RawKVPutRequest = RawKVKvPairRequest; -pub type RawKVBatchPutRequest = RawKVBatchKvPairRequest; -pub type RawKVDeleteRequest = RawKVKeyRequest; -pub type RawKVBatchDeleteRequest = RawKVBatchKeyRequest; -pub type RawKVScanRequest = RawKVRangeRequest; -pub type RawKVBatchScanRequest = RawKVBatchRangeRequest; -pub type RawKVDeleteRangeRequest = RawKVRequest; - -pub type RawKVGetResponse = Result; -pub type RawKVBatchGetResponse = Result>; -pub type RawKVPutResponse = Result<()>; -pub type RawKVBatchPutResponse = Result<()>; -pub type RawKVDeleteResponse = Result<()>; -pub type RawKVBatchDeleteResponse = Result<()>; -pub type RawKVDeleteRangeResponse = Result<()>; -pub type RawKVScanResponse = Result>; -pub type RawKVBatchScanResponse = Result>; - -impl RawKV { - pub fn get(key: K) -> RawKVGetRequest +pub type RawKeyRequest = RawRequest; +pub type RawBatchKeyRequest = RawBatchRequest; +pub type RawKvPairRequest = RawRequest; +pub type RawBatchKvPairRequest = RawBatchRequest; +pub type RawRangeRequest = RawRequest; +pub type RawBatchRangeRequest = RawBatchRequest; + +pub type RawGetRequest = RawKeyRequest; +pub type RawBatchGetRequest = RawBatchKeyRequest; +pub type RawPutRequest = RawKvPairRequest; +pub type RawBatchPutRequest = RawBatchKvPairRequest; +pub type RawDeleteRequest = RawKeyRequest; +pub type RawBatchDeleteRequest = RawBatchKeyRequest; +pub type RawScanRequest = RawRangeRequest; +pub type RawBatchScanRequest = RawBatchRangeRequest; +pub type RawDeleteRangeRequest = RawRequest; + +pub type RawGetResponse = Result; +pub type RawBatchGetResponse = Result>; +pub type RawPutResponse = Result<()>; +pub type RawBatchPutResponse = Result<()>; +pub type RawDeleteResponse = Result<()>; +pub type RawBatchDeleteResponse = Result<()>; +pub type RawDeleteRangeResponse = Result<()>; +pub type RawScanResponse = Result>; +pub type RawBatchScanResponse = Result>; + +impl RawKv { + pub fn get(key: K) -> RawGetRequest where K: Into, { - RawKVGetRequest::new(key.into()) + RawGetRequest::new(key.into()) } - pub fn batch_get(keys: I) -> RawKVBatchGetRequest + pub fn batch_get(keys: I) -> RawBatchGetRequest where I: IntoIterator, K: Into, { - RawKVBatchGetRequest::new(keys.into_iter()) + RawBatchGetRequest::new(keys.into_iter()) } - pub fn put

(pair: P) -> RawKVPutRequest + pub fn put

(pair: P) -> RawPutRequest where P: Into, { - RawKVPutRequest::new(pair.into()) + RawPutRequest::new(pair.into()) } - pub fn batch_put(pairs: I) -> RawKVBatchPutRequest + pub fn batch_put(pairs: I) -> RawBatchPutRequest where I: IntoIterator, P: Into, { - RawKVBatchPutRequest::new(pairs.into_iter()) + RawBatchPutRequest::new(pairs.into_iter()) } - pub fn delete(key: K) -> RawKVDeleteRequest + pub fn delete(key: K) -> RawDeleteRequest where K: Into, { - RawKVDeleteRequest::new(key.into()) + RawDeleteRequest::new(key.into()) } - pub fn batch_delete(keys: I) -> RawKVBatchDeleteRequest + pub fn batch_delete(keys: I) -> RawBatchDeleteRequest where I: IntoIterator, K: Into, { - RawKVBatchDeleteRequest::new(keys.into_iter()) + RawBatchDeleteRequest::new(keys.into_iter()) } - pub fn scan(range: R, limit: u32) -> RawKVScanRequest + pub fn scan(range: R, limit: u32) -> RawScanRequest where R: Into, { - RawKVScanRequest::new(range.into()).limit(limit) + RawScanRequest::new(range.into()).limit(limit) } - pub fn batch_scan(ranges: I, each_limit: u32) -> RawKVBatchScanRequest + pub fn batch_scan(ranges: I, each_limit: u32) -> RawBatchScanRequest where I: IntoIterator, R: Into, { - RawKVBatchScanRequest::new(ranges.into_iter()).each_limit(each_limit) + RawBatchScanRequest::new(ranges.into_iter()).each_limit(each_limit) } - pub fn delete_range(range: R) -> RawKVDeleteRangeRequest + pub fn delete_range(range: R) -> RawDeleteRangeRequest where R: Into, { - RawKVDeleteRangeRequest::new(range.into()) + RawDeleteRangeRequest::new(range.into()) } } -impl RawKVScanRequest { +impl RawScanRequest { pub fn limit(mut self, limit: u32) -> Self { self.t.limit = limit; self @@ -197,7 +197,7 @@ impl RawKVScanRequest { } } -impl RawKVBatchScanRequest { +impl RawBatchScanRequest { pub fn each_limit(mut self, each_limit: u32) -> Self { self.t.limit = each_limit; self @@ -209,74 +209,74 @@ impl RawKVBatchScanRequest { } } -impl Request for RawKVGetRequest { - type Response = RawKVGetResponse; +impl Request for RawGetRequest { + type Response = RawGetResponse; - fn execute(self, _kv: &TiKV) -> RawKVGetResponse { + fn execute(self, _kv: &TiKv) -> RawGetResponse { unimplemented!() } } -impl Request for RawKVBatchGetRequest { - type Response = RawKVBatchGetResponse; +impl Request for RawBatchGetRequest { + type Response = RawBatchGetResponse; - fn execute(self, _kv: &TiKV) -> RawKVBatchGetResponse { + fn execute(self, _kv: &TiKv) -> RawBatchGetResponse { unimplemented!() } } -impl Request for RawKVPutRequest { - type Response = RawKVPutResponse; +impl Request for RawPutRequest { + type Response = RawPutResponse; - fn execute(self, _kv: &TiKV) -> RawKVPutResponse { + fn execute(self, _kv: &TiKv) -> RawPutResponse { unimplemented!() } } -impl Request for RawKVBatchPutRequest { - type Response = RawKVBatchPutResponse; +impl Request for RawBatchPutRequest { + type Response = RawBatchPutResponse; - fn execute(self, _kv: &TiKV) -> RawKVBatchPutResponse { + fn execute(self, _kv: &TiKv) -> RawBatchPutResponse { unimplemented!() } } -impl Request for RawKVDeleteRequest { - type Response = RawKVDeleteResponse; +impl Request for RawDeleteRequest { + type Response = RawDeleteResponse; - fn execute(self, _kv: &TiKV) -> RawKVDeleteResponse { + fn execute(self, _kv: &TiKv) -> RawDeleteResponse { unimplemented!() } } -impl Request for RawKVBatchDeleteRequest { - type Response = RawKVBatchDeleteResponse; +impl Request for RawBatchDeleteRequest { + type Response = RawBatchDeleteResponse; - fn execute(self, _kv: &TiKV) -> RawKVBatchDeleteResponse { + fn execute(self, _kv: &TiKv) -> RawBatchDeleteResponse { unimplemented!() } } -impl Request for RawKVScanRequest { - type Response = RawKVScanResponse; +impl Request for RawScanRequest { + type Response = RawScanResponse; - fn execute(self, _kv: &TiKV) -> RawKVScanResponse { + fn execute(self, _kv: &TiKv) -> RawScanResponse { unimplemented!() } } -impl Request for RawKVBatchScanRequest { - type Response = RawKVBatchScanResponse; +impl Request for RawBatchScanRequest { + type Response = RawBatchScanResponse; - fn execute(self, _kv: &TiKV) -> RawKVBatchScanResponse { + fn execute(self, _kv: &TiKv) -> RawBatchScanResponse { unimplemented!() } } -impl Request for RawKVDeleteRangeRequest { - type Response = RawKVDeleteRangeResponse; +impl Request for RawDeleteRangeRequest { + type Response = RawDeleteRangeResponse; - fn execute(self, _kv: &TiKV) -> RawKVDeleteRangeResponse { + fn execute(self, _kv: &TiKv) -> RawDeleteRangeResponse { unimplemented!() } } diff --git a/src/lib.rs b/src/lib.rs index 4a142452..00528245 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,12 +31,12 @@ impl Into for (Key, Key) { pub trait Request: Sized { type Response: Sized; - fn execute(self, kv: &TiKV) -> Self::Response; + fn execute(self, kv: &TiKv) -> Self::Response; } -pub struct TiKV {} +pub struct TiKv {} -impl TiKV { +impl TiKv { pub fn execute(&self, request: E) -> R where E: Request, From c9933304560509d0f66d9b688ca3df700f683d1e Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Sun, 14 Oct 2018 17:48:13 +0800 Subject: [PATCH 03/11] Use Future in public api --- Cargo.toml | 3 +++ src/api/raw_kv.rs | 47 ++++++++++++++++++----------------------------- src/lib.rs | 11 +++++++++-- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f721f98..7271d151 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,6 @@ description = "The rust language implementation of TiKV client." [lib] name = "tikv_client" + +[dependencies] +futures = "0.1" diff --git a/src/api/raw_kv.rs b/src/api/raw_kv.rs index 5e6956eb..63f11982 100644 --- a/src/api/raw_kv.rs +++ b/src/api/raw_kv.rs @@ -1,5 +1,4 @@ use std::fmt::Debug; -use std::io::Result; use *; @@ -106,16 +105,6 @@ pub type RawScanRequest = RawRangeRequest; pub type RawBatchScanRequest = RawBatchRangeRequest; pub type RawDeleteRangeRequest = RawRequest; -pub type RawGetResponse = Result; -pub type RawBatchGetResponse = Result>; -pub type RawPutResponse = Result<()>; -pub type RawBatchPutResponse = Result<()>; -pub type RawDeleteResponse = Result<()>; -pub type RawBatchDeleteResponse = Result<()>; -pub type RawDeleteRangeResponse = Result<()>; -pub type RawScanResponse = Result>; -pub type RawBatchScanResponse = Result>; - impl RawKv { pub fn get(key: K) -> RawGetRequest where @@ -210,73 +199,73 @@ impl RawBatchScanRequest { } impl Request for RawGetRequest { - type Response = RawGetResponse; + type Response = Value; - fn execute(self, _kv: &TiKv) -> RawGetResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawBatchGetRequest { - type Response = RawBatchGetResponse; + type Response = KvPair; - fn execute(self, _kv: &TiKv) -> RawBatchGetResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawPutRequest { - type Response = RawPutResponse; + type Response = (); - fn execute(self, _kv: &TiKv) -> RawPutResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawBatchPutRequest { - type Response = RawBatchPutResponse; + type Response = (); - fn execute(self, _kv: &TiKv) -> RawBatchPutResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawDeleteRequest { - type Response = RawDeleteResponse; + type Response = (); - fn execute(self, _kv: &TiKv) -> RawDeleteResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawBatchDeleteRequest { - type Response = RawBatchDeleteResponse; + type Response = (); - fn execute(self, _kv: &TiKv) -> RawBatchDeleteResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawScanRequest { - type Response = RawScanResponse; + type Response = Vec; - fn execute(self, _kv: &TiKv) -> RawScanResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawBatchScanRequest { - type Response = RawBatchScanResponse; + type Response = Vec; - fn execute(self, _kv: &TiKv) -> RawBatchScanResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } impl Request for RawDeleteRangeRequest { - type Response = RawDeleteRangeResponse; + type Response = (); - fn execute(self, _kv: &TiKv) -> RawDeleteRangeResponse { + fn execute(self, _kv: &TiKv) -> TiKvFuture { unimplemented!() } } diff --git a/src/lib.rs b/src/lib.rs index 00528245..f573c919 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,8 @@ +extern crate futures; + +use std::io::Error; +use futures::Future; + pub mod api; pub struct Key(Vec); @@ -5,6 +10,8 @@ pub struct Value(Vec); pub struct KvPair(Key, Value); pub struct KeyRange(Key, Key); +pub type TiKvFuture = Box + Send>; + impl Into for Vec { fn into(self) -> Key { Key(self) @@ -31,13 +38,13 @@ impl Into for (Key, Key) { pub trait Request: Sized { type Response: Sized; - fn execute(self, kv: &TiKv) -> Self::Response; + fn execute(self, kv: &TiKv) -> TiKvFuture; } pub struct TiKv {} impl TiKv { - pub fn execute(&self, request: E) -> R + pub fn execute(&self, request: E) -> TiKvFuture where E: Request, R: Sized, From 403a9d564b7a1b58d5e8425e39f704bc473ebc30 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Sun, 14 Oct 2018 23:03:13 +0800 Subject: [PATCH 04/11] Do not use builder style interface for RawKv --- src/api/raw_kv.rs | 265 +++++----------------------------------------- src/lib.rs | 18 +--- 2 files changed, 30 insertions(+), 253 deletions(-) diff --git a/src/api/raw_kv.rs b/src/api/raw_kv.rs index 63f11982..59699cd6 100644 --- a/src/api/raw_kv.rs +++ b/src/api/raw_kv.rs @@ -1,271 +1,58 @@ -use std::fmt::Debug; +use {Key, KeyRange, KvFuture, KvPair, Value}; -use *; - -pub struct RawKv; - -macro_rules! define_cf_setter { - () => { - pub fn cf(mut self, cf: C) -> Self - where - C: Into, - { - self.cf = Some(cf.into()); - self - } - } -} - -trait RequestType: Default + Debug + Clone + Copy {} - -#[derive(Debug, Clone, Copy, Default)] -pub struct Get; -impl RequestType for Get {} -#[derive(Debug, Clone, Copy, Default)] -pub struct Put; -impl RequestType for Put {} -#[derive(Debug, Clone, Copy, Default)] -pub struct Delete; -impl RequestType for Delete {} -#[derive(Debug, Clone, Copy, Default)] -pub struct Scan { - limit: u32, - key_only: bool, -} -impl RequestType for Scan {} - -pub struct RawRequest -where - T: Default, -{ - t: T, - payload: P, - cf: Option, -} - -impl RawRequest -where - T: Default, -{ - pub fn new(payload: P) -> Self { - RawRequest { - t: T::default(), - payload, - cf: None, - } - } - - define_cf_setter!(); -} - -pub struct RawBatchRequest -where - T: Default, - P: Sized, -{ - t: T, - payloads: Vec

, - cf: Option, -} - -impl RawBatchRequest -where - T: Default, - P: Sized, -{ - pub fn new(payloads: I) -> Self - where - I: Iterator, - K: Into

, - { - RawBatchRequest { - t: T::default(), - payloads: payloads.map(Into::into).collect(), - cf: None, - } - } - - define_cf_setter!(); -} - -pub type RawKeyRequest = RawRequest; -pub type RawBatchKeyRequest = RawBatchRequest; -pub type RawKvPairRequest = RawRequest; -pub type RawBatchKvPairRequest = RawBatchRequest; -pub type RawRangeRequest = RawRequest; -pub type RawBatchRangeRequest = RawBatchRequest; - -pub type RawGetRequest = RawKeyRequest; -pub type RawBatchGetRequest = RawBatchKeyRequest; -pub type RawPutRequest = RawKvPairRequest; -pub type RawBatchPutRequest = RawBatchKvPairRequest; -pub type RawDeleteRequest = RawKeyRequest; -pub type RawBatchDeleteRequest = RawBatchKeyRequest; -pub type RawScanRequest = RawRangeRequest; -pub type RawBatchScanRequest = RawBatchRangeRequest; -pub type RawDeleteRangeRequest = RawRequest; - -impl RawKv { - pub fn get(key: K) -> RawGetRequest +pub trait RawKv { + fn get(&self, key: K, cf: C) -> KvFuture where K: Into, - { - RawGetRequest::new(key.into()) - } + C: Into>; - pub fn batch_get(keys: I) -> RawBatchGetRequest + fn batch_get(&self, keys: I, cf: C) -> KvFuture> where I: IntoIterator, K: Into, - { - RawBatchGetRequest::new(keys.into_iter()) - } + C: Into>; - pub fn put

(pair: P) -> RawPutRequest + fn put(&self, pair: P, cf: C) -> KvFuture<()> where P: Into, - { - RawPutRequest::new(pair.into()) - } + C: Into>; - pub fn batch_put(pairs: I) -> RawBatchPutRequest + fn batch_put(&self, pairs: I, cf: C) -> KvFuture<()> where I: IntoIterator, P: Into, - { - RawBatchPutRequest::new(pairs.into_iter()) - } + C: Into>; - pub fn delete(key: K) -> RawDeleteRequest + fn delete(&self, key: K, cf: C) -> KvFuture<()> where K: Into, - { - RawDeleteRequest::new(key.into()) - } + C: Into>; - pub fn batch_delete(keys: I) -> RawBatchDeleteRequest + fn batch_delete(&self, keys: I, cf: C) -> KvFuture<()> where I: IntoIterator, K: Into, - { - RawBatchDeleteRequest::new(keys.into_iter()) - } + C: Into>; - pub fn scan(range: R, limit: u32) -> RawScanRequest + fn scan(&self, range: R, limit: u32, key_only: bool, cf: C) -> KvFuture> where R: Into, - { - RawScanRequest::new(range.into()).limit(limit) - } + C: Into>; - pub fn batch_scan(ranges: I, each_limit: u32) -> RawBatchScanRequest + fn batch_scan( + &self, + ranges: I, + each_limit: u32, + key_only: bool, + cf: C, + ) -> KvFuture> where I: IntoIterator, R: Into, - { - RawBatchScanRequest::new(ranges.into_iter()).each_limit(each_limit) - } + C: Into>; - pub fn delete_range(range: R) -> RawDeleteRangeRequest + fn delete_range(&self, range: R, cf: C) -> KvFuture<()> where R: Into, - { - RawDeleteRangeRequest::new(range.into()) - } -} - -impl RawScanRequest { - pub fn limit(mut self, limit: u32) -> Self { - self.t.limit = limit; - self - } - - pub fn key_only(mut self) -> Self { - self.t.key_only = true; - self - } -} - -impl RawBatchScanRequest { - pub fn each_limit(mut self, each_limit: u32) -> Self { - self.t.limit = each_limit; - self - } - - pub fn key_only(mut self) -> Self { - self.t.key_only = true; - self - } -} - -impl Request for RawGetRequest { - type Response = Value; - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawBatchGetRequest { - type Response = KvPair; - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawPutRequest { - type Response = (); - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawBatchPutRequest { - type Response = (); - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawDeleteRequest { - type Response = (); - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawBatchDeleteRequest { - type Response = (); - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawScanRequest { - type Response = Vec; - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawBatchScanRequest { - type Response = Vec; - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } -} - -impl Request for RawDeleteRangeRequest { - type Response = (); - - fn execute(self, _kv: &TiKv) -> TiKvFuture { - unimplemented!() - } + C: Into>; } diff --git a/src/lib.rs b/src/lib.rs index f573c919..54be506c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ extern crate futures; -use std::io::Error; use futures::Future; +use std::io::Error; pub mod api; @@ -10,7 +10,7 @@ pub struct Value(Vec); pub struct KvPair(Key, Value); pub struct KeyRange(Key, Key); -pub type TiKvFuture = Box + Send>; +pub type KvFuture = Box + Send>; impl Into for Vec { fn into(self) -> Key { @@ -38,17 +38,7 @@ impl Into for (Key, Key) { pub trait Request: Sized { type Response: Sized; - fn execute(self, kv: &TiKv) -> TiKvFuture; + fn execute(self, kv: &Client) -> KvFuture; } -pub struct TiKv {} - -impl TiKv { - pub fn execute(&self, request: E) -> TiKvFuture - where - E: Request, - R: Sized, - { - request.execute(self) - } -} +pub struct Client {} From ec9e8dde398c21cdfe7d3e0af9e57d03c928e4c2 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Sun, 14 Oct 2018 23:38:57 +0800 Subject: [PATCH 05/11] Add TxnKv related interfaces Signed-off-by: Xiaoguang Sun --- src/api/mod.rs | 2 - src/client.rs | 121 ++++++++++++++++++ src/lib.rs | 15 ++- src/{api/raw_kv.rs => raw.rs} | 0 src/txn.rs | 225 ++++++++++++++++++++++++++++++++++ 5 files changed, 353 insertions(+), 10 deletions(-) delete mode 100644 src/api/mod.rs create mode 100644 src/client.rs rename src/{api/raw_kv.rs => raw.rs} (100%) create mode 100644 src/txn.rs diff --git a/src/api/mod.rs b/src/api/mod.rs deleted file mode 100644 index bfea31cf..00000000 --- a/src/api/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod raw_kv; -pub use self::raw_kv::RawKv; diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 00000000..7bd2d8f9 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,121 @@ +use raw::RawKv; +use txn::{Oracle, Snapshot, Timestamp, Transaction, TxnKv}; +use {Key, KeyRange, KvFuture, KvPair, Value}; + +pub struct Client {} + +impl RawKv for Client { + fn get(&self, key: K, cf: C) -> KvFuture + where + K: Into, + C: Into>, + { + drop(key); + drop(cf); + unimplemented!() + } + fn batch_get(&self, keys: I, cf: C) -> KvFuture> + where + I: IntoIterator, + K: Into, + C: Into>, + { + drop(keys); + drop(cf); + unimplemented!() + } + fn put(&self, pair: P, cf: C) -> KvFuture<()> + where + P: Into, + C: Into>, + { + drop(pair); + drop(cf); + unimplemented!() + } + fn batch_put(&self, pairs: I, cf: C) -> KvFuture<()> + where + I: IntoIterator, + P: Into, + C: Into>, + { + drop(pairs); + drop(cf); + unimplemented!() + } + fn delete(&self, key: K, cf: C) -> KvFuture<()> + where + K: Into, + C: Into>, + { + drop(key); + drop(cf); + unimplemented!() + } + fn batch_delete(&self, keys: I, cf: C) -> KvFuture<()> + where + I: IntoIterator, + K: Into, + C: Into>, + { + drop(keys); + drop(cf); + unimplemented!() + } + fn scan(&self, range: R, _limit: u32, _key_only: bool, cf: C) -> KvFuture> + where + R: Into, + C: Into>, + { + drop(range); + drop(cf); + unimplemented!() + } + fn batch_scan( + &self, + ranges: I, + _each_limit: u32, + _key_only: bool, + cf: C, + ) -> KvFuture> + where + I: IntoIterator, + R: Into, + C: Into>, + { + drop(ranges); + drop(cf); + unimplemented!() + } + fn delete_range(&self, range: R, cf: C) -> KvFuture<()> + where + R: Into, + C: Into>, + { + drop(range); + drop(cf); + unimplemented!() + } +} + +impl TxnKv for Client { + fn begin(&self) -> KvFuture { + unimplemented!() + } + + fn begin_with_timestamp(&self, _timestamp: Timestamp) -> KvFuture { + unimplemented!() + } + + fn snapshot(&self) -> KvFuture { + unimplemented!() + } + + fn current_timestamp(&self) -> Timestamp { + unimplemented!() + } + + fn oracle(&self) -> Oracle { + unimplemented!() + } +} diff --git a/src/lib.rs b/src/lib.rs index 54be506c..6290adb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,13 @@ extern crate futures; use futures::Future; use std::io::Error; -pub mod api; +mod client; +mod raw; +mod txn; + +pub use client::Client; +pub use raw::RawKv; +pub use txn::{Oracle, Snapshot, Timestamp, Transaction, TxnKv}; pub struct Key(Vec); pub struct Value(Vec); @@ -35,10 +41,3 @@ impl Into for (Key, Key) { KeyRange(self.0, self.1) } } - -pub trait Request: Sized { - type Response: Sized; - fn execute(self, kv: &Client) -> KvFuture; -} - -pub struct Client {} diff --git a/src/api/raw_kv.rs b/src/raw.rs similarity index 100% rename from src/api/raw_kv.rs rename to src/raw.rs diff --git a/src/txn.rs b/src/txn.rs new file mode 100644 index 00000000..823020ee --- /dev/null +++ b/src/txn.rs @@ -0,0 +1,225 @@ +use std::io::Error; + +use futures::{Poll, Stream}; + +use *; + +#[derive(Copy, Clone)] +pub struct Timestamp(u64); + +impl Into for u64 { + fn into(self) -> Timestamp { + Timestamp(self) + } +} + +impl Timestamp { + pub fn timestamp(self) -> u64 { + self.0 + } + + pub fn physical(self) -> i64 { + (self.0 >> 16) as i64 + } + + pub fn logical(self) -> i64 { + (self.0 & 0xFFFF as u64) as i64 + } +} + +pub struct Scanner; + +impl Stream for Scanner { + type Item = KvPair; + type Error = Error; + + fn poll(&mut self) -> Poll, Self::Error> { + unimplemented!() + } +} + +pub trait Retriever { + fn get(&self, key: K) -> KvFuture + where + K: Into; + + fn batch_get(&self, keys: I) -> KvFuture> + where + I: IntoIterator, + K: Into; + + fn seek(&self, key: K) -> KvFuture + where + K: Into; + + fn seek_reverse(&self, key: K) -> KvFuture + where + K: Into; +} + +pub trait Mutator { + fn set

(&mut self, pair: P) -> KvFuture<()> + where + P: Into; + + fn delete(&mut self, key: K) -> KvFuture<()> + where + K: Into; +} + +pub struct Transaction; + +impl Transaction { + pub fn commit(&mut self) -> KvFuture<()> { + unimplemented!() + } + + pub fn rollback(&mut self) -> KvFuture<()> { + unimplemented!() + } + + pub fn lock_keys(&mut self, keys: I) -> KvFuture<()> + where + I: IntoIterator, + K: Into, + { + drop(keys); + unimplemented!() + } + + pub fn is_readonly(&self) -> bool { + unimplemented!() + } + + pub fn start_ts(&self) -> Timestamp { + unimplemented!() + } + + pub fn snapshot(&self) -> KvFuture { + unimplemented!() + } +} + +impl Retriever for Transaction { + fn get(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn batch_get(&self, keys: I) -> KvFuture> + where + I: IntoIterator, + K: Into, + { + drop(keys); + unimplemented!() + } + + fn seek(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn seek_reverse(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } +} + +impl Mutator for Transaction { + fn set

(&mut self, pair: P) -> KvFuture<()> + where + P: Into, + { + drop(pair); + unimplemented!() + } + + fn delete(&mut self, key: K) -> KvFuture<()> + where + K: Into, + { + drop(key); + unimplemented!() + } +} + +pub struct Snapshot; + +impl Retriever for Snapshot { + fn get(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn batch_get(&self, keys: I) -> KvFuture> + where + I: IntoIterator, + K: Into, + { + drop(keys); + unimplemented!() + } + + fn seek(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn seek_reverse(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } +} + +pub struct Oracle; + +impl Oracle { + pub fn timestamp(&self) -> KvFuture { + unimplemented!() + } + + pub fn is_expired(&self, _lock_timestamp: Timestamp, _ttl: Timestamp) -> KvFuture { + unimplemented!() + } +} + +pub trait TxnKv { + fn begin(&self) -> KvFuture { + unimplemented!() + } + + fn begin_with_timestamp(&self, _timestamp: Timestamp) -> KvFuture { + unimplemented!() + } + + fn snapshot(&self) -> KvFuture { + unimplemented!() + } + + fn current_timestamp(&self) -> Timestamp { + unimplemented!() + } + + fn oracle(&self) -> Oracle { + unimplemented!() + } +} From 01b0362c4aee6dadcef0fea1e81377a31f539f74 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Mon, 15 Oct 2018 09:34:50 +0800 Subject: [PATCH 06/11] Eliminate Kv suffix as it is obvious here --- src/client.rs | 8 ++++---- src/lib.rs | 4 ++-- src/raw.rs | 2 +- src/txn.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/client.rs b/src/client.rs index 7bd2d8f9..1d021d2d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,10 +1,10 @@ -use raw::RawKv; -use txn::{Oracle, Snapshot, Timestamp, Transaction, TxnKv}; +use raw::Raw; +use txn::{Oracle, Snapshot, Timestamp, Transaction, Txn}; use {Key, KeyRange, KvFuture, KvPair, Value}; pub struct Client {} -impl RawKv for Client { +impl Raw for Client { fn get(&self, key: K, cf: C) -> KvFuture where K: Into, @@ -98,7 +98,7 @@ impl RawKv for Client { } } -impl TxnKv for Client { +impl Txn for Client { fn begin(&self) -> KvFuture { unimplemented!() } diff --git a/src/lib.rs b/src/lib.rs index 6290adb9..0ff5b2d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,8 @@ mod raw; mod txn; pub use client::Client; -pub use raw::RawKv; -pub use txn::{Oracle, Snapshot, Timestamp, Transaction, TxnKv}; +pub use raw::Raw; +pub use txn::{Oracle, Snapshot, Timestamp, Transaction, Txn}; pub struct Key(Vec); pub struct Value(Vec); diff --git a/src/raw.rs b/src/raw.rs index 59699cd6..1959d277 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -1,6 +1,6 @@ use {Key, KeyRange, KvFuture, KvPair, Value}; -pub trait RawKv { +pub trait Raw { fn get(&self, key: K, cf: C) -> KvFuture where K: Into, diff --git a/src/txn.rs b/src/txn.rs index 823020ee..a07a13af 100644 --- a/src/txn.rs +++ b/src/txn.rs @@ -202,7 +202,7 @@ impl Oracle { } } -pub trait TxnKv { +pub trait Txn { fn begin(&self) -> KvFuture { unimplemented!() } From 87ad84c48bdb9898b52ed9f48ecc67794251a409 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Tue, 16 Oct 2018 11:05:47 +0800 Subject: [PATCH 07/11] Add Client constructor --- Cargo.toml | 2 ++ src/client.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 3 +++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7271d151..0a395358 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ name = "tikv_client" [dependencies] futures = "0.1" +serde = "1.0" +serde_derive = "1.0" diff --git a/src/client.rs b/src/client.rs index 1d021d2d..aac0bc13 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,7 +2,60 @@ use raw::Raw; use txn::{Oracle, Snapshot, Timestamp, Transaction, Txn}; use {Key, KeyRange, KvFuture, KvPair, Value}; -pub struct Client {} +#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] +#[serde(default)] +#[serde(rename_all = "kebab-case")] +pub struct Config { + pub pd_endpoints: Vec, + pub ca_path: Option, + pub cert_path: Option, + pub key_path: Option, +} + +impl Config { + pub fn new(pd_endpoints: E) -> Self + where + E: IntoIterator, + { + Config { + pd_endpoints: pd_endpoints.into_iter().collect(), + ca_path: None, + cert_path: None, + key_path: None, + } + } + + pub fn security(mut self, ca_path: String, cert_path: String, key_path: String) -> Self { + self.ca_path = Some(ca_path); + self.cert_path = Some(cert_path); + self.key_path = Some(key_path); + self + } +} + +impl Into for String { + fn into(self) -> Config { + Config::new(Some(self)) + } +} + +impl<'a> Into for Vec { + fn into(self) -> Config { + Config::new(self) + } +} + +pub struct Client; + +impl Client { + pub fn new(config: C) -> KvFuture + where + C: Into, + { + drop(config); + unimplemented!() + } +} impl Raw for Client { fn get(&self, key: K, cf: C) -> KvFuture diff --git a/src/lib.rs b/src/lib.rs index 0ff5b2d9..2cc36978 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ extern crate futures; +extern crate serde; +#[macro_use] +extern crate serde_derive; use futures::Future; use std::io::Error; From 48a11e1720ab34785e90508bef13ac2ad8e9ff6a Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Tue, 16 Oct 2018 22:42:35 +0800 Subject: [PATCH 08/11] Rename certain package and traits Signed-off-by: Xiaoguang Sun --- src/client.rs | 31 -------------------- src/lib.rs | 52 +++++++++++++++++++++++++++++----- src/raw.rs | 12 ++++++-- src/{txn.rs => transaction.rs} | 12 ++++++-- 4 files changed, 65 insertions(+), 42 deletions(-) rename src/{txn.rs => transaction.rs} (95%) diff --git a/src/client.rs b/src/client.rs index aac0bc13..089faec0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,37 +2,6 @@ use raw::Raw; use txn::{Oracle, Snapshot, Timestamp, Transaction, Txn}; use {Key, KeyRange, KvFuture, KvPair, Value}; -#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] -#[serde(default)] -#[serde(rename_all = "kebab-case")] -pub struct Config { - pub pd_endpoints: Vec, - pub ca_path: Option, - pub cert_path: Option, - pub key_path: Option, -} - -impl Config { - pub fn new(pd_endpoints: E) -> Self - where - E: IntoIterator, - { - Config { - pd_endpoints: pd_endpoints.into_iter().collect(), - ca_path: None, - cert_path: None, - key_path: None, - } - } - - pub fn security(mut self, ca_path: String, cert_path: String, key_path: String) -> Self { - self.ca_path = Some(ca_path); - self.cert_path = Some(cert_path); - self.key_path = Some(key_path); - self - } -} - impl Into for String { fn into(self) -> Config { Config::new(Some(self)) diff --git a/src/lib.rs b/src/lib.rs index 2cc36978..41dbf8ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,13 +6,8 @@ extern crate serde_derive; use futures::Future; use std::io::Error; -mod client; -mod raw; -mod txn; - -pub use client::Client; -pub use raw::Raw; -pub use txn::{Oracle, Snapshot, Timestamp, Transaction, Txn}; +pub mod raw; +pub mod transaction; pub struct Key(Vec); pub struct Value(Vec); @@ -44,3 +39,46 @@ impl Into for (Key, Key) { KeyRange(self.0, self.1) } } + +#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] +#[serde(default)] +#[serde(rename_all = "kebab-case")] +pub struct Config { + pub pd_endpoints: Vec, + pub ca_path: Option, + pub cert_path: Option, + pub key_path: Option, +} + +impl Config { + pub fn new(pd_endpoints: E) -> Self + where + E: IntoIterator, + { + Config { + pd_endpoints: pd_endpoints.into_iter().collect(), + ca_path: None, + cert_path: None, + key_path: None, + } + } + + pub fn security(mut self, ca_path: String, cert_path: String, key_path: String) -> Self { + self.ca_path = Some(ca_path); + self.cert_path = Some(cert_path); + self.key_path = Some(key_path); + self + } +} + +impl Into for String { + fn into(self) -> Config { + Config::new(Some(self)) + } +} + +impl<'a> Into for Vec { + fn into(self) -> Config { + Config::new(self) + } +} diff --git a/src/raw.rs b/src/raw.rs index 1959d277..8070b2f8 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -1,6 +1,14 @@ -use {Key, KeyRange, KvFuture, KvPair, Value}; +use {Config, Key, KeyRange, KvFuture, KvPair, Value}; + +pub trait Client { + fn new(config: C) -> KvFuture + where + C: Into, + { + drop(config); + unimplemented!() + } -pub trait Raw { fn get(&self, key: K, cf: C) -> KvFuture where K: Into, diff --git a/src/txn.rs b/src/transaction.rs similarity index 95% rename from src/txn.rs rename to src/transaction.rs index a07a13af..4a11663e 100644 --- a/src/txn.rs +++ b/src/transaction.rs @@ -2,7 +2,7 @@ use std::io::Error; use futures::{Poll, Stream}; -use *; +use {Config, Key, KvFuture, KvPair, Value}; #[derive(Copy, Clone)] pub struct Timestamp(u64); @@ -202,7 +202,15 @@ impl Oracle { } } -pub trait Txn { +pub trait Client { + fn new(config: C) -> KvFuture + where + C: Into, + { + drop(config); + unimplemented!() + } + fn begin(&self) -> KvFuture { unimplemented!() } From d3043d5cae4ec733b877426e84ac8f27cde6634a Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Wed, 17 Oct 2018 22:19:35 +0800 Subject: [PATCH 09/11] Construct Config with multiple constructors --- src/client.rs | 143 --------------------------------------------- src/lib.rs | 32 +++++----- src/raw.rs | 6 +- src/transaction.rs | 6 +- 4 files changed, 17 insertions(+), 170 deletions(-) delete mode 100644 src/client.rs diff --git a/src/client.rs b/src/client.rs deleted file mode 100644 index 089faec0..00000000 --- a/src/client.rs +++ /dev/null @@ -1,143 +0,0 @@ -use raw::Raw; -use txn::{Oracle, Snapshot, Timestamp, Transaction, Txn}; -use {Key, KeyRange, KvFuture, KvPair, Value}; - -impl Into for String { - fn into(self) -> Config { - Config::new(Some(self)) - } -} - -impl<'a> Into for Vec { - fn into(self) -> Config { - Config::new(self) - } -} - -pub struct Client; - -impl Client { - pub fn new(config: C) -> KvFuture - where - C: Into, - { - drop(config); - unimplemented!() - } -} - -impl Raw for Client { - fn get(&self, key: K, cf: C) -> KvFuture - where - K: Into, - C: Into>, - { - drop(key); - drop(cf); - unimplemented!() - } - fn batch_get(&self, keys: I, cf: C) -> KvFuture> - where - I: IntoIterator, - K: Into, - C: Into>, - { - drop(keys); - drop(cf); - unimplemented!() - } - fn put(&self, pair: P, cf: C) -> KvFuture<()> - where - P: Into, - C: Into>, - { - drop(pair); - drop(cf); - unimplemented!() - } - fn batch_put(&self, pairs: I, cf: C) -> KvFuture<()> - where - I: IntoIterator, - P: Into, - C: Into>, - { - drop(pairs); - drop(cf); - unimplemented!() - } - fn delete(&self, key: K, cf: C) -> KvFuture<()> - where - K: Into, - C: Into>, - { - drop(key); - drop(cf); - unimplemented!() - } - fn batch_delete(&self, keys: I, cf: C) -> KvFuture<()> - where - I: IntoIterator, - K: Into, - C: Into>, - { - drop(keys); - drop(cf); - unimplemented!() - } - fn scan(&self, range: R, _limit: u32, _key_only: bool, cf: C) -> KvFuture> - where - R: Into, - C: Into>, - { - drop(range); - drop(cf); - unimplemented!() - } - fn batch_scan( - &self, - ranges: I, - _each_limit: u32, - _key_only: bool, - cf: C, - ) -> KvFuture> - where - I: IntoIterator, - R: Into, - C: Into>, - { - drop(ranges); - drop(cf); - unimplemented!() - } - fn delete_range(&self, range: R, cf: C) -> KvFuture<()> - where - R: Into, - C: Into>, - { - drop(range); - drop(cf); - unimplemented!() - } -} - -impl Txn for Client { - fn begin(&self) -> KvFuture { - unimplemented!() - } - - fn begin_with_timestamp(&self, _timestamp: Timestamp) -> KvFuture { - unimplemented!() - } - - fn snapshot(&self) -> KvFuture { - unimplemented!() - } - - fn current_timestamp(&self) -> Timestamp { - unimplemented!() - } - - fn oracle(&self) -> Oracle { - unimplemented!() - } -} diff --git a/src/lib.rs b/src/lib.rs index 41dbf8ef..e06b4b4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,22 +63,20 @@ impl Config { } } - pub fn security(mut self, ca_path: String, cert_path: String, key_path: String) -> Self { - self.ca_path = Some(ca_path); - self.cert_path = Some(cert_path); - self.key_path = Some(key_path); - self - } -} - -impl Into for String { - fn into(self) -> Config { - Config::new(Some(self)) - } -} - -impl<'a> Into for Vec { - fn into(self) -> Config { - Config::new(self) + pub fn with_security( + pd_endpoints: E, + ca_path: String, + cert_path: String, + key_path: String, + ) -> Self + where + E: IntoIterator, + { + Config { + pd_endpoints: pd_endpoints.into_iter().collect(), + ca_path: Some(ca_path), + cert_path: Some(cert_path), + key_path: Some(key_path), + } } } diff --git a/src/raw.rs b/src/raw.rs index 8070b2f8..2178c561 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -1,11 +1,7 @@ use {Config, Key, KeyRange, KvFuture, KvPair, Value}; pub trait Client { - fn new(config: C) -> KvFuture - where - C: Into, - { - drop(config); + fn new(_config: &Config) -> KvFuture { unimplemented!() } diff --git a/src/transaction.rs b/src/transaction.rs index 4a11663e..3a37afc2 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -203,11 +203,7 @@ impl Oracle { } pub trait Client { - fn new(config: C) -> KvFuture - where - C: Into, - { - drop(config); + fn new(_config: &Config) -> KvFuture { unimplemented!() } From a76504fcf429c89002442acc0b69ea6b49522cfd Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Thu, 18 Oct 2018 09:05:22 +0800 Subject: [PATCH 10/11] Use PathBuf instead of String for security files Signed-off-by: Xiaoguang Sun Signed-off-by: Xiaoguang Sun --- src/lib.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e06b4b4d..e1358f69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,10 @@ extern crate serde; #[macro_use] extern crate serde_derive; -use futures::Future; use std::io::Error; +use std::path::PathBuf; + +use futures::Future; pub mod raw; pub mod transaction; @@ -45,9 +47,9 @@ impl Into for (Key, Key) { #[serde(rename_all = "kebab-case")] pub struct Config { pub pd_endpoints: Vec, - pub ca_path: Option, - pub cert_path: Option, - pub key_path: Option, + pub ca_path: Option, + pub cert_path: Option, + pub key_path: Option, } impl Config { @@ -65,9 +67,9 @@ impl Config { pub fn with_security( pd_endpoints: E, - ca_path: String, - cert_path: String, - key_path: String, + ca_path: PathBuf, + cert_path: PathBuf, + key_path: PathBuf, ) -> Self where E: IntoIterator, From 46a8d056eb65f52f57f19bda7e4aece0c4fea69b Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Thu, 18 Oct 2018 10:09:26 +0800 Subject: [PATCH 11/11] Implement necessary common traits Signed-off-by: Xiaoguang Sun --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index e1358f69..8567b9f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,9 +11,13 @@ use futures::Future; pub mod raw; pub mod transaction; +#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub struct Key(Vec); +#[derive(Default, Clone, Eq, PartialEq, Hash, Debug)] pub struct Value(Vec); +#[derive(Default, Clone, Eq, PartialEq, Debug)] pub struct KvPair(Key, Value); +#[derive(Default, Clone, Eq, PartialEq, Debug)] pub struct KeyRange(Key, Key); pub type KvFuture = Box + Send>;