Skip to content

Commit

Permalink
Add sync API for raw client
Browse files Browse the repository at this point in the history
Signed-off-by: xuhui-lu <[email protected]>
  • Loading branch information
xuhui-lu committed Jun 13, 2021
1 parent 73a00ff commit dfe2d54
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/raw/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 @@ -159,6 +160,10 @@ 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 @@ -193,6 +198,13 @@ 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 @@ -222,6 +234,10 @@ 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 @@ -258,6 +274,13 @@ 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 @@ -288,6 +311,10 @@ 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 @@ -319,6 +346,10 @@ 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 @@ -347,6 +378,10 @@ 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 @@ -371,6 +406,10 @@ 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 @@ -400,6 +439,10 @@ 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 @@ -432,6 +475,14 @@ 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 @@ -468,6 +519,14 @@ 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 @@ -502,6 +561,15 @@ 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

0 comments on commit dfe2d54

Please sign in to comment.