Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Allow matcher.Namespace to support reverse match
Browse files Browse the repository at this point in the history
Also added a function to return all the mapping policies for a match
result
  • Loading branch information
Chao Wang committed Jun 22, 2017
1 parent 6ede279 commit 9ce119d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
20 changes: 19 additions & 1 deletion matcher/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ type OnNamespaceRemovedFn func(namespace []byte)
// OnRuleSetUpdatedFn is called when a ruleset is updated.
type OnRuleSetUpdatedFn func(namespace []byte, ruleSet RuleSet)

// Options provide a set of options for the msgpack-based reporter.
// Options provide a set of options for the matcher.
type Options interface {
// SetMatchMode sets the match mode.
SetMatchMode(value rules.MatchMode) Options

// MatchMode returns the match mode.
MatchMode() rules.MatchMode

// SetClockOptions sets the clock options.
SetClockOptions(value clock.Options) Options

Expand Down Expand Up @@ -139,6 +145,7 @@ type Options interface {
}

type options struct {
matchMode rules.MatchMode
clockOpts clock.Options
instrumentOpts instrument.Options
ruleSetOpts rules.Options
Expand All @@ -157,6 +164,7 @@ type options struct {
// NewOptions creates a new set of options.
func NewOptions() Options {
return &options{
matchMode: rules.ForwardMatch,
clockOpts: clock.NewOptions(),
instrumentOpts: instrument.NewOptions(),
ruleSetOpts: rules.NewOptions(),
Expand All @@ -170,6 +178,16 @@ func NewOptions() Options {
}
}

func (o *options) SetMatchMode(value rules.MatchMode) Options {
opts := *o
opts.matchMode = value
return &opts
}

func (o *options) MatchMode() rules.MatchMode {
return o.matchMode
}

func (o *options) SetClockOptions(value clock.Options) Options {
opts := *o
opts.clockOpts = value
Expand Down
6 changes: 4 additions & 2 deletions matcher/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ruleSet struct {
sync.RWMutex
runtime.Value

matchMode rules.MatchMode
namespace []byte
key string
store kv.Store
Expand All @@ -98,8 +99,9 @@ func newRuleSet(
r := &ruleSet{
namespace: namespace,
key: key,
store: opts.KVStore(),
opts: opts,
matchMode: opts.MatchMode(),
store: opts.KVStore(),
nowFn: opts.ClockOptions().NowFn(),
matchRangePast: opts.MatchRangePast(),
ruleSetOpts: opts.RuleSetOptions(),
Expand Down Expand Up @@ -154,7 +156,7 @@ func (r *ruleSet) Match(id []byte, fromNanos, toNanos int64) rules.MatchResult {
r.metrics.nilMatcher.Inc(1)
return rules.EmptyMatchResult
}
res := r.matcher.MatchAll(id, fromNanos, toNanos, rules.ForwardMatch)
res := r.matcher.MatchAll(id, fromNanos, toNanos, r.matchMode)
r.RUnlock()
r.metrics.match.ReportSuccess(r.nowFn().Sub(callStart))
return res
Expand Down
6 changes: 5 additions & 1 deletion matcher/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestRuleSetMatchWithMatcher(t *testing.T) {
require.Equal(t, []byte("foo"), mockMatcher.id)
require.Equal(t, fromNanos, mockMatcher.fromNanos)
require.Equal(t, toNanos, mockMatcher.toNanos)
require.Equal(t, rules.ReverseMatch, mockMatcher.mode)
}

func TestToRuleSetNilValue(t *testing.T) {
Expand Down Expand Up @@ -139,6 +140,7 @@ type mockMatcher struct {
id []byte
fromNanos int64
toNanos int64
mode rules.MatchMode
res rules.MatchResult
}

Expand All @@ -150,6 +152,7 @@ func (mm *mockMatcher) MatchAll(
mm.id = id
mm.fromNanos = fromNanos
mm.toNanos = toNanos
mm.mode = matchMode
return mm.res
}

Expand All @@ -174,6 +177,7 @@ func testRuleSet() (kv.Store, Cache, *ruleSet, Options) {
SetInitWatchTimeout(100 * time.Millisecond).
SetKVStore(store).
SetRuleSetKeyFn(func(ns []byte) string { return fmt.Sprintf("/rules/%s", ns) }).
SetOnRuleSetUpdatedFn(func(namespace []byte, ruleSet RuleSet) { cache.Register(namespace, ruleSet) })
SetOnRuleSetUpdatedFn(func(namespace []byte, ruleSet RuleSet) { cache.Register(namespace, ruleSet) }).
SetMatchMode(rules.ReverseMatch)
return store, cache, newRuleSet(testNamespace, testNamespacesKey, opts).(*ruleSet), opts
}
5 changes: 5 additions & 0 deletions rules/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (r *MatchResult) HasExpired(timeNanos int64) bool { return r.expireAtNanos
// NumRollups returns the number of rollup metrics.
func (r *MatchResult) NumRollups() int { return len(r.rollups) }

// AllMappings returns all the mapping policies in the match result.
func (r *MatchResult) AllMappings() policy.PoliciesList {
return r.mappings
}

// MappingsAt returns the active mapping policies at a given time.
func (r *MatchResult) MappingsAt(timeNanos int64) policy.PoliciesList {
return activePoliciesAt(r.mappings, timeNanos)
Expand Down
1 change: 1 addition & 0 deletions rules/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func TestMatchResult(t *testing.T) {
}

res := NewMatchResult(0, testExpireAtNanos, testResultMappings, testResultRollups)
require.Equal(t, testResultMappings, res.AllMappings())
for _, input := range inputs {
require.Equal(t, input.expectedMappings, res.MappingsAt(input.matchAtNanos))
require.Equal(t, len(input.expectedRollups), res.NumRollups())
Expand Down

0 comments on commit 9ce119d

Please sign in to comment.