Store start path in prefix bound iterator object (#17)
This commit is contained in:
parent
c6ac14bf08
commit
b40114b739
@ -27,6 +27,7 @@ import (
|
|||||||
// PrefixBoundIterator is a NodeIterator constrained by a lower & upper bound (as hex path prefixes)
|
// PrefixBoundIterator is a NodeIterator constrained by a lower & upper bound (as hex path prefixes)
|
||||||
type PrefixBoundIterator struct {
|
type PrefixBoundIterator struct {
|
||||||
trie.NodeIterator
|
trie.NodeIterator
|
||||||
|
StartPath []byte
|
||||||
EndPath []byte
|
EndPath []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,8 +44,8 @@ func (it *PrefixBoundIterator) Next(descend bool) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterator with an upper bound value (hex path prefix)
|
// Iterator with an upper bound value (hex path prefix)
|
||||||
func NewPrefixBoundIterator(it trie.NodeIterator, to []byte) *PrefixBoundIterator {
|
func NewPrefixBoundIterator(it trie.NodeIterator, from []byte, to []byte) *PrefixBoundIterator {
|
||||||
return &PrefixBoundIterator{NodeIterator: it, EndPath: to}
|
return &PrefixBoundIterator{NodeIterator: it, StartPath: from, EndPath: to}
|
||||||
}
|
}
|
||||||
|
|
||||||
// generates nibble slice prefixes at uniform intervals
|
// generates nibble slice prefixes at uniform intervals
|
||||||
@ -133,7 +134,7 @@ func SubtrieIterators(tree state.Trie, nbins uint) []trie.NodeIterator {
|
|||||||
var iters []trie.NodeIterator
|
var iters []trie.NodeIterator
|
||||||
eachPrefixRange(nil, nbins, func(from []byte, to []byte) {
|
eachPrefixRange(nil, nbins, func(from []byte, to []byte) {
|
||||||
it := tree.NodeIterator(HexToKeyBytes(from))
|
it := tree.NodeIterator(HexToKeyBytes(from))
|
||||||
iters = append(iters, NewPrefixBoundIterator(it, to))
|
iters = append(iters, NewPrefixBoundIterator(it, from, to))
|
||||||
})
|
})
|
||||||
return iters
|
return iters
|
||||||
}
|
}
|
||||||
@ -148,7 +149,7 @@ func (fac *SubtrieIteratorFactory) Length() int { return len(fac.startPaths) }
|
|||||||
|
|
||||||
func (fac *SubtrieIteratorFactory) IteratorAt(bin uint) *PrefixBoundIterator {
|
func (fac *SubtrieIteratorFactory) IteratorAt(bin uint) *PrefixBoundIterator {
|
||||||
it := fac.tree.NodeIterator(HexToKeyBytes(fac.startPaths[bin]))
|
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
|
// Cut a trie by path prefix, returning `nbins` iterators covering its subtries
|
||||||
|
@ -59,7 +59,7 @@ func TestIterator(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runCase := func(t *testing.T, tc testCase) {
|
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) {
|
for it.Next(true) {
|
||||||
if bytes.Compare(it.Path(), tc.lower) < 0 {
|
if bytes.Compare(it.Path(), tc.lower) < 0 {
|
||||||
t.Fatalf("iterator outside lower bound: %v", it.Path())
|
t.Fatalf("iterator outside lower bound: %v", it.Path())
|
||||||
|
Loading…
Reference in New Issue
Block a user