package com.atlassian.bitbucket.mesh.migration;

import com.atlassian.bitbucket.mesh.transaction.TransactionLog;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Path;

public interface HierarchyMigrationState {

    /**
     * Deletes the contents of the {@link #getTmpDir()} and the {@link #getTransactionLog()}, and if {@code everything}
     * is set to {@code true}, also the partition mapping for the hierarchy.
     */
    void clear(boolean everything);

    /**
     * @return the partition the migration is targeting
     */
    int getTargetPartition();

    /**
     * @return the directory where temporary migration state can be stored
     */
    @Nonnull
    Path getTmpDir();

    /**
     * @return the {@link TransactionLog} used to record concurrent writes while migration is in progress
     */
    @Nonnull
    TransactionLog getTransactionLog();

    /**
     * @return {@code true} if {@link #stop(boolean)} has been called with {@code completed} set to {@code true}
     *         (presumably when migration has finished);otherwise {@code false}
     */
    boolean isCompleted();

    /**
     * @return {@code true} if migration is currently running, or more precisely if {@link #start()} has been called,
     *         and {@link #stop(boolean)} has <em>NOT</em> been called
     */
    boolean isRunning();

    /**
     * Marks the migration as currently {@link #isRunning() running}
     */
    void start() throws IOException;

    /**
     * Marks the migration as NOT currently {@link #isRunning() running}. If {@code completed} is {@code true},
     * subsequent calls to {@link #isCompleted()} will return {@code true}.
     *
     * @param completed whether migration was completed successfully
     */
    void stop(boolean completed);
}
