forked from etcdv3/etcd-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv.rs
117 lines (103 loc) · 2.91 KB
/
kv.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! KV example.
use etcd_client::*;
#[derive(Debug)]
struct KV {
key: String,
value: String,
}
impl KV {
#[inline]
pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
KV {
key: key.into(),
value: value.into(),
}
}
#[inline]
pub fn key(&self) -> &str {
&self.key
}
#[inline]
pub fn value(&self) -> &str {
&self.value
}
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut client = Client::connect(["localhost:2379"], None).await?;
let alice = KV::new("Alice", "15");
let bob = KV::new("Bob", "20");
let chris = KV::new("Chris", "16");
// put kv
let resp = client.put(alice.key(), alice.value(), None).await?;
println!("put kv: {:?}", alice);
let revision = resp.header().unwrap().revision();
client.put(bob.key(), bob.value(), None).await?;
println!("put kv: {:?}", bob);
client.put(chris.key(), chris.value(), None).await?;
println!("put kv: {:?}", chris);
println!();
// get kv
let resp = client.get(bob.key(), None).await?;
if let Some(kv) = resp.kvs().first() {
println!("Get kv: {{{}: {}}}", kv.key_str()?, kv.value_str()?);
println!();
}
// get all kv pairs
println!("Get all users:");
let resp = client
.get("", Some(GetOptions::new().with_all_keys()))
.await?;
for kv in resp.kvs() {
println!("\t{{{}: {}}}", kv.key_str()?, kv.value_str()?);
}
println!();
// delete kv
let resp = client
.delete(chris.key(), Some(DeleteOptions::new().with_prev_key()))
.await?;
if let Some(kv) = resp.prev_kvs().first() {
println!("Delete kv: {{{}: {}}}", kv.key_str()?, kv.value_str()?);
println!();
}
// transaction
let resp = client.get(alice.key(), None).await?;
if let Some(kv) = resp.kvs().first() {
println!(
"Before transaction: {{{}: {}}}",
kv.key_str()?,
kv.value_str()?
);
}
let txn = Txn::new()
.when(vec![Compare::value(
alice.key(),
CompareOp::Equal,
alice.value(),
)])
.and_then(vec![TxnOp::put(
alice.key(),
"18",
Some(PutOptions::new().with_ignore_lease()),
)]);
println!("transaction: {:?}", txn);
let resp = client.txn(txn).await?;
for op_resp in resp.op_responses() {
println!("transaction resp: {:?}", op_resp);
}
let resp = client.get(alice.key(), None).await?;
if let Some(kv) = resp.kvs().first() {
println!(
"After transaction: {{{}: {}}}",
kv.key_str()?,
kv.value_str()?
);
}
println!();
// compact
client
.compact(revision, Some(CompactionOptions::new().with_physical()))
.await?;
println!("Compact to revision {}.", revision);
Ok(())
}