From ffdfb74f0beb5ae0a2911da9b468e9c1075ea750 Mon Sep 17 00:00:00 2001 From: xuhui-lu Date: Fri, 2 Jul 2021 18:19:14 -0700 Subject: [PATCH] Separate the sync client in a single file Signed-off-by: xuhui-lu --- src/lib.rs | 2 +- src/raw/client.rs | 114 ----------------------------------------- src/raw/mod.rs | 2 + src/raw/sync_client.rs | 114 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 115 deletions(-) create mode 100644 src/raw/sync_client.rs diff --git a/src/lib.rs b/src/lib.rs index 5f77eda2e..29af43fe6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,7 @@ pub use crate::backoff::Backoff; #[doc(inline)] pub use crate::kv::{BoundRange, IntoOwnedRange, Key, KvPair, Value}; #[doc(inline)] -pub use crate::raw::{lowering as raw_lowering, Client as RawClient, SyncClient, ColumnFamily}; +pub use crate::raw::{lowering as raw_lowering, Client as RawClient, SyncClient as SyncRawClient, ColumnFamily}; #[doc(inline)] pub use crate::request::RetryOptions; #[doc(inline)] diff --git a/src/raw/client.rs b/src/raw/client.rs index e73aa415f..a95fa9fec 100644 --- a/src/raw/client.rs +++ b/src/raw/client.rs @@ -628,117 +628,3 @@ impl Client { self.atomic.then(|| ()).ok_or(Error::UnsupportedMode) } } - -#[derive(Clone)] -pub struct SyncClient { - client: Client, -} - -impl SyncClient { - /// The Sync version of Client - /// - /// # Examples - /// - /// ```rust,no_run - /// # use tikv_client::SyncClient; - /// let client = SyncClient::new(vec!["192.168.0.100"]).await.unwrap(); - /// ``` - pub async fn new>(pd_endpoints: Vec) -> Result { - Self::new_with_config(pd_endpoints, Config::default()).await - } - - pub async fn new_with_config>( - pd_endpoints: Vec, - config: Config, - ) -> Result { - let client = Client::new_with_config(pd_endpoints, config) - Ok(SyncClient { - client: client - }) - } - - pub fn with_cf(&self, cf: ColumnFamily) -> SyncClient { - SyncClient { - client: self.clietn.with_cf(cf), - } - } - - pub fn with_atomic_for_cas(&self) -> SyncClient { - SyncClient { - client: self.client.with_atomic_for_cas() - } - } - - pub fn get(&self, key: impl Into) -> Result> { - block_on(self.client.get(key)) - } - - pub fn batch_get( - &self, - keys: impl IntoIterator>, - ) -> Result> { - block_on(self.client.batch_get(keys)) - } - - pub fn put(&self, key: impl Into, value: impl Into) -> Result<()> { - block_on(self.client.put(key, value)) - } - - pub fn batch_put(&self, pairs: impl IntoIterator>) -> Result<()> { - block_on(self.client.batch_put(pairs)) - } - - pub fn delete(&self, key: impl Into) -> Result<()> { - block_on(self.client.delete(key)) - } - - pub fn batch_delete(&self, keys: impl IntoIterator>) -> Result<()> { - block_on(self.client.batch_delete(keys)) - } - - pub fn delete_range(&self, range: impl Into) -> Result<()> { - block_on(self.client.delete_range(range)) - } - - pub fn scan(&self, range: impl Into, limit: u32) -> Result> { - block_on(self.client.scan(range, limit)) - } - - pub fn scan_keys(&self, range: impl Into, limit: u32) -> Result> { - block_on(self.client.scan_keys(range, limit)) - } - - pub fn batch_scan( - &self, - ranges: impl IntoIterator>, - each_limit: u32, - ) -> Result> { - block_on(self.client.batch_scan(ranges, each_limit)) - } - - pub fn batch_scan_keys( - &self, - ranges: impl IntoIterator>, - each_limit: u32, - ) -> Result> { - block_on(self.client.batch_scan_keys(ranges, each_limit)) - } - - pub fn compare_and_swap( - &self, - key: impl Into, - previous_value: impl Into>, - new_value: impl Into, - ) -> Result<(Option, bool)> { - block_on(self.client.compare_and_swap(key, previous_value, new_value)) - } - - - fn assert_non_atomic(&self) -> Result<()> { - (!self.atomic).then(|| ()).ok_or(Error::UnsupportedMode) - } - - fn assert_atomic(&self) -> Result<()> { - self.atomic.then(|| ()).ok_or(Error::UnsupportedMode) - } -} \ No newline at end of file diff --git a/src/raw/mod.rs b/src/raw/mod.rs index 3441fbbba..be92b0f2a 100644 --- a/src/raw/mod.rs +++ b/src/raw/mod.rs @@ -10,10 +10,12 @@ //! **Warning:** It is not advisable to use both raw and transactional functionality in the same keyspace. pub use self::client::Client; +pub use self::sync_client::SyncClient; use crate::Error; use std::{convert::TryFrom, fmt}; mod client; +mod sync_client; pub mod lowering; mod requests; diff --git a/src/raw/sync_client.rs b/src/raw/sync_client.rs new file mode 100644 index 000000000..823308f05 --- /dev/null +++ b/src/raw/sync_client.rs @@ -0,0 +1,114 @@ +// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. + +use crate::{ + config::Config, + raw::client::Client, + BoundRange, ColumnFamily, Key, KvPair, Result, Value, +}; +use futures::executor::block_on; +use std::{u32}; + +#[derive(Clone)] +pub struct SyncClient { + client: Client, +} + +impl SyncClient { + /// The synchronous version of RawClient + /// + /// # Examples + /// + /// ```rust,no_run + /// # use tikv_client::SyncRawClient; + /// let client = SyncRawClient::new(vec!["192.168.0.100"]).await.unwrap(); + /// ``` + pub async fn new>(pd_endpoints: Vec) -> Result { + Self::new_with_config(pd_endpoints, Config::default()).await + } + + pub async fn new_with_config>( + pd_endpoints: Vec, + config: Config, + ) -> Result { + let client = Client::new_with_config(pd_endpoints, config).await.unwrap(); + Ok(SyncClient { + client: client + }) + } + + pub fn with_cf(&self, cf: ColumnFamily) -> SyncClient { + SyncClient { + client: self.client.with_cf(cf), + } + } + + pub fn with_atomic_for_cas(&self) -> SyncClient { + SyncClient { + client: self.client.with_atomic_for_cas() + } + } + + pub fn get(&self, key: impl Into) -> Result> { + block_on(self.client.get(key)) + } + + pub fn batch_get( + &self, + keys: impl IntoIterator>, + ) -> Result> { + block_on(self.client.batch_get(keys)) + } + + pub fn put(&self, key: impl Into, value: impl Into) -> Result<()> { + block_on(self.client.put(key, value)) + } + + pub fn batch_put(&self, pairs: impl IntoIterator>) -> Result<()> { + block_on(self.client.batch_put(pairs)) + } + + pub fn delete(&self, key: impl Into) -> Result<()> { + block_on(self.client.delete(key)) + } + + pub fn batch_delete(&self, keys: impl IntoIterator>) -> Result<()> { + block_on(self.client.batch_delete(keys)) + } + + pub fn delete_range(&self, range: impl Into) -> Result<()> { + block_on(self.client.delete_range(range)) + } + + pub fn scan(&self, range: impl Into, limit: u32) -> Result> { + block_on(self.client.scan(range, limit)) + } + + pub fn scan_keys(&self, range: impl Into, limit: u32) -> Result> { + block_on(self.client.scan_keys(range, limit)) + } + + pub fn batch_scan( + &self, + ranges: impl IntoIterator>, + each_limit: u32, + ) -> Result> { + block_on(self.client.batch_scan(ranges, each_limit)) + } + + pub fn batch_scan_keys( + &self, + ranges: impl IntoIterator>, + each_limit: u32, + ) -> Result> { + block_on(self.client.batch_scan_keys(ranges, each_limit)) + } + + pub fn compare_and_swap( + &self, + key: impl Into, + previous_value: impl Into>, + new_value: impl Into, + ) -> Result<(Option, bool)> { + block_on(self.client.compare_and_swap(key, previous_value, new_value)) + } +} \ No newline at end of file