Implement parallel file processing

This commit is contained in:
2025-03-27 22:22:43 +01:00
parent 89eed3f847
commit 4e4b7bbd19

67
main.go
View File

@@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"sync"
@@ -13,7 +12,6 @@ import (
"github.com/go-git/go-git/v5"
"modify/logger"
"modify/processor"
)
type GlobalStats struct {
@@ -90,36 +88,45 @@ func main() {
// TODO: Utilize parallel workers for this
// Then for each file run all commands associated with the file
workers := make(chan struct{}, *utils.ParallelFiles)
wg := sync.WaitGroup{}
for file, commands := range associations {
fileData, err := os.ReadFile(file)
if err != nil {
logger.Error("Failed to read file %q: %v", file, err)
return
}
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
fileDataStr := string(fileData)
workers <- struct{}{}
wg.Add(1)
go func(file string, commands []utils.ModifyCommand) {
defer func() { <-workers }()
defer wg.Done()
// Aggregate all the modifications and execute them
modifications := []utils.ReplaceCommand{}
for _, command := range commands {
logger.Info("Processing file %q with command %q", file, command.Pattern)
// TODO: Run processor and return modifications
}
fileData, err := os.ReadFile(file)
if err != nil {
logger.Error("Failed to read file %q: %v", file, err)
return
}
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
fileDataStr := string(fileData)
// Sort commands in reverse order for safe replacements
sort.Slice(modifications, func(i, j int) bool {
return modifications[i].From > modifications[j].From
})
logger.Trace("Applying %d replacement commands in reverse order", len(modifications))
// Aggregate all the modifications and execute them
modifications := []utils.ReplaceCommand{}
for _, command := range commands {
logger.Info("Processing file %q with command %q", file, command.Pattern)
// TODO: Run processor and return modifications
}
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
logger.Info("Executed %d modifications for file %q", count, file)
// Sort commands in reverse order for safe replacements
sort.Slice(modifications, func(i, j int) bool {
return modifications[i].From > modifications[j].From
})
logger.Trace("Applying %d replacement commands in reverse order", len(modifications))
err = os.WriteFile(file, []byte(fileDataStr), 0644)
if err != nil {
logger.Error("Failed to write file %q: %v", file, err)
return
}
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
logger.Info("Executed %d modifications for file %q", count, file)
err = os.WriteFile(file, []byte(fileDataStr), 0644)
if err != nil {
logger.Error("Failed to write file %q: %v", file, err)
return
}
}(file, commands)
}
// This will also relieve processor of some of the file loading
@@ -169,12 +176,6 @@ func main() {
// return
// }
// Create the processor based on mode
var proc processor.Processor = &processor.RegexProcessor{}
var wg sync.WaitGroup
log.Printf("%#v", proc)
log.Printf("%#v", wg)
// Process each file
// for _, file := range files {
// wg.Add(1)