util.go 2.6 KB

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