From c00cba98075ecddc82de4690574e06a9fa687ca3 Mon Sep 17 00:00:00 2001 From: ewy Date: Fri, 17 Apr 2026 00:41:31 +0200 Subject: make state creation asynchronous --- model/new.go | 60 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 26 deletions(-) (limited to 'model') diff --git a/model/new.go b/model/new.go index c2d5e37..e23bbf6 100644 --- a/model/new.go +++ b/model/new.go @@ -6,44 +6,52 @@ import ( "path/filepath" "pik/identity" "strings" + "sync" ) func NewState(f fs.FS, locations []string, indexers []Indexer, runners []Runner) (*State, []error) { var errs []error st := &State{} + wg := sync.WaitGroup{} for _, loc := range locations { - _, dirName := filepath.Split(loc) - src := &Source{ - Path: loc, - Identity: identity.New(dirName), - } - loc = strings.TrimSuffix(loc, "/") - loc = strings.TrimPrefix(loc, "/") - - if loc == "" { - continue - } - - for _, indexer := range indexers { + wg.Go(func() { + _, dirName := filepath.Split(loc) + src := &Source{ + Path: loc, + Identity: identity.New(dirName), + } + loc = strings.TrimSuffix(loc, "/") + loc = strings.TrimPrefix(loc, "/") - s, err := fs.Sub(f, loc) - if err != nil && !errors.Is(err, fs.ErrNotExist) { - errs = append(errs, err) - continue + if loc == "" { + return } - targets, err := indexer.Index("/"+loc, s, runners) - if err != nil && !errors.Is(err, fs.ErrNotExist) { - errs = append(errs, err) - continue + + myWg := sync.WaitGroup{} + for _, indexer := range indexers { + myWg.Go(func() { + s, err := fs.Sub(f, loc) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + errs = append(errs, err) + return + } + targets, err := indexer.Index("/"+loc, s, runners) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + errs = append(errs, err) + return + } + src.Targets = append(src.Targets, targets...) + }) } - src.Targets = append(src.Targets, targets...) - } + myWg.Wait() - if src.Targets != nil { - st.Sources = append(st.Sources, src) - } + if src.Targets != nil { + st.Sources = append(st.Sources, src) + } + }) } + wg.Wait() return st, errs } -- cgit v1.3