Skip to content

Commit

Permalink
vochain/processarchive: swap filepath.Walk for os.ReadDir
Browse files Browse the repository at this point in the history
filepath.Walk is mainly useful for many levels of directories,
but the archive data directory is entirely flat.
Plus, filepath.Walk stats every file that it walks over.

This is a bit simpler to understand and also less indented.
  • Loading branch information
mvdan authored and p4u committed Nov 7, 2023
1 parent 5ee13c9 commit a551aa1
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions vochain/processarchive/processarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,33 @@ func BuildIndex(datadir string) (*Index, error) {
Entities: make(map[string][]*IndexProcess),
}
count := 0
if err := filepath.Walk(datadir, func(path string, info os.FileInfo, err error) error {
if len(info.Name()) == 64 {
content, err := os.ReadFile(path)
if err != nil {
return err
}
if content == nil {
log.Warnf("archive file %s is empty", path)
return nil
}
count++
p := &Process{}
if err := json.Unmarshal(content, p); err != nil {
return err
}
eid := fmt.Sprintf("%x", p.ProcessInfo.EntityID)
i.Entities[eid] = append(i.Entities[eid], &IndexProcess{ProcessID: p.ProcessInfo.ID})
}
return nil
}); err != nil {
entries, err := os.ReadDir(datadir)
if err != nil {
return nil, err
}
for _, entry := range entries {
name := entry.Name()
path := filepath.Join(datadir, name)
if len(entry.Name()) != 64 {
log.Warnf("archive file %s has an invalid name", path)
continue
}
content, err := os.ReadFile(filepath.Join(datadir, name))
if err != nil {
return nil, err
}
if content == nil {
log.Warnf("archive file %s is empty", path)
continue
}
count++
p := &Process{}
if err := json.Unmarshal(content, p); err != nil {
return nil, err
}
eid := fmt.Sprintf("%x", p.ProcessInfo.EntityID)
i.Entities[eid] = append(i.Entities[eid], &IndexProcess{ProcessID: p.ProcessInfo.ID})
}
indexData, err := json.MarshalIndent(i, " ", " ")
if err != nil {
return nil, err
Expand Down

0 comments on commit a551aa1

Please sign in to comment.