Skip to content

Commit 2f1ac74

Browse files
yihuangmmsqe
authored andcommitted
Problem: no api to create cachemulti store from external cache store (#258)
Solution: - add API NewFromParent to cache multistore. Update store/CHANGELOG.md Signed-off-by: yihuang <huang@crypto.com> fix test fix lint
1 parent 0224ec8 commit 2f1ac74

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

store/cachemulti/store.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Store struct {
2525

2626
traceWriter io.Writer
2727
traceContext types.TraceContext
28-
parentStore func(types.StoreKey) types.CacheWrap
28+
parentStore func(types.StoreKey) types.CacheWrapper
2929

3030
branched bool
3131
}
@@ -61,12 +61,17 @@ func NewStore(
6161
return NewFromKVStore(stores, traceWriter, traceContext)
6262
}
6363

64-
func newCacheMultiStoreFromCMS(cms Store) Store {
64+
// NewFromParent constructs a cache multistore with a parent store lazily,
65+
// the parent is usually another cache multistore or the block-stm multiversion store.
66+
func NewFromParent(
67+
parentStore func(types.StoreKey) types.CacheWrapper,
68+
traceWriter io.Writer, traceContext types.TraceContext,
69+
) Store {
6570
return Store{
6671
stores: make(map[types.StoreKey]types.CacheWrap),
67-
traceWriter: cms.traceWriter,
68-
traceContext: cms.traceContext,
69-
parentStore: cms.getCacheWrap,
72+
traceWriter: traceWriter,
73+
traceContext: traceContext,
74+
parentStore: parentStore,
7075
}
7176
}
7277

@@ -146,10 +151,10 @@ func (cms Store) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.Cac
146151
// CacheMultiStore implements MultiStore, returns a new CacheMultiStore from the
147152
// underlying CacheMultiStore.
148153
func (cms Store) CacheMultiStore() types.CacheMultiStore {
149-
return newCacheMultiStoreFromCMS(cms)
154+
return NewFromParent(cms.getCacheWrapper, cms.traceWriter, cms.traceContext)
150155
}
151156

152-
func (cms Store) getCacheWrap(key types.StoreKey) types.CacheWrap {
157+
func (cms Store) getCacheWrapper(key types.StoreKey) types.CacheWrapper {
153158
store, ok := cms.stores[key]
154159
if !ok && cms.parentStore != nil {
155160
// load on demand
@@ -163,7 +168,7 @@ func (cms Store) getCacheWrap(key types.StoreKey) types.CacheWrap {
163168

164169
// GetStore returns an underlying Store by key.
165170
func (cms Store) GetStore(key types.StoreKey) types.Store {
166-
store, ok := cms.getCacheWrap(key).(types.Store)
171+
store, ok := cms.getCacheWrapper(key).(types.Store)
167172
if !ok {
168173
panic(fmt.Sprintf("store with key %v is not Store", key))
169174
}
@@ -172,7 +177,7 @@ func (cms Store) GetStore(key types.StoreKey) types.Store {
172177

173178
// GetKVStore returns an underlying KVStore by key.
174179
func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
175-
store, ok := cms.getCacheWrap(key).(types.KVStore)
180+
store, ok := cms.getCacheWrapper(key).(types.KVStore)
176181
if !ok {
177182
panic(fmt.Sprintf("store with key %v is not KVStore", key))
178183
}
@@ -181,7 +186,7 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
181186

182187
// GetObjKVStore returns an underlying KVStore by key.
183188
func (cms Store) GetObjKVStore(key types.StoreKey) types.ObjKVStore {
184-
store, ok := cms.getCacheWrap(key).(types.ObjKVStore)
189+
store, ok := cms.getCacheWrapper(key).(types.ObjKVStore)
185190
if !ok {
186191
panic(fmt.Sprintf("store with key %v is not ObjKVStore", key))
187192
}

store/cachemulti/store_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestRunAtomic(t *testing.T) {
4343
keys["obj"]: objStore.CacheWrap(),
4444
}}
4545

46-
s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrap}
46+
s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrapper}
4747
err := s.RunAtomic(func(ms types.CacheMultiStore) error {
4848
ms.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value"))
4949
ms.GetObjKVStore(keys["obj"]).Set([]byte("key"), "value")
@@ -78,7 +78,7 @@ func TestBranchStore(t *testing.T) {
7878
keys["obj"]: objStore.CacheWrap(),
7979
}}
8080

81-
s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrap}
81+
s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrapper}
8282
s.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value"))
8383
snapshot := s.Clone()
8484
s.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value2"))

0 commit comments

Comments
 (0)