Skip to content

Commit

Permalink
core/rawdb: make forEach anonymous fn
Browse files Browse the repository at this point in the history
  • Loading branch information
s1na committed Dec 2, 2021
1 parent 95bf938 commit 374d17b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
26 changes: 25 additions & 1 deletion core/rawdb/freezer.go
Expand Up @@ -569,6 +569,30 @@ func (f *freezer) MigrateTable(kind string, isLegacy isLegacyFn, convert convert
if !ok {
return errUnknownTable
}
// forEach iterates every entry in the table serially and in order, calling `fn`
// with the item as argument. If `fn` returns an error the iteration stops
// and that error will be returned.
forEach := func(t *freezerTable, fn func(uint64, []byte) error) error {
items := atomic.LoadUint64(&t.items)
batchSize := uint64(1024)
maxBytes := uint64(1024 * 1024)
for i := uint64(0); i < items; {
if i+batchSize > items {
batchSize = items - i
}
data, err := t.RetrieveItems(i, batchSize, maxBytes)
if err != nil {
return err
}
for j, item := range data {
if err := fn(i+uint64(j), item); err != nil {
return err
}
}
i += uint64(len(data))
}
return nil
}
ancientsPath := filepath.Dir(table.index.Name())
// Set up new dir for the migrated table, the content of which
// we'll at the end move over to the ancients dir.
Expand All @@ -580,7 +604,7 @@ func (f *freezer) MigrateTable(kind string, isLegacy isLegacyFn, convert convert
batch := newTable.newBatch()
var out []byte
// Iterate through entries and transform them
table.forEach(func(i uint64, blob []byte) error {
forEach(table, func(i uint64, blob []byte) error {
if i%500000 == 0 {
log.Info("Processing legacy elements", "number", i)
}
Expand Down
25 changes: 0 additions & 25 deletions core/rawdb/freezer_table.go
Expand Up @@ -740,28 +740,3 @@ func (t *freezerTable) dumpIndex(w io.Writer, start, stop int64) {
}
fmt.Fprintf(w, "|--------------------------|\n")
}

// forEach iterates every entry in the table serially and in order, calling `fn`
// with the item as argument. If `fn` returns an error the iteration stops
// and that error will be returned.
func (t *freezerTable) forEach(fn func(uint64, []byte) error) error {
items := atomic.LoadUint64(&t.items)
batchSize := uint64(1024)
maxBytes := uint64(1024 * 1024)
for i := uint64(0); i < items; {
if i+batchSize > items {
batchSize = items - i
}
data, err := t.RetrieveItems(i, batchSize, maxBytes)
if err != nil {
return err
}
for j, item := range data {
if err := fn(i+uint64(j), item); err != nil {
return err
}
}
i += uint64(len(data))
}
return nil
}

0 comments on commit 374d17b

Please sign in to comment.