|
@@ -40,6 +40,8 @@ type DataStore interface {
|
|
|
// 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
|
|
@@ -512,23 +514,34 @@ func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
|
|
|
return ds.cache.list(kvObject)
|
|
|
}
|
|
|
|
|
|
+ var kvol []KVObject
|
|
|
+ cb := func(key string, val KVObject) {
|
|
|
+ kvol = append(kvol, val)
|
|
|
+ }
|
|
|
+ err := ds.iterateKVPairsFromStore(key, kvObject, cb)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return kvol, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (ds *datastore) 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 {
|
|
|
- return nil, fmt.Errorf("error listing objects, object does not implement KVConstructor interface")
|
|
|
+ return fmt.Errorf("error listing objects, object does not implement KVConstructor interface")
|
|
|
}
|
|
|
|
|
|
// Make sure the parent key exists
|
|
|
if err := ds.ensureParent(key); err != nil {
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
kvList, err := ds.store.List(key)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- var kvol []KVObject
|
|
|
for _, kvPair := range kvList {
|
|
|
if len(kvPair.Value) == 0 {
|
|
|
continue
|
|
@@ -536,16 +549,33 @@ func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
|
|
|
|
|
|
dstO := ctor.New()
|
|
|
if err := dstO.SetValue(kvPair.Value); err != nil {
|
|
|
- return nil, err
|
|
|
+ return 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)
|
|
|
+ callback(kvPair.Key, dstO)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
|
|
|
- kvol = append(kvol, dstO)
|
|
|
+func (ds *datastore) Map(key string, kvObject KVObject) (map[string]KVObject, error) {
|
|
|
+ if ds.sequential {
|
|
|
+ ds.Lock()
|
|
|
+ defer ds.Unlock()
|
|
|
}
|
|
|
|
|
|
+ kvol := make(map[string]KVObject)
|
|
|
+ cb := func(key string, val KVObject) {
|
|
|
+ // Trim the leading & trailing "/" to make it consistent across all stores
|
|
|
+ kvol[strings.Trim(key, "/")] = val
|
|
|
+ }
|
|
|
+ err := ds.iterateKVPairsFromStore(key, kvObject, cb)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
return kvol, nil
|
|
|
}
|
|
|
|