Skip to content

Commit bc6be5f

Browse files
committed
fix: improve performance
1 parent 38a158d commit bc6be5f

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

config/v3_5_experimental/types/config.go

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var (
3535
}
3636
)
3737

38+
var paths = map[string]struct{}{}
39+
3840
func (cfg Config) Validate(c path.ContextPath) (r report.Report) {
3941
systemdPath := "/etc/systemd/system/"
4042
unitPaths := map[string]struct{}{}
@@ -76,43 +78,21 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
7678
Path string
7779
Field string
7880
}
79-
paths := map[string]struct{}{}
8081
r := report.Report{}
8182

8283
for i, f := range cfg.Storage.Files {
83-
if _, exists := paths[f.Path]; exists {
84-
r.AddOnError(c.Append("storage", "files", i, "path"), errors.ErrPathConflictsParentDir) //TODO: should add different error?
85-
return r
86-
}
87-
paths[f.Path] = struct{}{}
88-
entries = append(entries, struct {
89-
Path string
90-
Field string
91-
}{Path: f.Path, Field: "files"})
84+
r = handlePathConflict(f.Path, "files", i, c, r, errors.ErrPathConflictsParentDir)
85+
addPathAndEntry(f.Path, "files", &entries)
9286
}
9387

9488
for i, d := range cfg.Storage.Directories {
95-
if _, exists := paths[d.Path]; exists {
96-
r.AddOnError(c.Append("storage", "directories", i, "path"), errors.ErrPathConflictsParentDir) //TODO: should add different error?
97-
return r
98-
}
99-
paths[d.Path] = struct{}{}
100-
entries = append(entries, struct {
101-
Path string
102-
Field string
103-
}{Path: d.Path, Field: "directories"})
89+
r = handlePathConflict(d.Path, "directories", i, c, r, errors.ErrPathConflictsParentDir)
90+
addPathAndEntry(d.Path, "directories", &entries)
10491
}
10592

10693
for i, l := range cfg.Storage.Links {
107-
if _, exists := paths[l.Path]; exists {
108-
r.AddOnError(c.Append("storage", "links", i, "path"), errors.ErrPathConflictsParentDir) //TODO: error to already exist path
109-
return r
110-
}
111-
paths[l.Path] = struct{}{}
112-
entries = append(entries, struct {
113-
Path string
114-
Field string
115-
}{Path: l.Path, Field: "links"})
94+
r = handlePathConflict(l.Path, "links", i, c, r, errors.ErrPathConflictsParentDir)
95+
addPathAndEntry(l.Path, "links", &entries)
11696
}
11797

11898
sort.Slice(entries, func(i, j int) bool {
@@ -122,7 +102,7 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
122102
for i, entry := range entries {
123103
if i > 0 && isWithin(entry.Path, entries[i-1].Path) {
124104
if entries[i-1].Field != "directories" {
125-
r.AddOnError(c.Append("storage", entry.Field, i, "path"), errors.ErrPathConflictsParentDir) //TODO: conflict parent directories error
105+
r.AddOnError(c.Append("storage", entry.Field, i, "path"), errors.ErrPathConflictsParentDir)
126106
return r
127107
}
128108
}
@@ -131,7 +111,20 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
131111
return r
132112
}
133113

134-
// check the depth
114+
func handlePathConflict(path, fieldName string, index int, c path.ContextPath, r report.Report, err error) report.Report {
115+
if _, exists := paths[path]; exists {
116+
r.AddOnError(c.Append("storage", fieldName, index, "path"), err)
117+
}
118+
return r
119+
}
120+
121+
func addPathAndEntry(path, fieldName string, entries *[]struct{ Path, Field string }) {
122+
*entries = append(*entries, struct {
123+
Path string
124+
Field string
125+
}{Path: path, Field: fieldName})
126+
}
127+
135128
func depth(path string) uint {
136129
var count uint
137130
for p := filepath.Clean(path); p != "/" && p != "."; count++ {
@@ -140,7 +133,6 @@ func depth(path string) uint {
140133
return count
141134
}
142135

143-
// isWithin checks if newPath is within prevPath.
144136
func isWithin(newPath, prevPath string) bool {
145137
return strings.HasPrefix(newPath, prevPath) && newPath != prevPath
146138
}

0 commit comments

Comments
 (0)