concurrent iterator: pass a constructor method instead of state.Trie
This allows us to use our alt trie implementation
This commit is contained in:
parent
3555790e76
commit
79a7643a28
@ -31,6 +31,10 @@ type PrefixBoundIterator struct {
|
|||||||
EndPath []byte
|
EndPath []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IteratorConstructor is a constructor returning a NodeIterator, which is used to decouple this
|
||||||
|
// code from the trie implementation.
|
||||||
|
type IteratorConstructor = func(startKey []byte) trie.NodeIterator
|
||||||
|
|
||||||
func (it *PrefixBoundIterator) Next(descend bool) bool {
|
func (it *PrefixBoundIterator) Next(descend bool) bool {
|
||||||
if it.EndPath == nil {
|
if it.EndPath == nil {
|
||||||
return it.NodeIterator.Next(descend)
|
return it.NodeIterator.Next(descend)
|
||||||
@ -130,10 +134,10 @@ func eachPrefixRange(prefix []byte, nbins uint, callback func([]byte, []byte)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SubtrieIterators cuts a trie by path prefix, returning `nbins` iterators covering its subtries
|
// SubtrieIterators cuts a trie by path prefix, returning `nbins` iterators covering its subtries
|
||||||
func SubtrieIterators(tree state.Trie, nbins uint) []trie.NodeIterator {
|
func SubtrieIterators(makeIterator IteratorConstructor, 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 := makeIterator(HexToKeyBytes(from))
|
||||||
iters = append(iters, NewPrefixBoundIterator(it, from, to))
|
iters = append(iters, NewPrefixBoundIterator(it, from, to))
|
||||||
})
|
})
|
||||||
return iters
|
return iters
|
||||||
|
@ -82,7 +82,7 @@ func TestIterator(t *testing.T) {
|
|||||||
allPaths := fixt.Block1_Paths
|
allPaths := fixt.Block1_Paths
|
||||||
cases := []uint{1, 2, 4, 8, 16, 32}
|
cases := []uint{1, 2, 4, 8, 16, 32}
|
||||||
runCase := func(t *testing.T, nbins uint) {
|
runCase := func(t *testing.T, nbins uint) {
|
||||||
iters := iter.SubtrieIterators(tree, nbins)
|
iters := iter.SubtrieIterators(tree.NodeIterator, nbins)
|
||||||
ix := 0
|
ix := 0
|
||||||
for b := uint(0); b < nbins; b++ {
|
for b := uint(0); b < nbins; b++ {
|
||||||
for it := iters[b]; it.Next(true); ix++ {
|
for it := iters[b]; it.Next(true); ix++ {
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/state"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
|
|
||||||
@ -122,7 +121,7 @@ func (tr *Tracker) dump() error {
|
|||||||
|
|
||||||
// Restore attempts to read iterator state from file
|
// Restore attempts to read iterator state from file
|
||||||
// if file doesn't exist, returns an empty slice with no error
|
// if file doesn't exist, returns an empty slice with no error
|
||||||
func (tr *Tracker) Restore(tree state.Trie) ([]trie.NodeIterator, error) {
|
func (tr *Tracker) Restore(makeIterator iter.IteratorConstructor) ([]trie.NodeIterator, error) {
|
||||||
file, err := os.Open(tr.recoveryFile)
|
file, err := os.Open(tr.recoveryFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@ -169,7 +168,7 @@ func (tr *Tracker) Restore(tree state.Trie) ([]trie.NodeIterator, error) {
|
|||||||
decrementPath(startPath)
|
decrementPath(startPath)
|
||||||
startPath = append(startPath, 0)
|
startPath = append(startPath, 0)
|
||||||
}
|
}
|
||||||
it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(startPath)), startPath, endPath)
|
it := iter.NewPrefixBoundIterator(makeIterator(iter.HexToKeyBytes(startPath)), startPath, endPath)
|
||||||
ret = append(ret, tr.Tracked(it, recoveredPath))
|
ret = append(ret, tr.Tracked(it, recoveredPath))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user