Memoize the match table
This commit is contained in:
@@ -37,14 +37,29 @@ func (c *ModifyCommand) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ehh.. Not much better... Guess this wasn't the big deal
|
||||||
|
var matchesMemoTable map[string]bool = make(map[string]bool)
|
||||||
|
func Matches(path string, glob string) (bool, error) {
|
||||||
|
key := fmt.Sprintf("%s:%s", path, glob)
|
||||||
|
if matches, ok := matchesMemoTable[key]; ok {
|
||||||
|
logger.Debug("Found match for file %q and glob %q in memo table", path, glob)
|
||||||
|
return matches, nil
|
||||||
|
}
|
||||||
|
matches, err := doublestar.Match(glob, path)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err)
|
||||||
|
}
|
||||||
|
matchesMemoTable[key] = matches
|
||||||
|
return matches, nil
|
||||||
|
}
|
||||||
|
|
||||||
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
|
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
|
||||||
associationCount := 0
|
associationCount := 0
|
||||||
fileCommands := make(map[string][]ModifyCommand)
|
fileCommands := make(map[string][]ModifyCommand)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
for _, glob := range command.Files {
|
||||||
// TODO: Maybe memoize this function call
|
matches, err := Matches(file, glob)
|
||||||
matches, err := doublestar.Match(glob, file)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
||||||
continue
|
continue
|
||||||
|
Reference in New Issue
Block a user