...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package leasing
16
17 import (
18 "bytes"
19
20 v3pb "go.etcd.io/etcd/api/v3/etcdserverpb"
21 v3 "go.etcd.io/etcd/client/v3"
22 )
23
24 func compareInt64(a, b int64) int {
25 switch {
26 case a < b:
27 return -1
28 case a > b:
29 return 1
30 default:
31 return 0
32 }
33 }
34
35 func evalCmp(resp *v3.GetResponse, tcmp v3.Cmp) bool {
36 var result int
37 if len(resp.Kvs) != 0 {
38 kv := resp.Kvs[0]
39 switch tcmp.Target {
40 case v3pb.Compare_VALUE:
41 if tv, _ := tcmp.TargetUnion.(*v3pb.Compare_Value); tv != nil {
42 result = bytes.Compare(kv.Value, tv.Value)
43 }
44 case v3pb.Compare_CREATE:
45 if tv, _ := tcmp.TargetUnion.(*v3pb.Compare_CreateRevision); tv != nil {
46 result = compareInt64(kv.CreateRevision, tv.CreateRevision)
47 }
48 case v3pb.Compare_MOD:
49 if tv, _ := tcmp.TargetUnion.(*v3pb.Compare_ModRevision); tv != nil {
50 result = compareInt64(kv.ModRevision, tv.ModRevision)
51 }
52 case v3pb.Compare_VERSION:
53 if tv, _ := tcmp.TargetUnion.(*v3pb.Compare_Version); tv != nil {
54 result = compareInt64(kv.Version, tv.Version)
55 }
56 }
57 }
58 switch tcmp.Result {
59 case v3pb.Compare_EQUAL:
60 return result == 0
61 case v3pb.Compare_NOT_EQUAL:
62 return result != 0
63 case v3pb.Compare_GREATER:
64 return result > 0
65 case v3pb.Compare_LESS:
66 return result < 0
67 }
68 return true
69 }
70
71 func gatherOps(ops []v3.Op) (ret []v3.Op) {
72 for _, op := range ops {
73 if !op.IsTxn() {
74 ret = append(ret, op)
75 continue
76 }
77 _, thenOps, elseOps := op.Txn()
78 ret = append(ret, gatherOps(append(thenOps, elseOps...))...)
79 }
80 return ret
81 }
82
83 func gatherResponseOps(resp []*v3pb.ResponseOp, ops []v3.Op) (ret []v3.Op) {
84 for i, op := range ops {
85 if !op.IsTxn() {
86 ret = append(ret, op)
87 continue
88 }
89 _, thenOps, elseOps := op.Txn()
90 if txnResp := resp[i].GetResponseTxn(); txnResp.Succeeded {
91 ret = append(ret, gatherResponseOps(txnResp.Responses, thenOps)...)
92 } else {
93 ret = append(ret, gatherResponseOps(txnResp.Responses, elseOps)...)
94 }
95 }
96 return ret
97 }
98
99 func copyHeader(hdr *v3pb.ResponseHeader) *v3pb.ResponseHeader {
100 h := *hdr
101 return &h
102 }
103
104 func closeAll(chs []chan<- struct{}) {
105 for _, ch := range chs {
106 close(ch)
107 }
108 }
109
View as plain text