-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate the sync client in a single file
Signed-off-by: xuhui-lu <[email protected]>
- Loading branch information
xuhui-lu
committed
Jul 3, 2021
1 parent
cfee0d9
commit ffdfb74
Showing
4 changed files
with
117 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<SyncClient> { | ||
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<SyncClient> { | ||
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<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)) | ||
} | ||
} |