Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 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 Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 17 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 18 | |
| 19 | |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 20 | import java.util.function.Consumer; |
| 21 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 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; |
| 27 | |
| 28 | /** |
| 29 | * Database. |
| 30 | */ |
| 31 | public 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 Yuta | 0f3aaff | 2015-09-22 14:58:12 -0700 | [diff] [blame] | 41 | * three configuration files - {@code name}, {@code database}, {@code database-defaults}, {@code resource}, and |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 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`. |
| 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 Yuta | 0f3aaff | 2015-09-22 14:58:12 -0700 | [diff] [blame] | 57 | * three configuration files - {@code name}, {@code database}, {@code database-defaults}, {@code resource}, and |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 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`. |
| 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 Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Tells whether the database supports change notifications. |
| 89 | * @return true if notifications are supported; false otherwise |
| 90 | */ |
Madan Jampani | 971b2eb | 2015-08-02 14:47:52 +0530 | [diff] [blame] | 91 | default boolean hasChangeNotificationSupport() { |
| 92 | return true; |
| 93 | } |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 94 | |
| 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 Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 106 | } |