forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
422 lines (341 loc) · 7.8 KB
/
index.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
420
421
422
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package ast
import (
"fmt"
"io"
"sort"
"strings"
"github.com/open-policy-agent/opa/util"
)
// RuleIndex defines the interface for rule indices.
type RuleIndex interface {
// Build tries to construct an index for the given rules. If the index was
// constructed, ok is true, otherwise false.
Build(rules []*Rule) (ok bool)
// Index returns a set of rules to evaluate and a default rule if one was
// present when the index was built.
Index(resolver ValueResolver) (rules []*Rule, defaultRule *Rule, err error)
}
type baseDocEqIndex struct {
isVirtual func(Ref) bool
root *trieNode
defaultRule *Rule
}
func newBaseDocEqIndex(isVirtual func(Ref) bool) *baseDocEqIndex {
return &baseDocEqIndex{
isVirtual: isVirtual,
root: newTrieNodeImpl(),
}
}
func (i *baseDocEqIndex) Build(rules []*Rule) bool {
refs := make(refValueIndex, len(rules))
// freq is map[ref]int where the values represent the frequency of the
// ref/key.
freq := util.NewHashMap(func(a, b util.T) bool {
r1, r2 := a.(Ref), b.(Ref)
return r1.Equal(r2)
}, func(x util.T) int {
return x.(Ref).Hash()
})
// Build refs and freq maps
for _, rule := range rules {
if rule.Default {
// Compiler guarantees that only one default will be defined per path.
i.defaultRule = rule
continue
}
for _, expr := range rule.Body {
ref, value, ok := i.getRefAndValue(expr)
if ok {
refs.Insert(rule, ref, value)
count, ok := freq.Get(ref)
if !ok {
count = 0
}
count = count.(int) + 1
freq.Put(ref, count)
}
}
}
// Sort by frequency
type refCountPair struct {
ref Ref
count int
}
sorted := make([]refCountPair, 0, freq.Len())
freq.Iter(func(k, v util.T) bool {
ref, count := k.(Ref), v.(int)
sorted = append(sorted, refCountPair{ref, count})
return false
})
sort.Slice(sorted, func(i, j int) bool {
if sorted[i].count > sorted[j].count {
return true
}
return false
})
// Build trie
for _, rule := range rules {
if rule.Default {
continue
}
node := i.root
if refs := refs[rule]; refs != nil {
for _, pair := range sorted {
value := refs.Get(pair.ref)
node = node.Insert(pair.ref, value)
}
}
node.rules = append(node.rules, rule)
}
return true
}
func (i *baseDocEqIndex) Index(resolver ValueResolver) ([]*Rule, *Rule, error) {
rules, err := i.traverse(i.root, resolver)
return rules, i.defaultRule, err
}
func (i *baseDocEqIndex) traverse(node *trieNode, resolver ValueResolver) ([]*Rule, error) {
if node == nil {
return nil, nil
}
result := make([]*Rule, len(node.rules))
copy(result, node.rules)
next := node.next
if next == nil {
return result, nil
}
children, err := next.Resolve(resolver)
if err != nil {
return nil, err
}
for _, child := range children {
rules, err := i.traverse(child, resolver)
if err != nil {
return nil, err
}
result = append(result, rules...)
}
return result, nil
}
func (i *baseDocEqIndex) getRefAndValue(expr *Expr) (Ref, Value, bool) {
if !expr.IsEquality() || expr.Negated {
return nil, nil, false
}
a, b := expr.Operand(0), expr.Operand(1)
if ref, value, ok := i.getRefAndValueFromTerms(a, b); ok {
return ref, value, true
}
return i.getRefAndValueFromTerms(b, a)
}
func (i *baseDocEqIndex) getRefAndValueFromTerms(a, b *Term) (Ref, Value, bool) {
ref, ok := a.Value.(Ref)
if !ok {
return nil, nil, false
}
if !RootDocumentNames.Contains(ref[0]) {
return nil, nil, false
}
if i.isVirtual(ref) {
return nil, nil, false
}
if ref.IsNested() || !ref.IsGround() {
return nil, nil, false
}
switch b := b.Value.(type) {
case Null, Boolean, Number, String, Var:
return ref, b, true
case Array:
stop := false
first := true
vis := NewGenericVisitor(func(x interface{}) bool {
if first {
first = false
return false
}
switch x.(type) {
// No nested structures or values that require evaluation (other than var).
case Array, Object, *Set, *ArrayComprehension, Ref:
stop = true
}
return stop
})
Walk(vis, b)
if !stop {
return ref, b, true
}
}
return nil, nil, false
}
type refValueIndex map[*Rule]*ValueMap
func (m refValueIndex) Insert(rule *Rule, ref Ref, value Value) {
vm, ok := m[rule]
if !ok {
vm = NewValueMap()
m[rule] = vm
}
vm.Put(ref, value)
}
type trieWalker interface {
Do(x interface{}) trieWalker
}
type trieNode struct {
ref Ref
next *trieNode
any *trieNode
undefined *trieNode
scalars map[Value]*trieNode
array *trieNode
rules []*Rule
}
func newTrieNodeImpl() *trieNode {
return &trieNode{
scalars: map[Value]*trieNode{},
}
}
func (node *trieNode) Do(walker trieWalker) {
next := walker.Do(node)
if next == nil {
return
}
if node.next != nil {
node.next.Do(next)
return
}
if node.any != nil {
node.any.Do(next)
}
if node.undefined != nil {
node.undefined.Do(next)
}
for _, child := range node.scalars {
child.Do(next)
}
if node.array != nil {
node.array.Do(next)
}
}
func (node *trieNode) Insert(ref Ref, value Value) *trieNode {
if node.next == nil {
node.next = newTrieNodeImpl()
node.next.ref = ref
}
return node.next.insertValue(value)
}
func (node *trieNode) Resolve(resolver ValueResolver) ([]*trieNode, error) {
v, err := resolver.Resolve(node.ref)
if err != nil {
return nil, err
}
result := []*trieNode{}
if node.undefined != nil {
result = append(result, node.undefined)
}
if v == nil {
return result, nil
}
if node.any != nil {
result = append(result, node.any)
}
result = append(result, node.resolveValue(v)...)
return result, nil
}
func (node *trieNode) insertValue(value Value) *trieNode {
switch value := value.(type) {
case nil:
if node.undefined == nil {
node.undefined = newTrieNodeImpl()
}
return node.undefined
case Var:
if node.any == nil {
node.any = newTrieNodeImpl()
}
return node.any
case Null, Boolean, Number, String:
child, ok := node.scalars[value]
if !ok {
child = newTrieNodeImpl()
node.scalars[value] = child
}
return child
case Array:
if node.array == nil {
node.array = newTrieNodeImpl()
}
return node.array.insertArray(value)
}
panic("illegal value")
}
func (node *trieNode) insertArray(arr Array) *trieNode {
if len(arr) == 0 {
return node
}
switch head := arr[0].Value.(type) {
case Var:
if node.any == nil {
node.any = newTrieNodeImpl()
}
return node.any.insertArray(arr[1:])
case Null, Boolean, Number, String:
child, ok := node.scalars[head]
if !ok {
child = newTrieNodeImpl()
node.scalars[head] = child
}
return child.insertArray(arr[1:])
}
panic("illegal value")
}
func (node *trieNode) resolveValue(value Value) []*trieNode {
switch value := value.(type) {
case Array:
if node.array == nil {
return nil
}
return node.array.resolveArray(value)
case Null, Boolean, Number, String:
child, ok := node.scalars[value]
if !ok {
return nil
}
return []*trieNode{child}
}
return nil
}
func (node *trieNode) resolveArray(arr Array) []*trieNode {
if len(arr) == 0 {
if node.next != nil || len(node.rules) > 0 {
return []*trieNode{node}
}
return nil
}
head := arr[0].Value
if !IsScalar(head) {
return nil
}
var result []*trieNode
if node.any != nil {
result = append(result, node.any.resolveArray(arr[1:])...)
}
child, ok := node.scalars[head]
if !ok {
return result
}
return append(result, child.resolveArray(arr[1:])...)
}
type triePrinter struct {
depth int
w io.Writer
}
func (p triePrinter) Do(x interface{}) trieWalker {
padding := strings.Repeat(" ", p.depth)
fmt.Fprintf(p.w, "%v%v\n", padding, x)
p.depth++
return p
}
func printTrie(w io.Writer, trie *trieNode) {
pp := triePrinter{0, w}
trie.Do(pp)
}