2017-12-01 08:52:54 -08:00
|
|
|
package store
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2017-12-01 08:52:54 -08:00
|
|
|
//----------------------------------------
|
|
|
|
// cacheMultiStore
|
|
|
|
|
2017-12-03 22:55:15 -08:00
|
|
|
// cacheMultiStore holds many cache-wrapped stores.
|
2017-12-01 08:52:54 -08:00
|
|
|
// Implements MultiStore.
|
|
|
|
type cacheMultiStore struct {
|
2018-01-30 05:59:28 -08:00
|
|
|
db CacheKVStore
|
|
|
|
stores map[StoreKey]CacheWrap
|
|
|
|
keysByName map[string]StoreKey
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-30 05:59:28 -08:00
|
|
|
var _ CacheMultiStore = cacheMultiStore{}
|
|
|
|
|
2017-12-04 00:23:10 -08:00
|
|
|
func newCacheMultiStoreFromRMS(rms *rootMultiStore) cacheMultiStore {
|
2017-12-01 08:52:54 -08:00
|
|
|
cms := cacheMultiStore{
|
2018-01-30 05:59:28 -08:00
|
|
|
db: NewCacheKVStore(dbStoreAdapter{rms.db}),
|
|
|
|
stores: make(map[StoreKey]CacheWrap, len(rms.stores)),
|
|
|
|
keysByName: rms.keysByName,
|
2017-12-04 00:23:10 -08:00
|
|
|
}
|
2018-01-22 05:44:24 -08:00
|
|
|
for key, store := range rms.stores {
|
|
|
|
cms.stores[key] = store.CacheWrap()
|
2017-12-04 00:23:10 -08:00
|
|
|
}
|
|
|
|
return cms
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCacheMultiStoreFromCMS(cms cacheMultiStore) cacheMultiStore {
|
2017-12-04 00:56:25 -08:00
|
|
|
cms2 := cacheMultiStore{
|
2018-01-26 04:19:33 -08:00
|
|
|
db: NewCacheKVStore(cms.db),
|
|
|
|
stores: make(map[StoreKey]CacheWrap, len(cms.stores)),
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
2018-01-22 05:44:24 -08:00
|
|
|
for key, store := range cms.stores {
|
|
|
|
cms2.stores[key] = store.CacheWrap()
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
2017-12-04 00:56:25 -08:00
|
|
|
return cms2
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Implements Store.
|
|
|
|
func (cms cacheMultiStore) GetStoreType() StoreType {
|
|
|
|
return sdk.StoreTypeMulti
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Implements CacheMultiStore.
|
2017-12-01 08:52:54 -08:00
|
|
|
func (cms cacheMultiStore) Write() {
|
|
|
|
cms.db.Write()
|
2018-01-22 05:44:24 -08:00
|
|
|
for _, store := range cms.stores {
|
|
|
|
store.Write()
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Implements CacheWrapper.
|
2017-12-04 00:56:25 -08:00
|
|
|
func (cms cacheMultiStore) CacheWrap() CacheWrap {
|
2017-12-12 20:13:51 -08:00
|
|
|
return cms.CacheMultiStore().(CacheWrap)
|
2017-12-04 00:56:25 -08:00
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Implements MultiStore.
|
2017-12-04 00:23:10 -08:00
|
|
|
func (cms cacheMultiStore) CacheMultiStore() CacheMultiStore {
|
|
|
|
return newCacheMultiStoreFromCMS(cms)
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Implements MultiStore.
|
|
|
|
func (cms cacheMultiStore) GetStore(key StoreKey) Store {
|
|
|
|
return cms.stores[key].(Store)
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Implements MultiStore.
|
|
|
|
func (cms cacheMultiStore) GetKVStore(key StoreKey) KVStore {
|
|
|
|
return cms.stores[key].(KVStore)
|
2017-12-01 08:52:54 -08:00
|
|
|
}
|
2018-01-30 05:59:28 -08:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|