go.etcd.io/etcd@v3.3.27+incompatible/clientv3/leasing/util.go (about) 1 // Copyright 2017 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package leasing 16 17 import ( 18 "bytes" 19 20 v3 "github.com/coreos/etcd/clientv3" 21 v3pb "github.com/coreos/etcd/etcdserver/etcdserverpb" 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 }