pubsub: use concrete queries instead of an interface (#7686)

Remove the pubsub.Query interface and instead use the concrete query type.
Nothing uses any other implementation but pubsub/query.

* query: remove the error from the Matches method
* Update all usage.
This commit is contained in:
M. J. Fromberger
2022-01-25 11:16:48 -08:00
committed by GitHub
parent f163acf499
commit 079c7af007
10 changed files with 39 additions and 79 deletions
+1 -4
View File
@@ -48,10 +48,7 @@ func BenchmarkMatchCustom(b *testing.B) {
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok, err := q.Matches(testEvents)
if err != nil {
b.Fatal(err)
} else if !ok {
if !q.Matches(testEvents) {
b.Error("no match")
}
}
+10 -15
View File
@@ -67,13 +67,18 @@ func Compile(ast syntax.Query) (*Query, error) {
return &Query{ast: ast, conds: conds}, nil
}
// Matches satisfies part of the pubsub.Query interface. This implementation
// never reports an error. A nil *Query matches all events.
func (q *Query) Matches(events []types.Event) (bool, error) {
// Matches reports whether q matches the given events. If q == nil, the query
// matches any non-empty collection of events.
func (q *Query) Matches(events []types.Event) bool {
if q == nil {
return true, nil
return true
}
return q.matchesEvents(events), nil
for _, cond := range q.conds {
if !cond.matchesAny(events) {
return false
}
}
return len(events) != 0
}
// String matches part of the pubsub.Query interface.
@@ -92,16 +97,6 @@ func (q *Query) Syntax() syntax.Query {
return q.ast
}
// matchesEvents reports whether all the conditions match the given events.
func (q *Query) matchesEvents(events []types.Event) bool {
for _, cond := range q.conds {
if !cond.matchesAny(events) {
return false
}
}
return len(events) != 0
}
// A condition is a compiled match condition. A condition matches an event if
// the event has the designated type, contains an attribute with the given
// name, and the match function returns true for the attribute value.
+2 -12
View File
@@ -7,13 +7,10 @@ import (
"time"
"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/internal/pubsub"
"github.com/tendermint/tendermint/internal/pubsub/query"
"github.com/tendermint/tendermint/internal/pubsub/query/syntax"
)
var _ pubsub.Query = (*query.Query)(nil)
// Example events from the OpenAPI documentation:
// https://github.com/tendermint/tendermint/blob/master/rpc/openapi/openapi.yaml
//
@@ -210,11 +207,7 @@ func TestCompiledMatches(t *testing.T) {
t.Fatalf("NewCompiled %#q: unexpected error: %v", tc.s, err)
}
got, err := c.Matches(tc.events)
if err != nil {
t.Errorf("Query: %#q\nInput: %+v\nMatches: got error %v",
tc.s, tc.events, err)
}
got := c.Matches(tc.events)
if got != tc.matches {
t.Errorf("Query: %#q\nInput: %+v\nMatches: got %v, want %v",
tc.s, tc.events, got, tc.matches)
@@ -231,10 +224,7 @@ func TestAllMatchesAll(t *testing.T) {
`Rilly|Blue=`,
)
for i := 0; i < len(events); i++ {
match, err := query.All.Matches(events[:i])
if err != nil {
t.Errorf("Matches failed: %w", err)
} else if !match {
if !query.All.Matches(events[:i]) {
t.Errorf("Did not match on %+v ", events[:i])
}
}