All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;

import java.util.Date;
import java.util.List;

/**
 * Aggregate trait that simplifies computing snapshots from events.
 *
 * @param   The Aggregate type
 * @param     The Event's id's type
 * @param       The Event type
 * @param  The snapshot's id's type
 * @param    The snapshot type
 *
 * @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
     */
    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
     */
    Observable getSnapshot(long maxPosition, 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
     */
    Observable getSnapshot(Date maxTimestamp, 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(SnapshotT snapshot);

    /**
     * Finds all events for a given list of aggregates.
     *
     * @param aggregates The list of aggregates
     *
     * @return The list of events
     */
    Observable findEventsForAggregates(List aggregates);

    /**
     * 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(SnapshotT snapshot, 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
     */
    Observable onException(Exception e, SnapshotT snapshot, EventT event);

    /**
     * @return An executor that applies events.
     */
    Executor getExecutor();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy