-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine_stringmap_bitmap.go
More file actions
351 lines (350 loc) · 10.6 KB
/
engine_stringmap_bitmap.go
File metadata and controls
351 lines (350 loc) · 10.6 KB
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
package expr
// import (
// "context"
// "fmt"
// "strconv"
// "sync"
//
// "github.com/RoaringBitmap/roaring"
// "github.com/cespare/xxhash/v2"
// "github.com/google/cel-go/common/operators"
// "github.com/ohler55/ojg/jp"
// )
//
// // bitmapStringLookup is an optimized version of stringLookup that uses Roaring Bitmaps
// // for much faster set operations and reduced memory usage
// type bitmapStringLookup struct {
// // Use sharded locks to reduce contention
// shards [64]struct {
// mu sync.RWMutex
// // For each field path, store bitmaps of pause IDs that match specific values
// equality map[string]map[string]*roaring.Bitmap // fieldPath -> hashedValue -> bitmap
// inequality map[string]map[string]*roaring.Bitmap // fieldPath -> hashedValue -> bitmap
// in map[string]map[string]*roaring.Bitmap // fieldPath -> hashedValue -> bitmap
// }
//
// // Global tracking of all fields we've seen
// vars map[string]struct{}
// varsMu sync.RWMutex
//
// // Mapping from pause ID to stored expression parts for final lookups
// pauseIndex map[uint32]*StoredExpressionPart
// pauseIndexMu sync.RWMutex
//
// concurrency int64
// nextPauseID uint32
// idMu sync.Mutex
// }
//
// func newBitmapStringEqualityMatcher(concurrency int64) MatchingEngine {
// engine := &bitmapStringLookup{
// vars: make(map[string]struct{}),
// pauseIndex: make(map[uint32]*StoredExpressionPart),
// concurrency: concurrency,
// }
//
// // Initialize shards
// for i := range engine.shards {
// engine.shards[i].equality = make(map[string]map[string]*roaring.Bitmap)
// engine.shards[i].inequality = make(map[string]map[string]*roaring.Bitmap)
// engine.shards[i].in = make(map[string]map[string]*roaring.Bitmap)
// }
//
// return engine
// }
//
// func (b *bitmapStringLookup) Type() EngineType {
// return EngineTypeStringHash
// }
//
// func (b *bitmapStringLookup) getShard(key string) *struct {
// mu sync.RWMutex
// equality map[string]map[string]*roaring.Bitmap
// inequality map[string]map[string]*roaring.Bitmap
// in map[string]map[string]*roaring.Bitmap
// } {
// hash := xxhash.Sum64String(key)
// return &b.shards[hash%64]
// }
//
// func (b *bitmapStringLookup) getNextPauseID() uint32 {
// b.idMu.Lock()
// defer b.idMu.Unlock()
// b.nextPauseID++
// return b.nextPauseID
// }
//
// func (b *bitmapStringLookup) hash(input string) string {
// ui := xxhash.Sum64String(input)
// return strconv.FormatUint(ui, 36)
// }
//
// func (b *bitmapStringLookup) Match(ctx context.Context, input map[string]any, result *MatchResult) error {
// // Instead of doing complex bitmap operations, let's use the same logic as the original
// // but optimize the storage with bitmaps. We'll collect all matching pause IDs
// // and let the group validation logic in the main aggregator handle the filtering.
//
// b.varsMu.RLock()
// fieldPaths := make([]string, 0, len(b.vars))
// for path := range b.vars {
// fieldPaths = append(fieldPaths, path)
// }
// b.varsMu.RUnlock()
//
// // For each field path we track, check if it exists in the input and collect matches
// for _, path := range fieldPaths {
// shard := b.getShard(path)
// shard.mu.RLock()
//
// x, err := jp.ParseString(path)
// if err != nil {
// shard.mu.RUnlock()
// continue
// }
//
// res := x.Get(input)
// if len(res) == 0 {
// res = []any{""}
// }
//
// switch val := res[0].(type) {
// case string:
// hashedVal := b.hash(val)
//
// // Check equality matches
// if valueMap, exists := shard.equality[path]; exists {
// if bitmap, exists := valueMap[hashedVal]; exists {
// b.addBitmapMatches(bitmap, result)
// }
// }
//
// // Check inequality matches (all except this value)
// if valueMap, exists := shard.inequality[path]; exists {
// for value, bitmap := range valueMap {
// if value != hashedVal {
// b.addBitmapMatches(bitmap, result)
// }
// }
// }
//
// case []any:
// // Handle 'in' operations for arrays
// for _, item := range val {
// if str, ok := item.(string); ok {
// hashedVal := b.hash(str)
// if valueMap, exists := shard.in[path]; exists {
// if bitmap, exists := valueMap[hashedVal]; exists {
// b.addBitmapMatches(bitmap, result)
// }
// }
// }
// }
// case []string:
// // Handle 'in' operations for string arrays
// for _, str := range val {
// hashedVal := b.hash(str)
// if valueMap, exists := shard.in[path]; exists {
// if bitmap, exists := valueMap[hashedVal]; exists {
// b.addBitmapMatches(bitmap, result)
// }
// }
// }
// }
//
// shard.mu.RUnlock()
// }
//
// return nil
// }
//
// // addBitmapMatches converts bitmap results to MatchResult format
// func (b *bitmapStringLookup) addBitmapMatches(bitmap *roaring.Bitmap, result *MatchResult) {
// b.pauseIndexMu.RLock()
// defer b.pauseIndexMu.RUnlock()
//
// for _, pauseID := range bitmap.ToArray() {
// if part, exists := b.pauseIndex[pauseID]; exists {
// result.Add(part.EvaluableID, part.GroupID)
// }
// }
// }
//
// func (b *bitmapStringLookup) Search(ctx context.Context, variable string, input any, result *MatchResult) {
// // This method is kept for interface compatibility but uses the same logic as Match
// testInput := map[string]any{variable: input}
// _ = b.Match(ctx, testInput, result) // Error is already handled in Match
// }
//
// func (b *bitmapStringLookup) Add(ctx context.Context, p ExpressionPart) error {
// // Generate a unique pause ID for this expression part
// pauseID := b.getNextPauseID()
//
// // Store the mapping from pause ID to expression part
// b.pauseIndexMu.Lock()
// b.pauseIndex[pauseID] = p.ToStored()
// b.pauseIndexMu.Unlock()
//
// // Track the variable
// b.varsMu.Lock()
// b.vars[p.Predicate.Ident] = struct{}{}
// b.varsMu.Unlock()
//
// shard := b.getShard(p.Predicate.Ident)
// shard.mu.Lock()
// defer shard.mu.Unlock()
//
// switch p.Predicate.Operator {
// case operators.Equals:
// hashedVal := b.hash(p.Predicate.LiteralAsString())
//
// if shard.equality[p.Predicate.Ident] == nil {
// shard.equality[p.Predicate.Ident] = make(map[string]*roaring.Bitmap)
// }
// if shard.equality[p.Predicate.Ident][hashedVal] == nil {
// shard.equality[p.Predicate.Ident][hashedVal] = roaring.New()
// }
// shard.equality[p.Predicate.Ident][hashedVal].Add(pauseID)
//
// case operators.NotEquals:
// hashedVal := b.hash(p.Predicate.LiteralAsString())
//
// if shard.inequality[p.Predicate.Ident] == nil {
// shard.inequality[p.Predicate.Ident] = make(map[string]*roaring.Bitmap)
// }
// if shard.inequality[p.Predicate.Ident][hashedVal] == nil {
// shard.inequality[p.Predicate.Ident][hashedVal] = roaring.New()
// }
// shard.inequality[p.Predicate.Ident][hashedVal].Add(pauseID)
//
// case operators.In:
// if str, ok := p.Predicate.Literal.(string); ok {
// hashedVal := b.hash(str)
//
// if shard.in[p.Predicate.Ident] == nil {
// shard.in[p.Predicate.Ident] = make(map[string]*roaring.Bitmap)
// }
// if shard.in[p.Predicate.Ident][hashedVal] == nil {
// shard.in[p.Predicate.Ident][hashedVal] = roaring.New()
// }
// shard.in[p.Predicate.Ident][hashedVal].Add(pauseID)
// }
//
// default:
// return fmt.Errorf("BitmapStringHash engines only support string equality/inequality/in operations")
// }
//
// return nil
// }
//
// func (b *bitmapStringLookup) Remove(ctx context.Context, parts []ExpressionPart) (int, error) {
// type removalInfo struct {
// p ExpressionPart
// bitmap *roaring.Bitmap
// pauseID uint32
// hashedVal string
// shard *struct {
// mu sync.RWMutex
// equality map[string]map[string]*roaring.Bitmap
// inequality map[string]map[string]*roaring.Bitmap
// in map[string]map[string]*roaring.Bitmap
// }
// }
//
// toRemove := make([]removalInfo, 0, len(parts))
// processedCount := 0
//
// b.pauseIndexMu.RLock()
// for _, p := range parts {
// // Check for context cancellation/timeout during Phase 1
// if ctx.Err() != nil {
// b.pauseIndexMu.RUnlock()
// // Return how many we successfully collected before timeout
// // We haven't modified state yet, so this is safe
// return processedCount, ctx.Err()
// }
//
// shard := b.getShard(p.Predicate.Ident)
// hashedVal := b.hash(p.Predicate.LiteralAsString())
// var bitmap *roaring.Bitmap
//
// shard.mu.RLock()
// switch p.Predicate.Operator {
// case operators.Equals:
// if valueMap, exists := shard.equality[p.Predicate.Ident]; exists {
// bitmap = valueMap[hashedVal]
// }
// case operators.NotEquals:
// if valueMap, exists := shard.inequality[p.Predicate.Ident]; exists {
// bitmap = valueMap[hashedVal]
// }
// case operators.In:
// if _, ok := p.Predicate.Literal.(string); ok {
// if valueMap, exists := shard.in[p.Predicate.Ident]; exists {
// bitmap = valueMap[hashedVal]
// }
// }
// }
// shard.mu.RUnlock()
//
// if bitmap != nil {
// for _, pauseID := range bitmap.ToArray() {
// if stored := b.pauseIndex[pauseID]; p.EqualsStored(stored) {
// toRemove = append(toRemove, removalInfo{
// p: p,
// bitmap: bitmap,
// pauseID: pauseID,
// hashedVal: hashedVal,
// shard: shard,
// })
// break
// }
// }
// }
// processedCount++
// }
// b.pauseIndexMu.RUnlock()
//
// pauseIDs := make([]uint32, 0, len(toRemove))
// for _, info := range toRemove {
// pauseIDs = append(pauseIDs, info.pauseID)
// }
//
// if len(pauseIDs) > 0 {
// b.pauseIndexMu.Lock()
// for _, pauseID := range pauseIDs {
// delete(b.pauseIndex, pauseID)
// }
// b.pauseIndexMu.Unlock()
// }
//
// byShard := make(map[*struct {
// mu sync.RWMutex
// equality map[string]map[string]*roaring.Bitmap
// inequality map[string]map[string]*roaring.Bitmap
// in map[string]map[string]*roaring.Bitmap
// }][]removalInfo)
//
// for _, info := range toRemove {
// byShard[info.shard] = append(byShard[info.shard], info)
// }
//
// for shard, infos := range byShard {
// shard.mu.Lock()
// for _, info := range infos {
// info.bitmap.Remove(info.pauseID)
// if info.bitmap.IsEmpty() {
// switch info.p.Predicate.Operator {
// case operators.Equals:
// delete(shard.equality[info.p.Predicate.Ident], info.hashedVal)
// case operators.NotEquals:
// delete(shard.inequality[info.p.Predicate.Ident], info.hashedVal)
// case operators.In:
// delete(shard.in[info.p.Predicate.Ident], info.hashedVal)
// }
// }
// }
// shard.mu.Unlock()
// }
//
// return processedCount, nil
// }