Skip to content

Commit

Permalink
add Discard method to CacheWrap
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 1, 2024
1 parent f69cf4b commit 01ca692
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#241](https://github.com/crypto-org-chain/cosmos-sdk/pull/241) Refactor the cache store to be btree backed, prepare to support copy-on-write atomic branching.
* [#242](https://github.com/crypto-org-chain/cosmos-sdk/pull/242) Init cache on cache lazily, save memory allocations.
* [#243](https://github.com/crypto-org-chain/cosmos-sdk/pull/243) Support `RunAtomic` API to use new CoW cache store.
* [#244](https://github.com/crypto-org-chain/cosmos-sdk/pull/244) Add `Discard` method to CacheWrap to discard the write buffer.

## v1.1.0 (March 20, 2024)

Expand Down
4 changes: 4 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (store *GStore[V]) Write() {
store.writeSet.Clear()
}

func (store *GStore[V]) Discard() {
store.writeSet.Clear()
}

// CacheWrap implements CacheWrapper.
func (store *GStore[V]) CacheWrap() types.CacheWrap {
return NewGStore(store, store.isZero, store.valueLen)
Expand Down
12 changes: 9 additions & 3 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func (cms Store) Write() {
}
}

func (cms Store) Discard() {
for _, store := range cms.stores {
store.Discard()
}
}

// Implements CacheWrapper.
func (cms Store) CacheWrap() types.CacheWrap {
return cms.CacheMultiStore().(types.CacheWrap)
Expand Down Expand Up @@ -202,10 +208,10 @@ func (cms Store) Restore(other Store) {
cms.stores[k].(types.BranchStore).Restore(v.(types.BranchStore))
}

for k := range cms.stores {
for k, v := range cms.stores {
if _, ok := other.stores[k]; !ok {
// delete the store if it's not in the other
delete(cms.stores, k)
// clear the cache store if it's not in the other
v.Discard()
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
}
Expand Down
3 changes: 3 additions & 0 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ type CacheWrap interface {

// Write syncs with the underlying store.
Write()

// Discard the write set
Discard()
}

type CacheWrapper interface {
Expand Down

0 comments on commit 01ca692

Please sign in to comment.