@@ -122,7 +122,6 @@ use std::{
122
122
path:: PathBuf ,
123
123
str,
124
124
time:: Duration ,
125
- u8:: { MAX as U8_MAX , MIN as U8_MIN } ,
126
125
} ;
127
126
128
127
mod errors;
@@ -197,16 +196,6 @@ impl Key {
197
196
fn into_inner ( self ) -> Vec < u8 > {
198
197
self . 0
199
198
}
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
- }
210
199
}
211
200
212
201
impl From < Vec < u8 > > for Key {
@@ -599,14 +588,31 @@ pub type KvFuture<Resp> = Box<dyn Future<Item = Resp, Error = Error>>;
599
588
/// which means all of the above types can be passed directly to those functions.
600
589
pub trait KeyRange : Sized {
601
590
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.
603
596
///
604
597
/// ```rust
605
598
/// use tikv_client::{KeyRange, Key};
606
- /// let range = vec![0]..vec![100];
599
+ /// // Exclusive
600
+ /// let range = "a".."z";
607
601
/// assert_eq!(
608
602
/// 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)
610
616
/// );
611
617
// ```
612
618
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>)>
618
624
let start = match range. 0 {
619
625
Bound :: Included ( v) => v,
620
626
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
+ } ,
627
631
Bound :: Unbounded => Err ( Error :: invalid_key_range ( ) ) ?,
628
632
} ;
629
633
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) ,
639
640
Bound :: Unbounded => None ,
640
641
} ;
641
642
Ok ( ( start, end) )
0 commit comments