From 4e4b7bbd194b024e7cc484b62da247d8edb8629e Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 27 Mar 2025 22:22:43 +0100 Subject: [PATCH] Implement parallel file processing --- main.go | 67 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index cf4c43d..d26a0e1 100644 --- a/main.go +++ b/main.go @@ -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)