compare.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2016 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 clientv3
  15. import (
  16. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  17. )
  18. type CompareTarget int
  19. type CompareResult int
  20. const (
  21. CompareVersion CompareTarget = iota
  22. CompareCreated
  23. CompareModified
  24. CompareValue
  25. )
  26. type Cmp pb.Compare
  27. func Compare(cmp Cmp, result string, v interface{}) Cmp {
  28. var r pb.Compare_CompareResult
  29. switch result {
  30. case "=":
  31. r = pb.Compare_EQUAL
  32. case "!=":
  33. r = pb.Compare_NOT_EQUAL
  34. case ">":
  35. r = pb.Compare_GREATER
  36. case "<":
  37. r = pb.Compare_LESS
  38. default:
  39. panic("Unknown result op")
  40. }
  41. cmp.Result = r
  42. switch cmp.Target {
  43. case pb.Compare_VALUE:
  44. val, ok := v.(string)
  45. if !ok {
  46. panic("bad compare value")
  47. }
  48. cmp.TargetUnion = &pb.Compare_Value{Value: []byte(val)}
  49. case pb.Compare_VERSION:
  50. cmp.TargetUnion = &pb.Compare_Version{Version: mustInt64(v)}
  51. case pb.Compare_CREATE:
  52. cmp.TargetUnion = &pb.Compare_CreateRevision{CreateRevision: mustInt64(v)}
  53. case pb.Compare_MOD:
  54. cmp.TargetUnion = &pb.Compare_ModRevision{ModRevision: mustInt64(v)}
  55. case pb.Compare_LEASE:
  56. cmp.TargetUnion = &pb.Compare_Lease{Lease: mustInt64orLeaseID(v)}
  57. default:
  58. panic("Unknown compare type")
  59. }
  60. return cmp
  61. }
  62. func Value(key string) Cmp {
  63. return Cmp{Key: []byte(key), Target: pb.Compare_VALUE}
  64. }
  65. func Version(key string) Cmp {
  66. return Cmp{Key: []byte(key), Target: pb.Compare_VERSION}
  67. }
  68. func CreateRevision(key string) Cmp {
  69. return Cmp{Key: []byte(key), Target: pb.Compare_CREATE}
  70. }
  71. func ModRevision(key string) Cmp {
  72. return Cmp{Key: []byte(key), Target: pb.Compare_MOD}
  73. }
  74. // LeaseValue compares a key's LeaseID to a value of your choosing. The empty
  75. // LeaseID is 0, otherwise known as `NoLease`.
  76. func LeaseValue(key string) Cmp {
  77. return Cmp{Key: []byte(key), Target: pb.Compare_LEASE}
  78. }
  79. // KeyBytes returns the byte slice holding with the comparison key.
  80. func (cmp *Cmp) KeyBytes() []byte { return cmp.Key }
  81. // WithKeyBytes sets the byte slice for the comparison key.
  82. func (cmp *Cmp) WithKeyBytes(key []byte) { cmp.Key = key }
  83. // ValueBytes returns the byte slice holding the comparison value, if any.
  84. func (cmp *Cmp) ValueBytes() []byte {
  85. if tu, ok := cmp.TargetUnion.(*pb.Compare_Value); ok {
  86. return tu.Value
  87. }
  88. return nil
  89. }
  90. // WithValueBytes sets the byte slice for the comparison's value.
  91. func (cmp *Cmp) WithValueBytes(v []byte) { cmp.TargetUnion.(*pb.Compare_Value).Value = v }
  92. // WithRange sets the comparison to scan the range [key, end).
  93. func (cmp Cmp) WithRange(end string) Cmp {
  94. cmp.RangeEnd = []byte(end)
  95. return cmp
  96. }
  97. // WithPrefix sets the comparison to scan all keys prefixed by the key.
  98. func (cmp Cmp) WithPrefix() Cmp {
  99. cmp.RangeEnd = getPrefix(cmp.Key)
  100. return cmp
  101. }
  102. // mustInt64 panics if val isn't an int or int64. It returns an int64 otherwise.
  103. func mustInt64(val interface{}) int64 {
  104. if v, ok := val.(int64); ok {
  105. return v
  106. }
  107. if v, ok := val.(int); ok {
  108. return int64(v)
  109. }
  110. panic("bad value")
  111. }
  112. // mustInt64orLeaseID panics if val isn't a LeaseID, int or int64. It returns an
  113. // int64 otherwise.
  114. func mustInt64orLeaseID(val interface{}) int64 {
  115. if v, ok := val.(LeaseID); ok {
  116. return int64(v)
  117. }
  118. return mustInt64(val)
  119. }