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