Merge from master

Signed-off-by: Andy Lok <andylokandy@hotmail.com>
This commit is contained in:
Andy Lok 2020-11-12 17:33:21 +08:00
parent aa2fae8f17
commit 21a8dc43ad
4 changed files with 16 additions and 44 deletions

View File

@ -86,8 +86,7 @@ async fn main() -> Result<()> {
let start = "k1";
let end = "k2";
let pairs = client
.with_key_only(true)
.scan((start..=end).to_owned(), 10)
.scan((start..=end).to_owned(), 10, true)
.await
.expect("Could not scan");
@ -107,7 +106,7 @@ async fn main() -> Result<()> {
(k1.to_owned()..=k3.to_owned()),
];
let kv_pairs = client
.batch_scan(batch_scan_keys.to_owned(), 10)
.batch_scan(batch_scan_keys.to_owned(), 10, true)
.await
.expect("Could not batch scan");
let vals: Vec<_> = kv_pairs

View File

@ -21,7 +21,7 @@ async fn get(client: &Client, key: Key) -> Option<Value> {
async fn scan(client: &Client, range: impl Into<BoundRange>, limit: u32) {
let mut txn = client.begin().await.expect("Could not begin a transaction");
txn.scan(range, limit)
txn.scan(range, limit, false)
.await
.expect("Could not scan key-value pairs in range")
.for_each(|pair| println!("{:?}", pair));

View File

@ -21,7 +21,6 @@ const MAX_RAW_KV_SCAN_LIMIT: u32 = 10240;
pub struct Client {
rpc: Arc<PdRpcClient>,
cf: Option<ColumnFamily>,
key_only: bool,
}
impl Client {
@ -37,11 +36,7 @@ impl Client {
/// ```
pub async fn new(config: Config) -> Result<Client> {
let rpc = Arc::new(PdRpcClient::connect(&config, false).await?);
Ok(Client {
rpc,
cf: None,
key_only: false,
})
Ok(Client { rpc, cf: None })
}
/// Set the column family of requests.
@ -67,34 +62,6 @@ impl Client {
Client {
rpc: self.rpc.clone(),
cf: Some(cf),
key_only: self.key_only,
}
}
/// Set the `key_only` option of requests.
///
/// This function returns a new `Client`, requests created with it will have the
/// supplied `key_only` option. The original `Client` can still be used. `key_only`
/// is only relevant for `scan`-like requests, for other kinds of request, it
/// will be ignored.
/// With `key_only` being true, `scan`-like requests will ignore values.
///
/// By default, `key_only` is set to false.
///
/// # Examples
/// ```rust,no_run
/// # use tikv_client::{Config, RawClient, ToOwnedRange};
/// # use futures::prelude::*;
/// # futures::executor::block_on(async {
/// let client = RawClient::new(Config::default()).await.unwrap().with_key_only(true);
/// let scan_request = client.scan(("TiKV"..="TiDB").to_owned(), 2);
/// # });
/// ```
pub fn with_key_only(&self, key_only: bool) -> Client {
Client {
rpc: self.rpc.clone(),
cf: self.cf.clone(),
key_only,
}
}
@ -279,16 +246,21 @@ impl Client {
/// # futures::executor::block_on(async {
/// # let client = RawClient::new(Config::default()).await.unwrap();
/// let inclusive_range = "TiKV"..="TiDB";
/// let req = client.scan(inclusive_range.to_owned(), 2);
/// let req = client.scan(inclusive_range.to_owned(), 2, true);
/// let result: Vec<KvPair> = req.await.unwrap();
/// # });
/// ```
pub async fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
pub async fn scan(
&self,
range: impl Into<BoundRange>,
limit: u32,
key_only: bool,
) -> Result<Vec<KvPair>> {
if limit > MAX_RAW_KV_SCAN_LIMIT {
return Err(Error::max_scan_limit_exceeded(limit, MAX_RAW_KV_SCAN_LIMIT));
}
let res = requests::new_raw_scan_request(range, limit, self.key_only, self.cf.clone())
let res = requests::new_raw_scan_request(range, limit, key_only, self.cf.clone())
.execute(self.rpc.clone(), OPTIMISTIC_BACKOFF)
.await;
res.map(|mut s| {
@ -316,7 +288,7 @@ impl Client {
/// let inclusive_range1 = "TiDB"..="TiKV";
/// let inclusive_range2 = "TiKV"..="TiSpark";
/// let iterable = vec![inclusive_range1.to_owned(), inclusive_range2.to_owned()];
/// let req = client.batch_scan(iterable, 2);
/// let req = client.batch_scan(iterable, 2, false);
/// let result = req.await;
/// # });
/// ```
@ -324,6 +296,7 @@ impl Client {
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32,
key_only: bool,
) -> Result<Vec<KvPair>> {
if each_limit > MAX_RAW_KV_SCAN_LIMIT {
return Err(Error::max_scan_limit_exceeded(
@ -332,7 +305,7 @@ impl Client {
));
}
requests::new_raw_batch_scan_request(ranges, each_limit, self.key_only, self.cf.clone())
requests::new_raw_batch_scan_request(ranges, each_limit, key_only, self.cf.clone())
.execute(self.rpc.clone(), OPTIMISTIC_BACKOFF)
.await
}

View File

@ -228,7 +228,7 @@ impl Transaction {
/// let key1: Key = b"TiKV".to_vec().into();
/// let key2: Key = b"TiDB".to_vec().into();
/// let result: Vec<KvPair> = txn
/// .scan(key1..key2, 10)
/// .scan(key1..key2, 10, true)
/// .await
/// .unwrap()
/// .collect();