Skip to content

Commit 034f0c2

Browse files
author
xuhui-lu
committed
Add sync API for raw client
Signed-off-by: xuhui-lu <[email protected]>
1 parent 4404c7e commit 034f0c2

File tree

3 files changed

+281
-2
lines changed

3 files changed

+281
-2
lines changed

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ pub use crate::backoff::Backoff;
123123
#[doc(inline)]
124124
pub use crate::kv::{BoundRange, IntoOwnedRange, Key, KvPair, Value};
125125
#[doc(inline)]
126-
pub use crate::raw::{lowering as raw_lowering, Client as RawClient, ColumnFamily};
126+
pub use crate::raw::{
127+
lowering as raw_lowering, Client as RawClient, ColumnFamily, SyncClient as SyncRawClient,
128+
};
127129
#[doc(inline)]
128130
pub use crate::request::RetryOptions;
129131
#[doc(inline)]

src/raw/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
//!
1010
//! **Warning:** It is not advisable to use both raw and transactional functionality in the same keyspace.
1111
12-
pub use self::client::Client;
12+
pub use self::{client::Client, sync_client::SyncClient};
1313
use crate::Error;
1414
use std::{convert::TryFrom, fmt};
1515

1616
mod client;
1717
pub mod lowering;
1818
mod requests;
19+
mod sync_client;
1920

2021
/// A [`ColumnFamily`](ColumnFamily) is an optional parameter for [`raw::Client`](Client) requests.
2122
///

src/raw/sync_client.rs

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2+
3+
use crate::{
4+
config::Config, raw::client::Client, BoundRange, ColumnFamily, Key, KvPair, Result, Value,
5+
};
6+
use slog::{Drain, Logger};
7+
use futures::executor::block_on;
8+
use std::u32;
9+
10+
#[derive(Clone)]
11+
pub struct SyncClient {
12+
client: Client,
13+
}
14+
15+
impl SyncClient {
16+
/// The synchronous version of RawClient
17+
///
18+
/// # Examples
19+
///
20+
/// ```rust,no_run
21+
/// # use tikv_client::SyncRawClient;
22+
/// let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
23+
/// ```
24+
pub fn new<S: Into<String>>(pd_endpoints: Vec<S>, logger: Option<Logger>) -> Result<SyncClient> {
25+
Self::new_with_config(pd_endpoints, Config::default(), logger)
26+
}
27+
28+
pub fn new_with_config<S: Into<String>>(
29+
pd_endpoints: Vec<S>,
30+
config: Config,
31+
logger: Option<Logger>,
32+
) -> Result<SyncClient> {
33+
let client = block_on(Client::new_with_config(pd_endpoints, config, logger)).unwrap();
34+
Ok(SyncClient { client: client })
35+
}
36+
37+
pub fn with_cf(&self, cf: ColumnFamily) -> SyncClient {
38+
SyncClient {
39+
client: self.client.with_cf(cf),
40+
}
41+
}
42+
43+
pub fn with_atomic_for_cas(&self) -> SyncClient {
44+
SyncClient {
45+
client: self.client.with_atomic_for_cas(),
46+
}
47+
}
48+
49+
/// Create a new 'get' request.
50+
///
51+
/// Once resolved this request will result in the fetching of the value associated with the
52+
/// given key.
53+
///
54+
/// Retuning `Ok(None)` indicates the key does not exist in TiKV.
55+
///
56+
/// # Examples
57+
/// ```rust,no_run
58+
/// # use tikv_client::{Value, Config, SyncRawClient};
59+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
60+
/// let key = "TiKV".to_owned();
61+
/// let req = client.get(key);
62+
/// let result: Option<Value> = req.unwrap();
63+
/// ```
64+
pub fn get(&self, key: impl Into<Key>) -> Result<Option<Value>> {
65+
block_on(self.client.get(key))
66+
}
67+
68+
/// Create a new 'batch get' request.
69+
///
70+
/// Once resolved this request will result in the fetching of the values associated with the
71+
/// given keys.
72+
///
73+
/// Non-existent entries will not appear in the result. The order of the keys is not retained in the result.
74+
///
75+
/// # Examples
76+
/// ```rust,no_run
77+
/// # use tikv_client::{KvPair, Config, SyncRawClient};
78+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
79+
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
80+
/// let req = client.batch_get(keys);
81+
/// let result: Vec<KvPair> = req.unwrap();
82+
/// ```
83+
pub fn batch_get(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<Vec<KvPair>> {
84+
block_on(self.client.batch_get(keys))
85+
}
86+
87+
/// Create a new 'put' request.
88+
///
89+
/// Once resolved this request will result in the setting of the value associated with the given key.
90+
///
91+
/// # Examples
92+
/// ```rust,no_run
93+
/// # use tikv_client::{Key, Value, Config, SyncRawClient};
94+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
95+
/// let key = "TiKV".to_owned();
96+
/// let val = "TiKV".to_owned();
97+
/// let req = client.put(key, val);
98+
/// let result: () = req.unwrap();
99+
/// ```
100+
pub fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
101+
block_on(self.client.put(key, value))
102+
}
103+
104+
/// Create a new 'batch put' request.
105+
///
106+
/// Once resolved this request will result in the setting of the values associated with the given keys.
107+
///
108+
/// # Examples
109+
/// ```rust,no_run
110+
/// # use tikv_client::{Result, KvPair, Key, Value, Config, SyncRawClient, IntoOwnedRange};
111+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
112+
/// let kvpair1 = ("PD".to_owned(), "Go".to_owned());
113+
/// let kvpair2 = ("TiKV".to_owned(), "Rust".to_owned());
114+
/// let iterable = vec![kvpair1, kvpair2];
115+
/// let req = client.batch_put(iterable);
116+
/// let result: () = req.unwrap();
117+
/// ```
118+
pub fn batch_put(&self, pairs: impl IntoIterator<Item = impl Into<KvPair>>) -> Result<()> {
119+
block_on(self.client.batch_put(pairs))
120+
}
121+
122+
/// Create a new 'delete' request.
123+
///
124+
/// Once resolved this request will result in the deletion of the given key.
125+
///
126+
/// It does not return an error if the key does not exist in TiKV.
127+
///
128+
/// # Examples
129+
/// ```rust,no_run
130+
/// # use tikv_client::{Key, Config, SyncRawClient};
131+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
132+
/// let key = "TiKV".to_owned();
133+
/// let req = client.delete(key);
134+
/// let result: () = req.unwrap();
135+
/// ```
136+
pub fn delete(&self, key: impl Into<Key>) -> Result<()> {
137+
block_on(self.client.delete(key))
138+
}
139+
140+
/// Create a new 'batch delete' request.
141+
///
142+
/// Once resolved this request will result in the deletion of the given keys.
143+
///
144+
/// It does not return an error if some of the keys do not exist and will delete the others.
145+
///
146+
/// # Examples
147+
/// ```rust,no_run
148+
/// # use tikv_client::{Config, SyncRawClient};
149+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
150+
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
151+
/// let req = client.batch_delete(keys);
152+
/// let result: () = req.unwrap();
153+
/// ```
154+
pub fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
155+
block_on(self.client.batch_delete(keys))
156+
}
157+
158+
/// Create a new 'delete range' request.
159+
///
160+
/// Once resolved this request will result in the deletion of all keys lying in the given range.
161+
///
162+
/// # Examples
163+
/// ```rust,no_run
164+
/// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange};
165+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
166+
/// let inclusive_range = "TiKV"..="TiDB";
167+
/// let req = client.delete_range(inclusive_range.into_owned());
168+
/// let result: () = req.unwrap();
169+
/// ```
170+
pub fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()> {
171+
block_on(self.client.delete_range(range))
172+
}
173+
174+
/// Create a new 'scan' request.
175+
///
176+
/// Once resolved this request will result in a `Vec` of key-value pairs that lies in the specified range.
177+
///
178+
/// If the number of eligible key-value pairs are greater than `limit`,
179+
/// only the first `limit` pairs are returned, ordered by the key.
180+
///
181+
///
182+
/// # Examples
183+
/// ```rust,no_run
184+
/// # use tikv_client::{KvPair, Config, SyncRawClient, IntoOwnedRange};
185+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
186+
/// let inclusive_range = "TiKV"..="TiDB";
187+
/// let req = client.scan(inclusive_range.into_owned(), 2);
188+
/// let result: Vec<KvPair> = req.unwrap();
189+
/// ```
190+
pub fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
191+
block_on(self.client.scan(range, limit))
192+
}
193+
194+
/// Create a new 'scan' request that only returns the keys.
195+
///
196+
/// Once resolved this request will result in a `Vec` of keys that lies in the specified range.
197+
///
198+
/// If the number of eligible keys are greater than `limit`,
199+
/// only the first `limit` pairs are returned, ordered by the key.
200+
///
201+
///
202+
/// # Examples
203+
/// ```rust,no_run
204+
/// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange};
205+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
206+
/// let inclusive_range = "TiKV"..="TiDB";
207+
/// let req = client.scan_keys(inclusive_range.into_owned(), 2);
208+
/// let result: Vec<Key> = req.unwrap();
209+
/// ```
210+
pub fn scan_keys(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
211+
block_on(self.client.scan_keys(range, limit))
212+
}
213+
214+
/// Create a new 'batch scan' request.
215+
///
216+
/// Once resolved this request will result in a set of scanners over the given keys.
217+
///
218+
/// **Warning**: This method is experimental. The `each_limit` parameter does not work as expected.
219+
/// It does not limit the number of results returned of each range,
220+
/// instead it limits the number of results in each region of each range.
221+
/// As a result, you may get **more than** `each_limit` key-value pairs for each range.
222+
/// But you should not miss any entries.
223+
///
224+
/// # Examples
225+
/// ```rust,no_run
226+
/// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange};
227+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
228+
/// let inclusive_range1 = "TiDB"..="TiKV";
229+
/// let inclusive_range2 = "TiKV"..="TiSpark";
230+
/// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
231+
/// let result = client.batch_scan(iterable, 2);
232+
/// ```
233+
pub fn batch_scan(
234+
&self,
235+
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
236+
each_limit: u32,
237+
) -> Result<Vec<KvPair>> {
238+
block_on(self.client.batch_scan(ranges, each_limit))
239+
}
240+
241+
/// Create a new 'batch scan' request that only returns the keys.
242+
///
243+
/// Once resolved this request will result in a set of scanners over the given keys.
244+
///
245+
/// **Warning**: This method is experimental.
246+
/// The `each_limit` parameter does not limit the number of results returned of each range,
247+
/// instead it limits the number of results in each region of each range.
248+
/// As a result, you may get **more than** `each_limit` key-value pairs for each range,
249+
/// but you should not miss any entries.
250+
///
251+
/// # Examples
252+
/// ```rust,no_run
253+
/// # use tikv_client::{Key, Config, SyncRawClient, IntoOwnedRange};
254+
/// # let client = SyncRawClient::new(vec!["192.168.0.100"]).unwrap();
255+
/// let inclusive_range1 = "TiDB"..="TiKV";
256+
/// let inclusive_range2 = "TiKV"..="TiSpark";
257+
/// let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
258+
/// let result = client.batch_scan(iterable, 2);
259+
/// ```
260+
pub fn batch_scan_keys(
261+
&self,
262+
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
263+
each_limit: u32,
264+
) -> Result<Vec<Key>> {
265+
block_on(self.client.batch_scan_keys(ranges, each_limit))
266+
}
267+
268+
pub fn compare_and_swap(
269+
&self,
270+
key: impl Into<Key>,
271+
previous_value: impl Into<Option<Value>>,
272+
new_value: impl Into<Value>,
273+
) -> Result<(Option<Value>, bool)> {
274+
block_on(self.client.compare_and_swap(key, previous_value, new_value))
275+
}
276+
}

0 commit comments

Comments
 (0)