Add GetStoreByName to MultiStore to help with Query lookups

This commit is contained in:
Ethan Frey 2018-01-30 14:59:28 +01:00 committed by Ethan Buchman
parent ed592cd94a
commit 57b28d95de
3 changed files with 39 additions and 4 deletions

View File

@ -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)
}

View File

@ -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) {

View File

@ -39,6 +39,8 @@ type MultiStore interface {
// Convenience for fetching substores.
GetStore(StoreKey) Store
GetKVStore(StoreKey) KVStore
GetStoreByName(string) Store
}
// From MultiStore.CacheMultiStore()....