From 21a8dc43ad0da7688d8087e46f9adeb7dbc8239d Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Thu, 12 Nov 2020 17:33:21 +0800 Subject: [PATCH] Merge from master Signed-off-by: Andy Lok --- examples/raw.rs | 5 ++-- examples/transaction.rs | 2 +- src/raw/client.rs | 51 ++++++++-------------------------- src/transaction/transaction.rs | 2 +- 4 files changed, 16 insertions(+), 44 deletions(-) diff --git a/examples/raw.rs b/examples/raw.rs index da10551..2ef2b22 100644 --- a/examples/raw.rs +++ b/examples/raw.rs @@ -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 diff --git a/examples/transaction.rs b/examples/transaction.rs index 7dadc47..a9ce996 100644 --- a/examples/transaction.rs +++ b/examples/transaction.rs @@ -21,7 +21,7 @@ async fn get(client: &Client, key: Key) -> Option { async fn scan(client: &Client, range: impl Into, 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)); diff --git a/src/raw/client.rs b/src/raw/client.rs index 7e6d10b..f53c3c7 100644 --- a/src/raw/client.rs +++ b/src/raw/client.rs @@ -21,7 +21,6 @@ const MAX_RAW_KV_SCAN_LIMIT: u32 = 10240; pub struct Client { rpc: Arc, cf: Option, - key_only: bool, } impl Client { @@ -37,11 +36,7 @@ impl Client { /// ``` pub async fn new(config: Config) -> Result { 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 = req.await.unwrap(); /// # }); /// ``` - pub async fn scan(&self, range: impl Into, limit: u32) -> Result> { + pub async fn scan( + &self, + range: impl Into, + limit: u32, + key_only: bool, + ) -> Result> { 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>, each_limit: u32, + key_only: bool, ) -> Result> { 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 } diff --git a/src/transaction/transaction.rs b/src/transaction/transaction.rs index 92953b7..8eec67a 100644 --- a/src/transaction/transaction.rs +++ b/src/transaction/transaction.rs @@ -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 = txn - /// .scan(key1..key2, 10) + /// .scan(key1..key2, 10, true) /// .await /// .unwrap() /// .collect();