Interface EventStoreTransaction
- All Known Implementing Classes:
DefaultEventStoreTransaction
EventStore
based on the resulting MessageStream.
Note that this transaction includes operations for source(SourcingCondition) the model as well as
appending events.
- Since:
- 5.0.0
- Author:
- Allard Buijze, Steven van Beelen
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendEvent(EventMessage eventMessage) Appends aneventMessageto be appended to anEventStorein this transaction.Returns the position in the event store of the lastappendedevent by this transaction.voidonAppend(Consumer<EventMessage> callback) Registers acallbackto invoke when an event isappendedto this transaction.default voidoverrideAppendCondition(UnaryOperator<AppendCondition> conditionOverride) Overrides theAppendConditionthat will be used when committing this transaction.default MessageStream<? extends EventMessage> source(SourcingCondition condition) Sources aMessageStreamof typeEventMessagebased on the givenconditionthat can be used to rehydrate a model.MessageStream<? extends EventMessage> source(SourcingCondition condition, @Nullable Consumer<Position> resumePositionCallback) Sources aMessageStreamof typeEventMessagebased on the givencondition, optionally invoking the givenresume positioncallback.
-
Method Details
-
source
Sources aMessageStreamof typeEventMessagebased on the givenconditionthat can be used to rehydrate a model.Note that using
no criteriadoes not make sense for sourcing, as it is not recommended to source the entire event store.Any
EventStoreTransactionusing theEventStorageEngine.source(SourcingCondition)is expected tofiltertheTerminalEventMessagewith theConsistencyMarker.- Parameters:
condition- TheSourcingConditionused to retrieve theMessageStreamcontaining the sequence of events that can rehydrate a model.- Returns:
- The
MessageStreamof typeEventMessagecontaining the event sequence complying to the givencondition.
-
source
MessageStream<? extends EventMessage> source(SourcingCondition condition, @Nullable Consumer<Position> resumePositionCallback) Sources aMessageStreamof typeEventMessagebased on the givencondition, optionally invoking the givenresume positioncallback.The provided
resumePositionCallback, if non-null, is invoked at most once and only after the returnedMessageStreamhas been consumed completely. For most implementations, theresume positionis only known when the stream reaches its terminal event. As such, the callback is guaranteed to be invoked only if the stream is fully consumed.If sourcing completes and no events are found, the callback will be invoked with the position specified in
sourcingConditionor with a greater position. Returning a greater position allows resuming from a point that already excludes positions known to be non-matching.If the stream terminates with an error, is closed prematurely, or is not consumed to completion, the callback is not guaranteed to be invoked.
The callback should not throw exceptions; doing so may result in undefined behavior.
Note that using
no criteriadoes not make sense for sourcing, as it is not recommended to source the entire event store.Any
EventStoreTransactionusing theEventStorageEngine.source(SourcingCondition)is expected tofiltertheTerminalEventMessagewith theConsistencyMarker.- Parameters:
condition- TheSourcingConditionused to retrieve theMessageStreamcontaining the sequence of events that can rehydrate a model.resumePositionCallback- An optional callback that receives thePositionfrom which sourcing may be resumed once it becomes available; the position provided is nevernull.- Returns:
- The
MessageStreamof typeEventMessagecontaining the event sequence complying to the givencondition. - Since:
- 5.0.3
-
appendEvent
Appends aneventMessageto be appended to anEventStorein this transaction.- Parameters:
eventMessage- TheEventMessageto append.
-
onAppend
Registers acallbackto invoke when an event isappendedto this transaction.Each
callbackregistration adds a new callback that is invoked on theappendEvent(EventMessage, AppendCondition)operation.- Parameters:
callback- AConsumerto invoke when an event is appended in this transaction.
-
overrideAppendCondition
Overrides theAppendConditionthat will be used when committing this transaction. The providedconditionOverridefunction receives the currentAppendConditionderived fromsourcingcalls (orAppendCondition.none()if no sourcing occurred) and returns the desired condition.This allows two primary use cases:
- Appending without sourcing — enforcing uniqueness constraints without first sourcing events. The
input is
AppendCondition.none(), and the override sets the desired criteria and marker (e.g.,AppendCondition.withCriteria(EventCriteria)which usesConsistencyMarker.ORIGINto check against the entire event store). - Narrowing (or broadening) the append condition — sourcing events with broad criteria for state
but only a subset causes real conflicts. The override can narrow the criteria via
AppendCondition.replaceCriteria(EventCriteria)while preserving the sourced marker.
Multiple calls to this method compose: each subsequent override receives the output of the previous one. The override is applied at commit time, after all
sourcecalls have been processed.Returning
AppendCondition.none()(ornull) from the override function bypasses conflict detection entirely.- Parameters:
conditionOverride- aUnaryOperatorthat transforms the currentAppendCondition
- Appending without sourcing — enforcing uniqueness constraints without first sourcing events. The
input is
-
appendPosition
ConsistencyMarker appendPosition()Returns the position in the event store of the lastappendedevent by this transaction.Will return
ConsistencyMarker.ORIGINif nothing has been appended yet.- Returns:
- The position in the event store of the last
appendedevent by this transaction.
-