From 57b28d95de858bc15eaef43c2e56d87366cdab14 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 30 Jan 2018 14:59:28 +0100 Subject: [PATCH] Add GetStoreByName to MultiStore to help with Query lookups --- store/cachemultistore.go | 25 +++++++++++++++++++++---- store/rootmultistore.go | 16 ++++++++++++++++ types/store.go | 2 ++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/store/cachemultistore.go b/store/cachemultistore.go index b53899b3d5..837e52dfe1 100644 --- a/store/cachemultistore.go +++ b/store/cachemultistore.go @@ -10,14 +10,18 @@ import ( // cacheMultiStore holds many cache-wrapped stores. // Implements MultiStore. type cacheMultiStore struct { - db CacheKVStore - stores map[StoreKey]CacheWrap + db CacheKVStore + stores map[StoreKey]CacheWrap + keysByName map[string]StoreKey } +var _ CacheMultiStore = cacheMultiStore{} + func newCacheMultiStoreFromRMS(rms *rootMultiStore) cacheMultiStore { cms := cacheMultiStore{ - db: NewCacheKVStore(dbStoreAdapter{rms.db}), - stores: make(map[StoreKey]CacheWrap, len(rms.stores)), + db: NewCacheKVStore(dbStoreAdapter{rms.db}), + stores: make(map[StoreKey]CacheWrap, len(rms.stores)), + keysByName: rms.keysByName, } for key, store := range rms.stores { cms.stores[key] = store.CacheWrap() @@ -68,3 +72,16 @@ func (cms cacheMultiStore) GetStore(key StoreKey) Store { func (cms cacheMultiStore) GetKVStore(key StoreKey) KVStore { return cms.stores[key].(KVStore) } + +// GetStoreByName will first convert the original name to +// a special key, before looking up the CommitStore. +// This is not exposed to the extensions (which will need the +// StoreKey), but is useful in main, and particularly app.Query, +// in order to convert human strings into CommitStores. +func (cms cacheMultiStore) GetStoreByName(name string) Store { + key := cms.keysByName[name] + if key == nil { + return nil + } + return cms.stores[key].(Store) +} diff --git a/store/rootmultistore.go b/store/rootmultistore.go index f964a1fd5b..2a0c831d81 100644 --- a/store/rootmultistore.go +++ b/store/rootmultistore.go @@ -24,6 +24,7 @@ type rootMultiStore struct { lastCommitID CommitID storesParams map[StoreKey]storeParams stores map[StoreKey]CommitStore + keysByName map[string]StoreKey } var _ CommitMultiStore = (*rootMultiStore)(nil) @@ -33,6 +34,7 @@ func NewCommitMultiStore(db dbm.DB) *rootMultiStore { db: db, storesParams: make(map[StoreKey]storeParams), stores: make(map[StoreKey]CommitStore), + keysByName: make(map[string]StoreKey), } } @@ -53,6 +55,7 @@ func (rs *rootMultiStore) MountStoreWithDB(key StoreKey, typ StoreType, db dbm.D db: db, typ: typ, } + rs.keysByName[key.Name()] = key } // Implements CommitMultiStore. @@ -169,6 +172,19 @@ func (rs *rootMultiStore) GetKVStore(key StoreKey) KVStore { return rs.stores[key].(KVStore) } +// GetStoreByName will first convert the original name to +// a special key, before looking up the CommitStore. +// This is not exposed to the extensions (which will need the +// StoreKey), but is useful in main, and particularly app.Query, +// in order to convert human strings into CommitStores. +func (rs *rootMultiStore) GetStoreByName(name string) Store { + key := rs.keysByName[name] + if key == nil { + return nil + } + return rs.stores[key] +} + //---------------------------------------- func (rs *rootMultiStore) loadCommitStoreFromParams(id CommitID, params storeParams) (store CommitStore, err error) { diff --git a/types/store.go b/types/store.go index f9e8c912a3..5847091a41 100644 --- a/types/store.go +++ b/types/store.go @@ -39,6 +39,8 @@ type MultiStore interface { // Convenience for fetching substores. GetStore(StoreKey) Store GetKVStore(StoreKey) KVStore + + GetStoreByName(string) Store } // From MultiStore.CacheMultiStore()....