kv.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/storage/backend"
  17. "github.com/coreos/etcd/storage/storagepb"
  18. )
  19. // CancelFunc tells an operation to abandon its work. A CancelFunc does not
  20. // wait for the work to stop.
  21. type CancelFunc func()
  22. type Snapshot backend.Snapshot
  23. type KV interface {
  24. // Rev returns the current revision of the KV.
  25. Rev() int64
  26. // Range gets the keys in the range at rangeRev.
  27. // If rangeRev <=0, range gets the keys at currentRev.
  28. // If `end` is nil, the request returns the key.
  29. // If `end` is not nil, it gets the keys in range [key, range_end).
  30. // Limit limits the number of keys returned.
  31. // If the required rev is compacted, ErrCompacted will be returned.
  32. Range(key, end []byte, limit, rangeRev int64) (kvs []storagepb.KeyValue, rev int64, err error)
  33. // Put puts the given key,value into the store.
  34. // A put also increases the rev of the store, and generates one event in the event history.
  35. Put(key, value []byte) (rev int64)
  36. // DeleteRange deletes the given range from the store.
  37. // A deleteRange increases the rev of the store if any key in the range exists.
  38. // The number of key deleted will be returned.
  39. // It also generates one event for each key delete in the event history.
  40. // if the `end` is nil, deleteRange deletes the key.
  41. // if the `end` is not nil, deleteRange deletes the keys in range [key, range_end).
  42. DeleteRange(key, end []byte) (n, rev int64)
  43. // TxnBegin begins a txn. Only Txn prefixed operation can be executed, others will be blocked
  44. // until txn ends. Only one on-going txn is allowed.
  45. // TxnBegin returns an int64 txn ID.
  46. // All txn prefixed operations with same txn ID will be done with the same rev.
  47. TxnBegin() int64
  48. // TxnEnd ends the on-going txn with txn ID. If the on-going txn ID is not matched, error is returned.
  49. TxnEnd(txnID int64) error
  50. TxnRange(txnID int64, key, end []byte, limit, rangeRev int64) (kvs []storagepb.KeyValue, rev int64, err error)
  51. TxnPut(txnID int64, key, value []byte) (rev int64, err error)
  52. TxnDeleteRange(txnID int64, key, end []byte) (n, rev int64, err error)
  53. Compact(rev int64) error
  54. // Get the hash of KV state.
  55. // This method is designed for consistency checking purpose.
  56. Hash() (uint32, error)
  57. // Snapshot snapshots the full KV store.
  58. Snapshot() Snapshot
  59. Restore() error
  60. Close() error
  61. }
  62. // Watcher watches on the KV. It will be notified if there is an event
  63. // happened on the watched key or prefix.
  64. type Watcher interface {
  65. // Event returns a channel that receives observed event that matches the
  66. // context of watcher. When watch finishes or is canceled or aborted, the
  67. // channel is closed and returns empty event.
  68. // Successive calls to Event return the same value.
  69. Event() <-chan storagepb.Event
  70. // Err returns a non-nil error value after Event is closed. Err returns
  71. // Compacted if the history was compacted, Canceled if watch is canceled,
  72. // or EOF if watch reaches the end revision. No other values for Err are defined.
  73. // After Event is closed, successive calls to Err return the same value.
  74. Err() error
  75. }
  76. // WatchableKV is a KV that can be watched.
  77. type WatchableKV interface {
  78. KV
  79. // Watcher watches the events happening or happened in etcd. The whole
  80. // event history can be watched unless compacted.
  81. // If `prefix` is true, watch observes all events whose key prefix could be the given `key`.
  82. // If `startRev` <=0, watch observes events after currentRev.
  83. // If `endRev` <=0, watch observes events until watch is cancelled.
  84. //
  85. // Canceling the watcher releases resources associated with it, so code
  86. // should always call cancel as soon as watch is done.
  87. Watcher(key []byte, prefix bool, startRev, endRev int64) (Watcher, CancelFunc)
  88. }