mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-02-09 15:27:11 +00:00
lib/logstorage: make sure that getCommonTokens returns common tokens in the original order of tokens inside tokenSets arg
This fixes flaky test TestGetCommonTokensForOrFilters: filter_or_test.go:143: unexpected tokens for field "_msg"; got ["foo" "bar"]; want ["bar" "foo"]
This commit is contained in:
parent
c00b64726c
commit
a3d8077959
1 changed files with 11 additions and 17 deletions
|
@ -496,33 +496,27 @@ func getCommonTokensAndTokenSets(values []string) ([]string, [][]string) {
|
||||||
return commonTokens, tokenSets
|
return commonTokens, tokenSets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getCommonTokens returns common tokens seen at every set of tokens inside tokenSets.
|
||||||
|
//
|
||||||
|
// The returned common tokens preserve the original order seen in tokenSets.
|
||||||
func getCommonTokens(tokenSets [][]string) []string {
|
func getCommonTokens(tokenSets [][]string) []string {
|
||||||
if len(tokenSets) == 0 {
|
if len(tokenSets) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
m := make(map[string]struct{}, len(tokenSets[0]))
|
commonTokens := append([]string{}, tokenSets[0]...)
|
||||||
for _, token := range tokenSets[0] {
|
|
||||||
m[token] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tokens := range tokenSets[1:] {
|
for _, tokens := range tokenSets[1:] {
|
||||||
if len(m) == 0 {
|
if len(commonTokens) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for token := range m {
|
dst := commonTokens[:0]
|
||||||
if !slices.Contains(tokens, token) {
|
for _, token := range commonTokens {
|
||||||
delete(m, token)
|
if slices.Contains(tokens, token) {
|
||||||
|
dst = append(dst, token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
commonTokens = dst
|
||||||
}
|
}
|
||||||
if len(m) == 0 {
|
return commonTokens
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
tokens := make([]string, 0, len(m))
|
|
||||||
for token := range m {
|
|
||||||
tokens = append(tokens, token)
|
|
||||||
}
|
|
||||||
return tokens
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue