Skip to content

Commit c056a75

Browse files
author
xuhui-lu
committed
Separate the sync client in a single file
Signed-off-by: xuhui-lu <luxuhui12345@126.com>
1 parent cfee0d9 commit c056a75

File tree

4 files changed

+320
-179
lines changed

4 files changed

+320
-179
lines changed

Diff for: src/lib.rs

+1-1
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)]

Diff for: src/raw/client.rs

-178
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ impl Client {
160160
plan.execute().await
161161
}
162162

163-
pub fn get_sync(&self, key: impl Into<Key>) -> Result<Option<Value>> {
164-
block_on(self.get(key))
165-
}
166-
167163
/// Create a new 'batch get' request.
168164
///
169165
/// Once resolved this request will result in the fetching of the values associated with the
@@ -198,13 +194,6 @@ impl Client {
198194
.map(|r| r.into_iter().map(Into::into).collect())
199195
}
200196

201-
pub fn batch_get_sync(
202-
&self,
203-
keys: impl IntoIterator<Item = impl Into<Key>>,
204-
) -> Result<Vec<KvPair>> {
205-
block_on(self.batch_get(keys))
206-
}
207-
208197
/// Create a new 'put' request.
209198
///
210199
/// Once resolved this request will result in the setting of the value associated with the given key.
@@ -234,10 +223,6 @@ impl Client {
234223
Ok(())
235224
}
236225

237-
pub fn put_sync(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
238-
block_on(self.put(key, value))
239-
}
240-
241226
/// Create a new 'batch put' request.
242227
///
243228
/// Once resolved this request will result in the setting of the values associated with the given keys.
@@ -274,10 +259,6 @@ impl Client {
274259
Ok(())
275260
}
276261

277-
pub fn batch_put_sync(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> {
278-
block_on(self.batch_put(pairs))
279-
}
280-
281262
/// Create a new 'delete' request.
282263
///
283264
/// Once resolved this request will result in the deletion of the given key.
@@ -308,10 +289,6 @@ impl Client {
308289
Ok(())
309290
}
310291

311-
pub fn delete_sync(&self, key: impl Into<Key>) -> Result<()> {
312-
block_on(self.delete(key))
313-
}
314-
315292
/// Create a new 'batch delete' request.
316293
///
317294
/// Once resolved this request will result in the deletion of the given keys.
@@ -343,10 +320,6 @@ impl Client {
343320
Ok(())
344321
}
345322

346-
pub fn batch_delete_sync(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
347-
block_on(self.batch_delete(keys))
348-
}
349-
350323
/// Create a new 'delete range' request.
351324
///
352325
/// Once resolved this request will result in the deletion of all keys lying in the given range.
@@ -375,10 +348,6 @@ impl Client {
375348
Ok(())
376349
}
377350

378-
pub fn delete_range_sync(&self, range: impl Into<BoundRange>) -> Result<()> {
379-
block_on(self.delete_range(range))
380-
}
381-
382351
/// Create a new 'scan' request.
383352
///
384353
/// Once resolved this request will result in a `Vec` of key-value pairs that lies in the specified range.
@@ -403,10 +372,6 @@ impl Client {
403372
self.scan_inner(range.into(), limit, false).await
404373
}
405374

406-
pub fn scan_sync(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
407-
block_on(self.scan(range, limit))
408-
}
409-
410375
/// Create a new 'scan' request that only returns the keys.
411376
///
412377
/// Once resolved this request will result in a `Vec` of keys that lies in the specified range.
@@ -436,10 +401,6 @@ impl Client {
436401
.collect())
437402
}
438403

439-
pub fn scan_keys_sync(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
440-
block_on(self.scan_keys(range, limit))
441-
}
442-
443404
/// Create a new 'batch scan' request.
444405
///
445406
/// Once resolved this request will result in a set of scanners over the given keys.
@@ -472,14 +433,6 @@ impl Client {
472433
self.batch_scan_inner(ranges, each_limit, false).await
473434
}
474435

475-
pub fn batch_scan_sync(
476-
&self,
477-
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
478-
each_limit: u32,
479-
) -> Result<Vec<KvPair>> {
480-
block_on(self.batch_scan(ranges, each_limit))
481-
}
482-
483436
/// Create a new 'batch scan' request that only returns the keys.
484437
///
485438
/// Once resolved this request will result in a set of scanners over the given keys.
@@ -516,14 +469,6 @@ impl Client {
516469
.collect())
517470
}
518471

519-
pub fn batch_scan_keys_sync(
520-
&self,
521-
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
522-
each_limit: u32,
523-
) -> Result<Vec<Key>> {
524-
block_on(self.batch_scan_keys(ranges, each_limit))
525-
}
526-
527472
/// Create a new *atomic* 'compare and set' request.
528473
///
529474
/// Once resolved this request will result in an atomic `compare and set'
@@ -558,15 +503,6 @@ impl Client {
558503
plan.execute().await
559504
}
560505

561-
pub async fn compare_and_swap_sync(
562-
&self,
563-
key: impl Into<Key>,
564-
previous_value: impl Into<Option<Value>>,
565-
new_value: impl Into<Value>,
566-
) -> Result<(Option<Value>, bool)> {
567-
block_on(self.compare_and_swap(key, previous_value, new_value))
568-
}
569-
570506
async fn scan_inner(
571507
&self,
572508
range: impl Into<BoundRange>,
@@ -628,117 +564,3 @@ impl Client {
628564
self.atomic.then(|| ()).ok_or(Error::UnsupportedMode)
629565
}
630566
}
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-
}

Diff for: src/raw/mod.rs

+2
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

0 commit comments

Comments
 (0)