com.github.rahulsom.grooves.queries.internal.BaseQuery Maven / Gradle / Ivy
package com.github.rahulsom.grooves.queries.internal;
import com.github.rahulsom.grooves.api.AggregateType;
import com.github.rahulsom.grooves.api.EventApplyOutcome;
import com.github.rahulsom.grooves.api.events.BaseEvent;
import com.github.rahulsom.grooves.api.snapshots.internal.BaseSnapshot;
import org.jetbrains.annotations.NotNull;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* Aggregate trait that simplifies computing snapshots from events.
*
* @param The type of {@link AggregateT}'s id
* @param The aggregate over which the query executes
* @param The type of the {@link EventT}'s id field
* @param The type of the Event
* @param The type of the {@link SnapshotT}'s id field
* @param The type of the Snapshot
* @param A reference to the query type. Typically a self reference.
*
* @author Rahul Somasunderam
*/
public interface BaseQuery<
AggregateIdT,
AggregateT extends AggregateType,
EventIdT,
EventT extends BaseEvent,
SnapshotIdT,
SnapshotT extends BaseSnapshot,
QueryT extends BaseQuery
> {
default Logger getLog() {
return LoggerFactory.getLogger(getClass());
}
/**
* When no snapshot is found in the database, the query has to create the zero of the snapshot.
* This is the implementation of such a snapshot.
*
* @return The empty snapshot
*/
@NotNull SnapshotT createEmptySnapshot();
/**
* Gets the last snapshot before said event. Is responsible for discarding attached entity.
*
* @param maxPosition The position before which a snapshot is required
* @param aggregate The aggregate for which a snapshot is required
*
* @return An observable that returns at most one Snapshot
*/
@NotNull Publisher getSnapshot(long maxPosition, @NotNull AggregateT aggregate);
/**
* Gets the last snapshot before given timestamp. Is responsible for discarding attached entity.
*
* @param maxTimestamp The maximum timestamp of the snapshot
* @param aggregate The aggregate for which a snapshot is required
*
* @return An observable that returns at most one Snapshot
*/
@NotNull Publisher getSnapshot(Date maxTimestamp, @NotNull AggregateT aggregate);
/**
* Decides whether applying more events is permitted on a snapshot.
*
* @param snapshot The snapshot
*
* @return whether more events can be applied
*/
boolean shouldEventsBeApplied(@NotNull SnapshotT snapshot);
/**
* Finds all events older than a given event.
*
* @param event The event before which events are eligible
*
* @return The list of events
*/
@NotNull Publisher findEventsBefore(@NotNull EventT event);
/**
* Adds an aggregate to the list of aggregates that are deprecated by the aggregate of a
* snapshot.
*
* @param snapshot The snapshot that points to the winning aggregate
* @param deprecatedAggregate The aggregate that is deprecated
*/
void addToDeprecates(@NotNull SnapshotT snapshot, @NotNull AggregateT deprecatedAggregate);
/**
* Exception handler when applying events.
*
* @param e The exception
* @param snapshot The snapshot
* @param event The event that resulted in an exception
*
* @return The outcome of handling the exception
*/
@NotNull Publisher onException(
@NotNull Exception e, @NotNull SnapshotT snapshot, @NotNull EventT event);
/**
* @return An executor that applies events.
*/
@NotNull > ExecutorT getExecutor();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy