Skip to content

Commit

Permalink
adding regexp capabilities to speed up pattern checks per line dras…
Browse files Browse the repository at this point in the history
…tically
  • Loading branch information
symonk committed Jul 22, 2024
1 parent a9b9712 commit 283d1f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
6 changes: 3 additions & 3 deletions internal/analyser/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type Loader interface {
}

type LoadedFile struct {
File *os.File
Threshold config.Options
File *os.File
Options config.Options
}

type FileLoader struct {
Expand All @@ -29,7 +29,7 @@ func (f *FileLoader) Load() ([]LoadedFile, error) {
if err != nil {
return loaded, err
}
loaded = append(loaded, LoadedFile{File: opened, Threshold: individualFile.Threshold})
loaded = append(loaded, LoadedFile{File: opened, Options: individualFile.Threshold})
}
return loaded, nil
}
14 changes: 6 additions & 8 deletions internal/analyser/strategy.go → internal/analyser/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package analyser
import (
"bufio"
"log/slog"
"regexp"

"github.com/symonk/log-analyse/internal/re"
)

var strategyMap = map[string]func(loadedFile LoadedFile) []string{
Expand All @@ -16,15 +17,12 @@ var strategyMap = map[string]func(loadedFile LoadedFile) []string{
func seqScanStrategyFn(loadedFile LoadedFile) []string {
lines := make([]string, 0)
scanner := bufio.NewScanner(loadedFile.File)
patterns, _ := re.CompileSlice(loadedFile.Options.Patterns)
for scanner.Scan() {
line := scanner.Text()
for _, pattern := range loadedFile.Threshold.Patterns {
ok, err := regexp.Match(pattern, []byte(line))
if err != nil {
slog.Error("error matching line with pattern", slog.String("line", line), slog.String("pattern", pattern))
}
if ok {
slog.Info("matched", slog.String("line", line), slog.String("pattern", pattern))
for _, pattern := range patterns {
if ok := pattern.Match([]byte(line)); ok {
slog.Info("matched", slog.String("line", line), slog.Any("pattern", pattern))
lines = append(lines, line)
}
}
Expand Down
24 changes: 24 additions & 0 deletions internal/re/re.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package re

import (
"errors"
"regexp"
)

// CompileSlice takes a slice of string of what could be
// regular expression patterns, attempts to compile them and return
// the compiled pattern for use along with a slice of errors for any
// that failed.
func CompileSlice(patterns []string) ([]*regexp.Regexp, error) {
errs := make([]error, 0, len(patterns))
regexps := make([]*regexp.Regexp, 0, len(patterns))
for _, pattern := range patterns {
c, err := regexp.Compile(pattern)
if err != nil {
errs = append(errs, err)
} else {
regexps = append(regexps, c)
}
}
return regexps, errors.Join(errs...)
}
1 change: 1 addition & 0 deletions internal/re/re_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package re

0 comments on commit 283d1f1

Please sign in to comment.