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: remove orphans #646

Merged
merged 7 commits into from
Jan 23, 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

### Breaking Changes

- [#646](https://github.com/cosmos/iavl/pull/646) Remove the `orphans` from the storage
- [#622](https://github.com/cosmos/iavl/pull/622) `export/newExporter()` and `ImmutableTree.Export()` returns error for nil arguements

## Unreleased
### API Changes

- [#646](https://github.com/cosmos/iavl/pull/646) Remove the `DeleteVersion`, `DeleteVersions`, `DeleteVersionsRange` and introduce a new endpoint of `DeleteVersionsTo` instead
- [#586](https://github.com/cosmos/iavl/pull/586) Remove the `RangeProof` and refactor the ics23_proof to use the internal methods.
- [#640](https://github.com/cosmos/iavl/pull/640) commit `NodeDB` batch in `LoadVersionForOverwriting`.
- [#636](https://github.com/cosmos/iavl/pull/636) Speed up rollback method: `LoadVersionForOverwriting`.
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func commitTree(b *testing.B, t *iavl.MutableTree) {
}

if version > historySize {
err = t.DeleteVersion(version - historySize)
err = t.DeleteVersionsTo(version - historySize)
if err != nil {
b.Errorf("Can't delete: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,14 @@ func TestExporter_DeleteVersionErrors(t *testing.T) {
require.NoError(t, err)
defer exporter.Close()

err = tree.DeleteVersion(2)
require.Error(t, err)
err = tree.DeleteVersion(1)
err = tree.DeleteVersionsTo(1)
require.NoError(t, err)

err = tree.DeleteVersionsTo(2)
require.Error(t, err)

exporter.Close()
err = tree.DeleteVersion(2)
err = tree.DeleteVersionsTo(2)
require.NoError(t, err)
}

Expand Down
9 changes: 5 additions & 4 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,17 @@ func (iter *NodeIterator) Next(isSkipped bool) {
return
}

leftNode, err := iter.ndb.GetNode(node.leftHash)
rightNode, err := iter.ndb.GetNode(node.rightHash)
if err != nil {
iter.err = err
return
}
iter.nodesToVisit = append(iter.nodesToVisit, leftNode)
rightNode, err := iter.ndb.GetNode(node.rightHash)
iter.nodesToVisit = append(iter.nodesToVisit, rightNode)

leftNode, err := iter.ndb.GetNode(node.leftHash)
if err != nil {
iter.err = err
return
}
iter.nodesToVisit = append(iter.nodesToVisit, rightNode)
iter.nodesToVisit = append(iter.nodesToVisit, leftNode)
}
44 changes: 43 additions & 1 deletion iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestIterator_WithDelete_Full_Ascending_Success(t *testing.T) {
_, _, err = tree.SaveVersion()
require.NoError(t, err)

err = tree.DeleteVersion(1)
err = tree.DeleteVersionsTo(1)
require.NoError(t, err)

latestVersion, err := tree.ndb.getLatestVersion()
Expand Down Expand Up @@ -330,3 +330,45 @@ func setupUnsavedFastIterator(t *testing.T, config *iteratorTestConfig) (dbm.Ite
itr := NewUnsavedFastIterator(config.startIterate, config.endIterate, config.ascending, tree.ndb, tree.unsavedFastNodeAdditions, tree.unsavedFastNodeRemovals)
return itr, mirror
}

func TestNodeIterator_Success(t *testing.T) {
tree, mirror := getRandomizedTreeAndMirror(t)

_, _, err := tree.SaveVersion()
require.NoError(t, err)

randomizeTreeAndMirror(t, tree, mirror)

_, _, err = tree.SaveVersion()
require.NoError(t, err)

// check if the iterating count is same with the entire node count of the tree
itr, err := NewNodeIterator(tree.root.hash, tree.ndb)
require.NoError(t, err)
nodeCount := 0
for ; itr.Valid(); itr.Next(false) {
nodeCount++
}
require.Equal(t, int64(nodeCount), tree.Size()*2-1)

// check if the skipped node count is right
itr, err = NewNodeIterator(tree.root.hash, tree.ndb)
require.NoError(t, err)
updateCount := 0
skipCount := 0
for itr.Valid() {
node := itr.GetNode()
updateCount++
if node.version < tree.Version() {
skipCount += int(node.size*2 - 2) // the size of the subtree without the root
}
itr.Next(node.version < tree.Version())
}
require.Equal(t, nodeCount, updateCount+skipCount)
}

func TestNodeIterator_WithEmptyRoot(t *testing.T) {
itr, err := NewNodeIterator([]byte{}, newNodeDB(dbm.NewMemDB(), 0, nil))
require.NoError(t, err)
require.False(t, itr.Valid())
}
Loading