@@ -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
128127mod 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
212201impl 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.
600589pub 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) )
0 commit comments