@@ -33,7 +33,7 @@ fn main() -> Result<()> {
3333 Config :: new ( args. pd )
3434 } ;
3535
36- // When we first create a client we recieve a `Connect` structure which must be resolved before
36+ // When we first create a client we receive a `Connect` structure which must be resolved before
3737 // the client is actually connected and usable.
3838 let unconnnected_client = Client :: new ( & config) ;
3939 let client = unconnnected_client. wait ( ) ?;
@@ -45,9 +45,8 @@ fn main() -> Result<()> {
4545 // Here we set the key `TiKV` to have the value `Rust` associated with it.
4646 let put_request = client. put ( KEY , VALUE ) ;
4747 put_request. wait ( ) ?; // Returns a `tikv_client::Error` on failure.
48- println ! ( "Put key \" {} \" , value \" {} \" ." , KEY , VALUE ) ;
48+ println ! ( "Put key {:?} , value {:?} ." , KEY , VALUE ) ;
4949
50- //
5150 // Unlike a standard Rust HashMap all calls take owned values. This is because under the hood
5251 // protobufs must take ownership of the data. If we only took a borrow we'd need to internally
5352 // clone it. This is against Rust API guidelines, so you must manage this yourself.
@@ -56,47 +55,60 @@ fn main() -> Result<()> {
5655 // This type is practical to use for real things, and usage forces an internal copy.
5756 //
5857 // It is best to pass a `Vec<u8>` in terms of explictness and speed. `String`s and a few other
59- // types are supported as well, but it all ends up as `Vec<u8>` in the end.
60- let key: String = String :: from ( KEY ) ;
61- let value: Value = client. get ( key. clone ( ) ) . wait ( ) ?. expect ( "value must exist" ) ;
62- assert_eq ! ( value. as_ref( ) , VALUE . as_bytes( ) ) ;
63- println ! ( "Get key \" {:?}\" returned value \" {:?}\" ." , value, KEY ) ;
58+ // types are supported as well, but it all ends up as `Vec<u8>` in the end.
59+ let value: Option < Value > = client. get ( KEY ) . wait ( ) ?;
60+ assert_eq ! ( value, Some ( Value :: from( VALUE ) ) ) ;
61+ println ! ( "Get key {:?} returned value {:?}." , Key :: from( KEY ) , value) ;
6462
6563 // You can also set the `ColumnFamily` used by the request.
6664 // This is *advanced usage* and should have some special considerations.
6765 client
68- . delete ( key . clone ( ) )
66+ . delete ( KEY )
6967 . wait ( )
7068 . expect ( "Could not delete value" ) ;
71- println ! ( "Key: {:?} deleted" , key ) ;
69+ println ! ( "Key: {:?} deleted" , Key :: from ( KEY ) ) ;
7270
73- // Get returns None for non-existing key
74- assert ! ( client. get( key) . wait( ) ?. is_none( ) ) ;
71+ // Here we check if the key has been deleted from the key-value store.
72+ let value: Option < Value > = client
73+ . get ( KEY )
74+ . wait ( )
75+ . expect ( "Could not get just deleted entry" ) ;
76+ assert ! ( value. is_none( ) ) ;
7577
76- let pairs: Vec < KvPair > = ( 1 ..3 )
77- . map ( |i| KvPair :: from ( ( Key :: from ( format ! ( "k{}" , i) ) , Value :: from ( format ! ( "v{}" , i) ) ) ) )
78- . collect ( ) ;
78+ // You can ask to write multiple key-values at the same time, it is much more
79+ // performant because it is passed in one request to the key-value store.
80+ let pairs = vec ! [
81+ KvPair :: from( ( "k1" , "v1" ) ) ,
82+ KvPair :: from( ( "k2" , "v2" ) ) ,
83+ KvPair :: from( ( "k3" , "v3" ) ) ,
84+ ] ;
7985 client
80- . batch_put ( pairs. clone ( ) )
86+ . batch_put ( pairs)
8187 . wait ( )
8288 . expect ( "Could not put pairs" ) ;
8389
84- let keys = vec ! [ Key :: from ( b"k1" . to_vec ( ) ) , Key :: from ( b"k2" . to_vec ( ) ) ] ;
85-
90+ // Same thing when you want to retrieve multiple values.
91+ let keys = vec ! [ Key :: from ( "k1" ) , Key :: from ( "k2" ) ] ;
8692 let values = client
8793 . batch_get ( keys. clone ( ) )
8894 . wait ( )
8995 . expect ( "Could not get values" ) ;
9096 println ! ( "Found values: {:?} for keys: {:?}" , values, keys) ;
9197
92- let start: Key = b"k1" . to_vec ( ) . into ( ) ;
93- let end: Key = b"k2" . to_vec ( ) . into ( ) ;
94- client
95- . scan ( start. clone ( ) ..end. clone ( ) , 10 )
98+ // Scanning a range of keys is also possible giving it two bounds
99+ // it will returns all entries between these two.
100+ let start = "k1" ;
101+ let end = "k2" ;
102+ let pairs = client
103+ . scan ( start..=end, 10 )
96104 . key_only ( )
97105 . wait ( )
98106 . expect ( "Could not scan" ) ;
99107
108+ let keys: Vec < _ > = pairs. into_iter ( ) . map ( |p| p. key ( ) . clone ( ) ) . collect ( ) ;
109+ assert_eq ! ( & keys, & [ Key :: from( "k1" ) , Key :: from( "k2" ) ] ) ;
110+ println ! ( "Scaning from {:?} to {:?} gives: {:?}" , start, end, keys) ;
111+
100112 // Cleanly exit.
101113 Ok ( ( ) )
102114}
0 commit comments