package com.atlassian.bitbucket.mesh.repository;

import com.fasterxml.jackson.core.JsonGenerator;

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

/**
 * Represents a location on disk where repositories are stored.
 */
public interface DataStore {

    /**
     * @return information about the datastore such as available space and type of datastore
     */
    @Nonnull
    DataStoreInfo getInfo();

    /**
     * @return the type of datastore
     */
    @Nonnull
    DataStoreType getType();

    /**
     * Get all configured root directories in which work tree directories may have been created.
     *
     * @return a set of configured <em>root</em> directories for work trees
     */
    @Nonnull
    Set<Path> getWorkTreesDirs();

    /**
     * Lifecycle method that is called when the application starts up. {@code DataStore} implementations that
     * need exclusive ownership of the data store directory should acquire a lock in this method.
     */
    default void start() {
    }

    /**
     * Lifecycle method that is called before the application shuts down. {@code DataStore} implementations
     * should release any locks or other resources that they hold when this method is called.
     */
    default void stop() {
    }

    //DataStore does not extend SupportInfoProvider because DataStore beans are created in the application context
    //as top-level beans, but we want to have the DataStoreManager coordinate the writing of support info for all
    //data stores in the "data-stores" section.
    void writeSupportInfo(@Nonnull JsonGenerator json) throws IOException;
}
