Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Rane <rahulrane50@gmail.com>
  • Loading branch information
rahulrane50 committed Feb 16, 2024
1 parent 384c42f commit 6c3a936
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/raw/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,16 @@ impl<PdC: PdClient> Client<PdC> {
self.scan_inner(range.into(), limit, false, false).await
}

// Create a new 'scan' request but scans in "reverse" direction.
/// Create a new 'scan' request but scans in "reverse" direction.
///
/// Once resolved this request will result in a `Vec` of key-value pairs that lies in the specified range.
///
/// If the number of eligible key-value pairs are greater than `limit`,
/// only the first `limit` pairs are returned, ordered by the key.
///
///
/// Reverse Scan queries continuous kv pairs in range (endKey, startKey],
/// from startKey(upperBound) to endKey(lowerBound), up to limit pairs.
/// Reverse Scan queries continuous kv pairs in range [startKey, endKey),
/// from startKey(lowerBound) to endKey(upperBound) in reverse order, up to limit pairs.
/// The returned keys are in reversed lexicographical order.
/// If you want to include the endKey or exclude the startKey, push a '\0' to the key.
/// It doesn't support Scanning from "", because locating the last Region is not yet implemented.
Expand All @@ -522,7 +522,7 @@ impl<PdC: PdClient> Client<PdC> {
/// # use futures::prelude::*;
/// # futures::executor::block_on(async {
/// # let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
/// let inclusive_range = "TiDB"..="TiKV";
/// let inclusive_range = "TiKV"..="TiDB";
/// let req = client.scan_reverse(inclusive_range.into_owned(), 2);
/// let result: Vec<KvPair> = req.await.unwrap();
/// # });
Expand Down
9 changes: 7 additions & 2 deletions src/raw/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,13 @@ pub fn new_raw_scan_request(
cf: Option<ColumnFamily>,
) -> kvrpcpb::RawScanRequest {
let mut req = kvrpcpb::RawScanRequest::default();
req.start_key = start_key;
req.end_key = end_key;
if !reverse {
req.start_key = start_key;
req.end_key = end_key;
} else {
req.start_key = end_key;
req.end_key = start_key;
}
req.limit = limit;
req.key_only = key_only;
req.reverse = reverse;
Expand Down
22 changes: 13 additions & 9 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@ async fn raw_req() -> Result<()> {
assert_eq!(res[6].1, "v5".as_bytes());

// reverse scan
// By default end key is exclusive, so k5 is not included and start key in included

// By default end key is exclusive, so k5 is not included and start key in included
let res = client
.scan_reverse("k5".to_owned().."k2".to_owned(), 5)
.scan_reverse("k2".to_owned().."k5".to_owned(), 5)
.await?;
assert_eq!(res.len(), 3);
assert_eq!(res[0].1, "v4".as_bytes());
Expand All @@ -574,7 +574,7 @@ async fn raw_req() -> Result<()> {

// by default end key in exclusive and start key is inclusive but now exclude start key
let res = client
.scan_reverse("k5".to_owned()..="k2".to_owned(), 5)
.scan_reverse("k2\0".to_owned().."k5".to_owned(), 5)
.await?;
assert_eq!(res.len(), 2);
assert_eq!(res[0].1, "v4".as_bytes());
Expand All @@ -583,7 +583,7 @@ async fn raw_req() -> Result<()> {
// reverse scan
// by default end key is exclusive and start key is inclusive but now include end key
let res = client
.scan_reverse("k5\0".to_owned().."k2".to_owned(), 5)
.scan_reverse("k2".to_owned()..="k5".to_owned(), 5)
.await?;
assert_eq!(res.len(), 4);
assert_eq!(res[0].1, "v5".as_bytes());
Expand All @@ -593,7 +593,7 @@ async fn raw_req() -> Result<()> {

// by default end key is exclusive and start key is inclusive but now include end key and exclude start key
let res = client
.scan_reverse("k5\0".to_owned()..="k2".to_owned(), 5)
.scan_reverse("k2\0".to_owned()..="k5".to_owned(), 5)
.await?;
assert_eq!(res.len(), 3);
assert_eq!(res[0].1, "v5".as_bytes());
Expand All @@ -602,16 +602,20 @@ async fn raw_req() -> Result<()> {

// limit results to first 2
let res = client
.scan_reverse("k5".to_owned().."k2".to_owned(), 2)
.scan_reverse("k2".to_owned().."k5".to_owned(), 2)
.await?;
assert_eq!(res.len(), 2);
assert_eq!(res[0].1, "v4".as_bytes());
assert_eq!(res[1].1, "v3".as_bytes());

//let range = "k5"..; // Upperbound (k5, +inf). This is NOT SUPPORTED by TiKV.
let range = BoundRange::range_from(Key::from("k5".to_owned()));
// if endKey is not provided then it scan everything including end key
let range = BoundRange::range_from(Key::from("k2".to_owned()));
let res = client.scan_reverse(range, 20).await?;
assert_eq!(res.len(), 0);
assert_eq!(res.len(), 4);
assert_eq!(res[0].1, "v5".as_bytes());
assert_eq!(res[1].1, "v4".as_bytes());
assert_eq!(res[2].1, "v3".as_bytes());
assert_eq!(res[3].1, "v2".as_bytes());

Ok(())
}
Expand Down

0 comments on commit 6c3a936

Please sign in to comment.