Skip to content

Commit

Permalink
Remove Stats structure
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 25, 2022
1 parent 684e011 commit df1f006
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 447 deletions.
7 changes: 6 additions & 1 deletion internal/trie/node/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ type Branch struct {
// which is updated to match the trie Generation once they are
// inserted, moved or iterated over.
Generation uint64
Stats Stats

// Statistics

// Descendants is the number of descendant nodes for
// this particular node.
Descendants uint32
}

// NewBranch creates a new branch using the arguments given.
Expand Down
6 changes: 3 additions & 3 deletions internal/trie/node/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package node
// children as well.
func (b *Branch) Copy(copyChildren bool) Node {
cpy := &Branch{
Dirty: b.Dirty,
Generation: b.Generation,
Stats: b.Stats,
Dirty: b.Dirty,
Generation: b.Generation,
Descendants: b.Descendants,
}

if copyChildren {
Expand Down
20 changes: 3 additions & 17 deletions internal/trie/node/stats.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
package node

// Stats contains statistical information for a node.
type Stats struct {
// Descendants is the number of descendant nodes for
// this particular node.
Descendants uint32
}

// NewStats creates a new Stats structure given the arguments.
func NewStats(descendants uint32) Stats {
return Stats{
Descendants: descendants,
}
}

// GetDescendants returns the number of descendants in the branch.
func (b *Branch) GetDescendants() (descendants uint32) {
return b.Stats.Descendants
return b.Descendants
}

// AddDescendants adds descendant nodes count to the node stats.
func (b *Branch) AddDescendants(n uint32) {
b.Stats.Descendants += n
b.Descendants += n
}

// SubDescendants subtracts descendant nodes count from the node stats.
func (b *Branch) SubDescendants(n uint32) {
b.Stats.Descendants -= n
b.Descendants -= n
}
32 changes: 5 additions & 27 deletions internal/trie/node/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,12 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_NewStats(t *testing.T) {
t.Parallel()

const descendants uint32 = 10
stats := NewStats(descendants)

expected := Stats{
Descendants: descendants,
}
assert.Equal(t, expected, stats)
}

func Test_Branch_GetDescendants(t *testing.T) {
t.Parallel()

const descendants uint32 = 10
branch := &Branch{
Stats: Stats{
Descendants: descendants,
},
Descendants: descendants,
}
result := branch.GetDescendants()

Expand All @@ -41,15 +27,11 @@ func Test_Branch_AddDescendants(t *testing.T) {
finalDescendants uint32 = 12
)
branch := &Branch{
Stats: Stats{
Descendants: initialDescendants,
},
Descendants: initialDescendants,
}
branch.AddDescendants(addDescendants)
expected := &Branch{
Stats: Stats{
Descendants: finalDescendants,
},
Descendants: finalDescendants,
}

assert.Equal(t, expected, branch)
Expand All @@ -64,15 +46,11 @@ func Test_Branch_SubDescendants(t *testing.T) {
finalDescendants uint32 = 8
)
branch := &Branch{
Stats: Stats{
Descendants: initialDescendants,
},
Descendants: initialDescendants,
}
branch.SubDescendants(subDescendants)
expected := &Branch{
Stats: Stats{
Descendants: finalDescendants,
},
Descendants: finalDescendants,
}

assert.Equal(t, expected, branch)
Expand Down
6 changes: 2 additions & 4 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,10 +1091,8 @@ func handleDeletion(branch *node.Branch, key []byte) (newNode Node, branchChildM
Value: childBranch.Value,
Generation: branch.Generation,
Dirty: true,
Stats: node.Stats{
// this is the descendants of the original branch minus one
Descendants: childBranch.GetDescendants(),
},
// this is the descendants of the original branch minus one
Descendants: childBranch.GetDescendants(),
}

// Adopt the grand-children
Expand Down
Loading

0 comments on commit df1f006

Please sign in to comment.