@@ -31,15 +31,12 @@ type coreMatcher struct {
3131
3232// coreFields groups the updateable fields in coreMatcher.
3333// state is the start of the automaton.
34- // namesUsed is a map of field names that are used in any of the patterns that this automaton encodes. Typically,
34+ // segmentsTree is a tree of segments that are used in any of the patterns that this automaton encodes.Typically,
3535// patterns only consider a subset of the fields in an incoming data object, and there is no reason to consider
3636// fields that do not appear in patterns when using the automaton for matching.
37- // fakeField is used when the flattener for an event returns no fields, because it could still match if
38- // there were patterns with "exists":false. So in this case we run one fake field through the matcher
39- // which will cause it to notice that any "exists":false patterns should match.
4037type coreFields struct {
41- state * fieldMatcher
42- namesUsed map [ string ] bool
38+ state * fieldMatcher
39+ segmentsTree * segmentsTree
4340}
4441
4542func newCoreMatcher () * coreMatcher {
@@ -49,8 +46,8 @@ func newCoreMatcher() *coreMatcher {
4946 // user-supplied path-name because it's not valid in UTF-8
5047 m := coreMatcher {}
5148 m .updateable .Store (& coreFields {
52- state : newFieldMatcher (),
53- namesUsed : make ( map [ string ] bool ),
49+ state : newFieldMatcher (),
50+ segmentsTree : newSegmentsIndex ( ),
5451 })
5552 return & m
5653}
@@ -62,7 +59,7 @@ func (m *coreMatcher) start() *coreFields {
6259// addPattern - the patternBytes is a JSON object. The X is what the matcher returns to indicate that the
6360// provided pattern has been matched. In many applications it might be a string which is the pattern's name.
6461func (m * coreMatcher ) addPattern (x X , patternJSON string ) error {
65- patternFields , patternNamesUsed , err := patternFromJSON ([]byte (patternJSON ))
62+ patternFields , err := patternFromJSON ([]byte (patternJSON ))
6663 if err != nil {
6764 return err
6865 }
@@ -75,15 +72,13 @@ func (m *coreMatcher) addPattern(x X, patternJSON string) error {
7572
7673 // we build up the new coreMatcher state in freshStart so we can atomically switch it in once complete
7774 freshStart := & coreFields {}
78- freshStart .namesUsed = make (map [string ]bool )
7975 current := m .start ()
76+ freshStart .segmentsTree = current .segmentsTree .copy ()
8077 freshStart .state = current .state
8178
82- for k := range current .namesUsed {
83- freshStart .namesUsed [k ] = true
84- }
85- for used := range patternNamesUsed {
86- freshStart .namesUsed [used ] = true
79+ // Add paths to the segments tree index.
80+ for _ , field := range patternFields {
81+ freshStart .segmentsTree .add (field .path )
8782 }
8883
8984 // now we add each of the name/value pairs in fields slice to the automaton, starting with the start state -
@@ -132,7 +127,7 @@ func (m *coreMatcher) deletePatterns(_ X) error {
132127// This is a leftover from previous times, is only used by tests, but it's used by a *lot*
133128// so removing it would require a lot of tedious work
134129func (m * coreMatcher ) matchesForJSONEvent (event []byte ) ([]X , error ) {
135- fields , err := newJSONFlattener ().Flatten (event , m )
130+ fields , err := newJSONFlattener ().Flatten (event , m . getSegmentsTreeTracker () )
136131 if err != nil {
137132 return nil , err
138133 }
@@ -251,7 +246,6 @@ func noArrayTrailConflict(from []ArrayPos, to []ArrayPos) bool {
251246 return true
252247}
253248
254- func (m * coreMatcher ) IsNameUsed (label []byte ) bool {
255- _ , ok := m .start ().namesUsed [string (label )]
256- return ok
249+ func (m * coreMatcher ) getSegmentsTreeTracker () SegmentsTreeTracker {
250+ return m .start ().segmentsTree
257251}
0 commit comments