kvstore.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package kvstore
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. // Backend represents a KV Store Backend
  7. type Backend string
  8. // BOLTDB backend
  9. const BOLTDB Backend = "boltdb"
  10. var (
  11. // ErrBackendNotSupported is thrown when the backend k/v store is not supported by libkv
  12. ErrBackendNotSupported = errors.New("Backend storage not supported yet, please choose one of")
  13. // ErrKeyModified is thrown during an atomic operation if the index does not match the one in the store
  14. ErrKeyModified = errors.New("Unable to complete atomic operation, key modified")
  15. // ErrKeyNotFound is thrown when the key is not found in the store during a Get operation
  16. ErrKeyNotFound = errors.New("Key not found in store")
  17. // ErrPreviousNotSpecified is thrown when the previous value is not specified for an atomic operation
  18. ErrPreviousNotSpecified = errors.New("Previous K/V pair should be provided for the Atomic operation")
  19. // ErrKeyExists is thrown when the previous value exists in the case of an AtomicPut
  20. ErrKeyExists = errors.New("Previous K/V pair exists, cannot complete Atomic operation")
  21. )
  22. // Config contains the options for a storage client
  23. type Config struct {
  24. ConnectionTimeout time.Duration
  25. Bucket string
  26. }
  27. // Store represents the backend K/V storage
  28. // Each store should support every call listed
  29. // here. Or it couldn't be implemented as a K/V
  30. // backend for libkv
  31. type Store interface {
  32. // Put a value at the specified key
  33. Put(key string, value []byte) error
  34. // Exists verifies if a Key exists in the store.
  35. Exists(key string) (bool, error)
  36. // List the content of a given prefix
  37. List(directory string) ([]*KVPair, error)
  38. // AtomicPut performs an atomic CAS operation on a single value.
  39. // Pass previous = nil to create a new key.
  40. AtomicPut(key string, value []byte, previous *KVPair) (*KVPair, error)
  41. // AtomicDelete performs an atomic delete of a single value.
  42. AtomicDelete(key string, previous *KVPair) error
  43. // Delete deletes a value at "key". Unlike AtomicDelete it doesn't check
  44. // whether the deleted key is at a specific version before deleting.
  45. Delete(key string) error
  46. // Close the store connection
  47. Close()
  48. }
  49. // KVPair represents {Key, Value, Lastindex} tuple
  50. type KVPair struct {
  51. Key string
  52. Value []byte
  53. LastIndex uint64
  54. }