Skip to content

Commit

Permalink
feat: expose nodeDB's DeleteVersionsFrom method
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell committed Jun 24, 2024
1 parent ad49f93 commit 19e0b83
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/cometbft/cometbft-db v0.7.0
github.com/confio/ics23/go v0.9.0
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.6.0
github.com/golangci/golangci-lint v1.50.1
github.com/stretchr/testify v1.8.1
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
Expand Down
10 changes: 10 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,16 @@ func (tree *MutableTree) DeleteVersion(version int64) error {
return nil
}

// 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, *Node, error) {
version := tree.version + 1
Expand Down
27 changes: 27 additions & 0 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ func TestDelete(t *testing.T) {
require.Equal(t, 0, bytes.Compare([]byte("Fred"), proof.GetExist().Value))
}

func TestDeleteVersionsFrom(t *testing.T) {
tree := setupMutableTree(t, 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(t, false)
Expand Down

0 comments on commit 19e0b83

Please sign in to comment.