forked from soyking/e3ch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput.go
66 lines (59 loc) · 1.06 KB
/
put.go
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
package client
import (
"go.etcd.io/etcd/clientv3"
)
// set kv or directory
func (clt *EtcdHRCHYClient) Put(key, value string) error {
return clt.put(key, value, false)
}
// mustEmpty to confirm the key has not been set
func (clt *EtcdHRCHYClient) put(key string, value string, mustEmpty bool) error {
key, _, err := clt.ensureKey(key)
if err != nil {
return err
}
// parentKey should be directory
// key should not be directory
//cmp := []clientv3.Cmp{
// clientv3.Compare(
// clientv3.Value(parentKey),
// "=",
// clt.dirValue,
// ),
//}
var cmp []clientv3.Cmp
if mustEmpty {
cmp = append(
cmp,
clientv3.Compare(
clientv3.Version(key),
"=",
0,
),
)
} else {
cmp = append(
cmp,
clientv3.Compare(
clientv3.Value(key),
"!=",
clt.dirValue,
),
)
}
txn := clt.client.Txn(clt.ctx)
// make sure the parentKey is a directory
txn.If(
cmp...,
).Then(
clientv3.OpPut(key, value),
)
txnResp, err := txn.Commit()
if err != nil {
return err
}
if !txnResp.Succeeded {
return ErrorPutKey
}
return nil
}