-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_number.go
264 lines (218 loc) · 5.74 KB
/
engine_number.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
package expr
import (
"context"
"fmt"
"sync"
// "github.com/google/btree"
"github.com/google/cel-go/common/operators"
"github.com/ohler55/ojg/jp"
"github.com/tidwall/btree"
)
func newNumberMatcher(concurrency int64) MatchingEngine {
return &numbers{
lock: &sync.RWMutex{},
paths: map[string]struct{}{},
concurrency: concurrency,
exact: btree.NewMap[float64, []*StoredExpressionPart](64),
gt: btree.NewMap[float64, []*StoredExpressionPart](64),
lt: btree.NewMap[float64, []*StoredExpressionPart](64),
}
}
type numbers struct {
lock *sync.RWMutex
// paths stores all variable names as JSON paths used within the engine.
paths map[string]struct{}
exact *btree.Map[float64, []*StoredExpressionPart]
gt *btree.Map[float64, []*StoredExpressionPart]
lt *btree.Map[float64, []*StoredExpressionPart]
concurrency int64
}
func (n numbers) Type() EngineType {
return EngineTypeBTree
}
func (n *numbers) Match(ctx context.Context, input map[string]any) (matched []*StoredExpressionPart, err error) {
l := &sync.Mutex{}
matched = []*StoredExpressionPart{}
pool := newErrPool(errPoolOpts{concurrency: n.concurrency})
for item := range n.paths {
path := item
pool.Go(func() error {
x, err := jp.ParseString(path)
if err != nil {
return err
}
res := x.Get(input)
if len(res) == 0 {
return nil
}
var val float64
switch v := res[0].(type) {
case int:
val = float64(v)
case int64:
val = float64(v)
case float64:
val = v
default:
return nil
}
// This matches null, nil (as null), and any non-null items.
l.Lock()
found := n.Search(ctx, path, val)
matched = append(matched, found...)
l.Unlock()
return nil
})
}
return matched, pool.Wait()
}
// Search returns all ExpressionParts which match the given input, ignoring the variable name
// entirely.
func (n *numbers) Search(ctx context.Context, variable string, input any) (matched []*StoredExpressionPart) {
n.lock.RLock()
defer n.lock.RUnlock()
// initialize matched
matched = []*StoredExpressionPart{}
var val float64
switch v := input.(type) {
case int:
val = float64(v)
case int64:
val = float64(v)
case float64:
val = v
default:
return nil
}
// First, find exact matches.
if vals, _ := n.exact.Get(val); len(vals) > 0 {
// Save memory by re-assigning vals, no need to append to an empty list
for _, m := range vals {
if m.Ident != nil && *m.Ident != variable {
continue
}
// This is a candidatre.
matched = append(matched, m)
}
}
// Then, find all expressions that match GT this number by walking tree
// from beginning to this number.
n.gt.Scan(func(n float64, matches []*StoredExpressionPart) bool {
if n >= val {
return false
}
for _, m := range matches {
if m.Ident != nil && *m.Ident != variable {
continue
}
// This is a candidatre.
matched = append(matched, m)
}
return true
})
// Then, find all expressions that match LT this number by walking tree
// from beginning to this number.
n.lt.Reverse(func(n float64, matches []*StoredExpressionPart) bool {
if n <= val {
return false
}
// This is a candidatre.
for _, m := range matches {
if m.Ident != nil && *m.Ident != variable {
continue
}
// This is a candidatre.
matched = append(matched, m)
}
return true
})
return matched
}
func (n *numbers) Add(ctx context.Context, p ExpressionPart) error {
// If this is not equals, ignore.
if p.Predicate.Operator == operators.NotEquals {
return fmt.Errorf("Number engine does not support !=")
}
// Add the number to the btree.
val, err := p.Predicate.LiteralAsFloat64()
if err != nil {
return err
}
n.paths[p.Predicate.Ident] = struct{}{}
n.lock.Lock()
defer n.lock.Unlock()
switch p.Predicate.Operator {
// Each of these have at least one equality match.
case operators.Equals, operators.GreaterEquals, operators.LessEquals:
item, ok := n.exact.Get(val)
if !ok {
item = []*StoredExpressionPart{}
}
item = append(item, p.ToStored())
n.exact.Set(val, item)
}
// Check for >=, >, <, <= separately.
switch p.Predicate.Operator {
case operators.Greater, operators.GreaterEquals:
item, ok := n.gt.Get(val)
if !ok {
item = []*StoredExpressionPart{}
}
item = append(item, p.ToStored())
n.gt.Set(val, item)
case operators.Less, operators.LessEquals:
item, ok := n.lt.Get(val)
if !ok {
item = []*StoredExpressionPart{}
}
item = append(item, p.ToStored())
n.lt.Set(val, item)
}
return nil
}
func (n *numbers) Remove(ctx context.Context, p ExpressionPart) error {
// If this is not equals, ignore.
if p.Predicate.Operator == operators.NotEquals {
return fmt.Errorf("Number engine does not support !=")
}
// Add the number to the btree.
val, err := p.Predicate.LiteralAsFloat64()
if err != nil {
return err
}
n.lock.Lock()
defer n.lock.Unlock()
remove := func(b *btree.Map[float64, []*StoredExpressionPart]) error {
item, ok := b.Get(val)
if !ok {
return ErrExpressionPartNotFound
}
// Remove the expression part from the leaf.
for i, eval := range item {
if p.EqualsStored(eval) {
item = append(item[:i], item[i+1:]...)
b.Set(val, item)
return nil
}
}
return nil
}
var equalErr, gtErr, ltErr error
switch p.Predicate.Operator {
// Each of these have at least one equality match.
case operators.Equals, operators.GreaterEquals, operators.LessEquals:
equalErr = remove(n.exact)
}
// Check for >=, >, <, <= separately.
switch p.Predicate.Operator {
case operators.Greater, operators.GreaterEquals:
gtErr = remove(n.gt)
case operators.Less, operators.LessEquals:
ltErr = remove(n.lt)
}
if equalErr != nil && gtErr != nil && ltErr != nil {
return ErrExpressionPartNotFound
}
// At least one expr part was found.
return nil
}