|
@@ -12,42 +12,20 @@ import (
|
|
|
"github.com/docker/docker/libnetwork/types"
|
|
|
)
|
|
|
|
|
|
-// DataStore exported
|
|
|
-type DataStore interface {
|
|
|
- // GetObject gets data from datastore and unmarshals to the specified object
|
|
|
- GetObject(key string, o KVObject) error
|
|
|
- // PutObjectAtomic provides an atomic add and update operation for a Record
|
|
|
- PutObjectAtomic(kvObject KVObject) error
|
|
|
- // DeleteObjectAtomic performs an atomic delete operation
|
|
|
- DeleteObjectAtomic(kvObject KVObject) error
|
|
|
- // 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)
|
|
|
- // Map returns a Map of KVObjects
|
|
|
- Map(key string, kvObject KVObject) (map[string]KVObject, error)
|
|
|
- // Scope returns the scope of the store
|
|
|
- Scope() string
|
|
|
- // KVStore returns access to the KV Store
|
|
|
- KVStore() store.Store
|
|
|
- // Close closes the data store
|
|
|
- Close()
|
|
|
-}
|
|
|
-
|
|
|
// ErrKeyModified is raised for an atomic update when the update is working on a stale state
|
|
|
var (
|
|
|
ErrKeyModified = store.ErrKeyModified
|
|
|
ErrKeyNotFound = store.ErrKeyNotFound
|
|
|
)
|
|
|
|
|
|
-type datastore struct {
|
|
|
+type Store struct {
|
|
|
mu sync.Mutex
|
|
|
scope string
|
|
|
store store.Store
|
|
|
cache *cache
|
|
|
}
|
|
|
|
|
|
-// KVObject is Key/Value interface used by objects to be part of the DataStore
|
|
|
+// KVObject is Key/Value interface used by objects to be part of the Store.
|
|
|
type KVObject interface {
|
|
|
// Key method lets an object provide the Key to be used in KV Store
|
|
|
Key() []string
|
|
@@ -61,7 +39,7 @@ type KVObject interface {
|
|
|
Index() uint64
|
|
|
// SetIndex method allows the datastore to store the latest DB Index into the object
|
|
|
SetIndex(uint64)
|
|
|
- // True if the object exists in the datastore, false if it hasn't been stored yet.
|
|
|
+ // Exists returns 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
|
|
|
// DataScope indicates the storage scope of the KV object
|
|
@@ -166,7 +144,7 @@ func ParseKey(key string) ([]string, error) {
|
|
|
}
|
|
|
|
|
|
// newClient used to connect to KV Store
|
|
|
-func newClient(kv string, addr string, config *store.Config) (DataStore, error) {
|
|
|
+func newClient(kv string, addr string, config *store.Config) (*Store, error) {
|
|
|
if config == nil {
|
|
|
config = &store.Config{}
|
|
|
}
|
|
@@ -192,14 +170,14 @@ func newClient(kv string, addr string, config *store.Config) (DataStore, error)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- ds := &datastore{scope: LocalScope, store: s}
|
|
|
+ ds := &Store{scope: LocalScope, store: s}
|
|
|
ds.cache = newCache(ds)
|
|
|
|
|
|
return ds, nil
|
|
|
}
|
|
|
|
|
|
-// NewDataStore creates a new instance of LibKV data store
|
|
|
-func NewDataStore(cfg ScopeCfg) (DataStore, error) {
|
|
|
+// New creates a new Store instance.
|
|
|
+func New(cfg ScopeCfg) (*Store, error) {
|
|
|
if cfg.Client.Provider == "" || cfg.Client.Address == "" {
|
|
|
cfg = DefaultScope("")
|
|
|
}
|
|
@@ -207,8 +185,8 @@ func NewDataStore(cfg ScopeCfg) (DataStore, error) {
|
|
|
return newClient(cfg.Client.Provider, cfg.Client.Address, cfg.Client.Config)
|
|
|
}
|
|
|
|
|
|
-// NewDataStoreFromConfig creates a new instance of LibKV data store starting from the datastore config data
|
|
|
-func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, error) {
|
|
|
+// FromConfig creates a new instance of LibKV data store starting from the datastore config data.
|
|
|
+func FromConfig(dsc discoverapi.DatastoreConfigData) (*Store, error) {
|
|
|
var (
|
|
|
ok bool
|
|
|
sCfgP *store.Config
|
|
@@ -219,15 +197,13 @@ func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, err
|
|
|
return nil, fmt.Errorf("cannot parse store configuration: %v", dsc.Config)
|
|
|
}
|
|
|
|
|
|
- scopeCfg := ScopeCfg{
|
|
|
+ ds, err := New(ScopeCfg{
|
|
|
Client: ScopeClientCfg{
|
|
|
Address: dsc.Address,
|
|
|
Provider: dsc.Provider,
|
|
|
Config: sCfgP,
|
|
|
},
|
|
|
- }
|
|
|
-
|
|
|
- ds, err := NewDataStore(scopeCfg)
|
|
|
+ })
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("failed to construct datastore client from datastore configuration %v: %v", dsc, err)
|
|
|
}
|
|
@@ -235,20 +211,23 @@ func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, err
|
|
|
return ds, err
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) Close() {
|
|
|
+// Close closes the data store.
|
|
|
+func (ds *Store) Close() {
|
|
|
ds.store.Close()
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) Scope() string {
|
|
|
+// Scope returns the scope of the store.
|
|
|
+func (ds *Store) Scope() string {
|
|
|
return ds.scope
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) KVStore() store.Store {
|
|
|
+// KVStore returns access to the KV Store.
|
|
|
+func (ds *Store) KVStore() store.Store {
|
|
|
return ds.store
|
|
|
}
|
|
|
|
|
|
-// PutObjectAtomic adds a new Record based on an object into the datastore
|
|
|
-func (ds *datastore) PutObjectAtomic(kvObject KVObject) error {
|
|
|
+// PutObjectAtomic provides an atomic add and update operation for a Record.
|
|
|
+func (ds *Store) PutObjectAtomic(kvObject KVObject) error {
|
|
|
var (
|
|
|
previous *store.KVPair
|
|
|
pair *store.KVPair
|
|
@@ -297,8 +276,8 @@ add_cache:
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// GetObject returns a record matching the key
|
|
|
-func (ds *datastore) GetObject(key string, o KVObject) error {
|
|
|
+// GetObject gets data from the store and unmarshals to the specified object.
|
|
|
+func (ds *Store) GetObject(key string, o KVObject) error {
|
|
|
ds.mu.Lock()
|
|
|
defer ds.mu.Unlock()
|
|
|
|
|
@@ -321,7 +300,7 @@ func (ds *datastore) GetObject(key string, o KVObject) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) ensureParent(parent string) error {
|
|
|
+func (ds *Store) ensureParent(parent string) error {
|
|
|
exists, err := ds.store.Exists(parent)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -332,7 +311,9 @@ func (ds *datastore) ensureParent(parent string) error {
|
|
|
return ds.store.Put(parent, []byte{})
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
|
|
|
+// 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.
|
|
|
+func (ds *Store) List(key string, kvObject KVObject) ([]KVObject, error) {
|
|
|
ds.mu.Lock()
|
|
|
defer ds.mu.Unlock()
|
|
|
|
|
@@ -351,7 +332,7 @@ func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
|
|
|
return kvol, nil
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) iterateKVPairsFromStore(key string, kvObject KVObject, callback func(string, KVObject)) error {
|
|
|
+func (ds *Store) iterateKVPairsFromStore(key string, kvObject KVObject, callback func(string, KVObject)) error {
|
|
|
// Bail out right away if the kvObject does not implement KVConstructor
|
|
|
ctor, ok := kvObject.(KVConstructor)
|
|
|
if !ok {
|
|
@@ -387,7 +368,8 @@ func (ds *datastore) iterateKVPairsFromStore(key string, kvObject KVObject, call
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (ds *datastore) Map(key string, kvObject KVObject) (map[string]KVObject, error) {
|
|
|
+// Map returns a Map of KVObjects.
|
|
|
+func (ds *Store) Map(key string, kvObject KVObject) (map[string]KVObject, error) {
|
|
|
ds.mu.Lock()
|
|
|
defer ds.mu.Unlock()
|
|
|
|
|
@@ -403,8 +385,8 @@ func (ds *datastore) Map(key string, kvObject KVObject) (map[string]KVObject, er
|
|
|
return kvol, nil
|
|
|
}
|
|
|
|
|
|
-// DeleteObjectAtomic performs atomic delete on a record
|
|
|
-func (ds *datastore) DeleteObjectAtomic(kvObject KVObject) error {
|
|
|
+// DeleteObjectAtomic performs atomic delete on a record.
|
|
|
+func (ds *Store) DeleteObjectAtomic(kvObject KVObject) error {
|
|
|
ds.mu.Lock()
|
|
|
defer ds.mu.Unlock()
|
|
|
|