Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up a merge conflict on log filter #4815

Merged
merged 2 commits into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions pkg/logql/log/filter.go
Expand Up @@ -83,6 +83,7 @@ func NewAndFilter(left Filterer, right Filterer) Filterer {
func (a andFilter) Filter(line []byte) bool {
return a.left.Filter(line) && a.right.Filter(line)
}

func (a andFilter) ToStage() Stage {
return StageFunc{
process: func(line []byte, _ *LabelsBuilder) ([]byte, bool) {
Expand Down Expand Up @@ -245,22 +246,30 @@ type containsFilter struct {
}

func (l *containsFilter) Filter(line []byte) bool {
if !l.caseInsensitive {
return bytes.Contains(line, l.match)
return contains(line, l.match, l.caseInsensitive)
}

func contains(line, substr []byte, caseInsensitive bool) bool {
if !caseInsensitive {
return bytes.Contains(line, substr)
}
if len(l.match) == 0 {
return containsLower(line, substr)
}

func containsLower(line, substr []byte) bool {
if len(substr) == 0 {
return true
}
if len(l.match) > len(line) {
if len(substr) > len(line) {
return false
}
j := 0
for len(line) > 0 {
// ascii fast case
if c := line[0]; c < utf8.RuneSelf {
if c == l.match[j] || c+'a'-'A' == l.match[j] {
if c == substr[j] || c+'a'-'A' == substr[j] {
j++
if j == len(l.match) {
if j == len(substr) {
return true
}
line = line[1:]
Expand All @@ -272,10 +281,10 @@ func (l *containsFilter) Filter(line []byte) bool {
}
// unicode slow case
lr, lwid := utf8.DecodeRune(line)
mr, mwid := utf8.DecodeRune(l.match[j:])
mr, mwid := utf8.DecodeRune(substr[j:])
if lr == mr || mr == unicode.To(unicode.LowerCase, lr) {
j += mwid
if j == len(l.match) {
if j == len(substr) {
return true
}
line = line[lwid:]
Expand Down Expand Up @@ -327,10 +336,7 @@ func (f *containsAllFilter) Empty() bool {

func (f containsAllFilter) Filter(line []byte) bool {
for _, m := range f.matches {
if m.caseInsensitive {
line = bytes.ToLower(line)
}
if !bytes.Contains(line, m.match) {
if !contains(line, m.match, m.caseInsensitive) {
return false
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/logql/log/ip.go
Expand Up @@ -192,7 +192,7 @@ func (f *ipFilter) filter(line []byte) bool {
}
ip, err := netaddr.ParseIP(string(line[start : start+iplen]))
if err == nil {
if contains(f.matcher, ip) {
if containsIP(f.matcher, ip) {
return true, 0
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func (f *ipFilter) filter(line []byte) bool {
return false
}

func contains(matcher IPMatcher, ip netaddr.IP) bool {
func containsIP(matcher IPMatcher, ip netaddr.IP) bool {
switch m := matcher.(type) {
case netaddr.IP:
return m.Compare(ip) == 0
Expand Down