Store start path in prefix bound iterator object (#17)

This commit is contained in:
prathamesh0 2022-08-03 16:12:45 +05:30 committed by GitHub
parent c6ac14bf08
commit b40114b739
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -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 {
@ -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
@ -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
}
@ -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

View File

@ -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())