Add tracker package, update and simplify #1

Merged
roysc merged 5 commits from dev into main 2023-09-19 16:58:25 +00:00
3 changed files with 18 additions and 12 deletions
Showing only changes of commit 9d890b2282 - Show all commits

View File

@ -40,9 +40,13 @@ func (it *PrefixBoundIterator) Next(descend bool) bool {
if !it.NodeIterator.Next(descend) { if !it.NodeIterator.Next(descend) {
return false return false
} }
// stop if underlying iterator went past upper bound // Stop if underlying iterator went past upper bound.
cmp := bytes.Compare(it.Path(), it.EndPath) // Note: this results in a single node of overlap between binned iterators. The more correct
return cmp <= 0 // behavior would be to make this a strict less-than, so that iterators cover mutually disjoint
// subtries. Unfortunately, the NodeIterator constructor takes a compact path, meaning
// odd-length paths must be padded with a 0, so e.g. [8] becomes [8, 0], which means we would
// skip [8]. So, we use <= here to cover that node for the "next" bin.
Review

Good comment.

Good comment.
return bytes.Compare(it.Path(), it.EndPath) <= 0
} }
// NewPrefixBoundIterator returns an iterator with an upper bound value (hex path prefix) // NewPrefixBoundIterator returns an iterator with an upper bound value (hex path prefix)

View File

@ -87,12 +87,13 @@ func TestIterator(t *testing.T) {
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++ {
if !bytes.Equal(allPaths[ix], it.Path()) { if !bytes.Equal(allPaths[ix], it.Path()) {
t.Fatalf("wrong path value\nexpected:\t%v\nactual:\t\t%v", t.Fatalf("wrong path value in bin %d (index %d)\nexpected:\t%v\nactual:\t\t%v",
allPaths[ix], it.Path()) b, ix, allPaths[ix], it.Path())
} }
} }
// if the last node path was even-length, it will be duplicated // if the last node path for the previous bin was even-length, the next iterator
if len(allPaths[ix-1])&0b1 == 0 { // will seek to the same node and it will be duplicated (see comment in Next()).
if len(allPaths[ix-1])&1 == 0 {
ix-- ix--
} }
} }

View File

@ -46,8 +46,8 @@ func (tr *Tracker) CaptureSignal(cancelCtx context.CancelFunc) {
go func() { go func() {
sig := <-sigChan sig := <-sigChan
log.Error("Signal received (%v), stopping", "signal", sig) log.Error("Signal received (%v), stopping", "signal", sig)
// cancel context on receiving a signal // Cancel context on receiving a signal. On cancellation, all tracked iterators complete
// on ctx cancellation, all the iterators complete processing of their current node before stopping // processing of their current node before stopping.
cancelCtx() cancelCtx()
}() }()
} }
@ -90,8 +90,9 @@ func (tr *Tracker) dump() error {
return out.WriteAll(rows) return out.WriteAll(rows)
} }
// Restore attempts to read iterator state from file // Restore attempts to read iterator state from the recovery file.
// if file doesn't exist, returns an empty slice with no error // If the file doesn't exist, returns an empty slice with no error.
// Restored iterators are constructed in the same order as in the returned slice.
func (tr *Tracker) Restore(makeIterator iter.IteratorConstructor) ([]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 {
@ -185,7 +186,7 @@ func (it *Iterator) Next(descend bool) bool {
} }
// Subtracts 1 from the last byte in a path slice, carrying if needed. // Subtracts 1 from the last byte in a path slice, carrying if needed.
// Does nothing, returning false, for all-zero inputs. // Does nothing, returning false, for all-zero inputs (underflow).
func decrementPath(path []byte) bool { func decrementPath(path []byte) bool {
// check for all zeros // check for all zeros
allzero := true allzero := true