kv.go 3.5 KB

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