@@ -33,9 +33,9 @@ fn main() -> Result<()> {
33
33
Config :: new ( args. pd )
34
34
} ;
35
35
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
37
37
// the client is actually connected and usable.
38
- let unconnnected_client = Client :: new ( & config) ;
38
+ let unconnnected_client = Client :: new ( config) ;
39
39
let client = unconnnected_client. wait ( ) ?;
40
40
41
41
// Requests are created from the connected client. These calls return structures which
@@ -45,9 +45,8 @@ fn main() -> Result<()> {
45
45
// Here we set the key `TiKV` to have the value `Rust` associated with it.
46
46
let put_request = client. put ( KEY , VALUE ) ;
47
47
put_request. wait ( ) ?; // Returns a `tikv_client::Error` on failure.
48
- println ! ( "Put key \" {} \" , value \" {} \" ." , KEY , VALUE ) ;
48
+ println ! ( "Put key {:?} , value {:?} ." , KEY , VALUE ) ;
49
49
50
- //
51
50
// Unlike a standard Rust HashMap all calls take owned values. This is because under the hood
52
51
// protobufs must take ownership of the data. If we only took a borrow we'd need to internally
53
52
// clone it. This is against Rust API guidelines, so you must manage this yourself.
@@ -56,47 +55,54 @@ fn main() -> Result<()> {
56
55
// This type is practical to use for real things, and usage forces an internal copy.
57
56
//
58
57
// 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) ;
64
62
65
63
// You can also set the `ColumnFamily` used by the request.
66
64
// 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 ) ) ;
72
67
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 )
81
71
. wait ( )
82
- . expect ( "Could not put pairs" ) ;
72
+ . expect ( "Could not get just deleted entry" ) ;
73
+ assert ! ( value. is_none( ) ) ;
83
74
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" ) ;
85
83
84
+ // Same thing when you want to retrieve multiple values.
85
+ let keys = vec ! [ Key :: from( "k1" ) , Key :: from( "k2" ) ] ;
86
86
let values = client
87
87
. batch_get ( keys. clone ( ) )
88
88
. wait ( )
89
89
. expect ( "Could not get values" ) ;
90
90
println ! ( "Found values: {:?} for keys: {:?}" , values, keys) ;
91
91
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 )
96
98
. key_only ( )
97
99
. wait ( )
98
100
. expect ( "Could not scan" ) ;
99
101
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
+
100
106
// Cleanly exit.
101
107
Ok ( ( ) )
102
108
}
0 commit comments