-
Notifications
You must be signed in to change notification settings - Fork 193
/
snhasher.go
306 lines (240 loc) · 7.31 KB
/
snhasher.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
// 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 querytracker
import (
"fmt"
"sort"
"strings"
"github.com/cespare/xxhash"
"github.com/siglens/siglens/pkg/segment/structs"
"github.com/siglens/siglens/pkg/segment/utils"
)
/*
ALGORITHM for creating a unique hash
1. In each struct maintain a hid (hashid), it is pre-determined way of creating hash out of
of elements of that struct in the sequence they are defined in the go files
2. If a struct has sub-structs then we recursively call the hashid func
3. if the hid is already present then we use it else we calculate it for that data type
4. we keep doing recursive until we get base data types of go like int, bool, string, etc...
5. This is a pretty standard way of creating id borrowed from the java world
*/
func GetHashForQuery(n *structs.SearchNode) string {
return fmt.Sprintf("%v", getHashForSearchNode(n))
}
func GetHashForAggs(a *structs.QueryAggregators) string {
return fmt.Sprintf("%v", getHashForAggregators(a))
}
func getHashForAggregators(a *structs.QueryAggregators) uint64 {
if a == nil {
return 0
}
// the only parts of aggs we need to hash are the groupby configs
// the bucketing/sorting/early exiting does not change pqs vs not, agileTree vs not
val := fmt.Sprintf("%v:%v:%v",
getHashForGroupBy(a.GroupByRequest),
getHashForSegmentStats(a.MeasureOperations),
getHashForTimeHistogram(a.TimeHistogram),
)
return xxhash.Sum64String(val)
}
func getHashForSearchNode(sn *structs.SearchNode) uint64 {
if sn == nil {
return 0
}
val := fmt.Sprintf("%v:%v:%v",
getHashForSearchCondition(sn.AndSearchConditions),
getHashForSearchCondition(sn.OrSearchConditions),
getHashForSearchCondition(sn.ExclusionSearchConditions),
)
return xxhash.Sum64String(val)
}
func getHashForSearchCondition(sc *structs.SearchCondition) uint64 {
if sc == nil {
return 0
}
sqhids := make([]uint64, len(sc.SearchQueries))
for _, sq := range sc.SearchQueries {
sqhids = append(sqhids, getHashForSearchQuery(sq))
}
sort.Slice(sqhids, func(i, j int) bool { return sqhids[i] < sqhids[j] })
snhids := make([]uint64, len(sc.SearchNode))
for _, sn := range sc.SearchNode {
snhids = append(snhids, getHashForSearchNode(sn))
}
sort.Slice(snhids, func(i, j int) bool { return snhids[i] < snhids[j] })
var sb strings.Builder
for _, entry := range sqhids {
sb.WriteString(fmt.Sprintf("%v:", entry))
}
for _, entry := range snhids {
sb.WriteString(fmt.Sprintf("%v:", entry))
}
return xxhash.Sum64String(sb.String())
}
func getHashForSearchQuery(sq *structs.SearchQuery) uint64 {
if sq == nil {
return 0
}
val := fmt.Sprintf("%v:%v:%v:%v",
getHashForSearchExpression(sq.ExpressionFilter),
getHashForMatchFilter(sq.MatchFilter),
sq.SearchType,
getHashForQueryInfo(sq.QueryInfo))
return xxhash.Sum64String(val)
}
func getHashForSearchExpression(se *structs.SearchExpression) uint64 {
if se == nil {
return 0
}
val := fmt.Sprintf("%v:%v:%v:%v",
getHashForSearchExpressionInput(se.LeftSearchInput),
se.FilterOp,
getHashForSearchExpressionInput(se.RightSearchInput),
getHashForSearchInfo(se.SearchInfo))
return xxhash.Sum64String(val)
}
func getHashForMatchFilter(mf *structs.MatchFilter) uint64 {
if mf == nil {
return 0
}
mwords := make([]string, len(mf.MatchWords))
for _, w := range mf.MatchWords {
mwords = append(mwords, string(w))
}
sort.Strings(mwords)
val := fmt.Sprintf("%v:%v:%v:%v:%v",
mf.MatchColumn,
mwords,
mf.MatchOperator,
mf.MatchPhrase,
mf.MatchType)
return xxhash.Sum64String(val)
}
func getHashForQueryInfo(qi *structs.QueryInfo) uint64 {
if qi == nil {
return 0
}
val := fmt.Sprintf("%v:%v",
qi.ColName,
getHashForDtypeEnclosure(qi.QValDte))
return xxhash.Sum64String(val)
}
func getHashForSearchInfo(si *structs.SearchInfo) uint64 {
if si == nil {
return 0
}
val := fmt.Sprintf("%v:%v",
si.ColEncoding,
getHashForDtypeEnclosure(si.QValDte))
return xxhash.Sum64String(val)
}
func getHashForDtypeEnclosure(dte *utils.DtypeEnclosure) uint64 {
if dte == nil {
return 0
}
var val string
switch dte.Dtype {
case utils.SS_DT_BOOL:
val = fmt.Sprintf("%v:%v", dte.Dtype, dte.BoolVal)
case utils.SS_DT_STRING:
val = fmt.Sprintf("%v:%v", dte.Dtype, dte.StringVal)
case utils.SS_DT_UNSIGNED_NUM:
val = fmt.Sprintf("%v:%v", dte.Dtype, dte.UnsignedVal)
case utils.SS_DT_SIGNED_NUM:
val = fmt.Sprintf("%v:%v", dte.Dtype, dte.SignedVal)
case utils.SS_DT_FLOAT:
val = fmt.Sprintf("%v:%v", dte.Dtype, dte.FloatVal)
}
return xxhash.Sum64String(val)
}
func getHashForSearchExpressionInput(sei *structs.SearchExpressionInput) uint64 {
if sei == nil {
return 0
}
val := fmt.Sprintf("%v:%v:%v",
sei.ColumnName,
getHashForExpression(sei.ComplexRelation),
getHashForDtypeEnclosure(sei.ColumnValue))
return xxhash.Sum64String(val)
}
func getHashForExpression(e *structs.Expression) uint64 {
if e == nil {
return 0
}
val := fmt.Sprintf("%v:%v:%v",
getHashForExpressionInput(e.LeftInput),
e.ExpressionOp,
getHashForExpressionInput(e.RightInput))
return xxhash.Sum64String(val)
}
func getHashForExpressionInput(ei *structs.ExpressionInput) uint64 {
if ei == nil {
return 0
}
val := fmt.Sprintf("%v:%v",
getHashForDtypeEnclosure(ei.ColumnValue),
ei.ColumnName)
return xxhash.Sum64String(val)
}
func getHashForGroupBy(r *structs.GroupByRequest) uint64 {
if r == nil {
return 0
}
val := fmt.Sprintf("%v:%v",
getHashForGroupByColumns(r.GroupByColumns),
getHashForMeasureOperations(r.MeasureOperations))
return xxhash.Sum64String(val)
}
func getHashForSegmentStats(mOps []*structs.MeasureAggregator) uint64 {
return getHashForMeasureOperations(mOps)
}
func getHashForTimeHistogram(tb *structs.TimeBucket) uint64 {
if tb == nil {
return 0
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%v:", tb.EndTime))
sb.WriteString(fmt.Sprintf("%v:", tb.StartTime))
sb.WriteString(fmt.Sprintf("%v", tb.IntervalMillis))
return xxhash.Sum64String(sb.String())
}
func getHashForGroupByColumns(cols []string) uint64 {
if len(cols) == 0 {
return 0
}
sort.Strings(cols)
var sb strings.Builder
for _, entry := range cols {
sb.WriteString(fmt.Sprintf("%v:", entry))
}
return xxhash.Sum64String(sb.String())
}
func getHashForMeasureOperations(measureOps []*structs.MeasureAggregator) uint64 {
if len(measureOps) == 0 {
return 0
}
temp := make([]string, len(measureOps))
for idx, m := range measureOps {
temp[idx] = fmt.Sprintf("%+v-%+v", m.MeasureCol, m.MeasureFunc.String())
}
sort.Strings(temp)
var sb strings.Builder
for _, entry := range temp {
sb.WriteString(fmt.Sprintf("%v:", entry))
}
return xxhash.Sum64String(sb.String())
}