2015-05-08 09:26:35 -04:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2015-10-05 07:18:10 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-06-05 16:31:12 -04:00
|
|
|
"reflect"
|
2015-05-08 09:26:35 -04:00
|
|
|
"strings"
|
2015-10-11 21:51:10 -04:00
|
|
|
"sync"
|
2015-05-08 09:26:35 -04:00
|
|
|
|
2015-06-11 08:32:15 -04:00
|
|
|
"github.com/docker/libkv"
|
|
|
|
"github.com/docker/libkv/store"
|
2015-09-16 07:39:46 -04:00
|
|
|
"github.com/docker/libkv/store/boltdb"
|
2015-09-16 06:15:56 -04:00
|
|
|
"github.com/docker/libkv/store/consul"
|
|
|
|
"github.com/docker/libkv/store/etcd"
|
|
|
|
"github.com/docker/libkv/store/zookeeper"
|
2015-06-05 16:31:12 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-08 09:26:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
//DataStore exported
|
|
|
|
type DataStore interface {
|
2015-06-01 00:19:10 -04:00
|
|
|
// GetObject gets data from datastore and unmarshals to the specified object
|
2015-10-05 07:18:10 -04:00
|
|
|
GetObject(key string, o KVObject) error
|
2015-05-08 09:26:35 -04:00
|
|
|
// PutObject adds a new Record based on an object into the datastore
|
2015-10-05 07:18:10 -04:00
|
|
|
PutObject(kvObject KVObject) error
|
2015-05-08 09:26:35 -04:00
|
|
|
// PutObjectAtomic provides an atomic add and update operation for a Record
|
2015-10-05 07:18:10 -04:00
|
|
|
PutObjectAtomic(kvObject KVObject) error
|
2015-06-01 12:43:24 -04:00
|
|
|
// DeleteObject deletes a record
|
2015-10-05 07:18:10 -04:00
|
|
|
DeleteObject(kvObject KVObject) error
|
2015-06-01 12:43:24 -04:00
|
|
|
// DeleteObjectAtomic performs an atomic delete operation
|
2015-10-05 07:18:10 -04:00
|
|
|
DeleteObjectAtomic(kvObject KVObject) error
|
2015-06-01 12:43:24 -04:00
|
|
|
// DeleteTree deletes a record
|
2015-10-05 07:18:10 -04:00
|
|
|
DeleteTree(kvObject KVObject) error
|
|
|
|
// Watchable returns whether the store is watchable are not
|
|
|
|
Watchable() bool
|
|
|
|
// Watch for changes on a KVObject
|
|
|
|
Watch(kvObject KVObject, stopCh <-chan struct{}) (<-chan KVObject, error)
|
2015-10-22 00:31:43 -04:00
|
|
|
// RestartWatch retriggers stopped Watches
|
|
|
|
RestartWatch()
|
|
|
|
// Active returns if the store is active
|
|
|
|
Active() bool
|
2015-10-05 07:18:10 -04:00
|
|
|
// List returns of a list of KVObjects belonging to the parent
|
|
|
|
// key. The caller must pass a KVObject of the same type as
|
|
|
|
// the objects that need to be listed
|
|
|
|
List(string, KVObject) ([]KVObject, error)
|
|
|
|
// Scope returns the scope of the store
|
|
|
|
Scope() string
|
2015-05-08 09:26:35 -04:00
|
|
|
// KVStore returns access to the KV Store
|
|
|
|
KVStore() store.Store
|
2015-10-05 07:18:10 -04:00
|
|
|
// Close closes the data store
|
|
|
|
Close()
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-06-11 08:32:15 -04:00
|
|
|
// ErrKeyModified is raised for an atomic update when the update is working on a stale state
|
2015-06-17 12:13:31 -04:00
|
|
|
var (
|
|
|
|
ErrKeyModified = store.ErrKeyModified
|
|
|
|
ErrKeyNotFound = store.ErrKeyNotFound
|
|
|
|
)
|
2015-06-11 08:32:15 -04:00
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
type datastore struct {
|
2015-10-22 00:31:43 -04:00
|
|
|
scope string
|
|
|
|
store store.Store
|
|
|
|
cache *cache
|
|
|
|
watchCh chan struct{}
|
|
|
|
active bool
|
2015-10-11 21:51:10 -04:00
|
|
|
sync.Mutex
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
// KVObject is Key/Value interface used by objects to be part of the DataStore
|
|
|
|
type KVObject interface {
|
2015-05-15 18:23:59 -04:00
|
|
|
// Key method lets an object to provide the Key to be used in KV Store
|
2015-05-08 09:26:35 -04:00
|
|
|
Key() []string
|
2015-06-01 12:43:24 -04:00
|
|
|
// KeyPrefix method lets an object to return immediate parent key that can be used for tree walk
|
|
|
|
KeyPrefix() []string
|
2015-05-15 18:23:59 -04:00
|
|
|
// Value method lets an object to marshal its content to be stored in the KV store
|
2015-05-08 09:26:35 -04:00
|
|
|
Value() []byte
|
2015-06-18 18:13:38 -04:00
|
|
|
// SetValue is used by the datastore to set the object's value when loaded from the data store.
|
|
|
|
SetValue([]byte) error
|
2015-05-15 18:23:59 -04:00
|
|
|
// Index method returns the latest DB Index as seen by the object
|
2015-05-08 09:26:35 -04:00
|
|
|
Index() uint64
|
2015-05-15 18:23:59 -04:00
|
|
|
// SetIndex method allows the datastore to store the latest DB Index into the object
|
2015-05-08 09:26:35 -04:00
|
|
|
SetIndex(uint64)
|
2015-06-18 18:13:38 -04:00
|
|
|
// True if the object exists in the datastore, false if it hasn't been stored yet.
|
|
|
|
// When SetIndex() is called, the object has been stored.
|
|
|
|
Exists() bool
|
2015-09-16 07:39:46 -04:00
|
|
|
// DataScope indicates the storage scope of the KV object
|
2015-10-05 07:18:10 -04:00
|
|
|
DataScope() string
|
2015-09-22 10:09:39 -04:00
|
|
|
// Skip provides a way for a KV Object to avoid persisting it in the KV Store
|
|
|
|
Skip() bool
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
// KVConstructor interface defines methods which can construct a KVObject from another.
|
|
|
|
type KVConstructor interface {
|
|
|
|
// New returns a new object which is created based on the
|
|
|
|
// source object
|
|
|
|
New() KVObject
|
|
|
|
// CopyTo deep copies the contents of the implementing object
|
|
|
|
// to the passed destination object
|
|
|
|
CopyTo(KVObject) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScopeCfg represents Datastore configuration.
|
|
|
|
type ScopeCfg struct {
|
2015-10-08 17:59:47 -04:00
|
|
|
Client ScopeClientCfg
|
2015-10-05 07:18:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScopeClientCfg represents Datastore Client-only mode configuration
|
|
|
|
type ScopeClientCfg struct {
|
|
|
|
Provider string
|
|
|
|
Address string
|
|
|
|
Config *store.Config
|
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
// LocalScope indicates to store the KV object in local datastore such as boltdb
|
2015-10-05 07:18:10 -04:00
|
|
|
LocalScope = "local"
|
2015-09-16 07:39:46 -04:00
|
|
|
// GlobalScope indicates to store the KV object in global datastore such as consul/etcd/zookeeper
|
2015-10-05 07:18:10 -04:00
|
|
|
GlobalScope = "global"
|
|
|
|
defaultPrefix = "/var/lib/docker/network/files"
|
2015-09-16 07:39:46 -04:00
|
|
|
)
|
|
|
|
|
2015-05-15 18:23:59 -04:00
|
|
|
const (
|
|
|
|
// NetworkKeyPrefix is the prefix for network key in the kv store
|
|
|
|
NetworkKeyPrefix = "network"
|
2015-05-31 14:49:11 -04:00
|
|
|
// EndpointKeyPrefix is the prefix for endpoint key in the kv store
|
|
|
|
EndpointKeyPrefix = "endpoint"
|
2015-05-15 18:23:59 -04:00
|
|
|
)
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
var (
|
|
|
|
defaultScopes = makeDefaultScopes()
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeDefaultScopes() map[string]*ScopeCfg {
|
|
|
|
def := make(map[string]*ScopeCfg)
|
|
|
|
def[LocalScope] = &ScopeCfg{
|
|
|
|
Client: ScopeClientCfg{
|
2015-10-23 13:13:29 -04:00
|
|
|
Provider: string(store.BOLTDB),
|
2015-10-12 01:47:07 -04:00
|
|
|
Address: defaultPrefix + "/local-kv.db",
|
2015-10-05 07:18:10 -04:00
|
|
|
Config: &store.Config{
|
2015-10-08 18:02:03 -04:00
|
|
|
Bucket: "libnetwork",
|
2015-10-05 07:18:10 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
2015-10-23 10:45:22 -04:00
|
|
|
var defaultRootChain = []string{"docker", "network", "v1.0"}
|
|
|
|
var rootChain = defaultRootChain
|
2015-06-05 16:31:12 -04:00
|
|
|
|
2015-09-16 06:15:56 -04:00
|
|
|
func init() {
|
|
|
|
consul.Register()
|
|
|
|
zookeeper.Register()
|
|
|
|
etcd.Register()
|
2015-09-16 07:39:46 -04:00
|
|
|
boltdb.Register()
|
2015-09-16 06:15:56 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
// DefaultScopes returns a map of default scopes and it's config for clients to use.
|
|
|
|
func DefaultScopes(dataDir string) map[string]*ScopeCfg {
|
|
|
|
if dataDir != "" {
|
2015-10-12 01:47:07 -04:00
|
|
|
defaultScopes[LocalScope].Client.Address = dataDir + "/network/files/local-kv.db"
|
2015-10-05 07:18:10 -04:00
|
|
|
return defaultScopes
|
|
|
|
}
|
|
|
|
|
2015-10-12 01:47:07 -04:00
|
|
|
defaultScopes[LocalScope].Client.Address = defaultPrefix + "/local-kv.db"
|
2015-10-05 07:18:10 -04:00
|
|
|
return defaultScopes
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid checks if the scope config has valid configuration.
|
|
|
|
func (cfg *ScopeCfg) IsValid() bool {
|
|
|
|
if cfg == nil ||
|
|
|
|
strings.TrimSpace(cfg.Client.Provider) == "" ||
|
|
|
|
strings.TrimSpace(cfg.Client.Address) == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
//Key provides convenient method to create a Key
|
|
|
|
func Key(key ...string) string {
|
2015-06-05 16:31:12 -04:00
|
|
|
keychain := append(rootChain, key...)
|
2015-05-08 09:26:35 -04:00
|
|
|
str := strings.Join(keychain, "/")
|
|
|
|
return str + "/"
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
//ParseKey provides convenient method to unpack the key to complement the Key function
|
|
|
|
func ParseKey(key string) ([]string, error) {
|
|
|
|
chain := strings.Split(strings.Trim(key, "/"), "/")
|
|
|
|
|
|
|
|
// The key must atleast be equal to the rootChain in order to be considered as valid
|
|
|
|
if len(chain) <= len(rootChain) || !reflect.DeepEqual(chain[0:len(rootChain)], rootChain) {
|
|
|
|
return nil, types.BadRequestErrorf("invalid Key : %s", key)
|
|
|
|
}
|
|
|
|
return chain[len(rootChain):], nil
|
|
|
|
}
|
2015-05-08 09:26:35 -04:00
|
|
|
|
|
|
|
// newClient used to connect to KV Store
|
2015-10-14 02:46:42 -04:00
|
|
|
func newClient(scope string, kv string, addr string, config *store.Config, cached bool) (DataStore, error) {
|
2015-10-23 10:45:22 -04:00
|
|
|
|
2015-10-08 18:23:19 -04:00
|
|
|
if cached && scope != LocalScope {
|
|
|
|
return nil, fmt.Errorf("caching supported only for scope %s", LocalScope)
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
if config == nil {
|
|
|
|
config = &store.Config{}
|
|
|
|
}
|
2015-10-14 02:46:42 -04:00
|
|
|
|
2015-10-23 13:13:29 -04:00
|
|
|
var addrs []string
|
|
|
|
|
|
|
|
if kv == string(store.BOLTDB) {
|
|
|
|
// Parse file path
|
|
|
|
addrs = strings.Split(addr, ",")
|
|
|
|
} else {
|
|
|
|
// Parse URI
|
|
|
|
parts := strings.SplitN(addr, "/", 2)
|
|
|
|
addrs = strings.Split(parts[0], ",")
|
|
|
|
|
|
|
|
// Add the custom prefix to the root chain
|
|
|
|
if len(parts) == 2 {
|
|
|
|
rootChain = append([]string{parts[1]}, defaultRootChain...)
|
|
|
|
}
|
2015-10-23 10:45:22 -04:00
|
|
|
}
|
2015-10-14 02:46:42 -04:00
|
|
|
|
|
|
|
store, err := libkv.NewStore(store.Backend(kv), addrs, config)
|
2015-05-08 09:26:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-26 12:44:51 -04:00
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
ds := &datastore{scope: scope, store: store, active: true, watchCh: make(chan struct{})}
|
2015-10-05 07:18:10 -04:00
|
|
|
if cached {
|
|
|
|
ds.cache = newCache(ds)
|
|
|
|
}
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
return ds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDataStore creates a new instance of LibKV data store
|
2015-10-05 07:18:10 -04:00
|
|
|
func NewDataStore(scope string, cfg *ScopeCfg) (DataStore, error) {
|
2015-10-08 18:23:19 -04:00
|
|
|
if cfg == nil || cfg.Client.Provider == "" || cfg.Client.Address == "" {
|
|
|
|
c, ok := defaultScopes[scope]
|
|
|
|
if !ok || c.Client.Provider == "" || c.Client.Address == "" {
|
|
|
|
return nil, fmt.Errorf("unexpected scope %s without configuration passed", scope)
|
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
2015-10-08 18:23:19 -04:00
|
|
|
cfg = c
|
2015-10-05 07:18:10 -04:00
|
|
|
}
|
|
|
|
|
2015-10-08 18:23:19 -04:00
|
|
|
var cached bool
|
|
|
|
if scope == LocalScope {
|
|
|
|
cached = true
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
2015-10-08 18:23:19 -04:00
|
|
|
return newClient(scope, cfg.Client.Provider, cfg.Client.Address, cfg.Client.Config, cached)
|
2015-10-05 07:18:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *datastore) Close() {
|
|
|
|
ds.store.Close()
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) Scope() string {
|
|
|
|
return ds.scope
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
func (ds *datastore) Active() bool {
|
|
|
|
return ds.active
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) Watchable() bool {
|
|
|
|
return ds.scope != LocalScope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *datastore) Watch(kvObject KVObject, stopCh <-chan struct{}) (<-chan KVObject, error) {
|
|
|
|
sCh := make(chan struct{})
|
|
|
|
|
|
|
|
ctor, ok := kvObject.(KVConstructor)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error watching object type %T, object does not implement KVConstructor interface", kvObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
kvpCh, err := ds.store.Watch(Key(kvObject.Key()...), sCh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kvoCh := make(chan KVObject)
|
|
|
|
|
|
|
|
go func() {
|
2015-10-22 00:31:43 -04:00
|
|
|
retry_watch:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Make sure to get a new instance of watch channel
|
|
|
|
ds.Lock()
|
|
|
|
watchCh := ds.watchCh
|
|
|
|
ds.Unlock()
|
|
|
|
|
|
|
|
loop:
|
2015-10-05 07:18:10 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
close(sCh)
|
|
|
|
return
|
|
|
|
case kvPair := <-kvpCh:
|
2015-10-04 10:42:59 -04:00
|
|
|
// If the backend KV store gets reset libkv's go routine
|
|
|
|
// for the watch can exit resulting in a nil value in
|
|
|
|
// channel.
|
|
|
|
if kvPair == nil {
|
2015-10-22 00:31:43 -04:00
|
|
|
ds.Lock()
|
|
|
|
ds.active = false
|
|
|
|
ds.Unlock()
|
|
|
|
break loop
|
2015-10-04 10:42:59 -04:00
|
|
|
}
|
2015-10-22 00:31:43 -04:00
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
dstO := ctor.New()
|
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
if err = dstO.SetValue(kvPair.Value); err != nil {
|
2015-10-05 07:18:10 -04:00
|
|
|
log.Printf("Could not unmarshal kvpair value = %s", string(kvPair.Value))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
dstO.SetIndex(kvPair.LastIndex)
|
|
|
|
kvoCh <- dstO
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 00:31:43 -04:00
|
|
|
|
|
|
|
// Wait on watch channel for a re-trigger when datastore becomes active
|
|
|
|
<-watchCh
|
|
|
|
|
|
|
|
kvpCh, err = ds.store.Watch(Key(kvObject.Key()...), sCh)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not watch the key %s in store: %v", Key(kvObject.Key()...), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
goto retry_watch
|
2015-10-05 07:18:10 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
return kvoCh, nil
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:31:43 -04:00
|
|
|
func (ds *datastore) RestartWatch() {
|
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
|
|
|
ds.active = true
|
|
|
|
watchCh := ds.watchCh
|
|
|
|
ds.watchCh = make(chan struct{})
|
|
|
|
close(watchCh)
|
|
|
|
}
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
func (ds *datastore) KVStore() store.Store {
|
|
|
|
return ds.store
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectAtomic adds a new Record based on an object into the datastore
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) PutObjectAtomic(kvObject KVObject) error {
|
|
|
|
var (
|
|
|
|
previous *store.KVPair
|
|
|
|
pair *store.KVPair
|
|
|
|
err error
|
|
|
|
)
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
2015-10-05 07:18:10 -04:00
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
if kvObject == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object : nil")
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
kvObjValue := kvObject.Value()
|
|
|
|
|
|
|
|
if kvObjValue == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
if kvObject.Skip() {
|
|
|
|
goto add_cache
|
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
if kvObject.Exists() {
|
|
|
|
previous = &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}
|
|
|
|
} else {
|
|
|
|
previous = nil
|
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
|
|
|
_, pair, err = ds.store.AtomicPut(Key(kvObject.Key()...), kvObjValue, previous, nil)
|
2015-05-08 09:26:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
|
|
|
|
kvObject.SetIndex(pair.LastIndex)
|
2015-10-05 07:18:10 -04:00
|
|
|
|
|
|
|
add_cache:
|
|
|
|
if ds.cache != nil {
|
|
|
|
return ds.cache.add(kvObject)
|
|
|
|
}
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject adds a new Record based on an object into the datastore
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) PutObject(kvObject KVObject) error {
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
if kvObject == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object : nil")
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
|
|
|
if kvObject.Skip() {
|
|
|
|
goto add_cache
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ds.putObjectWithKey(kvObject, kvObject.Key()...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
add_cache:
|
|
|
|
if ds.cache != nil {
|
|
|
|
return ds.cache.add(kvObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) putObjectWithKey(kvObject KVObject, key ...string) error {
|
2015-05-08 09:26:35 -04:00
|
|
|
kvObjValue := kvObject.Value()
|
|
|
|
|
|
|
|
if kvObjValue == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
return ds.store.Put(Key(key...), kvObjValue, nil)
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-06-01 00:19:10 -04:00
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
// GetObject returns a record matching the key
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) GetObject(key string, o KVObject) error {
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
if ds.cache != nil {
|
|
|
|
return ds.cache.get(key, o)
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:19:10 -04:00
|
|
|
kvPair, err := ds.store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
|
|
|
if err := o.SetValue(kvPair.Value); err != nil {
|
2015-06-18 18:13:38 -04:00
|
|
|
return err
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-06-18 18:13:38 -04:00
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
// Make sure the object has a correct view of the DB index in
|
|
|
|
// case we need to modify it and update the DB.
|
2015-06-18 18:13:38 -04:00
|
|
|
o.SetIndex(kvPair.LastIndex)
|
|
|
|
return nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-13 13:43:52 -04:00
|
|
|
func (ds *datastore) ensureParent(parent string) error {
|
|
|
|
exists, err := ds.store.Exists(parent)
|
2015-10-05 07:18:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-13 13:43:52 -04:00
|
|
|
return ds.store.Put(parent, []byte{}, &store.WriteOptions{IsDir: true})
|
2015-10-05 07:18:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
if ds.cache != nil {
|
|
|
|
return ds.cache.list(kvObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out right away if the kvObject does not implement KVConstructor
|
|
|
|
ctor, ok := kvObject.(KVConstructor)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error listing objects, object does not implement KVConstructor interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the parent key exists
|
2015-10-13 13:43:52 -04:00
|
|
|
if err := ds.ensureParent(key); err != nil {
|
2015-10-05 07:18:10 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kvList, err := ds.store.List(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var kvol []KVObject
|
|
|
|
for _, kvPair := range kvList {
|
|
|
|
if len(kvPair.Value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dstO := ctor.New()
|
|
|
|
if err := dstO.SetValue(kvPair.Value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the object has a correct view of the DB index in
|
|
|
|
// case we need to modify it and update the DB.
|
|
|
|
dstO.SetIndex(kvPair.LastIndex)
|
|
|
|
|
|
|
|
kvol = append(kvol, dstO)
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvol, nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
// DeleteObject unconditionally deletes a record from the store
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) DeleteObject(kvObject KVObject) error {
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
// cleaup the cache first
|
|
|
|
if ds.cache != nil {
|
|
|
|
ds.cache.del(kvObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
if kvObject.Skip() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
return ds.store.Delete(Key(kvObject.Key()...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjectAtomic performs atomic delete on a record
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) DeleteObjectAtomic(kvObject KVObject) error {
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
if kvObject == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object : nil")
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
previous := &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
|
|
|
if kvObject.Skip() {
|
|
|
|
goto del_cache
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ds.store.AtomicDelete(Key(kvObject.Key()...), previous); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
del_cache:
|
|
|
|
// cleanup the cache only if AtomicDelete went through successfully
|
|
|
|
if ds.cache != nil {
|
|
|
|
return ds.cache.del(kvObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTree unconditionally deletes a record from the store
|
2015-10-05 07:18:10 -04:00
|
|
|
func (ds *datastore) DeleteTree(kvObject KVObject) error {
|
2015-10-11 21:51:10 -04:00
|
|
|
ds.Lock()
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
// cleaup the cache first
|
|
|
|
if ds.cache != nil {
|
|
|
|
ds.cache.del(kvObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
if kvObject.Skip() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
return ds.store.DeleteTree(Key(kvObject.KeyPrefix()...))
|
|
|
|
}
|