kv.go 3.6 KB

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