blob: cc1936a1369dbf8d5358e236b290c8033dcc7fe9 [file] [log] [blame]
package org.onosproject.store.consistent.impl;
import net.kuujo.copycat.cluster.ClusterConfig;
import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
import net.kuujo.copycat.cluster.internal.coordinator.CoordinatorConfig;
import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
import net.kuujo.copycat.resource.Resource;
/**
* Database.
*/
public interface Database extends DatabaseProxy<String, byte[]>, Resource<Database> {
/**
* Creates a new database with the default cluster configuration.<p>
*
* The database will be constructed with the default cluster configuration. The default cluster configuration
* searches for two resources on the classpath - {@code cluster} and {cluster-defaults} - in that order. Configuration
* options specified in {@code cluster.conf} will override those in {cluster-defaults.conf}.<p>
*
* Additionally, the database will be constructed with an database configuration that searches the classpath for
* three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
* {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
* as the map resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
* configurations will be loaded according to namespaces as well; for example, `databases.conf`.
*
* @param name The database name.
* @return The database.
*/
static Database create(String name) {
return create(name, new ClusterConfig(), new DatabaseConfig());
}
/**
* Creates a new database.<p>
*
* The database will be constructed with an database configuration that searches the classpath for
* three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
* {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
* as the database resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
* configurations will be loaded according to namespaces as well; for example, `databases.conf`.
*
* @param name The database name.
* @param cluster The cluster configuration.
* @return The database.
*/
static Database create(String name, ClusterConfig cluster) {
return create(name, cluster, new DatabaseConfig());
}
/**
* Creates a new database.
*
* @param name The database name.
* @param cluster The cluster configuration.
* @param config The database configuration.
* @return The database.
*/
static Database create(String name, ClusterConfig cluster, DatabaseConfig config) {
ClusterCoordinator coordinator =
new DefaultClusterCoordinator(new CoordinatorConfig().withName(name).withClusterConfig(cluster));
return coordinator.<Database>getResource(name, config.resolve(cluster))
.addStartupTask(() -> coordinator.open().thenApply(v -> null))
.addShutdownTask(coordinator::close);
}
}