2015-06-12 20:51:57 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Backend represents a KV Store Backend
|
|
|
|
type Backend string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CONSUL backend
|
2015-07-06 07:14:20 +00:00
|
|
|
CONSUL Backend = "consul"
|
2015-06-12 20:51:57 +00:00
|
|
|
// ETCD backend
|
2015-07-06 07:14:20 +00:00
|
|
|
ETCD Backend = "etcd"
|
2015-06-12 20:51:57 +00:00
|
|
|
// ZK backend
|
2015-07-06 07:14:20 +00:00
|
|
|
ZK Backend = "zk"
|
2015-09-16 18:54:28 +00:00
|
|
|
// BOLTDB backend
|
|
|
|
BOLTDB Backend = "boltdb"
|
2015-06-12 20:51:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-10-13 18:33:47 +00:00
|
|
|
// ErrBackendNotSupported is thrown when the backend k/v store is not supported by libkv
|
|
|
|
ErrBackendNotSupported = errors.New("Backend storage not supported yet, please choose one of")
|
|
|
|
// ErrCallNotSupported is thrown when a method is not implemented/supported by the current backend
|
|
|
|
ErrCallNotSupported = errors.New("The current call is not supported with this backend")
|
2015-06-12 20:51:57 +00:00
|
|
|
// ErrNotReachable is thrown when the API cannot be reached for issuing common store operations
|
|
|
|
ErrNotReachable = errors.New("Api not reachable")
|
|
|
|
// ErrCannotLock is thrown when there is an error acquiring a lock on a key
|
|
|
|
ErrCannotLock = errors.New("Error acquiring the lock")
|
|
|
|
// ErrKeyModified is thrown during an atomic operation if the index does not match the one in the store
|
|
|
|
ErrKeyModified = errors.New("Unable to complete atomic operation, key modified")
|
|
|
|
// ErrKeyNotFound is thrown when the key is not found in the store during a Get operation
|
|
|
|
ErrKeyNotFound = errors.New("Key not found in store")
|
|
|
|
// ErrPreviousNotSpecified is thrown when the previous value is not specified for an atomic operation
|
|
|
|
ErrPreviousNotSpecified = errors.New("Previous K/V pair should be provided for the Atomic operation")
|
2016-05-08 07:31:30 +00:00
|
|
|
// ErrKeyExists is thrown when the previous value exists in the case of an AtomicPut
|
|
|
|
ErrKeyExists = errors.New("Previous K/V pair exists, cannot complete Atomic operation")
|
2015-06-12 20:51:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains the options for a storage client
|
|
|
|
type Config struct {
|
2015-09-25 17:02:31 +00:00
|
|
|
ClientTLS *ClientTLSConfig
|
2015-06-12 20:51:57 +00:00
|
|
|
TLS *tls.Config
|
|
|
|
ConnectionTimeout time.Duration
|
2015-09-16 18:54:28 +00:00
|
|
|
Bucket string
|
2015-10-05 03:02:02 +00:00
|
|
|
PersistConnection bool
|
2016-05-08 07:31:30 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
2015-06-12 20:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
// ClientTLSConfig contains data for a Client TLS configuration in the form
|
2015-10-13 18:33:47 +00:00
|
|
|
// the etcd client wants it. Eventually we'll adapt it for ZK and Consul.
|
2015-09-25 17:02:31 +00:00
|
|
|
type ClientTLSConfig struct {
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
CACertFile string
|
|
|
|
}
|
|
|
|
|
2015-06-12 20:51:57 +00:00
|
|
|
// Store represents the backend K/V storage
|
|
|
|
// Each store should support every call listed
|
|
|
|
// here. Or it couldn't be implemented as a K/V
|
|
|
|
// backend for libkv
|
|
|
|
type Store interface {
|
|
|
|
// Put a value at the specified key
|
|
|
|
Put(key string, value []byte, options *WriteOptions) error
|
|
|
|
|
|
|
|
// Get a value given its key
|
|
|
|
Get(key string) (*KVPair, error)
|
|
|
|
|
|
|
|
// Delete the value at the specified key
|
|
|
|
Delete(key string) error
|
|
|
|
|
|
|
|
// Verify if a Key exists in the store
|
|
|
|
Exists(key string) (bool, error)
|
|
|
|
|
|
|
|
// Watch for changes on a key
|
|
|
|
Watch(key string, stopCh <-chan struct{}) (<-chan *KVPair, error)
|
|
|
|
|
|
|
|
// WatchTree watches for changes on child nodes under
|
2015-09-16 18:54:28 +00:00
|
|
|
// a given directory
|
2015-06-12 20:51:57 +00:00
|
|
|
WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*KVPair, error)
|
|
|
|
|
2015-09-16 18:54:28 +00:00
|
|
|
// NewLock creates a lock for a given key.
|
2015-06-12 20:51:57 +00:00
|
|
|
// The returned Locker is not held and must be acquired
|
|
|
|
// with `.Lock`. The Value is optional.
|
|
|
|
NewLock(key string, options *LockOptions) (Locker, error)
|
|
|
|
|
|
|
|
// List the content of a given prefix
|
|
|
|
List(directory string) ([]*KVPair, error)
|
|
|
|
|
|
|
|
// DeleteTree deletes a range of keys under a given directory
|
|
|
|
DeleteTree(directory string) error
|
|
|
|
|
2015-07-06 07:14:20 +00:00
|
|
|
// Atomic CAS operation on a single value.
|
|
|
|
// Pass previous = nil to create a new key.
|
2015-06-12 20:51:57 +00:00
|
|
|
AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error)
|
|
|
|
|
|
|
|
// Atomic delete of a single value
|
|
|
|
AtomicDelete(key string, previous *KVPair) (bool, error)
|
|
|
|
|
|
|
|
// Close the store connection
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVPair represents {Key, Value, Lastindex} tuple
|
|
|
|
type KVPair struct {
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
LastIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOptions contains optional request parameters
|
|
|
|
type WriteOptions struct {
|
2015-10-13 18:33:47 +00:00
|
|
|
IsDir bool
|
|
|
|
TTL time.Duration
|
2015-06-12 20:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockOptions contains optional request parameters
|
|
|
|
type LockOptions struct {
|
2015-10-05 03:02:02 +00:00
|
|
|
Value []byte // Optional, value to associate with the lock
|
|
|
|
TTL time.Duration // Optional, expiration ttl associated with the lock
|
|
|
|
RenewLock chan struct{} // Optional, chan used to control and stop the session ttl renewal for the lock
|
2015-06-12 20:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locker provides locking mechanism on top of the store.
|
|
|
|
// Similar to `sync.Lock` except it may return errors.
|
|
|
|
type Locker interface {
|
2015-10-05 03:02:02 +00:00
|
|
|
Lock(stopChan chan struct{}) (<-chan struct{}, error)
|
2015-06-12 20:51:57 +00:00
|
|
|
Unlock() error
|
|
|
|
}
|