go.etcd.io/etcd@v3.3.27+incompatible/mvcc/kv.go (about) 1 // Copyright 2015 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 mvcc 16 17 import ( 18 "github.com/coreos/etcd/lease" 19 "github.com/coreos/etcd/mvcc/backend" 20 "github.com/coreos/etcd/mvcc/mvccpb" 21 ) 22 23 type RangeOptions struct { 24 Limit int64 25 Rev int64 26 Count bool 27 } 28 29 type RangeResult struct { 30 KVs []mvccpb.KeyValue 31 Rev int64 32 Count int 33 } 34 35 type ReadView interface { 36 // FirstRev returns the first KV revision at the time of opening the txn. 37 // After a compaction, the first revision increases to the compaction 38 // revision. 39 FirstRev() int64 40 41 // Rev returns the revision of the KV at the time of opening the txn. 42 Rev() int64 43 44 // Range gets the keys in the range at rangeRev. 45 // The returned rev is the current revision of the KV when the operation is executed. 46 // If rangeRev <=0, range gets the keys at currentRev. 47 // If `end` is nil, the request returns the key. 48 // If `end` is not nil and not empty, it gets the keys in range [key, range_end). 49 // If `end` is not nil and empty, it gets the keys greater than or equal to key. 50 // Limit limits the number of keys returned. 51 // If the required rev is compacted, ErrCompacted will be returned. 52 Range(key, end []byte, ro RangeOptions) (r *RangeResult, err error) 53 } 54 55 // TxnRead represents a read-only transaction with operations that will not 56 // block other read transactions. 57 type TxnRead interface { 58 ReadView 59 // End marks the transaction is complete and ready to commit. 60 End() 61 } 62 63 type WriteView interface { 64 // DeleteRange deletes the given range from the store. 65 // A deleteRange increases the rev of the store if any key in the range exists. 66 // The number of key deleted will be returned. 67 // The returned rev is the current revision of the KV when the operation is executed. 68 // It also generates one event for each key delete in the event history. 69 // if the `end` is nil, deleteRange deletes the key. 70 // if the `end` is not nil, deleteRange deletes the keys in range [key, range_end). 71 DeleteRange(key, end []byte) (n, rev int64) 72 73 // Put puts the given key, value into the store. Put also takes additional argument lease to 74 // attach a lease to a key-value pair as meta-data. KV implementation does not validate the lease 75 // id. 76 // A put also increases the rev of the store, and generates one event in the event history. 77 // The returned rev is the current revision of the KV when the operation is executed. 78 Put(key, value []byte, lease lease.LeaseID) (rev int64) 79 } 80 81 // TxnWrite represents a transaction that can modify the store. 82 type TxnWrite interface { 83 TxnRead 84 WriteView 85 // Changes gets the changes made since opening the write txn. 86 Changes() []mvccpb.KeyValue 87 } 88 89 // txnReadWrite coerces a read txn to a write, panicking on any write operation. 90 type txnReadWrite struct{ TxnRead } 91 92 func (trw *txnReadWrite) DeleteRange(key, end []byte) (n, rev int64) { panic("unexpected DeleteRange") } 93 func (trw *txnReadWrite) Put(key, value []byte, lease lease.LeaseID) (rev int64) { 94 panic("unexpected Put") 95 } 96 func (trw *txnReadWrite) Changes() []mvccpb.KeyValue { return nil } 97 98 func NewReadOnlyTxnWrite(txn TxnRead) TxnWrite { return &txnReadWrite{txn} } 99 100 type KV interface { 101 ReadView 102 WriteView 103 104 // Read creates a read transaction. 105 Read() TxnRead 106 107 // Write creates a write transaction. 108 Write() TxnWrite 109 110 // Hash computes the hash of the KV's backend. 111 Hash() (hash uint32, revision int64, err error) 112 113 // HashByRev computes the hash of all MVCC revisions up to a given revision. 114 HashByRev(rev int64) (hash uint32, revision int64, compactRev int64, err error) 115 116 // Compact frees all superseded keys with revisions less than rev. 117 Compact(rev int64) (<-chan struct{}, error) 118 119 // Commit commits outstanding txns into the underlying backend. 120 Commit() 121 122 // Restore restores the KV store from a backend. 123 Restore(b backend.Backend) error 124 Close() error 125 } 126 127 // WatchableKV is a KV that can be watched. 128 type WatchableKV interface { 129 KV 130 Watchable 131 } 132 133 // Watchable is the interface that wraps the NewWatchStream function. 134 type Watchable interface { 135 // NewWatchStream returns a WatchStream that can be used to 136 // watch events happened or happening on the KV. 137 NewWatchStream() WatchStream 138 } 139 140 // ConsistentWatchableKV is a WatchableKV that understands the consistency 141 // algorithm and consistent index. 142 // If the consistent index of executing entry is not larger than the 143 // consistent index of ConsistentWatchableKV, all operations in 144 // this entry are skipped and return empty response. 145 type ConsistentWatchableKV interface { 146 WatchableKV 147 // ConsistentIndex returns the current consistent index of the KV. 148 ConsistentIndex() uint64 149 }