Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 832c6bd

Browse files
committed
comments and better names
1 parent 1aa6922 commit 832c6bd

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

idx/memory/tag_query_id_filter.go

+41-10
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilt
8181
continue
8282
}
8383

84+
// if we don't use an inverted set of meta records, then we want to check if
85+
// all meta records involved in a meta tag filter use the "=" operator.
86+
// if this is the case then it is cheaper to build a set of acceptable tags
87+
// based on the meta record expressions and just check whether they are present
88+
// in a metric that gets filtered, compared to doing a full tag index lookup
89+
// to check whether a metric has one of the necessary meta tags associated
90+
// with it.
91+
optimizeForOnlyEqualOperators := !invertSetOfMetaRecords
8492
var metaRecordFilters []tagquery.MetricDefinitionFilter
85-
onlyEqualOperators := !invertSetOfMetaRecords
86-
singleEqualExprPerRecord := true
93+
singleExprPerRecord := true
8794
records := make([]tagquery.MetaTagRecord, len(metaRecordIds))
8895
for i, id := range metaRecordIds {
8996
record, ok := ctx.metaTagRecords.records[id]
@@ -93,24 +100,31 @@ func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilt
93100
continue
94101
}
95102

96-
if onlyEqualOperators {
103+
if optimizeForOnlyEqualOperators {
97104
for exprIdx := range record.Expressions {
98105
if record.Expressions[exprIdx].GetOperator() != tagquery.EQUAL {
99-
onlyEqualOperators = false
106+
optimizeForOnlyEqualOperators = false
100107
break
101108
}
102109
}
103-
records[i] = record
104-
if len(record.Expressions) > 1 {
105-
singleEqualExprPerRecord = false
110+
if optimizeForOnlyEqualOperators {
111+
records[i] = record
112+
if len(record.Expressions) > 1 {
113+
singleExprPerRecord = false
114+
}
106115
}
107116
}
108117

109118
metaRecordFilters = append(metaRecordFilters, record.GetMetricDefinitionFilter(ctx.index.idHasTag))
110119
}
111120

112-
if onlyEqualOperators {
113-
if singleEqualExprPerRecord {
121+
if optimizeForOnlyEqualOperators {
122+
// there are two different ways how we optimize for the case where all expressions
123+
// of all involved meta records are using the "=" operator. the first and fastest
124+
// way can only be used if each involved meta record only has one single expression,
125+
// otherwise we use the second way which is a bit more expensive but it also works
126+
// if some of the involved meta records have multiple expressions.
127+
if singleExprPerRecord {
114128
res.filters[i].testByMetaTags = metaRecordFilterBySetOfValidValues(records)
115129
} else {
116130
res.filters[i].testByMetaTags = metaRecordFilterBySetOfValidValueSets(records)
@@ -127,7 +141,16 @@ func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilt
127141
return &res
128142
}
129143

144+
// metaRecordFilterBySetOfValidValues creates a filter function to filter by a meta tag
145+
// which only involves meta records of which each only has exactly one expression and that
146+
// expression is using the "=" operator. this is quite a narrow scenario, but since it is
147+
// a very common use case it makes sense to optimize for it.
130148
func metaRecordFilterBySetOfValidValues(records []tagquery.MetaTagRecord) tagquery.MetricDefinitionFilter {
149+
// we first build a set of valid tags and names.
150+
// since we know that each of the involved meta records uses exactly one expression
151+
// which is using the "=" operator we know that if a given metric's name matches a
152+
// value in validNames or if one of its tags matches a value in validValues then this
153+
// is sufficient to let it pass the filter.
131154
validValues := make(map[string]struct{})
132155
validNames := make(map[string]struct{})
133156
var builder strings.Builder
@@ -154,12 +177,17 @@ func metaRecordFilterBySetOfValidValues(records []tagquery.MetaTagRecord) tagque
154177
}
155178
}
156179

180+
// metaRecordFilterBySetOfValidValueSets creates a filter function to filter by a meta tag
181+
// which only involves meta records of which all expressions are only using the "=" operator,
182+
// it is ok if one meta record uses multiple such expressions.
157183
func metaRecordFilterBySetOfValidValueSets(records []tagquery.MetaTagRecord) tagquery.MetricDefinitionFilter {
184+
// we first build a set of tag and name value combinations of which each is sufficient
185+
// to pass the generated filter when a metric contains all values of one of these
186+
// combinations
158187
validValueSets := make([]struct {
159188
name string
160189
tags []string
161190
}, len(records))
162-
163191
var builder strings.Builder
164192
for i := range records {
165193
validValueSets[i].tags = make([]string, 0, len(records[i].Expressions))
@@ -172,10 +200,13 @@ func metaRecordFilterBySetOfValidValueSets(records []tagquery.MetaTagRecord) tag
172200
builder.Reset()
173201
}
174202
}
203+
204+
// the tags must be sorted because that's a requirement for sliceContainsElements()
175205
sort.Strings(validValueSets[i].tags)
176206
}
177207

178208
return func(_ schema.MKey, name string, tags []string) tagquery.FilterDecision {
209+
// iterate over the acceptable value combinations and check if one matches this metric
179210
for _, validValueSet := range validValueSets {
180211
if len(validValueSet.name) > 0 {
181212
if name != validValueSet.name {

0 commit comments

Comments
 (0)