Skip to content

Commit ffdfb74

Browse files
author
xuhui-lu
committed
Separate the sync client in a single file
Signed-off-by: xuhui-lu <[email protected]>
1 parent cfee0d9 commit ffdfb74

File tree

4 files changed

+117
-115
lines changed

4 files changed

+117
-115
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub use crate::backoff::Backoff;
122122
#[doc(inline)]
123123
pub use crate::kv::{BoundRange, IntoOwnedRange, Key, KvPair, Value};
124124
#[doc(inline)]
125-
pub use crate::raw::{lowering as raw_lowering, Client as RawClient, SyncClient, ColumnFamily};
125+
pub use crate::raw::{lowering as raw_lowering, Client as RawClient, SyncClient as SyncRawClient, ColumnFamily};
126126
#[doc(inline)]
127127
pub use crate::request::RetryOptions;
128128
#[doc(inline)]

src/raw/client.rs

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -628,117 +628,3 @@ impl Client {
628628
self.atomic.then(|| ()).ok_or(Error::UnsupportedMode)
629629
}
630630
}
631-
632-
#[derive(Clone)]
633-
pub struct SyncClient {
634-
client: Client,
635-
}
636-
637-
impl SyncClient {
638-
/// The Sync version of Client
639-
///
640-
/// # Examples
641-
///
642-
/// ```rust,no_run
643-
/// # use tikv_client::SyncClient;
644-
/// let client = SyncClient::new(vec!["192.168.0.100"]).await.unwrap();
645-
/// ```
646-
pub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Client> {
647-
Self::new_with_config(pd_endpoints, Config::default()).await
648-
}
649-
650-
pub async fn new_with_config<S: Into<String>>(
651-
pd_endpoints: Vec<S>,
652-
config: Config,
653-
) -> Result<Client> {
654-
let client = Client::new_with_config(pd_endpoints, config)
655-
Ok(SyncClient {
656-
client: client
657-
})
658-
}
659-
660-
pub fn with_cf(&self, cf: ColumnFamily) -> SyncClient {
661-
SyncClient {
662-
client: self.clietn.with_cf(cf),
663-
}
664-
}
665-
666-
pub fn with_atomic_for_cas(&self) -> SyncClient {
667-
SyncClient {
668-
client: self.client.with_atomic_for_cas()
669-
}
670-
}
671-
672-
pub fn get(&self, key: impl Into<Key>) -> Result<Option<Value>> {
673-
block_on(self.client.get(key))
674-
}
675-
676-
pub fn batch_get(
677-
&self,
678-
keys: impl IntoIterator<Item = impl Into<Key>>,
679-
) -> Result<Vec<KvPair>> {
680-
block_on(self.client.batch_get(keys))
681-
}
682-
683-
pub fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
684-
block_on(self.client.put(key, value))
685-
}
686-
687-
pub fn batch_put(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> {
688-
block_on(self.client.batch_put(pairs))
689-
}
690-
691-
pub fn delete(&self, key: impl Into<Key>) -> Result<()> {
692-
block_on(self.client.delete(key))
693-
}
694-
695-
pub fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
696-
block_on(self.client.batch_delete(keys))
697-
}
698-
699-
pub fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()> {
700-
block_on(self.client.delete_range(range))
701-
}
702-
703-
pub fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
704-
block_on(self.client.scan(range, limit))
705-
}
706-
707-
pub fn scan_keys(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
708-
block_on(self.client.scan_keys(range, limit))
709-
}
710-
711-
pub fn batch_scan(
712-
&self,
713-
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
714-
each_limit: u32,
715-
) -> Result<Vec<KvPair>> {
716-
block_on(self.client.batch_scan(ranges, each_limit))
717-
}
718-
719-
pub fn batch_scan_keys(
720-
&self,
721-
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
722-
each_limit: u32,
723-
) -> Result<Vec<Key>> {
724-
block_on(self.client.batch_scan_keys(ranges, each_limit))
725-
}
726-
727-
pub fn compare_and_swap(
728-
&self,
729-
key: impl Into<Key>,
730-
previous_value: impl Into<Option<Value>>,
731-
new_value: impl Into<Value>,
732-
) -> Result<(Option<Value>, bool)> {
733-
block_on(self.client.compare_and_swap(key, previous_value, new_value))
734-
}
735-
736-
737-
fn assert_non_atomic(&self) -> Result<()> {
738-
(!self.atomic).then(|| ()).ok_or(Error::UnsupportedMode)
739-
}
740-
741-
fn assert_atomic(&self) -> Result<()> {
742-
self.atomic.then(|| ()).ok_or(Error::UnsupportedMode)
743-
}
744-
}

src/raw/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
//! **Warning:** It is not advisable to use both raw and transactional functionality in the same keyspace.
1111
1212
pub use self::client::Client;
13+
pub use self::sync_client::SyncClient;
1314
use crate::Error;
1415
use std::{convert::TryFrom, fmt};
1516

1617
mod client;
18+
mod sync_client;
1719
pub mod lowering;
1820
mod requests;
1921

src/raw/sync_client.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2+
3+
use crate::{
4+
config::Config,
5+
raw::client::Client,
6+
BoundRange, ColumnFamily, Key, KvPair, Result, Value,
7+
};
8+
use futures::executor::block_on;
9+
use std::{u32};
10+
11+
#[derive(Clone)]
12+
pub struct SyncClient {
13+
client: Client,
14+
}
15+
16+
impl SyncClient {
17+
/// The synchronous version of RawClient
18+
///
19+
/// # Examples
20+
///
21+
/// ```rust,no_run
22+
/// # use tikv_client::SyncRawClient;
23+
/// let client = SyncRawClient::new(vec!["192.168.0.100"]).await.unwrap();
24+
/// ```
25+
pub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<SyncClient> {
26+
Self::new_with_config(pd_endpoints, Config::default()).await
27+
}
28+
29+
pub async fn new_with_config<S: Into<String>>(
30+
pd_endpoints: Vec<S>,
31+
config: Config,
32+
) -> Result<SyncClient> {
33+
let client = Client::new_with_config(pd_endpoints, config).await.unwrap();
34+
Ok(SyncClient {
35+
client: client
36+
})
37+
}
38+
39+
pub fn with_cf(&self, cf: ColumnFamily) -> SyncClient {
40+
SyncClient {
41+
client: self.client.with_cf(cf),
42+
}
43+
}
44+
45+
pub fn with_atomic_for_cas(&self) -> SyncClient {
46+
SyncClient {
47+
client: self.client.with_atomic_for_cas()
48+
}
49+
}
50+
51+
pub fn get(&self, key: impl Into<Key>) -> Result<Option<Value>> {
52+
block_on(self.client.get(key))
53+
}
54+
55+
pub fn batch_get(
56+
&self,
57+
keys: impl IntoIterator<Item = impl Into<Key>>,
58+
) -> Result<Vec<KvPair>> {
59+
block_on(self.client.batch_get(keys))
60+
}
61+
62+
pub fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
63+
block_on(self.client.put(key, value))
64+
}
65+
66+
pub fn batch_put(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> {
67+
block_on(self.client.batch_put(pairs))
68+
}
69+
70+
pub fn delete(&self, key: impl Into<Key>) -> Result<()> {
71+
block_on(self.client.delete(key))
72+
}
73+
74+
pub fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
75+
block_on(self.client.batch_delete(keys))
76+
}
77+
78+
pub fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()> {
79+
block_on(self.client.delete_range(range))
80+
}
81+
82+
pub fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
83+
block_on(self.client.scan(range, limit))
84+
}
85+
86+
pub fn scan_keys(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
87+
block_on(self.client.scan_keys(range, limit))
88+
}
89+
90+
pub fn batch_scan(
91+
&self,
92+
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
93+
each_limit: u32,
94+
) -> Result<Vec<KvPair>> {
95+
block_on(self.client.batch_scan(ranges, each_limit))
96+
}
97+
98+
pub fn batch_scan_keys(
99+
&self,
100+
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
101+
each_limit: u32,
102+
) -> Result<Vec<Key>> {
103+
block_on(self.client.batch_scan_keys(ranges, each_limit))
104+
}
105+
106+
pub fn compare_and_swap(
107+
&self,
108+
key: impl Into<Key>,
109+
previous_value: impl Into<Option<Value>>,
110+
new_value: impl Into<Value>,
111+
) -> Result<(Option<Value>, bool)> {
112+
block_on(self.client.compare_and_swap(key, previous_value, new_value))
113+
}
114+
}

0 commit comments

Comments
 (0)