-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathevaluator.go
887 lines (763 loc) · 23.9 KB
/
evaluator.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
package grange
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/deckarep/golang-set"
"github.com/orcaman/concurrent-map"
)
// State holds data that queries operate over. Queries in grange are
// deterministic, so the same query will always return the same result for a
// given state. Clients are expected to build their own state to query from
// their own datasource, such as a database or files on disk.
//
// State maintains an internal cache of expanded values to speed up queries.
// After constructing a large state it is recommended to call PrimeCache()
// before querying, otherwise initial queries will likely take longer than
// later ones as the cache is built up incrementally.
type State struct {
clusters map[string]Cluster
defaultCluster string
// Populated lazily as groups are evaluated. They won't change unless state
// changes.
// clusterCache map[string]map[string]*Result
// To make it concurrent read write capable , using concurrent-map.
// As concurrent-map stores value type as interface,
// we need to cast them back whenever we retrive values
// cmap.ConcurrentMap is of type map[string] interface
clusterCache cmap.ConcurrentMap
// clusters() query is expensive, we want to cache results of each such query
// var cachedClusterQueryResults map[string] Result
cachedCQR cmap.ConcurrentMap
metrics metrics
}
type metrics struct {
// useful to know how old state is
initializedAt time.Time
cacheInitializedAt time.Time
errorsDuringCacheBuild int
buildTimeForCache float64
buildTimeForClusterCache float64
buildTimeForCachedCQR float64
numberOfTruncatedResults int64
}
// A Cluster is mapping of arbitrary keys to arrays of values. The only
// required key is CLUSTER, which is the default set of values for the cluster.
type Cluster map[string][]string
// A set of values returned by a query. The size of this set is limited by
// MaxResults.
type Result struct {
mapset.Set
}
var (
// Maximum number of characters that grange will try to parse in a query.
// Queries longer than this will be rejected. This limit also applies to
// cluster and group names and values. Combined with MaxResults, this limits
// result sizes to approximately 4MB.
MaxQuerySize = 4000
// The maximum number of results a query can return. Execution will be
// short-circuited once this many results have been gathered. No error will
// be returned.
MaxResults = 10000
// Maximum number of subqueries that will be evaluated, including evaluation
// of cluster values. If this is exceeded, an error will be returned.
// Primarily useful for aborting cycles, but also can shortcut really
// expensive queries. This should not be exceeded in normal operation.
MaxQueryDepth = 100
// The default cluster for new states, used by @ and ? syntax. Can be changed
// per-state using SetDefaultCluster.
DefaultCluster = "GROUPS"
// Parallelism to be used while building primeCache
primeCacheParallelismFactor = 2
)
// Clusters is a getter for all clusters that have been added to the state.
// There isn't really a good reason to use this other than for debugging
// purposes.
func (s *State) Clusters() map[string]Cluster {
return s.clusters
}
// NewState creates a new state to be passed into EvalRange. This will need to
// be used at least once before you can query anything.
func NewState() State {
state := State{
clusters: map[string]Cluster{},
defaultCluster: DefaultCluster,
metrics: metrics{},
}
state.metrics.initializedAt = time.Now()
state.ResetCache()
return state
}
// NewResult is mostly used internally, but is handy in testing scenarios when
// you need to compare a query result to a known value.
func NewResult(args ...interface{}) Result {
return Result{mapset.NewSetFromSlice(args)}
}
// AddCluster adds a new cluster to the state and resets the cache.
func (state *State) AddCluster(name string, c Cluster) {
state.clusters[name] = c
state.ResetCache()
}
// Changes the default cluster for the state.
func (state *State) SetDefaultCluster(name string) {
state.defaultCluster = name
}
// PrimeCache traverses over the entire state to expand all values and store
// them in the state's cache. Subsequent queries will be able to use the cache
// immediately, rather than having to build it up incrementally.
//
// It returns all errors encountered during the traverse. This isn't
// necessarily a critical problem, often errors will be in obscure keys, but
// you should probably try to fix them.
func (state *State) PrimeCache() []error {
// Limiting parallelism to 2
// splitting clusters in slices and spawn go routine for each
startTime := time.Now()
clusters := state.clusterNamesAsArray()
arrayOfClusterSlices := splitIntoSlices(clusters, primeCacheParallelismFactor)
var wg sync.WaitGroup
resultCh := make(chan mapset.Set, primeCacheParallelismFactor)
errorCh := make(chan []error, primeCacheParallelismFactor)
defer close(resultCh)
defer close(errorCh)
for _, slice := range arrayOfClusterSlices {
wg.Add(1)
go func(s []string) {
defer wg.Done()
res, err := buildPrimeClusterCacheForSlice(state, s)
resultCh <- res
errorCh <- err
}(slice)
}
done := make(chan interface{})
go func() {
wg.Wait()
// sleep makes thats buffers in resultCh and errorCh are read
// time.Sleep(1*time.Millisecond)
close(done)
}()
results := mapset.NewSet()
errors := []error{}
Loop:
for {
select {
case r := <-resultCh:
results = results.Union(r)
case err := <-errorCh:
errors = append(errors, err...)
case <-done:
if len(resultCh) == 0 && len(errorCh) == 0 {
break Loop
}
}
}
// end of Loop:
wg.Wait()
state.metrics.buildTimeForClusterCache = time.Since(startTime).Seconds()
state.populateCachedCQRforSet(results)
state.metrics.buildTimeForCachedCQR =
time.Since(startTime).Seconds() - state.metrics.buildTimeForClusterCache
state.metrics.buildTimeForCache = time.Since(startTime).Seconds()
state.metrics.errorsDuringCacheBuild = len(errors)
state.metrics.cacheInitializedAt = time.Now()
return errors
}
// StateMetrics returns metrics related to state, cache as map[string]int64
func (state *State) StateMetrics() map[string]int64 {
metrics := make(map[string]int64)
metrics["stateInitializedAt"] = state.metrics.initializedAt.Unix()
metrics["cacheInitializedAt"] = state.metrics.cacheInitializedAt.Unix()
metrics["numberOfClusters"] = int64(len(state.clusters))
metrics["numberOfCachedClusters"] = int64(state.clusterCache.Count())
metrics["numberOfcachedCQR"] = int64(state.cachedCQR.Count())
metrics["cacheTotalBuildTimeInSeconds"] = int64(state.metrics.buildTimeForCache)
metrics["cacheBuildTimeForClustersInSeconds"] = int64(state.metrics.buildTimeForClusterCache)
metrics["cacheBuildTimeForCQRInSeconds"] = int64(state.metrics.buildTimeForCachedCQR)
metrics["errorsDuringCacheBuild"] = int64(state.metrics.errorsDuringCacheBuild)
metrics["numberOfTruncatedResults"] = state.metrics.numberOfTruncatedResults
return metrics
}
// buildPrimeClusterCacheForSlice is used internally for building ClusterCache.
// returns results of CLUSTER_NAME:CLUSTER , which is used for building cachedCQR
// also returns array of errors encounter during parsing clusters
func buildPrimeClusterCacheForSlice(state *State, clusters []string) (mapset.Set, []error) {
var errs []error
res := mapset.NewSet()
for _, clusterName := range clusters {
context := newContext()
context.currentClusterName = clusterName
for key, _ := range state.clusters[clusterName] {
err := clusterLookup(state, &context, key)
// we are interested only results for key CLUSTER
if key == "CLUSTER" {
res = res.Union(context.currentResult.Set)
}
if err != nil {
errs = append(errs, err)
}
}
}
return res, errs
}
func (state *State) clusterNamesAsArray() []string {
i := 0
ret := make([]string, len(state.clusters))
for name := range state.clusters {
ret[i] = name
i++
}
return ret
}
// splices array into ~'count' of slices
// if len(array) is less than count,
// we splice array into slices of length 1
// if len(array) is not multiple of count,
// we end up returning more than 'count' number of slices
func splitIntoSlices(array []string, count int) [][]string {
var ret [][]string
lengthOfArray := len(array)
if lengthOfArray == 0 {
return append(ret, array)
}
sliceLength := lengthOfArray / count
if sliceLength == 0 {
sliceLength = 1
}
for i := 0; i < lengthOfArray; i += sliceLength {
if i+sliceLength < lengthOfArray {
ret = append(ret, array[i:i+sliceLength])
} else {
ret = append(ret, array[i:lengthOfArray])
}
}
return ret
}
// ResetCache clears cached expansions. The public API for modifying state
// already calls this when necessary, so you shouldn't really have a need to
// call this.
func (state *State) ResetCache() {
// state.clusterCache = map[string]map[string]*Result{}
state.clusterCache = cmap.New()
state.cachedCQR = cmap.New()
}
// Query is the main interface to grange. See the main package documentation
// for query language specification. On error, an empty result is returned
// alongside the error. Queries that are longer than MaxQuerySize are
// considered errors.
//
// The size of the returned result is capped by MaxResults.
//
// This method is only thread-safe if PrimeCache() has previously been called
// on the state.
func (state *State) Query(input string) (Result, error) {
if len(input) > MaxQuerySize {
return NewResult(),
errors.New(fmt.Sprintf("Query is too long, max length is %d", MaxQuerySize))
}
context := newContext()
return evalRangeWithContext(input, state, &context)
}
// Used for populating state.cachedCQR
// Go over all clusters in state.clusters, look if element is part of cluster definition,
// if yes, add name of cluster to cachedCQR[element]
// parse through all clusters before adding to state.cachedCQR
// as a precaution, we do not want to cache empty results
func (state *State) populateCachedCQRforSet(set mapset.Set) {
context := newContext()
// skip processing for already cached elements
notCached := make(map[string]Result)
for element := range set.Iter() {
if !state.cachedCQR.Has(element.(string)) {
notCached[element.(string)] = NewResult()
}
}
if len(notCached) == 0 {
return
}
for clusterName, _ := range state.clusters {
subContext := context.subCluster(clusterName)
clusterLookup(state, &subContext, "CLUSTER")
for key := range notCached {
if subContext.currentResult.Contains(key) {
notCached[key].Add(clusterName)
}
}
}
for key, val := range notCached {
if val.Cardinality() > 0 {
state.cachedCQR.Set(key, val)
}
}
}
func (state *State) addValueToCachedCQR(key string, value string) {
if tmp, ok := state.cachedCQR.Get(key); ok {
tmp.(Result).Add(value)
} else {
state.cachedCQR.Set(key, NewResult(value))
}
}
type tooManyResults struct{}
type evalContext struct {
currentClusterName string
currentResult Result
workingResult *Result
depth int
}
func newContext() evalContext {
return evalContext{currentResult: NewResult()}
}
func parseRange(input string) (parserNode, error) {
r := &rangeQuery{Buffer: input}
r.Init()
if err := r.Parse(); err != nil {
return nil, err
}
r.Execute()
if len(r.nodeStack) > 0 {
return r.nodeStack[0], nil
} else {
return nodeNull{}, nil
}
}
func evalRangeWithContext(input string, state *State, context *evalContext) (Result, error) {
err := evalRangeInplace(input, state, context)
return context.currentResult, err
}
// Useful internally so that results do not need to be copied all over the place
func evalRangeInplace(input string, state *State, context *evalContext) (err error) {
if context.depth > MaxQueryDepth {
return errors.New("Query exceeded maximum recursion limit")
}
node, parseError := parseRange(input)
if parseError != nil {
return errors.New("Could not parse query: " + input)
}
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case tooManyResults:
// No error returned, we just chop off the results
state.metrics.numberOfTruncatedResults += 1
err = nil
case error:
err = r.(error)
default:
panic(r)
}
}
}()
return node.(evalNode).visit(state, context)
}
func (c evalContext) hasResults() bool {
return c.currentResult.Cardinality() == 0
}
func (n nodeBraces) visit(state *State, context *evalContext) error {
leftContext := context.sub()
rightContext := context.sub()
middleContext := context.sub()
if err := n.left.(evalNode).visit(state, &leftContext); err != nil {
return err
}
if err := n.node.(evalNode).visit(state, &middleContext); err != nil {
return err
}
if err := n.right.(evalNode).visit(state, &rightContext); err != nil {
return err
}
if leftContext.hasResults() {
leftContext.addResult("")
}
if middleContext.hasResults() {
middleContext.addResult("")
}
if rightContext.hasResults() {
rightContext.addResult("")
}
for l := range leftContext.resultIter() {
for m := range middleContext.resultIter() {
for r := range rightContext.resultIter() {
context.addResult(fmt.Sprintf("%s%s%s", l, m, r))
}
}
}
return nil
}
// Hack, see note on nodeBraceStart definition in nodes.go
func (n nodeBraceStart) visit(state *State, context *evalContext) error {
return nil
}
func (n nodeLocalClusterLookup) visit(state *State, context *evalContext) error {
var evalErr error
subContext := context.sub()
evalErr = n.node.(evalNode).visit(state, &subContext)
if evalErr != nil {
return evalErr
}
for key := range subContext.resultIter() {
if context.currentClusterName == "" {
context.addResult("$" + key.(string))
} else {
evalErr = clusterLookup(state, context, key.(string))
if evalErr != nil {
return evalErr
}
}
}
return nil
}
func (n nodeClusterLookup) visit(state *State, context *evalContext) error {
var evalErr error
subContext := context.sub()
evalErr = n.node.(evalNode).visit(state, &subContext)
if evalErr != nil {
return evalErr
}
keyContext := context.sub()
evalErr = n.key.(evalNode).visit(state, &keyContext)
if evalErr != nil {
return evalErr
}
for clusterName := range subContext.resultIter() {
context.currentClusterName = clusterName.(string)
for key := range keyContext.resultIter() {
evalErr = clusterLookup(state, context, key.(string))
if evalErr != nil {
return evalErr
}
}
}
return nil
}
func (c evalContext) sub() evalContext {
ret := newContext()
ret.currentClusterName = c.currentClusterName
ret.depth = c.depth + 1
return ret
}
func (c evalContext) subCluster(clusterName string) evalContext {
ret := c.sub()
ret.currentClusterName = clusterName
return ret
}
func (n nodeOperator) visit(state *State, context *evalContext) error {
switch n.op {
case operatorIntersect:
leftContext := context.sub()
if err := n.left.(evalNode).visit(state, &leftContext); err != nil {
return err
}
if leftContext.currentResult.Cardinality() == 0 {
// Optimization: no need to compute right side if left side is empty
return nil
}
rightContext := context.sub()
// nodeRegexp needs to know about LHS to filter correctly
rightContext.workingResult = &leftContext.currentResult
if err := n.right.(evalNode).visit(state, &rightContext); err != nil {
return err
}
for x := range leftContext.currentResult.Intersect(rightContext.currentResult.Set).Iter() {
context.addResult(x.(string))
}
case operatorSubtract:
leftContext := context.sub()
if err := n.left.(evalNode).visit(state, &leftContext); err != nil {
return err
}
if leftContext.currentResult.Cardinality() == 0 {
// Optimization: no need to compute right side if left side is empty
return nil
}
rightContext := context.sub()
// nodeRegexp needs to know about LHS to filter correctly
rightContext.workingResult = &leftContext.currentResult
if err := n.right.(evalNode).visit(state, &rightContext); err != nil {
return err
}
for x := range leftContext.currentResult.Difference(rightContext.currentResult.Set).Iter() {
context.addResult(x.(string))
}
case operatorUnion:
if err := n.left.(evalNode).visit(state, context); err != nil {
return err
}
if err := n.right.(evalNode).visit(state, context); err != nil {
return err
}
}
return nil
}
func (n nodeConstant) visit(state *State, context *evalContext) error {
context.addResult(n.val)
return nil
}
var (
numericRangeRegexp = regexp.MustCompile("^(.*?)(\\d+)\\.\\.([^\\d]*?)?(\\d+)(.*)$")
)
func (n nodeText) visit(state *State, context *evalContext) error {
match := numericRangeRegexp.FindStringSubmatch(n.val)
if len(match) == 0 {
context.addResult(n.val)
return nil
}
leftStr := match[1]
leftStrToMatch := match[1]
leftN := match[2]
rightStr := match[3]
rightN := match[4]
trailing := match[5]
// Equalize the numeric portions. n10..2 will initally be {"n", "10", 2"}, but
// needs to be converted to {"n1", "0", "2"}.
for {
if len(leftN) <= len(rightN) {
break
}
leftStr += leftN[0:1]
leftN = leftN[1:]
}
// a1..a4 is valid, a1..b4 is invalid
if !strings.HasSuffix(leftStrToMatch, rightStr) {
context.addResult(n.val)
return nil
}
width := strconv.Itoa(len(leftN))
low, _ := strconv.Atoi(leftN)
high, _ := strconv.Atoi(rightN)
for x := low; x <= high; x++ {
context.addResult(fmt.Sprintf("%s%0"+width+"d%s", leftStr, x, trailing))
}
return nil
}
func (n nodeGroupQuery) visit(state *State, context *evalContext) error {
subContext := context.sub()
if err := n.node.(evalNode).visit(state, &subContext); err != nil {
return err
}
lookingFor := subContext.currentResult
// It's theoretically nicer to re-use clusterLookup here, but it's an order
// of magnitude slower than poking around the cache directly.
clusterName := state.defaultCluster
var cachedClusterDetails cmap.ConcurrentMap
if tmp, ok := state.clusterCache.Get(clusterName); ok {
cachedClusterDetails = tmp.(cmap.ConcurrentMap)
} else {
cachedClusterDetails = cmap.New()
state.clusterCache.Set(clusterName, cachedClusterDetails)
}
for groupName, group := range state.clusters[state.defaultCluster] {
key := groupName
var results *Result
if tmp, ok := cachedClusterDetails.Get(key); ok {
results = tmp.(*Result)
} else {
subContext := context.subCluster(state.defaultCluster)
for _, value := range group {
err := evalRangeInplace(value, state, &subContext)
if err != nil {
return err
}
}
results = &subContext.currentResult
cachedClusterDetails.Set(key, results)
}
for x := range lookingFor.Iter() {
if results.Contains(x) {
context.addResult(groupName)
break
}
}
}
return nil
}
func (n nodeFunction) visit(state *State, context *evalContext) error {
switch n.name {
case "allclusters":
if err := n.verifyParams(0); err != nil {
return err
}
for clusterKey, _ := range state.clusters {
context.addResult(clusterKey)
}
case "count":
if err := n.verifyParams(1); err != nil {
return err
}
valueContext := context.sub()
if err := n.params[0].(evalNode).visit(state, &valueContext); err != nil {
return err
}
context.addResult(strconv.Itoa(valueContext.currentResult.Cardinality()))
case "has":
if err := n.verifyParams(2); err != nil {
return err
}
keyContext := context.sub()
valueContext := context.sub()
if err := n.params[0].(evalNode).visit(state, &keyContext); err != nil {
return err
}
if err := n.params[1].(evalNode).visit(state, &valueContext); err != nil {
return err
}
key := (<-keyContext.resultIter()).(string)
for clusterName, _ := range state.clusters {
subContext := context.subCluster(clusterName)
clusterLookup(state, &subContext, key)
l := subContext.currentResult.Set
r := valueContext.currentResult.Set
if l.Intersect(r).Cardinality() > 0 {
context.addResult(clusterName)
}
}
case "clusters":
if err := n.verifyParams(1); err != nil {
return err
}
subContext := context.sub()
if err := n.params[0].(evalNode).visit(state, &subContext); err != nil {
return err
}
lookingFor := subContext.currentResult
context.addSetToResult(state.getResultsFromCachedCQRforSet(lookingFor))
case "mem":
if err := n.verifyParams(2); err != nil {
return err
}
clusterContext := context.sub()
valueContext := context.sub()
if err := n.params[0].(evalNode).visit(state, &clusterContext); err != nil {
return err
}
if err := n.params[1].(evalNode).visit(state, &valueContext); err != nil {
return err
}
for clusterName := range state.clusters {
subContext := context.subCluster(clusterName)
clusterLookup(state, &subContext, "KEYS")
for _, clusterKey := range subContext.currentResult.Set.ToSlice() {
clusterKeyContext := subContext.sub()
clusterLookup(state, &clusterKeyContext, clusterKey.(string))
if clusterKeyContext.currentResult.Set.Intersect(valueContext.currentResult.Set).Cardinality() > 0 {
context.addResult(clusterKey.(string))
}
}
}
default:
return errors.New(fmt.Sprintf("Unknown function: %s", n.name))
}
return nil
}
func (n nodeFunction) verifyParams(expected int) error {
if len(n.params) != expected {
msg := fmt.Sprintf("Wrong number of params for %s: expected %d, got %d.",
n.name,
expected,
len(n.params),
)
return errors.New(msg)
}
return nil
}
func (n nodeRegexp) visit(state *State, context *evalContext) error {
if context.workingResult == nil {
subContext := context.sub()
state.allValues(&subContext)
context.workingResult = &subContext.currentResult
}
r, err := regexp.Compile(n.val)
if err != nil {
return err
}
for x := range context.workingResult.Iter() {
if r.MatchString(x.(string)) {
context.addResult(x.(string))
}
}
return nil
}
func (n nodeNull) visit(state *State, context *evalContext) error {
return nil
}
func (state *State) allValues(context *evalContext) error {
// Expand everything into the set
return evalRangeInplace("@{%"+state.defaultCluster+":KEYS}", state, context)
}
func clusterLookup(state *State, context *evalContext, key string) error {
var evalErr error
clusterName := context.currentClusterName
if clusterName == "" {
clusterName = state.defaultCluster
}
cluster := state.clusters[clusterName]
if key == "KEYS" {
for k, _ := range cluster {
context.currentResult.Add(k) // TODO: addResult
}
return nil
}
var cachedClusterDetails cmap.ConcurrentMap
if tmp, ok := state.clusterCache.Get(clusterName); ok {
cachedClusterDetails = tmp.(cmap.ConcurrentMap)
} else {
cachedClusterDetails = cmap.New()
state.clusterCache.Set(clusterName, cachedClusterDetails)
}
var results *Result
if tmp, ok := cachedClusterDetails.Get(key); ok {
results = tmp.(*Result)
} else {
clusterExp := cluster[key] // TODO: Error handling
subContext := context.subCluster(context.currentClusterName)
for _, value := range clusterExp {
evalErr = evalRangeInplace(value, state, &subContext)
if evalErr != nil {
return evalErr
}
}
results = &subContext.currentResult
cachedClusterDetails.Set(key, results)
}
for x := range results.Iter() {
context.addResult(x.(string))
}
return nil
}
func (c *evalContext) addResult(value string) {
if c.currentResult.Cardinality() >= MaxResults {
panic(tooManyResults{})
}
if len(value) > MaxQuerySize {
panic(errors.New(
fmt.Sprintf("Value would exceed max query size: %s...", value[0:20])))
}
c.currentResult.Add(value)
}
func (c *evalContext) addSetToResult(set mapset.Set) {
if c.currentResult.Cardinality()+set.Cardinality() >= MaxResults {
panic(tooManyResults{})
}
for value := range set.Iter() {
c.currentResult.Add(value.(string))
}
}
func (c *evalContext) resultIter() <-chan interface{} {
return c.currentResult.Iter()
}
type evalNode interface {
visit(*State, *evalContext) error
}
func (state *State) getResultsFromCachedCQRforSet(set mapset.Set) Result {
context := newContext()
state.populateCachedCQRforSet(set)
for name := range set.Iter() {
if subResult, ok := state.cachedCQR.Get(name.(string)); ok {
//add subresults to context.currentResult
context.addSetToResult(subResult.(Result))
}
}
return context.currentResult
}