-
Notifications
You must be signed in to change notification settings - Fork 180
/
segstream.go
218 lines (205 loc) · 6.84 KB
/
segstream.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
// 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 (
"github.com/siglens/siglens/pkg/config"
"github.com/siglens/siglens/pkg/segment/structs"
"github.com/siglens/siglens/pkg/segment/utils"
log "github.com/sirupsen/logrus"
)
/*
Main function exported to check colWips against persistent queries during ingest
Internally, updates the bitset with recNum for all queries that matched
*/
func applyStreamingSearchToRecord(wipBlock WipBlock, psNode map[string]*structs.SearchNode,
recNum uint16) {
holderDte := &utils.DtypeEnclosure{}
tsKey := config.GetTimeStampKey()
for pqid, sNode := range psNode {
holderDte.Reset()
if applySearchSingleNode(wipBlock.colWips, sNode, holderDte, tsKey) {
wipBlock.addRecordToMatchedResults(recNum, pqid)
}
}
}
func applySearchSingleNode(colWips map[string]*ColWip, sNode *structs.SearchNode, holderDte *utils.DtypeEnclosure, tsKey string) bool {
retVal := false
if sNode.AndSearchConditions != nil {
andConditions := applySearchSingleCondition(colWips, sNode.AndSearchConditions, utils.And, holderDte, tsKey)
if !andConditions {
return false
}
retVal = true
}
// at least one must pass. If and conditions are defined, then this is a noop check
if sNode.OrSearchConditions != nil {
orConditions := applySearchSingleCondition(colWips, sNode.OrSearchConditions, utils.Or, holderDte, tsKey)
retVal = retVal || orConditions
}
if !retVal {
return retVal
}
// all must fail
if sNode.ExclusionSearchConditions != nil {
exclusionConditions := applySearchSingleCondition(colWips, sNode.ExclusionSearchConditions, utils.Exclusion, holderDte, tsKey)
if exclusionConditions {
return false
}
}
return true
}
func applySearchSingleCondition(colWips map[string]*ColWip, sCond *structs.SearchCondition, op utils.LogicalOperator,
holderDte *utils.DtypeEnclosure, tsKey string) bool {
orMatch := false
if sCond.SearchNode != nil {
for _, sNode := range sCond.SearchNode {
retVal := applySearchSingleNode(colWips, sNode, holderDte, tsKey)
if !retVal && op == utils.And {
return retVal
} else {
orMatch = orMatch || retVal
}
}
}
if sCond.SearchQueries != nil {
for _, query := range sCond.SearchQueries {
retVal := applySearchSingleQuery(colWips, query, op, holderDte, tsKey)
if !retVal && op == utils.And {
return retVal
} else {
orMatch = orMatch || retVal
}
}
}
if op == utils.And {
// previous false values would have returned already
return true
}
return orMatch
}
func applySearchSingleQuery(colWips map[string]*ColWip, sQuery *structs.SearchQuery, op utils.LogicalOperator,
holderDte *utils.DtypeEnclosure, tsKey string) bool {
switch sQuery.SearchType {
case structs.MatchAll:
return true
case structs.MatchWords:
rawVal, ok := colWips[sQuery.MatchFilter.MatchColumn]
if !ok {
return false
}
retVal, err := ApplySearchToMatchFilterRawCsg(sQuery.MatchFilter, rawVal.getLastRecord())
if err != nil {
log.Errorf("applySearchSingleQuery: failed to apply match words search! error: %v", err)
return false
}
return retVal
case structs.MatchWordsAllColumns:
for cname, colVal := range colWips {
if cname == tsKey {
continue
}
retVal, _ := ApplySearchToMatchFilterRawCsg(sQuery.MatchFilter, colVal.getLastRecord())
if retVal {
return true
}
}
return false
case structs.SimpleExpression:
rawVal, ok := colWips[sQuery.QueryInfo.ColName]
if !ok {
return false
}
retVal, err := ApplySearchToExpressionFilterSimpleCsg(sQuery.QueryInfo.QValDte, sQuery.ExpressionFilter.FilterOp, rawVal.getLastRecord(), false, holderDte)
if err != nil {
log.Errorf("applySearchSingleQuery: failed to apply simple expression search! error: %v", err)
return false
}
return retVal
case structs.RegexExpression:
rawVal, ok := colWips[sQuery.QueryInfo.ColName]
if !ok {
return false
}
retVal, err := ApplySearchToExpressionFilterSimpleCsg(sQuery.QueryInfo.QValDte, sQuery.ExpressionFilter.FilterOp, rawVal.getLastRecord(), true, holderDte)
if err != nil {
log.Errorf("applySearchSingleQuery: failed to apply wildcard expression search! error: %v", err)
return false
}
return retVal
case structs.RegexExpressionAllColumns:
for cname, colVal := range colWips {
if cname == tsKey {
continue
}
retVal, _ := ApplySearchToExpressionFilterSimpleCsg(sQuery.QueryInfo.QValDte, sQuery.ExpressionFilter.FilterOp, colVal.getLastRecord(), true, holderDte)
if retVal {
return true
}
}
return false
case structs.SimpleExpressionAllColumns:
for cname, colVal := range colWips {
if cname == tsKey {
continue
}
retVal, _ := ApplySearchToExpressionFilterSimpleCsg(sQuery.QueryInfo.QValDte, sQuery.ExpressionFilter.FilterOp, colVal.getLastRecord(), false, holderDte)
if retVal {
return true
}
}
return false
case structs.MatchDictArraySingleColumn:
rawVal, ok := colWips[sQuery.QueryInfo.ColName]
if !ok {
return false
}
retVal, err := ApplySearchToDictArrayFilter([]byte(sQuery.QueryInfo.ColName), sQuery.QueryInfo.QValDte, rawVal.getLastRecord(), sQuery.ExpressionFilter.FilterOp, true, holderDte)
if err != nil {
log.Errorf("ApplySearchToDictArrayFilter: failed to apply wildcard expression search! error: %v", err)
return false
}
return retVal
case structs.MatchDictArrayAllColumns:
for cname, colVal := range colWips {
if cname == tsKey {
continue
}
retVal, _ := ApplySearchToDictArrayFilter([]byte(sQuery.QueryInfo.ColName), sQuery.QueryInfo.QValDte, colVal.getLastRecord(), sQuery.ExpressionFilter.FilterOp, false, holderDte)
if retVal {
return true
}
}
return false
default:
log.Errorf("applySearchSingleQuery: unsupported query type! %+v", sQuery.SearchType)
return false
}
}
/*
Adds recNum as a matched record in the current bitset based on pqid
*/
func (wipBlock *WipBlock) addRecordToMatchedResults(recNum uint16, pqid string) {
pqMatch, ok := wipBlock.pqMatches[pqid]
if !ok {
log.Errorf("addRecordToMatchedResults: tried to match a record for a pqid that does not exist")
return
}
pqMatch.AddMatchedRecord(uint(recNum))
}
func (colWip *ColWip) getLastRecord() []byte {
return colWip.cbuf[colWip.cstartidx:colWip.cbufidx]
}