Skip to content

Commit

Permalink
Store start path in prefix bound iterator object (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 authored Aug 3, 2022
1 parent c6ac14b commit b40114b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
// PrefixBoundIterator is a NodeIterator constrained by a lower & upper bound (as hex path prefixes)
type PrefixBoundIterator struct {
trie.NodeIterator
EndPath []byte
StartPath []byte
EndPath []byte
}

func (it *PrefixBoundIterator) Next(descend bool) bool {
Expand All @@ -43,8 +44,8 @@ func (it *PrefixBoundIterator) Next(descend bool) bool {
}

// Iterator with an upper bound value (hex path prefix)
func NewPrefixBoundIterator(it trie.NodeIterator, to []byte) *PrefixBoundIterator {
return &PrefixBoundIterator{NodeIterator: it, EndPath: to}
func NewPrefixBoundIterator(it trie.NodeIterator, from []byte, to []byte) *PrefixBoundIterator {
return &PrefixBoundIterator{NodeIterator: it, StartPath: from, EndPath: to}
}

// generates nibble slice prefixes at uniform intervals
Expand Down Expand Up @@ -133,7 +134,7 @@ func SubtrieIterators(tree state.Trie, nbins uint) []trie.NodeIterator {
var iters []trie.NodeIterator
eachPrefixRange(nil, nbins, func(from []byte, to []byte) {
it := tree.NodeIterator(HexToKeyBytes(from))
iters = append(iters, NewPrefixBoundIterator(it, to))
iters = append(iters, NewPrefixBoundIterator(it, from, to))
})
return iters
}
Expand All @@ -148,7 +149,7 @@ func (fac *SubtrieIteratorFactory) Length() int { return len(fac.startPaths) }

func (fac *SubtrieIteratorFactory) IteratorAt(bin uint) *PrefixBoundIterator {
it := fac.tree.NodeIterator(HexToKeyBytes(fac.startPaths[bin]))
return NewPrefixBoundIterator(it, fac.endPaths[bin])
return NewPrefixBoundIterator(it, fac.startPaths[bin], fac.endPaths[bin])
}

// Cut a trie by path prefix, returning `nbins` iterators covering its subtries
Expand Down
2 changes: 1 addition & 1 deletion iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestIterator(t *testing.T) {
}

runCase := func(t *testing.T, tc testCase) {
it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(tc.lower)), tc.upper)
it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(tc.lower)), tc.lower, tc.upper)
for it.Next(true) {
if bytes.Compare(it.Path(), tc.lower) < 0 {
t.Fatalf("iterator outside lower bound: %v", it.Path())
Expand Down

0 comments on commit b40114b

Please sign in to comment.