kvstore.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. PersistConnection bool
  27. }
  28. // Store represents the backend K/V storage
  29. // Each store should support every call listed
  30. // here. Or it couldn't be implemented as a K/V
  31. // backend for libkv
  32. type Store interface {
  33. // Put a value at the specified key
  34. Put(key string, value []byte) error
  35. // Exists verifies if a Key exists in the store.
  36. Exists(key string) (bool, error)
  37. // List the content of a given prefix
  38. List(directory string) ([]*KVPair, error)
  39. // AtomicPut performs an atomic CAS operation on a single value.
  40. // Pass previous = nil to create a new key.
  41. AtomicPut(key string, value []byte, previous *KVPair) (*KVPair, error)
  42. // AtomicDelete performs an atomic delete of a single value.
  43. AtomicDelete(key string, previous *KVPair) error
  44. // Close the store connection
  45. Close()
  46. }
  47. // KVPair represents {Key, Value, Lastindex} tuple
  48. type KVPair struct {
  49. Key string
  50. Value []byte
  51. LastIndex uint64
  52. }