Skip to content

Commit

Permalink
feat: expose nodeDB's DeleteVersionsFrom method (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell authored Jul 26, 2024
1 parent 59e2992 commit 558a18c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Changelog

## Unreleased

### Improvements

- [#952](https://github.com/cosmos/iavl/pull/952) Add `DeleteVersionsFrom(int64)` API.
- [#961](https://github.com/cosmos/iavl/pull/961) Add new `GetLatestVersion` API to get the latest version.

## v1.2.0 May 13, 2024
Expand Down
10 changes: 10 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,16 @@ func (tree *MutableTree) DeleteVersionsTo(toVersion int64) error {
return tree.ndb.Commit()
}

// DeleteVersionsFrom removes from the given version upwards from the MutableTree.
// It will not block the SaveVersion() call, instead it will be queued and executed deferred.
func (tree *MutableTree) DeleteVersionsFrom(fromVersion int64) error {
if err := tree.ndb.DeleteVersionsFrom(fromVersion); err != nil {
return err
}

return tree.ndb.Commit()
}

// Rotate right and return the new node and orphan.
func (tree *MutableTree) rotateRight(node *Node) (*Node, error) {
var err error
Expand Down
29 changes: 28 additions & 1 deletion mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestNewIteratorConcurrency(t *testing.T) {
}
}

func TestDelete(t *testing.T) {
func TestDeleteVersionsTo(t *testing.T) {
tree := setupMutableTree(false)

_, err := tree.set([]byte("k1"), []byte("Fred"))
Expand All @@ -132,6 +132,33 @@ func TestDelete(t *testing.T) {
require.Equal(t, 0, bytes.Compare([]byte("Fred"), proof.GetExist().Value))
}

func TestDeleteVersionsFrom(t *testing.T) {
tree := setupMutableTree(false)

_, err := tree.set([]byte("k1"), []byte("Wilma"))
require.NoError(t, err)
_, version, err := tree.SaveVersion()
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)

require.NoError(t, tree.DeleteVersionsFrom(version+1))

proof, err := tree.GetVersionedProof([]byte("k1"), version)
require.Nil(t, err)
require.Equal(t, 0, bytes.Compare([]byte("Wilma"), proof.GetExist().Value))

proof, err = tree.GetVersionedProof([]byte("k1"), version+1)
require.EqualError(t, err, ErrVersionDoesNotExist.Error())
require.Nil(t, proof)

proof, err = tree.GetVersionedProof([]byte("k1"), version+2)
require.EqualError(t, err, ErrVersionDoesNotExist.Error())
require.Nil(t, proof)
}

func TestGetRemove(t *testing.T) {
require := require.New(t)
tree := setupMutableTree(false)
Expand Down

0 comments on commit 558a18c

Please sign in to comment.