-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathtag_rule.go
84 lines (76 loc) · 2.07 KB
/
tag_rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package predicate
import (
"fmt"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/storage/reads/datatypes"
)
// TagRuleNode is a node type of a single tag rule.
type TagRuleNode influxdb.TagRule
var specialKey = map[string]string{
"_measurement": models.MeasurementTagKey,
"_field": models.FieldKeyTagKey,
}
// NodeTypeLiteral convert a TagRuleNode to a nodeTypeLiteral.
func NodeTypeLiteral(tr TagRuleNode) *datatypes.Node {
switch tr.Operator {
case influxdb.RegexEqual:
fallthrough
case influxdb.NotRegexEqual:
return &datatypes.Node{
NodeType: datatypes.NodeTypeLiteral,
Value: &datatypes.Node_RegexValue{
RegexValue: tr.Value,
},
}
default:
return &datatypes.Node{
NodeType: datatypes.NodeTypeLiteral,
Value: &datatypes.Node_StringValue{
StringValue: tr.Value,
},
}
}
}
// NodeComparison convert influxdb.Operator to Node_Comparison.
func NodeComparison(op influxdb.Operator) (datatypes.Node_Comparison, error) {
switch op {
case influxdb.Equal:
return datatypes.ComparisonEqual, nil
case influxdb.NotEqual:
return datatypes.ComparisonNotEqual, nil
case influxdb.RegexEqual:
fallthrough
case influxdb.NotRegexEqual:
return 0, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("Operator %s is not supported for delete predicate yet", op),
}
default:
return 0, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("Unsupported operator: %s", op),
}
}
}
// ToDataType convert a TagRuleNode to datatypes.Node.
func (n TagRuleNode) ToDataType() (*datatypes.Node, error) {
compare, err := NodeComparison(n.Operator)
if err != nil {
return nil, err
}
if special, ok := specialKey[n.Key]; ok {
n.Key = special
}
return &datatypes.Node{
NodeType: datatypes.NodeTypeComparisonExpression,
Value: &datatypes.Node_Comparison_{Comparison: compare},
Children: []*datatypes.Node{
{
NodeType: datatypes.NodeTypeTagRef,
Value: &datatypes.Node_TagRefValue{TagRefValue: n.Key},
},
NodeTypeLiteral(n),
},
}, nil
}