Skip to content

Commit

Permalink
feat: add api to save ChangeSet (#695)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <[email protected]>
  • Loading branch information
cool-develope and tac0turtle authored Mar 2, 2023
1 parent d2dd845 commit de1532f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [#636](https://github.com/cosmos/iavl/pull/636) Speed up rollback method: `LoadVersionForOverwriting`.
- [#654](https://github.com/cosmos/iavl/pull/654) Add API `TraverseStateChanges` to extract state changes from iavl versions.
- [#638](https://github.com/cosmos/iavl/pull/638) Make LazyLoadVersion check the opts.InitialVersion, add API `LazyLoadVersionForOverwriting`.
- [#695](https://github.com/cosmos/iavl/pull/695) Add API `SaveChangeSet` to save the changeset as a new version.

## 0.19.4 (October 28, 2022)

Expand Down
15 changes: 3 additions & 12 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,10 @@ func TestDiffRoundTrip(t *testing.T) {
db := db.NewMemDB()
tree, err := NewMutableTree(db, 0, true)
require.NoError(t, err)
for _, cs := range changeSets {
for _, pair := range cs.Pairs {
if pair.Delete {
_, removed, err := tree.Remove(pair.Key)
require.True(t, removed)
require.NoError(t, err)
} else {
_, err := tree.Set(pair.Key, pair.Value)
require.NoError(t, err)
}
}
_, _, err := tree.SaveVersion()
for i := range changeSets {
v, err := tree.SaveChangeSet(&changeSets[i])
require.NoError(t, err)
require.Equal(t, int64(i+1), v)
}

// extract change sets from db
Expand Down
26 changes: 26 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,3 +1012,29 @@ func (tree *MutableTree) balance(node *Node) (newSelf *Node, err error) {
// Nothing changed
return node, nil
}

// SaveChangeSet saves a ChangeSet to the tree.
// It is used to replay a ChangeSet as a new version.
func (tree *MutableTree) SaveChangeSet(cs *ChangeSet) (int64, error) {
// if the tree has uncommitted changes, return error
if tree.root != nil && !tree.root.persisted {
return 0, fmt.Errorf("cannot save changeset with uncommitted changes")
}
for _, pair := range cs.Pairs {
if pair.Delete {
_, removed, err := tree.Remove(pair.Key)
if !removed {
return 0, fmt.Errorf("attempted to remove non-existent key %s", pair.Key)
}
if err != nil {
return 0, err
}
} else {
if _, err := tree.Set(pair.Key, pair.Value); err != nil {
return 0, err
}
}
}
_, version, err := tree.SaveVersion()
return version, err
}

0 comments on commit de1532f

Please sign in to comment.