2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onosproject.store.consistent.impl;
20 import java.util.function.Consumer;
22 import net.kuujo.copycat.cluster.ClusterConfig;
23 import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
24 import net.kuujo.copycat.cluster.internal.coordinator.CoordinatorConfig;
25 import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
26 import net.kuujo.copycat.resource.Resource;
31 public interface Database extends DatabaseProxy<String, byte[]>, Resource<Database> {
34 * Creates a new database with the default cluster configuration.<p>
36 * The database will be constructed with the default cluster configuration. The default cluster configuration
37 * searches for two resources on the classpath - {@code cluster} and {cluster-defaults} - in that order. Configuration
38 * options specified in {@code cluster.conf} will override those in {cluster-defaults.conf}.<p>
40 * Additionally, the database will be constructed with an database configuration that searches the classpath for
41 * three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
42 * {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
43 * as the map resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
44 * configurations will be loaded according to namespaces as well; for example, `databases.conf`.
46 * @param name The database name.
47 * @return The database.
49 static Database create(String name) {
50 return create(name, new ClusterConfig(), new DatabaseConfig());
54 * Creates a new database.<p>
56 * The database will be constructed with an database configuration that searches the classpath for
57 * three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
58 * {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
59 * as the database resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
60 * configurations will be loaded according to namespaces as well; for example, `databases.conf`.
62 * @param name The database name.
63 * @param cluster The cluster configuration.
64 * @return The database.
66 static Database create(String name, ClusterConfig cluster) {
67 return create(name, cluster, new DatabaseConfig());
71 * Creates a new database.
73 * @param name The database name.
74 * @param cluster The cluster configuration.
75 * @param config The database configuration.
77 * @return The database.
79 static Database create(String name, ClusterConfig cluster, DatabaseConfig config) {
80 ClusterCoordinator coordinator =
81 new DefaultClusterCoordinator(new CoordinatorConfig().withName(name).withClusterConfig(cluster));
82 return coordinator.<Database>getResource(name, config.resolve(cluster))
83 .addStartupTask(() -> coordinator.open().thenApply(v -> null))
84 .addShutdownTask(coordinator::close);
88 * Tells whether the database supports change notifications.
89 * @return true if notifications are supported; false otherwise
91 default boolean hasChangeNotificationSupport() {
96 * Registers a new consumer of StateMachineUpdates.
97 * @param consumer consumer to register
99 void registerConsumer(Consumer<StateMachineUpdate> consumer);
102 * Unregisters a consumer of StateMachineUpdates.
103 * @param consumer consumer to unregister
105 void unregisterConsumer(Consumer<StateMachineUpdate> consumer);