Improve the raw example

Signed-off-by: Clément Renault <renault.cle@gmail.com>
This commit is contained in:
Clément Renault 2019-01-21 17:35:14 +01:00
parent c47b92bab4
commit 3c3d7f8df3
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
1 changed files with 33 additions and 23 deletions

View File

@ -33,7 +33,7 @@ fn main() -> Result<()> {
Config::new(args.pd) Config::new(args.pd)
}; };
// When we first create a client we recieve a `Connect` structure which must be resolved before // When we first create a client we receive a `Connect` structure which must be resolved before
// the client is actually connected and usable. // the client is actually connected and usable.
let unconnnected_client = Client::new(&config); let unconnnected_client = Client::new(&config);
let client = unconnnected_client.wait()?; let client = unconnnected_client.wait()?;
@ -45,9 +45,8 @@ fn main() -> Result<()> {
// Here we set the key `TiKV` to have the value `Rust` associated with it. // Here we set the key `TiKV` to have the value `Rust` associated with it.
let put_request = client.put(KEY, VALUE); let put_request = client.put(KEY, VALUE);
put_request.wait()?; // Returns a `tikv_client::Error` on failure. put_request.wait()?; // Returns a `tikv_client::Error` on failure.
println!("Put key \"{}\", value \"{}\".", KEY, VALUE); println!("Put key {:?}, value {:?}.", KEY, VALUE);
//
// Unlike a standard Rust HashMap all calls take owned values. This is because under the hood // Unlike a standard Rust HashMap all calls take owned values. This is because under the hood
// protobufs must take ownership of the data. If we only took a borrow we'd need to internally // protobufs must take ownership of the data. If we only took a borrow we'd need to internally
// clone it. This is against Rust API guidelines, so you must manage this yourself. // clone it. This is against Rust API guidelines, so you must manage this yourself.
@ -57,48 +56,59 @@ fn main() -> Result<()> {
// //
// It is best to pass a `Vec<u8>` in terms of explictness and speed. `String`s and a few other // It is best to pass a `Vec<u8>` in terms of explictness and speed. `String`s and a few other
// types are supported as well, but it all ends up as `Vec<u8>` in the end. // types are supported as well, but it all ends up as `Vec<u8>` in the end.
let key: String = String::from(KEY); let value: Option<Value> = client.get(KEY).wait()?;
let value: Value = client.get(key.clone()).wait()?.expect("value must exist"); assert_eq!(value, Some(Value::from(VALUE)));
assert_eq!(value.as_ref(), VALUE.as_bytes()); println!("Get key {:?} returned value {:?}.", Key::from(KEY), value);
println!("Get key \"{:?}\" returned value \"{:?}\".", value, KEY);
// You can also set the `ColumnFamily` used by the request. // You can also set the `ColumnFamily` used by the request.
// This is *advanced usage* and should have some special considerations. // This is *advanced usage* and should have some special considerations.
client client
.delete(key.clone()) .delete(KEY)
.wait() .wait()
.expect("Could not delete value"); .expect("Could not delete value");
println!("Key: {:?} deleted", key); println!("Key: {:?} deleted", Key::from(KEY));
client // Here we check if the key has been deleted from the key-value store.
.get(key) let value: Option<Value> = client
.get(KEY)
.wait() .wait()
.expect_err("Get returned value for not existing key"); .expect("Could not get just deleted entry");
assert!(value.is_none());
let pairs: Vec<KvPair> = (1..3) // You can ask to write multiple key-values at the same time, it is much more
.map(|i| KvPair::from((Key::from(format!("k{}", i)), Value::from(format!("v{}", i))))) // performant because it is passed in one request to the key-value store.
.collect(); let pairs = vec![
KvPair::from(("k1", "v1")),
KvPair::from(("k2", "v2")),
KvPair::from(("k3", "v3")),
];
client client
.batch_put(pairs.clone()) .batch_put(pairs)
.wait() .wait()
.expect("Could not put pairs"); .expect("Could not put pairs");
let keys = vec![Key::from(b"k1".to_vec()), Key::from(b"k2".to_vec())]; // Same thing when you want to retrieve multiple values.
let keys = vec![Key::from("k1"), Key::from("k2")];
let values = client let values = client
.batch_get(keys.clone()) .batch_get(keys.clone())
.wait() .wait()
.expect("Could not get values"); .expect("Could not get values");
println!("Found values: {:?} for keys: {:?}", values, keys); println!("Found values: {:?} for keys: {:?}", values, keys);
let start: Key = b"k1".to_vec().into(); // Scanning a range of keys is also possible giving it two bounds
let end: Key = b"k2".to_vec().into(); // it will returns all entries between these two.
client let start = "k1";
.scan(start.clone()..end.clone(), 10) let end = "k2";
let pairs = client
.scan(start..=end, 10)
.key_only() .key_only()
.wait() .wait()
.expect("Could not scan"); .expect("Could not scan");
let keys: Vec<_> = pairs.into_iter().map(|p| p.key().clone()).collect();
assert_eq!(&keys, &[Key::from("k1"), Key::from("k2")]);
println!("Scaning from {:?} to {:?} gives: {:?}", start, end, keys);
// Cleanly exit. // Cleanly exit.
Ok(()) Ok(())
} }