Interface EventCriteria
- All Known Subinterfaces:
EventCriterion,EventTypeRestrictableEventCriteria
EventStoreTransaction#source(SourcingCondition) sourcing or
invalid reference
streaming
Filtering
Filtering happens based on the tags of the event, indicating an association during publishing of the event. For example, a student enrolling in a course may have the "student" tag with the value of its id. This value is determined during publishing by theTagResolver. If an instance of a criteria contains
multiple tags, the event must contain all of them to be considered a match.
EventCriteria criteria = EventCriteria
.havingTags(Tag.of("student", "matchingStudent"))
After first defining the tags to filter on through havingTags(Tag...) or one of its variants, the scope of
the read on the event store can further be limited on the type, through
EventTypeRestrictableEventCriteria.andBeingOneOfTypes(QualifiedName...). This is optional, and will default
to all types if not specified.
EventCriteria criteria = EventCriteria
// Reads all events with this tag
.havingTags(Tag.of("student", "matchingStudent"))
.or()
// Only reads events with this tag and type
.havingTags("course", "matchingCourse")
.andBeingOneOfTypes(new QualifiedName("CourseRegistered"))
When using an event store that supports it, using such a type-filtered criteria will narrow the scope of your transaction, leading to better performance and a lower chance of concurrency conflicts. So while optional, it's recommended to use it when possible.
Combining
You can combine multiple criteria usingeither(EventCriteria...), or in a fluent fashion using
or(). This allows you to create more complex criteria that match events based on multiple tags or types.
However, it's not possible to create AND conditions between multiple criteria.
EventCriteria criteria = EventCriteria
.havingTags(Tag.of("student", "matchingStudent"))
.andBeingOneOfTypes(new QualifiedName("StudentEnrolled"))
.or()
.havingTags("course", "matchingCourse")
.andBeingOneOfTypes(new QualifiedName("CourseRegistered"))
Examples
To make it easier to understand how the criteria work, here are some examples. These examples all have the following
events in the event store:
- Event [StudentRegistered, student -> matchingStudent]
- Event [CourseRegistered, course -> matchingCourse]
- Event [StudentAssignedToCourse, student -> nonMatchingStudent, course -> matchingCourse]
- Event [StudentAssignedToCourse, student -> matchingStudent, course -> nonMatchingCourse]
- Event [StudentAssignedToCourse, student -> matchingStudent, course -> matchingStudent]
- Event [StudentRegistered, student -> nonMatchingStudent]
- Event [CourseRegistered, course -> nonMatchingCourse]
- Event [StudentAssignedToCourse, student -> nonMatchingStudent, course -> nonMatchingCourse]
Example 1
EventCriteria criteria = EventCriteria
.havingTags(Tag.of("student", "matchingStudent"))
This criteria will match all events with the tag "student" and the value "matchingStudent". The following events will
match:
- Event [StudentRegistered, student -> matchingStudent]
- Event [StudentAssignedToCourse, student -> matchingStudent, course -> nonMatchingCourse]
- Event [StudentAssignedToCourse, student -> matchingStudent, course -> matchingStudent]
Example 2
We don't have to pass in tags as Tag instances. We can also pass them in as
key-value pairs:
EventCriteria criteria = EventCriteria
.havingTags("student", "matchingStudent", "course", "matchingCourse")
EventCriteria criteria = EventCriteria
.havingTags(Tag.of("student", "matchingStudent"))
EventCriteria criteria = EventCriteria
.havingTags("student", "matchingStudent", "course", "matchingCourse")
This criteria will match all events with both the "student" tag of value "matchingStudent" and the "course" tag with value "matchingStudent". The following events will match:
- Event [StudentRegistered, student -> matchingStudent]
- Event [StudentAssignedToCourse, student -> matchingStudent, course -> matchingStudent]
Example 3
We can also combine multiple criteria usingor(EventCriteria), such as searching for multiple students:
EventCriteria criteria = EventCriteria
.havingTags("student", "matchingStudent")
.or()
.havingTags("student", "nonMatchingStudent")
This works as an OR condition, meaning that the following events will match:
- Event [StudentRegistered, student -> matchingStudent]
- Event [StudentAssignedToCourse, student -> matchingStudent, course -> nonMatchingCourse]
- Event [StudentAssignedToCourse, student -> nonMatchingStudent, course -> matchingCourse]
- Event [StudentAssignedToCourse, student -> nonMatchingStudent, course -> nonMatchingCourse]
Example 4
Last but not least, let's say that we only want the "StudentRegistered" events for the "matchingStudent". This can be done by using theEventTypeRestrictableEventCriteria.andBeingOneOfTypes(QualifiedName...) method:
EventCriteria criteria = EventCriteria
.havingTags("student", "matchingStudent")
.andBeingOneOfTypes(new QualifiedName("StudentRegistered"))
The following events will match:
- Event [StudentRegistered, student -> matchingStudent]
EventTypeRestrictableEventCriteria for all the methods available to you.
Flattening
The criteria can be flattened into aSet of EventCriterion instances, which are a specialized
representation of the criteria that are guaranteed to be non-nested. As such, these instances can be used to read
their EventCriterion.tags() and EventCriterion.types() without the need to interpret conditions,
as this is already done by the flatten method.- Since:
- 5.0.0
- Author:
- Michal Negacz, Milan Savić, Marco Amann, Sara Pellegrini, Steven van Beelen, Mitchell Herrijgers
-
Method Summary
Modifier and TypeMethodDescriptionstatic EventCriteriaeither(Collection<EventCriteria> eventCriteria) Create anEventCriteriathat matches events that match either of the givenEventCriteria.static EventCriteriaeither(EventCriteria... eventCriteria) Create anEventCriteriathat matches events that match either of the givenEventCriteria.flatten()booleanIndicates whether thisEventCriteriainstance has any criteria defined.Construct aEventCriteriathat allows any events.havingTags(String... tags) Define, as key-value pairs, that the event must contain all the providedtagsto match.havingTags(Set<Tag> tags) Define that the event must contain all the providedtagsto match.havingTags(Tag... tags) Define that the event must contain all the providedtagsto match.booleanmatches(QualifiedName type, Set<Tag> tags) Indicates whether the giventypeandtagsmatches the types and tags defined in this or these criteria.default OrEventCriteriaBuilderor()Start a builder to construct an additionalEventCriteriathat will be combined with this one usingor(EventCriteria).or(EventCriteria criteria) Create anEventCriteriathat matches events from eitherthisor the givencriteria EventCriteria.
-
Method Details
-
havingAnyTag
Construct aEventCriteriathat allows any events.Use this instance when all events are of interest during
or when there are no consistency boundaries to validate duringinvalid reference
streaming. Note that thisinvalid reference
appendingEventCriteriadoes not make sense for, as it is not recommended to source the entire event store.invalid reference
sourcingEvent though this criteria will not filter any tags, you can limit the types of events to be matched by using the
EventTypeRestrictableEventCriteria.andBeingOneOfTypes(Set)method.- Returns:
- An
EventCriteriathat contains no criteria at all.
-
havingTags
Define that the event must contain all the providedtagsto match. These tags function in an AND relation, meaning that an event must have all tags to match. A partial match is not sufficient.You can further limit the types of events to be matched by using the
EventTypeRestrictableEventCriteria.andBeingOneOfTypes(Set)method.- Parameters:
tags- The tags to match against.- Returns:
- The completed EventCriteria instance.
-
havingTags
Define that the event must contain all the providedtagsto match. These tags function in an AND relation, meaning that an event must have all tags to match. A partial match is not sufficient.You can further limit the types of events to be matched by using the
EventTypeRestrictableEventCriteria.andBeingOneOfTypes(Set)method.- Parameters:
tags- The tags to match against.- Returns:
- The completed EventCriteria instance.
-
havingTags
Define, as key-value pairs, that the event must contain all the providedtagsto match. These tags function in an AND relation, meaning that an event must have all tags to match. A partial match is not sufficient.You can further limit the types of events to be matched by using the
EventTypeRestrictableEventCriteria.andBeingOneOfTypes(Set)method.- Parameters:
tags- The tags to match against.- Returns:
- The completed EventCriteria instance.
-
or
Create anEventCriteriathat matches events from eitherthisor the givencriteria EventCriteria.- Parameters:
criteria- TheEventCriteriato match in addition to thisEventCriteria.- Returns:
- An
EventCriteriathat matches events that match either thisEventCriteriaor the givenEventCriteria.
-
either
Create anEventCriteriathat matches events that match either of the givenEventCriteria.- Parameters:
eventCriteria- TheEventCriteriaof which one must match.- Returns:
- An
EventCriteriathat matches events that match either of the givenEventCriteria.
-
either
Create anEventCriteriathat matches events that match either of the givenEventCriteria.- Parameters:
eventCriteria- TheEventCriteriaof which one must match.- Returns:
- An
EventCriteriathat matches events that match either of the givenEventCriteria.
-
or
Start a builder to construct an additionalEventCriteriathat will be combined with this one usingor(EventCriteria). The resultingEventCriteriawill match events that match either thisEventCriteriaor the built one.- Returns:
- A builder to construct an EventCriteria instance that will match events that match either this
EventCriteriaor the built one.
-
matches
Indicates whether the giventypeandtagsmatches the types and tags defined in this or these criteria. If no types are defined, any giventypewill be considered a match.- Parameters:
type- The type to match against this criteria instance.tags- The tags to match against this criteria instance.- Returns:
trueif the type matches, otherwisefalse.
-
flatten
Set<EventCriterion> flatten()Flatten this, possibly nested,EventCriteriainto aSetofEventCriterion. TheseEventCriterioninstances can be used by theto construct queries without the need to interpret the criteria.invalid reference
EventStore- Returns:
- The flattened set of
EventCriteria.
-
hasCriteria
boolean hasCriteria()Indicates whether thisEventCriteriainstance has any criteria defined.- Returns:
trueif thisEventCriteriainstance has criteria defined, otherwisefalse.
-