-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib/trie): add descendants count to trie branches (#2310)
* Note: descendants are stored in every node since we have a delete operation that cuts of an entire branch, so it's more performant to have it get the count of descendants to remove from the branch being removed, rather than iterating over this branch and all its children. * Adapt existing tests * Add test cases to reach back full test coverage
- Loading branch information
Showing
12 changed files
with
962 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package node | ||
|
||
// GetDescendants returns the number of descendants in the branch. | ||
func (b *Branch) GetDescendants() (descendants uint32) { | ||
return b.Descendants | ||
} | ||
|
||
// AddDescendants adds descendant nodes count to the node stats. | ||
func (b *Branch) AddDescendants(n uint32) { | ||
b.Descendants += n | ||
} | ||
|
||
// SubDescendants subtracts descendant nodes count from the node stats. | ||
func (b *Branch) SubDescendants(n uint32) { | ||
b.Descendants -= n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package node | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Branch_GetDescendants(t *testing.T) { | ||
t.Parallel() | ||
|
||
const descendants uint32 = 10 | ||
branch := &Branch{ | ||
Descendants: descendants, | ||
} | ||
result := branch.GetDescendants() | ||
|
||
assert.Equal(t, descendants, result) | ||
} | ||
|
||
func Test_Branch_AddDescendants(t *testing.T) { | ||
t.Parallel() | ||
|
||
const ( | ||
initialDescendants uint32 = 10 | ||
addDescendants uint32 = 2 | ||
finalDescendants uint32 = 12 | ||
) | ||
branch := &Branch{ | ||
Descendants: initialDescendants, | ||
} | ||
branch.AddDescendants(addDescendants) | ||
expected := &Branch{ | ||
Descendants: finalDescendants, | ||
} | ||
|
||
assert.Equal(t, expected, branch) | ||
} | ||
|
||
func Test_Branch_SubDescendants(t *testing.T) { | ||
t.Parallel() | ||
|
||
const ( | ||
initialDescendants uint32 = 10 | ||
subDescendants uint32 = 2 | ||
finalDescendants uint32 = 8 | ||
) | ||
branch := &Branch{ | ||
Descendants: initialDescendants, | ||
} | ||
branch.SubDescendants(subDescendants) | ||
expected := &Branch{ | ||
Descendants: finalDescendants, | ||
} | ||
|
||
assert.Equal(t, expected, branch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.