kv.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "github.com/coreos/etcd/lease"
  17. "github.com/coreos/etcd/mvcc/backend"
  18. "github.com/coreos/etcd/mvcc/mvccpb"
  19. )
  20. type KV interface {
  21. // Rev returns the current revision of the KV.
  22. Rev() int64
  23. // FirstRev returns the first revision of the KV.
  24. // After a compaction, the first revision increases to the compaction
  25. // revision.
  26. FirstRev() int64
  27. // Range gets the keys in the range at rangeRev.
  28. // The returned rev is the current revision of the KV when the operation is executed.
  29. // If rangeRev <=0, range gets the keys at currentRev.
  30. // If `end` is nil, the request returns the key.
  31. // If `end` is not nil and not empty, it gets the keys in range [key, range_end).
  32. // If `end` is not nil and empty, it gets the keys greater than or equal to key.
  33. // Limit limits the number of keys returned.
  34. // If the required rev is compacted, ErrCompacted will be returned.
  35. Range(key, end []byte, limit, rangeRev int64) (kvs []mvccpb.KeyValue, rev int64, err error)
  36. // Put puts the given key, value into the store. Put also takes additional argument lease to
  37. // attach a lease to a key-value pair as meta-data. KV implementation does not validate the lease
  38. // id.
  39. // A put also increases the rev of the store, and generates one event in the event history.
  40. // The returned rev is the current revision of the KV when the operation is executed.
  41. Put(key, value []byte, lease lease.LeaseID) (rev int64)
  42. // DeleteRange deletes the given range from the store.
  43. // A deleteRange increases the rev of the store if any key in the range exists.
  44. // The number of key deleted will be returned.
  45. // The returned rev is the current revision of the KV when the operation is executed.
  46. // It also generates one event for each key delete in the event history.
  47. // if the `end` is nil, deleteRange deletes the key.
  48. // if the `end` is not nil, deleteRange deletes the keys in range [key, range_end).
  49. DeleteRange(key, end []byte) (n, rev int64)
  50. // TxnBegin begins a txn. Only Txn prefixed operation can be executed, others will be blocked
  51. // until txn ends. Only one on-going txn is allowed.
  52. // TxnBegin returns an int64 txn ID.
  53. // All txn prefixed operations with same txn ID will be done with the same rev.
  54. TxnBegin() int64
  55. // TxnEnd ends the on-going txn with txn ID. If the on-going txn ID is not matched, error is returned.
  56. TxnEnd(txnID int64) error
  57. // TxnRange returns the current revision of the KV when the operation is executed.
  58. TxnRange(txnID int64, key, end []byte, limit, rangeRev int64) (kvs []mvccpb.KeyValue, rev int64, err error)
  59. TxnPut(txnID int64, key, value []byte, lease lease.LeaseID) (rev int64, err error)
  60. TxnDeleteRange(txnID int64, key, end []byte) (n, rev int64, err error)
  61. // Compact frees all superseded keys with revisions less than rev.
  62. Compact(rev int64) (<-chan struct{}, error)
  63. // Hash retrieves the hash of KV state and revision.
  64. // This method is designed for consistency checking purpose.
  65. Hash() (hash uint32, revision int64, err error)
  66. // Commit commits txns into the underlying backend.
  67. Commit()
  68. // Restore restores the KV store from a backend.
  69. Restore(b backend.Backend) error
  70. Close() error
  71. }
  72. // WatchableKV is a KV that can be watched.
  73. type WatchableKV interface {
  74. KV
  75. Watchable
  76. }
  77. // Watchable is the interface that wraps the NewWatchStream function.
  78. type Watchable interface {
  79. // NewWatchStream returns a WatchStream that can be used to
  80. // watch events happened or happening on the KV.
  81. NewWatchStream() WatchStream
  82. }
  83. // ConsistentWatchableKV is a WatchableKV that understands the consistency
  84. // algorithm and consistent index.
  85. // If the consistent index of executing entry is not larger than the
  86. // consistent index of ConsistentWatchableKV, all operations in
  87. // this entry are skipped and return empty response.
  88. type ConsistentWatchableKV interface {
  89. WatchableKV
  90. // ConsistentIndex returns the current consistent index of the KV.
  91. ConsistentIndex() uint64
  92. }