blob: bacf9f692c9c7cc95ab7ca5afb4eac8faded21a4 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Madan Jampanif4c88502016-01-21 12:35:36 -080017package org.onosproject.store.primitives.impl;
Madan Jampani94c23532015-02-05 17:40:01 -080018
19
Madan Jampani648451f2015-07-21 22:09:05 -070020import java.util.function.Consumer;
21
Madan Jampani94c23532015-02-05 17:40:01 -080022import net.kuujo.copycat.cluster.ClusterConfig;
23import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
24import net.kuujo.copycat.cluster.internal.coordinator.CoordinatorConfig;
25import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
26import net.kuujo.copycat.resource.Resource;
27
28/**
29 * Database.
30 */
31public interface Database extends DatabaseProxy<String, byte[]>, Resource<Database> {
32
33 /**
34 * Creates a new database with the default cluster configuration.<p>
35 *
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>
39 *
40 * Additionally, the database will be constructed with an database configuration that searches the classpath for
HIGUCHI Yuta0f3aaff2015-09-22 14:58:12 -070041 * three configuration files - {@code name}, {@code database}, {@code database-defaults}, {@code resource}, and
Madan Jampani94c23532015-02-05 17:40:01 -080042 * {@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`.
45 *
46 * @param name The database name.
47 * @return The database.
48 */
49 static Database create(String name) {
50 return create(name, new ClusterConfig(), new DatabaseConfig());
51 }
52
53 /**
54 * Creates a new database.<p>
55 *
56 * The database will be constructed with an database configuration that searches the classpath for
HIGUCHI Yuta0f3aaff2015-09-22 14:58:12 -070057 * three configuration files - {@code name}, {@code database}, {@code database-defaults}, {@code resource}, and
Madan Jampani94c23532015-02-05 17:40:01 -080058 * {@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`.
61 *
62 * @param name The database name.
63 * @param cluster The cluster configuration.
64 * @return The database.
65 */
66 static Database create(String name, ClusterConfig cluster) {
67 return create(name, cluster, new DatabaseConfig());
68 }
69
70 /**
71 * Creates a new database.
72 *
73 * @param name The database name.
74 * @param cluster The cluster configuration.
75 * @param config The database configuration.
76
77 * @return The database.
78 */
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);
85 }
Madan Jampani648451f2015-07-21 22:09:05 -070086
87 /**
88 * Tells whether the database supports change notifications.
89 * @return true if notifications are supported; false otherwise
90 */
Madan Jampani971b2eb2015-08-02 14:47:52 +053091 default boolean hasChangeNotificationSupport() {
92 return true;
93 }
Madan Jampani648451f2015-07-21 22:09:05 -070094
95 /**
96 * Registers a new consumer of StateMachineUpdates.
97 * @param consumer consumer to register
98 */
99 void registerConsumer(Consumer<StateMachineUpdate> consumer);
100
101 /**
102 * Unregisters a consumer of StateMachineUpdates.
103 * @param consumer consumer to unregister
104 */
105 void unregisterConsumer(Consumer<StateMachineUpdate> consumer);
Madan Jampani7804c992015-07-20 13:20:19 -0700106}