From 558a18ca3a26b35ba1682a31b59271e481210618 Mon Sep 17 00:00:00 2001 From: Robert Pirtle Date: Fri, 26 Jul 2024 06:36:50 -0700 Subject: [PATCH] feat: expose nodeDB's DeleteVersionsFrom method (#952) --- CHANGELOG.md | 3 +++ mutable_tree.go | 10 ++++++++++ mutable_tree_test.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a4e297e8..d35d81d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/mutable_tree.go b/mutable_tree.go index 1497fc074..a671c3cd3 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -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 diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 9d8aca5cc..f184f7142 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -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")) @@ -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)