@@ -33,7 +33,7 @@ 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
38
let unconnnected_client = Client :: new ( & config) ;
39
39
let client = unconnnected_client. wait ( ) ?;
@@ -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,60 @@ 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
65
client
68
- . delete ( key . clone ( ) )
66
+ . delete ( KEY )
69
67
. wait ( )
70
68
. expect ( "Could not delete value" ) ;
71
- println ! ( "Key: {:?} deleted" , key ) ;
69
+ println ! ( "Key: {:?} deleted" , Key :: from ( KEY ) ) ;
72
70
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( ) ) ;
75
77
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
+ ] ;
79
85
client
80
- . batch_put ( pairs. clone ( ) )
86
+ . batch_put ( pairs)
81
87
. wait ( )
82
88
. expect ( "Could not put pairs" ) ;
83
89
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" ) ] ;
86
92
let values = client
87
93
. batch_get ( keys. clone ( ) )
88
94
. wait ( )
89
95
. expect ( "Could not get values" ) ;
90
96
println ! ( "Found values: {:?} for keys: {:?}" , values, keys) ;
91
97
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 )
96
104
. key_only ( )
97
105
. wait ( )
98
106
. expect ( "Could not scan" ) ;
99
107
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
+
100
112
// Cleanly exit.
101
113
Ok ( ( ) )
102
114
}
0 commit comments