2016-05-13 06:50:33 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-09-15 23:54:11 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-04-25 22:32:58 +03:00
|
|
|
package mvcc
|
2015-05-15 06:25:52 +03:00
|
|
|
|
2015-06-08 19:26:56 +03:00
|
|
|
import (
|
2019-04-27 00:17:16 +03:00
|
|
|
"go.etcd.io/etcd/v3/lease"
|
|
|
|
"go.etcd.io/etcd/v3/mvcc/backend"
|
|
|
|
"go.etcd.io/etcd/v3/mvcc/mvccpb"
|
2015-06-08 19:26:56 +03:00
|
|
|
)
|
2015-05-15 06:25:52 +03:00
|
|
|
|
2016-06-22 02:20:55 +03:00
|
|
|
type RangeOptions struct {
|
|
|
|
Limit int64
|
|
|
|
Rev int64
|
|
|
|
Count bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type RangeResult struct {
|
|
|
|
KVs []mvccpb.KeyValue
|
|
|
|
Rev int64
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
type ReadView interface {
|
|
|
|
// FirstRev returns the first KV revision at the time of opening the txn.
|
2016-01-26 05:39:08 +03:00
|
|
|
// After a compaction, the first revision increases to the compaction
|
|
|
|
// revision.
|
|
|
|
FirstRev() int64
|
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
// Rev returns the revision of the KV at the time of opening the txn.
|
|
|
|
Rev() int64
|
|
|
|
|
2015-05-31 08:56:33 +03:00
|
|
|
// Range gets the keys in the range at rangeRev.
|
2016-01-31 03:51:12 +03:00
|
|
|
// The returned rev is the current revision of the KV when the operation is executed.
|
2015-05-31 08:56:33 +03:00
|
|
|
// If rangeRev <=0, range gets the keys at currentRev.
|
2015-05-21 22:03:58 +03:00
|
|
|
// If `end` is nil, the request returns the key.
|
2016-02-12 20:43:39 +03:00
|
|
|
// If `end` is not nil and not empty, it gets the keys in range [key, range_end).
|
|
|
|
// If `end` is not nil and empty, it gets the keys greater than or equal to key.
|
2015-05-21 22:03:58 +03:00
|
|
|
// Limit limits the number of keys returned.
|
2015-05-31 18:59:31 +03:00
|
|
|
// If the required rev is compacted, ErrCompacted will be returned.
|
2016-06-22 02:20:55 +03:00
|
|
|
Range(key, end []byte, ro RangeOptions) (r *RangeResult, err error)
|
2017-01-05 04:01:31 +03:00
|
|
|
}
|
2015-05-21 22:03:58 +03:00
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
// TxnRead represents a read-only transaction with operations that will not
|
|
|
|
// block other read transactions.
|
|
|
|
type TxnRead interface {
|
|
|
|
ReadView
|
|
|
|
// End marks the transaction is complete and ready to commit.
|
|
|
|
End()
|
|
|
|
}
|
2015-05-21 22:03:58 +03:00
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
type WriteView interface {
|
2015-05-21 22:03:58 +03:00
|
|
|
// DeleteRange deletes the given range from the store.
|
2015-05-31 08:56:33 +03:00
|
|
|
// A deleteRange increases the rev of the store if any key in the range exists.
|
2015-05-21 22:03:58 +03:00
|
|
|
// The number of key deleted will be returned.
|
2016-01-31 03:51:12 +03:00
|
|
|
// The returned rev is the current revision of the KV when the operation is executed.
|
2015-05-21 22:03:58 +03:00
|
|
|
// It also generates one event for each key delete in the event history.
|
|
|
|
// if the `end` is nil, deleteRange deletes the key.
|
|
|
|
// if the `end` is not nil, deleteRange deletes the keys in range [key, range_end).
|
2015-05-31 08:56:33 +03:00
|
|
|
DeleteRange(key, end []byte) (n, rev int64)
|
2015-05-21 22:03:58 +03:00
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
// Put puts the given key, value into the store. Put also takes additional argument lease to
|
|
|
|
// attach a lease to a key-value pair as meta-data. KV implementation does not validate the lease
|
|
|
|
// id.
|
|
|
|
// A put also increases the rev of the store, and generates one event in the event history.
|
|
|
|
// The returned rev is the current revision of the KV when the operation is executed.
|
|
|
|
Put(key, value []byte, lease lease.LeaseID) (rev int64)
|
|
|
|
}
|
2015-05-31 18:59:31 +03:00
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
// TxnWrite represents a transaction that can modify the store.
|
|
|
|
type TxnWrite interface {
|
|
|
|
TxnRead
|
|
|
|
WriteView
|
|
|
|
// Changes gets the changes made since opening the write txn.
|
|
|
|
Changes() []mvccpb.KeyValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// txnReadWrite coerces a read txn to a write, panicking on any write operation.
|
|
|
|
type txnReadWrite struct{ TxnRead }
|
|
|
|
|
|
|
|
func (trw *txnReadWrite) DeleteRange(key, end []byte) (n, rev int64) { panic("unexpected DeleteRange") }
|
|
|
|
func (trw *txnReadWrite) Put(key, value []byte, lease lease.LeaseID) (rev int64) {
|
|
|
|
panic("unexpected Put")
|
|
|
|
}
|
2017-06-09 06:14:23 +03:00
|
|
|
func (trw *txnReadWrite) Changes() []mvccpb.KeyValue { return nil }
|
|
|
|
|
|
|
|
func NewReadOnlyTxnWrite(txn TxnRead) TxnWrite { return &txnReadWrite{txn} }
|
2017-01-05 04:01:31 +03:00
|
|
|
|
|
|
|
type KV interface {
|
|
|
|
ReadView
|
|
|
|
WriteView
|
|
|
|
|
|
|
|
// Read creates a read transaction.
|
|
|
|
Read() TxnRead
|
|
|
|
|
|
|
|
// Write creates a write transaction.
|
|
|
|
Write() TxnWrite
|
2015-06-08 19:26:56 +03:00
|
|
|
|
2017-07-15 02:44:16 +03:00
|
|
|
// Hash computes the hash of the KV's backend.
|
2016-05-03 00:37:56 +03:00
|
|
|
Hash() (hash uint32, revision int64, err error)
|
2015-09-14 07:29:10 +03:00
|
|
|
|
2017-07-15 02:44:16 +03:00
|
|
|
// HashByRev computes the hash of all MVCC revisions up to a given revision.
|
|
|
|
HashByRev(rev int64) (hash uint32, revision int64, compactRev int64, err error)
|
|
|
|
|
2017-01-05 04:01:31 +03:00
|
|
|
// Compact frees all superseded keys with revisions less than rev.
|
|
|
|
Compact(rev int64) (<-chan struct{}, error)
|
|
|
|
|
|
|
|
// Commit commits outstanding txns into the underlying backend.
|
2015-10-27 07:06:08 +03:00
|
|
|
Commit()
|
|
|
|
|
2016-01-07 03:25:25 +03:00
|
|
|
// Restore restores the KV store from a backend.
|
|
|
|
Restore(b backend.Backend) error
|
2015-06-08 19:26:56 +03:00
|
|
|
Close() error
|
2015-05-21 22:03:58 +03:00
|
|
|
}
|
2015-09-09 19:49:28 +03:00
|
|
|
|
|
|
|
// WatchableKV is a KV that can be watched.
|
|
|
|
type WatchableKV interface {
|
|
|
|
KV
|
2015-11-04 02:57:35 +03:00
|
|
|
Watchable
|
|
|
|
}
|
2015-09-09 19:49:28 +03:00
|
|
|
|
2016-01-03 02:59:32 +03:00
|
|
|
// Watchable is the interface that wraps the NewWatchStream function.
|
2015-11-04 02:57:35 +03:00
|
|
|
type Watchable interface {
|
2016-01-03 02:59:32 +03:00
|
|
|
// NewWatchStream returns a WatchStream that can be used to
|
2016-01-08 11:21:19 +03:00
|
|
|
// watch events happened or happening on the KV.
|
2016-01-03 02:59:32 +03:00
|
|
|
NewWatchStream() WatchStream
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
2015-10-21 21:08:19 +03:00
|
|
|
|
|
|
|
// ConsistentWatchableKV is a WatchableKV that understands the consistency
|
|
|
|
// algorithm and consistent index.
|
2015-10-27 07:42:49 +03:00
|
|
|
// If the consistent index of executing entry is not larger than the
|
|
|
|
// consistent index of ConsistentWatchableKV, all operations in
|
|
|
|
// this entry are skipped and return empty response.
|
2015-10-21 21:08:19 +03:00
|
|
|
type ConsistentWatchableKV interface {
|
|
|
|
WatchableKV
|
2016-03-30 21:37:55 +03:00
|
|
|
// ConsistentIndex returns the current consistent index of the KV.
|
|
|
|
ConsistentIndex() uint64
|
2015-10-21 21:08:19 +03:00
|
|
|
}
|