package com.atlassian.stash.internal.idx;

import com.atlassian.bitbucket.idx.CommitIndexer;
import com.atlassian.stash.internal.repository.InternalRepository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Set;

/**
 * DAO for storing when indexers have last indexed a particular repository.
 */
public interface IndexerStateDao {

    /**
     * @param repository the repository
     * @param indexer    the indexer
     * @return when {@code indexer} last indexed {@code repository}. {@code null} if it has never indexed {@code repository}
     */
    @Nullable
    Long getLastRunTimestamp(@Nonnull InternalRepository repository, @Nonnull CommitIndexer indexer);

    /**
     * @param repository the repository
     * @param indexers   a collection of indexers
     * @return the oldest time when any {@code indexer} last indexed {@code repository}. {@code null} if at least one
     * of them has ever indexed {@code repository}
     */
    @Nullable
    Long getOldestLastRunTimestamp(@Nonnull InternalRepository repository,
                                   @Nonnull Iterable<CommitIndexer> indexers);

    /**
     * @param repository the repository
     * @return a set of all the 'last run dates' that are referenced by any indexer
     */
    @Nonnull
    Set<Long> getReferencedLastRunTimestamps(@Nonnull InternalRepository repository);

    /**
     * Sets / updates the last-run-date for the indexer and the repository
     *
     * @param repository the repository
     * @param indexer    the indexer
     * @param timestamp  the timestamp to store
     */
    void setLastRunTimestamp(@Nonnull InternalRepository repository, @Nonnull CommitIndexer indexer, long timestamp);

    /**
     * Sets / updates the last-run-date for each of the indexers and the repository
     *
     * @param repository the repository
     * @param indexers   the indexers
     * @param timestamp  the timestamp to store
     */
    void setLastRunTimestamp(@Nonnull InternalRepository repository, @Nonnull Iterable<CommitIndexer> indexers,
                             long timestamp);
}
