Skip to content

Commit 0a2c4c0

Browse files
committed
Make KeyRange output correctly formatted keys to match TikV protocol
Signed-off-by: Ana Hobden <[email protected]>
1 parent 20192c3 commit 0a2c4c0

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

src/lib.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ use std::{
122122
path::PathBuf,
123123
str,
124124
time::Duration,
125-
u8::{MAX as U8_MAX, MIN as U8_MIN},
126125
};
127126

128127
mod errors;
@@ -197,16 +196,6 @@ impl Key {
197196
fn into_inner(self) -> Vec<u8> {
198197
self.0
199198
}
200-
201-
#[inline]
202-
fn push(&mut self, v: u8) {
203-
self.0.push(v)
204-
}
205-
206-
#[inline]
207-
fn pop(&mut self) {
208-
self.0.pop();
209-
}
210199
}
211200

212201
impl From<Vec<u8>> for Key {
@@ -599,14 +588,31 @@ pub type KvFuture<Resp> = Box<dyn Future<Item = Resp, Error = Error>>;
599588
/// which means all of the above types can be passed directly to those functions.
600589
pub trait KeyRange: Sized {
601590
fn into_bounds(self) -> (Bound<Key>, Bound<Key>);
602-
/// Return the keys that match the given bounds, inclusively.
591+
/// Ranges used in scanning TiKV have a particularity to them.
592+
///
593+
/// The **start** of a scan is inclusive, unless appended with an '\0', then it is exclusive.
594+
///
595+
/// The **end** of a scan is exclusive, unless appended with an '\0', then it is inclusive.
603596
///
604597
/// ```rust
605598
/// use tikv_client::{KeyRange, Key};
606-
/// let range = vec![0]..vec![100];
599+
/// // Exclusive
600+
/// let range = "a".."z";
607601
/// assert_eq!(
608602
/// range.into_keys().unwrap(),
609-
/// (Key::from(vec![0]), Some(Key::from(vec![99])))
603+
/// (Key::from("a"), Some(Key::from("z")))
604+
/// );
605+
/// // Inclusive
606+
/// let range = "a"..="z";
607+
/// assert_eq!(
608+
/// range.into_keys().unwrap(),
609+
/// (Key::from("a"), Some(Key::from("z\0")))
610+
/// );
611+
/// // Open
612+
/// let range = "a"..;
613+
/// assert_eq!(
614+
/// range.into_keys().unwrap(),
615+
/// (Key::from("a"), None)
610616
/// );
611617
// ```
612618
fn into_keys(self) -> Result<(Key, Option<Key>)> {
@@ -618,24 +624,19 @@ fn range_to_keys(range: (Bound<Key>, Bound<Key>)) -> Result<(Key, Option<Key>)>
618624
let start = match range.0 {
619625
Bound::Included(v) => v,
620626
Bound::Excluded(mut v) => {
621-
match v.last_mut() {
622-
None | Some(&mut U8_MAX) => v.push(0),
623-
Some(v) => *v += 1,
624-
}
625-
v
626-
}
627+
let mut buf = b"\0".to_vec();
628+
buf.append(&mut v.0);
629+
Key(buf)
630+
},
627631
Bound::Unbounded => Err(Error::invalid_key_range())?,
628632
};
629633
let end = match range.1 {
630-
Bound::Included(v) => Some(v),
631-
Bound::Excluded(mut v) => Some({
632-
match v.last_mut() {
633-
None => (),
634-
Some(&mut U8_MIN) => v.pop(),
635-
Some(v) => *v -= 1,
636-
}
637-
v
638-
}),
634+
Bound::Included(mut v) => {
635+
let mut buf = b"\0".to_vec();
636+
v.0.append(&mut buf);
637+
Some(v)
638+
},
639+
Bound::Excluded(v) => Some(v),
639640
Bound::Unbounded => None,
640641
};
641642
Ok((start, end))

src/rpc/tikv/client.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -557,19 +557,7 @@ impl KvClient {
557557
.map(|k| req.set_start_key(k.into_inner()))
558558
.unwrap();
559559
end_key
560-
.map(|k| {
561-
// Scan in TiKV has a particularity to it.
562-
//
563-
// The **start** of a scan is inclusive, unless appended with an '\0', then it is
564-
// exclusive. The **end** of a scan is exclusive, unless appended with an '\0',
565-
// then it is inclusive.
566-
//
567-
// Because of `KeyRange::into_keys()` we *know* the keys we have are inclusive
568-
// (KeyRange calculates it).
569-
let mut end_key = k.into_inner();
570-
end_key.append(&mut vec![0]);
571-
req.set_end_key(end_key);
572-
})
560+
.map(|k| req.set_end_key(k.into_inner()))
573561
.unwrap();
574562
req.set_limit(limit);
575563
req.set_key_only(key_only);

0 commit comments

Comments
 (0)