Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add api to save ChangeSet #695

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}