@@ -33,9 +33,9 @@ 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.
38- let unconnnected_client = Client :: new ( & config) ;
38+ let unconnnected_client = Client :: new ( config) ;
3939 let client = unconnnected_client. wait ( ) ?;
4040
4141 // Requests are created from the connected client. These calls return structures which
@@ -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,54 @@ 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.
67- client
68- . delete ( key. clone ( ) )
69- . wait ( )
70- . expect ( "Could not delete value" ) ;
71- println ! ( "Key: {:?} deleted" , key) ;
65+ client. delete ( KEY ) . wait ( ) . expect ( "Could not delete value" ) ;
66+ println ! ( "Key: {:?} deleted" , Key :: from( KEY ) ) ;
7267
73- // Get returns None for non-existing key
74- assert ! ( client. get( key) . wait( ) ?. is_none( ) ) ;
75-
76- let pairs: Vec < KvPair > = ( 1 ..3 )
77- . map ( |i| KvPair :: from ( ( Key :: from ( format ! ( "k{}" , i) ) , Value :: from ( format ! ( "v{}" , i) ) ) ) )
78- . collect ( ) ;
79- client
80- . batch_put ( pairs. clone ( ) )
68+ // Here we check if the key has been deleted from the key-value store.
69+ let value: Option < Value > = client
70+ . get ( KEY )
8171 . wait ( )
82- . expect ( "Could not put pairs" ) ;
72+ . expect ( "Could not get just deleted entry" ) ;
73+ assert ! ( value. is_none( ) ) ;
8374
84- let keys = vec ! [ Key :: from( b"k1" . to_vec( ) ) , Key :: from( b"k2" . to_vec( ) ) ] ;
75+ // You can ask to write multiple key-values at the same time, it is much more
76+ // performant because it is passed in one request to the key-value store.
77+ let pairs = vec ! [
78+ KvPair :: from( ( "k1" , "v1" ) ) ,
79+ KvPair :: from( ( "k2" , "v2" ) ) ,
80+ KvPair :: from( ( "k3" , "v3" ) ) ,
81+ ] ;
82+ client. batch_put ( pairs) . wait ( ) . expect ( "Could not put pairs" ) ;
8583
84+ // Same thing when you want to retrieve multiple values.
85+ let keys = vec ! [ Key :: from( "k1" ) , Key :: from( "k2" ) ] ;
8686 let values = client
8787 . batch_get ( keys. clone ( ) )
8888 . wait ( )
8989 . expect ( "Could not get values" ) ;
9090 println ! ( "Found values: {:?} for keys: {:?}" , values, keys) ;
9191
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 )
92+ // Scanning a range of keys is also possible giving it two bounds
93+ // it will returns all entries between these two.
94+ let start = "k1" ;
95+ let end = "k2" ;
96+ let pairs = client
97+ . scan ( start..=end, 10 )
9698 . key_only ( )
9799 . wait ( )
98100 . expect ( "Could not scan" ) ;
99101
102+ let keys: Vec < _ > = pairs. into_iter ( ) . map ( |p| p. key ( ) . clone ( ) ) . collect ( ) ;
103+ assert_eq ! ( & keys, & [ Key :: from( "k1" ) , Key :: from( "k2" ) ] ) ;
104+ println ! ( "Scaning from {:?} to {:?} gives: {:?}" , start, end, keys) ;
105+
100106 // Cleanly exit.
101107 Ok ( ( ) )
102108}
0 commit comments