Skip to content

Commit

Permalink
Separate the sync client in a single file
Browse files Browse the repository at this point in the history
Signed-off-by: xuhui-lu <[email protected]>
  • Loading branch information
xuhui-lu committed Jul 5, 2021
1 parent cfee0d9 commit 767d2d8
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 181 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ 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, ColumnFamily, SyncClient as SyncRawClient,
};
#[doc(inline)]
pub use crate::request::RetryOptions;
#[doc(inline)]
Expand Down
179 changes: 0 additions & 179 deletions src/raw/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
request::{Collect, Plan},
BoundRange, ColumnFamily, Key, KvPair, Result, Value,
};
use futures::executor::block_on;
use log::debug;
use std::{sync::Arc, u32};

Expand Down Expand Up @@ -160,10 +159,6 @@ impl Client {
plan.execute().await
}

pub fn get_sync(&self, key: impl Into<Key>) -> Result<Option<Value>> {
block_on(self.get(key))
}

/// Create a new 'batch get' request.
///
/// Once resolved this request will result in the fetching of the values associated with the
Expand Down Expand Up @@ -198,13 +193,6 @@ impl Client {
.map(|r| r.into_iter().map(Into::into).collect())
}

pub fn batch_get_sync(
&self,
keys: impl IntoIterator<Item = impl Into<Key>>,
) -> Result<Vec<KvPair>> {
block_on(self.batch_get(keys))
}

/// Create a new 'put' request.
///
/// Once resolved this request will result in the setting of the value associated with the given key.
Expand Down Expand Up @@ -234,10 +222,6 @@ impl Client {
Ok(())
}

pub fn put_sync(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
block_on(self.put(key, value))
}

/// Create a new 'batch put' request.
///
/// Once resolved this request will result in the setting of the values associated with the given keys.
Expand Down Expand Up @@ -274,10 +258,6 @@ impl Client {
Ok(())
}

pub fn batch_put_sync(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> {
block_on(self.batch_put(pairs))
}

/// Create a new 'delete' request.
///
/// Once resolved this request will result in the deletion of the given key.
Expand Down Expand Up @@ -308,10 +288,6 @@ impl Client {
Ok(())
}

pub fn delete_sync(&self, key: impl Into<Key>) -> Result<()> {
block_on(self.delete(key))
}

/// Create a new 'batch delete' request.
///
/// Once resolved this request will result in the deletion of the given keys.
Expand Down Expand Up @@ -343,10 +319,6 @@ impl Client {
Ok(())
}

pub fn batch_delete_sync(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
block_on(self.batch_delete(keys))
}

/// Create a new 'delete range' request.
///
/// Once resolved this request will result in the deletion of all keys lying in the given range.
Expand Down Expand Up @@ -375,10 +347,6 @@ impl Client {
Ok(())
}

pub fn delete_range_sync(&self, range: impl Into<BoundRange>) -> Result<()> {
block_on(self.delete_range(range))
}

/// Create a new 'scan' request.
///
/// Once resolved this request will result in a `Vec` of key-value pairs that lies in the specified range.
Expand All @@ -403,10 +371,6 @@ impl Client {
self.scan_inner(range.into(), limit, false).await
}

pub fn scan_sync(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
block_on(self.scan(range, limit))
}

/// Create a new 'scan' request that only returns the keys.
///
/// Once resolved this request will result in a `Vec` of keys that lies in the specified range.
Expand Down Expand Up @@ -436,10 +400,6 @@ impl Client {
.collect())
}

pub fn scan_keys_sync(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
block_on(self.scan_keys(range, limit))
}

/// Create a new 'batch scan' request.
///
/// Once resolved this request will result in a set of scanners over the given keys.
Expand Down Expand Up @@ -472,14 +432,6 @@ impl Client {
self.batch_scan_inner(ranges, each_limit, false).await
}

pub fn batch_scan_sync(
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32,
) -> Result<Vec<KvPair>> {
block_on(self.batch_scan(ranges, each_limit))
}

/// Create a new 'batch scan' request that only returns the keys.
///
/// Once resolved this request will result in a set of scanners over the given keys.
Expand Down Expand Up @@ -516,14 +468,6 @@ impl Client {
.collect())
}

pub fn batch_scan_keys_sync(
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32,
) -> Result<Vec<Key>> {
block_on(self.batch_scan_keys(ranges, each_limit))
}

/// Create a new *atomic* 'compare and set' request.
///
/// Once resolved this request will result in an atomic `compare and set'
Expand Down Expand Up @@ -558,15 +502,6 @@ impl Client {
plan.execute().await
}

pub async fn compare_and_swap_sync(
&self,
key: impl Into<Key>,
previous_value: impl Into<Option<Value>>,
new_value: impl Into<Value>,
) -> Result<(Option<Value>, bool)> {
block_on(self.compare_and_swap(key, previous_value, new_value))
}

async fn scan_inner(
&self,
range: impl Into<BoundRange>,
Expand Down Expand Up @@ -628,117 +563,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<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Client> {
Self::new_with_config(pd_endpoints, Config::default()).await
}

pub async fn new_with_config<S: Into<String>>(
pd_endpoints: Vec<S>,
config: Config,
) -> Result<Client> {
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<Key>) -> Result<Option<Value>> {
block_on(self.client.get(key))
}

pub fn batch_get(
&self,
keys: impl IntoIterator<Item = impl Into<Key>>,
) -> Result<Vec<KvPair>> {
block_on(self.client.batch_get(keys))
}

pub fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
block_on(self.client.put(key, value))
}

pub fn batch_put(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> {
block_on(self.client.batch_put(pairs))
}

pub fn delete(&self, key: impl Into<Key>) -> Result<()> {
block_on(self.client.delete(key))
}

pub fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
block_on(self.client.batch_delete(keys))
}

pub fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()> {
block_on(self.client.delete_range(range))
}

pub fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
block_on(self.client.scan(range, limit))
}

pub fn scan_keys(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
block_on(self.client.scan_keys(range, limit))
}

pub fn batch_scan(
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32,
) -> Result<Vec<KvPair>> {
block_on(self.client.batch_scan(ranges, each_limit))
}

pub fn batch_scan_keys(
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32,
) -> Result<Vec<Key>> {
block_on(self.client.batch_scan_keys(ranges, each_limit))
}

pub fn compare_and_swap(
&self,
key: impl Into<Key>,
previous_value: impl Into<Option<Value>>,
new_value: impl Into<Value>,
) -> Result<(Option<Value>, 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)
}
}
3 changes: 2 additions & 1 deletion src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
//!
//! **Warning:** It is not advisable to use both raw and transactional functionality in the same keyspace.

pub use self::client::Client;
pub use self::{client::Client, sync_client::SyncClient};
use crate::Error;
use std::{convert::TryFrom, fmt};

mod client;
pub mod lowering;
mod requests;
mod sync_client;

/// A [`ColumnFamily`](ColumnFamily) is an optional parameter for [`raw::Client`](Client) requests.
///
Expand Down
Loading

0 comments on commit 767d2d8

Please sign in to comment.