kv.go 3.6 KB

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