kv.go 5.1 KB

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