-
Notifications
You must be signed in to change notification settings - Fork 67
/
filter.go
273 lines (248 loc) · 5.54 KB
/
filter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package expr
import (
"bytes"
"errors"
"net/netip"
"strings"
"github.com/brimdata/zed"
"github.com/brimdata/zed/pkg/byteconv"
"github.com/brimdata/zed/zcode"
)
type searchByPred struct {
pred Boolean
expr Evaluator
types map[zed.Type]bool
}
func SearchByPredicate(pred Boolean, e Evaluator) Evaluator {
return &searchByPred{
pred: pred,
expr: e,
types: make(map[zed.Type]bool),
}
}
func (s *searchByPred) Eval(ectx Context, val *zed.Value) *zed.Value {
if s.expr != nil {
val = s.expr.Eval(ectx, val)
if val.IsError() {
return zed.False
}
}
tmpVal := ectx.NewValue(nil, nil)
if errMatch == val.Walk(func(typ zed.Type, body zcode.Bytes) error {
if s.searchType(typ) {
return errMatch
}
*tmpVal = *zed.NewValue(typ, body)
if s.pred(tmpVal) {
return errMatch
}
return nil
}) {
return zed.True
}
return zed.False
}
func (s *searchByPred) searchType(typ zed.Type) bool {
if match, ok := s.types[typ]; ok {
return match
}
var match bool
recType := zed.TypeRecordOf(typ)
if recType != nil {
var nameIter FieldNameIter
nameIter.Init(recType)
for !nameIter.Done() {
if s.pred(zed.NewString(string(nameIter.Next()))) {
match = true
break
}
}
}
s.types[typ] = match
s.types[recType] = match
return match
}
// stringSearch is like strings.Contains() but with case-insensitive
// comparison.
func stringSearch(a, b string) bool {
alen := len(a)
blen := len(b)
if blen > alen {
return false
}
end := alen - blen + 1
i := 0
for i < end {
if strings.EqualFold(a[i:i+blen], b) {
return true
}
i++
}
return false
}
var errMatch = errors.New("match")
type search struct {
text string
compare Boolean
expr Evaluator
}
// NewSearch creates a filter that searches Zed records for the
// given value, which must be of a type other than string. The filter
// matches a record that contains this value either as the value of any
// field or inside any set or array. It also matches a record if the string
// representaton of the search value appears inside inside any string-valued
// field (or inside any element of a set or array of strings).
func NewSearch(searchtext string, searchval *zed.Value, expr Evaluator) (Evaluator, error) {
if zed.TypeUnder(searchval.Type) == zed.TypeNet {
return &searchCIDR{
net: zed.DecodeNet(searchval.Bytes()),
bytes: searchval.Bytes(),
}, nil
}
typedCompare, err := Comparison("==", searchval)
if err != nil {
return nil, err
}
return &search{searchtext, typedCompare, expr}, nil
}
func (s *search) Eval(ectx Context, val *zed.Value) *zed.Value {
if s.expr != nil {
val = s.expr.Eval(ectx, val)
if val.IsError() {
return zed.False
}
}
tmpVal := ectx.NewValue(nil, nil)
if errMatch == val.Walk(func(typ zed.Type, body zcode.Bytes) error {
if typ.ID() == zed.IDString {
if stringSearch(byteconv.UnsafeString(body), s.text) {
return errMatch
}
return nil
}
*tmpVal = *zed.NewValue(typ, body)
if s.compare(tmpVal) {
return errMatch
}
return nil
}) {
return zed.True
}
return zed.False
}
type searchCIDR struct {
net netip.Prefix
bytes zcode.Bytes
}
func (s *searchCIDR) Eval(_ Context, val *zed.Value) *zed.Value {
if errMatch == val.Walk(func(typ zed.Type, body zcode.Bytes) error {
switch typ.ID() {
case zed.IDNet:
if bytes.Equal(body, s.bytes) {
return errMatch
}
case zed.IDIP:
if s.net.Contains(zed.DecodeIP(body)) {
return errMatch
}
}
return nil
}) {
return zed.True
}
return zed.False
}
type searchString struct {
term string
expr Evaluator
types map[zed.Type]bool
}
// NewSearchString is like NewSeach but handles the special case of matching
// field names in addition to string values.
func NewSearchString(term string, expr Evaluator) Evaluator {
return &searchString{
term: term,
expr: expr,
types: make(map[zed.Type]bool),
}
}
func (s *searchString) searchType(typ zed.Type) bool {
if match, ok := s.types[typ]; ok {
return match
}
var match bool
recType := zed.TypeRecordOf(typ)
if recType != nil {
var nameIter FieldNameIter
nameIter.Init(recType)
for !nameIter.Done() {
if stringSearch(byteconv.UnsafeString(nameIter.Next()), s.term) {
match = true
break
}
}
}
s.types[typ] = match
s.types[recType] = match
return match
}
func (s *searchString) Eval(ectx Context, val *zed.Value) *zed.Value {
if s.expr != nil {
val = s.expr.Eval(ectx, val)
if val.IsError() {
return zed.False
}
}
// Memoize the result of a search across the names in the
// record fields for each unique record type.
if s.searchType(val.Type) {
return zed.True
}
if errMatch == val.Walk(func(typ zed.Type, body zcode.Bytes) error {
if s.searchType(typ) {
return errMatch
}
if typ.ID() == zed.IDString &&
stringSearch(byteconv.UnsafeString(body), s.term) {
return errMatch
}
return nil
}) {
return zed.True
}
return zed.False
}
type filter struct {
expr Evaluator
pred Boolean
}
func NewFilter(expr Evaluator, pred Boolean) Evaluator {
return &filter{expr, pred}
}
func (f *filter) Eval(ectx Context, this *zed.Value) *zed.Value {
val := f.expr.Eval(ectx, this)
if val.IsError() {
return val
}
if f.pred(val) {
return zed.True
}
return zed.False
}
type filterApplier struct {
zctx *zed.Context
expr Evaluator
}
func NewFilterApplier(zctx *zed.Context, e Evaluator) Evaluator {
return &filterApplier{zctx, e}
}
func (f *filterApplier) Eval(ectx Context, this *zed.Value) *zed.Value {
val, ok := EvalBool(f.zctx, ectx, this, f.expr)
if ok {
if val.Bool() {
return this
}
return f.zctx.Missing()
}
return val
}