package com.atlassian.bitbucket.mesh.migration;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Optional;

public interface MigrationStateManager {

    /**
     * Creates a new migration state for the provided hierarchy and immediately persists the state (on disk) to
     * ensure that subsequent calls to {@link #getForHierarchy(String)} return the state, even when called from
     * other processes (e.g. different Bitbucket Server and/or Mesh nodes accessing the same shared home).
     *
     * @param hierarchyId the hierarchy ID
     * @param targetPartition the partition the hierarchy is being migrated to
     * @return the newly created {@link HierarchyMigrationState}
     * @throws IOException if the migration state could not be persisted
     */
    @Nonnull
    HierarchyMigrationState createForHierarchy(@Nonnull String hierarchyId, int targetPartition) throws IOException;

    /**
     * @param hierarchyId the hierarchy ID
     * @return the {@link HierarchyMigrationState migration state}, or {@link Optional#empty()} if migration has not
     *         started for the hierarchy in question
     */
    @Nonnull
    Optional<HierarchyMigrationState> getForHierarchy(@Nonnull String hierarchyId);
}
