Skip to content

Commit

Permalink
kvstore use change set api
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 3, 2023
1 parent 06ee541 commit 5106b68
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions store/memiavlstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/iavl"
"github.com/crypto-org-chain/cronos/memiavl"
"github.com/tendermint/tendermint/libs/log"

Expand All @@ -23,6 +24,8 @@ var (
type Store struct {
tree memiavl.Tree
logger log.Logger

changeSet memiavl.ChangeSet
}

func LoadStoreWithInitialVersion(dir string, logger log.Logger, initialVersion int64) (types.CommitKVStore, error) {
Expand All @@ -34,10 +37,12 @@ func LoadStoreWithInitialVersion(dir string, logger log.Logger, initialVersion i
}

func (st *Store) Commit() types.CommitID {
st.tree.ApplyChangeSet(&st.changeSet)
hash, version, err := st.tree.SaveVersion(true)
if err != nil {
panic(err)
}
st.changeSet.Pairs = st.changeSet.Pairs[:0]

return types.CommitID{
Version: version,
Expand Down Expand Up @@ -79,8 +84,13 @@ func (st *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.Ca
return cachekv.NewStore(tracekv.NewStore(st, w, tc))
}

// Implements types.KVStore.
//
// we assume Set is only called in `Commit`, so the written state is only visible after commit.
func (st *Store) Set(key, value []byte) {
st.tree.Set(key, value)
st.changeSet.Pairs = append(st.changeSet.Pairs, &iavl.KVPair{
Key: key, Value: value,
})
}

// Implements types.KVStore.
Expand All @@ -94,8 +104,12 @@ func (st *Store) Has(key []byte) bool {
}

// Implements types.KVStore.
//
// we assume Delete is only called in `Commit`, so the written state is only visible after commit.
func (st *Store) Delete(key []byte) {
st.tree.Remove(key)
st.changeSet.Pairs = append(st.changeSet.Pairs, &iavl.KVPair{
Key: key, Delete: true,
})
}

func (st *Store) Iterator(start, end []byte) types.Iterator {
Expand Down

0 comments on commit 5106b68

Please sign in to comment.