/
rawchecker.go
419 lines (377 loc) · 12.6 KB
/
rawchecker.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) 2021-2024 SigScalr, Inc.
//
// This file is part of SigLens Observability Solution
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package writer
import (
"bytes"
"errors"
"fmt"
. "github.com/siglens/siglens/pkg/segment/structs"
. "github.com/siglens/siglens/pkg/segment/utils"
dtu "github.com/siglens/siglens/pkg/common/dtypeutils"
"github.com/siglens/siglens/pkg/utils"
log "github.com/sirupsen/logrus"
)
func ApplySearchToMatchFilterRawCsg(match *MatchFilter, col []byte) (bool, error) {
if len(match.MatchWords) == 0 {
return true, nil
}
if len(col) == 0 {
return false, errors.New("column does not exist")
}
if col[0] != VALTYPE_ENC_SMALL_STRING[0] {
return false, nil
}
idx := uint16(1) // for encoding type
// next 2 bytes tell us the len of column
clen := utils.BytesToUint16LittleEndian(col[idx : idx+COL_OFF_BYTE_SIZE])
idx += COL_OFF_BYTE_SIZE
// todo MatchWords struct can store bytes
if match.MatchOperator == And {
var foundQword bool = true
if match.MatchType == MATCH_PHRASE {
regexp, err := match.GetRegexp()
if err != nil {
log.Errorf("ApplySearchToMatchFilterRawCsg: error getting match regex: %v", err)
return false, err
}
if regexp != nil {
foundQword = regexp.Match(col[idx : idx+clen])
} else {
foundQword = utils.IsSubWordPresent(col[idx:idx+clen], match.MatchPhrase)
}
} else {
for _, qword := range match.MatchWords {
foundQword = utils.IsSubWordPresent(col[idx:idx+clen], []byte(qword))
if !foundQword {
break
}
}
}
return foundQword, nil
}
if match.MatchOperator == Or {
var foundQword bool
for _, qword := range match.MatchWords {
foundQword = utils.IsSubWordPresent(col[idx:idx+clen], []byte(qword))
if foundQword {
return true, nil
}
}
return false, nil
}
return false, nil
}
func ApplySearchToDictArrayFilter(col []byte, qValDte *DtypeEnclosure, rec []byte, fop FilterOperator, isRegexSearch bool,
holderDte *DtypeEnclosure) (bool, error) {
if qValDte == nil {
return false, nil
}
if len(rec) == 0 || rec[0] != VALTYPE_DICT_ARRAY[0] {
return false, nil
} else if rec[0] == VALTYPE_DICT_ARRAY[0] {
//loop over the dict arrray till we reach the end
totalLen := utils.BytesToInt16LittleEndian(rec[1:])
idx := uint16(3)
var keyEquals, valEquals bool
var err error
for idx < uint16(totalLen) {
strlen := utils.BytesToUint16LittleEndian(rec[idx : idx+2])
idx += 2
if int(strlen) == len(col) {
keyEquals = bytes.Equal(rec[idx:idx+strlen], col)
}
idx += strlen
if !keyEquals {
switch rec[idx] {
case VALTYPE_ENC_SMALL_STRING[0]:
// one byte for type & two for reclen
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3 + strlen
case VALTYPE_ENC_BOOL[0]:
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3 + strlen
case VALTYPE_ENC_INT64[0], VALTYPE_ENC_FLOAT64[0]:
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3 + strlen
default:
log.Errorf("ApplySearchToDictArrayFilter:SS_DT_ARRAY_DICT unknown type=%v\n", rec[idx])
return false, errors.New("invalid rec type")
}
continue
}
switch rec[idx] {
case VALTYPE_ENC_SMALL_STRING[0]:
// one byte for type & two for reclen
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3
valEquals = bytes.Equal(rec[idx:idx+strlen], qValDte.StringValBytes)
idx += strlen
case VALTYPE_ENC_BOOL[0]:
// valEquals, err = fopOnBool(rec[idx:], qValDte, fop)
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3
valEquals = bytes.Equal(rec[idx:idx+strlen], qValDte.StringValBytes)
idx += strlen
case VALTYPE_ENC_INT64[0]:
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3
valEquals = bytes.Equal(rec[idx:idx+strlen], qValDte.StringValBytes)
idx += strlen
case VALTYPE_ENC_FLOAT64[0]:
strlen := utils.BytesToUint16LittleEndian(rec[idx+1 : idx+3])
idx += 3
valEquals = bytes.Equal(rec[idx:idx+strlen], qValDte.StringValBytes)
idx += strlen
default:
log.Errorf("ApplySearchToDictArrayFilter:SS_DT_ARRAY_DICT unknown type=%v\n", rec[idx])
return false, errors.New("invalid rec type")
}
if keyEquals && valEquals {
return true, nil
}
}
return keyEquals && valEquals, err
}
return false, nil
}
func ApplySearchToExpressionFilterSimpleCsg(qValDte *DtypeEnclosure, fop FilterOperator,
col []byte, isRegexSearch bool, holderDte *DtypeEnclosure) (bool, error) {
holderDte.Reset()
return filterOpOnDataType(col, qValDte, fop, isRegexSearch, holderDte)
}
func isValTypeEncANumber(valTypeEnc byte) bool {
switch valTypeEnc {
case VALTYPE_ENC_INT8[0], VALTYPE_ENC_INT16[0], VALTYPE_ENC_INT32[0], VALTYPE_ENC_INT64[0],
VALTYPE_ENC_UINT8[0], VALTYPE_ENC_UINT16[0], VALTYPE_ENC_UINT32[0], VALTYPE_ENC_UINT64[0],
VALTYPE_ENC_FLOAT64[0]:
return true
}
return false
}
func filterOpOnDataType(rec []byte, qValDte *DtypeEnclosure, fop FilterOperator,
isRegexSearch bool, recDte *DtypeEnclosure) (bool, error) {
if qValDte == nil {
return false, nil
}
switch qValDte.Dtype {
case SS_DT_STRING:
if len(rec) == 0 || rec[0] != VALTYPE_ENC_SMALL_STRING[0] {
// if we are doing a regex search on a number, we need to convert the number to string
if isRegexSearch && isValTypeEncANumber(rec[0]) {
return filterOpOnRecNumberEncType(rec, qValDte, fop, isRegexSearch, recDte)
}
return false, nil
}
return fopOnString(rec, qValDte, fop, isRegexSearch)
case SS_DT_BOOL:
if len(rec) == 0 || rec[0] != VALTYPE_ENC_BOOL[0] {
return false, nil
}
return fopOnBool(rec, qValDte, fop)
case SS_DT_SIGNED_NUM, SS_DT_UNSIGNED_NUM, SS_DT_FLOAT:
return fopOnNumber(rec, qValDte, recDte, fop)
case SS_DT_BACKFILL:
return false, nil
default:
return false, errors.New("filterOpOnDataType:could not complete op")
}
}
func filterOpOnRecNumberEncType(rec []byte, qValDte *DtypeEnclosure, fop FilterOperator,
isRegexSearch bool, recDte *DtypeEnclosure) (bool, error) {
if qValDte == nil || !isRegexSearch {
return false, nil
}
validNumberType, err := getNumberRecDte(rec, recDte)
if !validNumberType {
return false, err
}
regexp := qValDte.GetRegexp()
if regexp == nil {
return false, errors.New("qValDte had nil regexp compilation")
}
var recValString string
if recDte.Dtype == SS_DT_FLOAT {
recValString = fmt.Sprintf("%f", recDte.FloatVal)
} else if recDte.Dtype == SS_DT_UNSIGNED_NUM {
recValString = fmt.Sprintf("%d", recDte.UnsignedVal)
} else if recDte.Dtype == SS_DT_SIGNED_NUM {
recValString = fmt.Sprintf("%d", recDte.SignedVal)
} else {
return false, errors.New("filterOpOnRecNumberEncType: unknown dtype")
}
if fop == Equals {
return regexp.Match([]byte(recValString)), nil
} else if fop == NotEquals {
return !regexp.Match([]byte(recValString)), nil
} else {
return false, nil
}
}
func fopOnString(rec []byte, qValDte *DtypeEnclosure, fop FilterOperator,
isRegexSearch bool) (bool, error) {
var sOff uint16 = 3
switch fop {
case Equals:
if isRegexSearch {
regexp := qValDte.GetRegexp()
if regexp == nil {
return false, errors.New("qValDte had nil regexp compilation")
}
return regexp.Match(rec[sOff:]), nil
}
if len(rec[sOff:]) != len(qValDte.StringVal) {
return false, nil
}
return bytes.Equal(rec[sOff:], qValDte.StringValBytes), nil
case NotEquals:
if isRegexSearch {
regexp := qValDte.GetRegexp()
if regexp == nil {
return false, errors.New("qValDte had nil regexp compilation")
}
return !regexp.Match(rec[sOff:]), nil
}
return !bytes.Equal(rec[sOff:], qValDte.StringValBytes), nil
}
return false, nil
}
func fopOnBool(rec []byte, qValDte *DtypeEnclosure, fop FilterOperator) (bool, error) {
switch fop {
case Equals:
return rec[1] == qValDte.BoolVal, nil
case NotEquals:
return rec[1] != qValDte.BoolVal, nil
}
return false, nil
}
func getNumberRecDte(rec []byte, recDte *DtypeEnclosure) (bool, error) {
if len(rec) == 0 {
return false, nil
}
// first find recDte's Dtype and typecast it
switch rec[0] {
case VALTYPE_ENC_BACKFILL[0]:
return false, nil
case VALTYPE_ENC_BOOL[0]:
return false, nil
case VALTYPE_ENC_SMALL_STRING[0]:
return false, nil
case VALTYPE_ENC_INT8[0]:
recDte.Dtype = SS_DT_SIGNED_NUM
recDte.SignedVal = int64(rec[1])
case VALTYPE_ENC_INT16[0]:
recDte.Dtype = SS_DT_SIGNED_NUM
recDte.SignedVal = int64(utils.BytesToInt16LittleEndian(rec[1:3]))
case VALTYPE_ENC_INT32[0]:
recDte.Dtype = SS_DT_SIGNED_NUM
recDte.SignedVal = int64(utils.BytesToInt32LittleEndian(rec[1:5]))
case VALTYPE_ENC_INT64[0]:
recDte.Dtype = SS_DT_SIGNED_NUM
recDte.SignedVal = utils.BytesToInt64LittleEndian(rec[1:9])
case VALTYPE_ENC_UINT8[0]:
recDte.Dtype = SS_DT_UNSIGNED_NUM
recDte.UnsignedVal = uint64(rec[1])
case VALTYPE_ENC_UINT16[0]:
recDte.Dtype = SS_DT_UNSIGNED_NUM
recDte.UnsignedVal = uint64(utils.BytesToUint16LittleEndian(rec[1:3]))
case VALTYPE_ENC_UINT32[0]:
recDte.Dtype = SS_DT_UNSIGNED_NUM
recDte.UnsignedVal = uint64(utils.BytesToUint32LittleEndian(rec[1:5]))
case VALTYPE_ENC_UINT64[0]:
recDte.Dtype = SS_DT_UNSIGNED_NUM
recDte.UnsignedVal = utils.BytesToUint64LittleEndian(rec[1:9])
case VALTYPE_ENC_FLOAT64[0]:
recDte.Dtype = SS_DT_FLOAT
recDte.FloatVal = utils.BytesToFloat64LittleEndian(rec[1:9])
case VALTYPE_DICT_ARRAY[0], VALTYPE_RAW_JSON[0]:
return false, nil
default:
log.Errorf("fopOnNumber: dont know how to convert type=%v", rec[0])
return false, errors.New("fopOnNumber: invalid rec type")
}
return true, nil
}
func fopOnNumber(rec []byte, qValDte *DtypeEnclosure,
recDte *DtypeEnclosure, op FilterOperator) (bool, error) {
validNumberType, err := getNumberRecDte(rec, recDte)
if !validNumberType {
return false, err
}
// now create a float (highest level for rec, only if we need to based on query
if qValDte.Dtype == SS_DT_FLOAT && recDte.Dtype != SS_DT_FLOAT {
// todo need to check err
recDte.FloatVal, _ = dtu.ConvertToFloat(recDte.UnsignedVal, 64)
}
return compareNumberDte(recDte, qValDte, op)
}
/*
We never convert any qValDte params, caller's responsibility to store
all possible values in a heierarichal order.
We will only convert the recDte (stored val) to appropriate formats as needed
*/
func compareNumberDte(recDte *DtypeEnclosure, qValDte *DtypeEnclosure, op FilterOperator) (bool, error) {
switch recDte.Dtype {
case SS_DT_FLOAT:
switch op {
case Equals:
return dtu.AlmostEquals(recDte.FloatVal, qValDte.FloatVal), nil
case NotEquals:
return !dtu.AlmostEquals(recDte.FloatVal, qValDte.FloatVal), nil
case LessThan:
return recDte.FloatVal < qValDte.FloatVal, nil
case LessThanOrEqualTo:
return recDte.FloatVal <= qValDte.FloatVal, nil
case GreaterThan:
return recDte.FloatVal > qValDte.FloatVal, nil
case GreaterThanOrEqualTo:
return recDte.FloatVal >= qValDte.FloatVal, nil
}
case SS_DT_UNSIGNED_NUM:
switch op {
case Equals:
return recDte.UnsignedVal == qValDte.UnsignedVal, nil
case NotEquals:
return recDte.UnsignedVal != qValDte.UnsignedVal, nil
case LessThan:
//todo rec is unsigned but if qVal is signed and is negative num we need to handle that case
return recDte.UnsignedVal < qValDte.UnsignedVal, nil
case LessThanOrEqualTo:
return recDte.UnsignedVal <= qValDte.UnsignedVal, nil
case GreaterThan:
return recDte.UnsignedVal > qValDte.UnsignedVal, nil
case GreaterThanOrEqualTo:
return recDte.UnsignedVal >= qValDte.UnsignedVal, nil
}
case SS_DT_SIGNED_NUM:
switch op {
case Equals:
return recDte.SignedVal == qValDte.SignedVal, nil
case NotEquals:
return recDte.SignedVal != qValDte.SignedVal, nil
case LessThan:
return recDte.SignedVal < qValDte.SignedVal, nil
case LessThanOrEqualTo:
return recDte.SignedVal <= qValDte.SignedVal, nil
case GreaterThan:
return recDte.SignedVal > qValDte.SignedVal, nil
case GreaterThanOrEqualTo:
return recDte.SignedVal >= qValDte.SignedVal, nil
}
}
log.Errorf("CompareNumbers: unknown op=%v or recDte=%v, qValDte=%v", op, recDte, qValDte)
return false, errors.New("unknown op or dtype")
}