cosmos-sdk/store/cachemultistore.go

78 lines
1.9 KiB
Go
Raw Normal View History

2017-12-01 08:52:54 -08:00
package store
//----------------------------------------
// 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 {
db CacheKVStore
2017-12-19 21:23:51 -08:00
nextVersion int64
2017-12-01 08:52:54 -08:00
lastCommitID CommitID
2018-01-12 13:48:54 -08:00
substores map[SubstoreKey]CacheWrap
2017-12-01 08:52:54 -08:00
}
2017-12-04 00:23:10 -08:00
func newCacheMultiStoreFromRMS(rms *rootMultiStore) cacheMultiStore {
2017-12-01 08:52:54 -08:00
cms := cacheMultiStore{
db: NewCacheKVStore(rms.db),
2017-12-19 21:23:51 -08:00
nextVersion: rms.nextVersion,
2017-12-04 00:23:10 -08:00
lastCommitID: rms.lastCommitID,
2018-01-12 13:48:54 -08:00
substores: make(map[SubstoreKey]CacheWrap, len(rms.substores)),
2017-12-04 00:23:10 -08:00
}
2018-01-12 13:48:54 -08:00
for key, substore := range rms.substores {
cms.substores[key] = substore.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{
db: NewCacheKVStore(cms.db),
2017-12-19 21:23:51 -08:00
nextVersion: cms.nextVersion,
2017-12-04 00:23:10 -08:00
lastCommitID: cms.lastCommitID,
2018-01-12 13:48:54 -08:00
substores: make(map[SubstoreKey]CacheWrap, len(cms.substores)),
2017-12-01 08:52:54 -08:00
}
2018-01-12 13:48:54 -08:00
for key, substore := range cms.substores {
cms2.substores[key] = substore.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
}
// Implements CacheMultiStore
func (cms cacheMultiStore) LastCommitID() CommitID {
return cms.lastCommitID
}
// Implements CacheMultiStore
2017-12-19 21:23:51 -08:00
func (cms cacheMultiStore) NextVersion() int64 {
return cms.nextVersion
2017-12-01 08:52:54 -08:00
}
// Implements CacheMultiStore
func (cms cacheMultiStore) Write() {
cms.db.Write()
2017-12-04 00:23:10 -08:00
for _, substore := range cms.substores {
2017-12-03 15:01:17 -08:00
substore.Write()
2017-12-01 08:52:54 -08:00
}
}
2017-12-04 00:56:25 -08:00
// Implements CacheMultiStore
func (cms cacheMultiStore) CacheWrap() CacheWrap {
return cms.CacheMultiStore().(CacheWrap)
2017-12-04 00:56:25 -08:00
}
2017-12-01 08:52:54 -08:00
// Implements CacheMultiStore
2017-12-04 00:23:10 -08:00
func (cms cacheMultiStore) CacheMultiStore() CacheMultiStore {
return newCacheMultiStoreFromCMS(cms)
2017-12-01 08:52:54 -08:00
}
// Implements CacheMultiStore
2018-01-12 13:48:54 -08:00
func (cms cacheMultiStore) GetStore(key SubstoreKey) interface{} {
return cms.substores[key]
2017-12-01 08:52:54 -08:00
}
// Implements CacheMultiStore
2018-01-12 13:48:54 -08:00
func (cms cacheMultiStore) GetKVStore(key SubstoreKey) KVStore {
return cms.substores[key].(KVStore)
2017-12-01 08:52:54 -08:00
}