blob: 91cfa4b7c9e5431129cbac965d81c57862f9f150 [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 Jampani94c23532015-02-05 17:40:01 -080017package org.onosproject.store.consistent.impl;
18
19
20import net.kuujo.copycat.cluster.ClusterConfig;
21import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
22import net.kuujo.copycat.cluster.internal.coordinator.CoordinatorConfig;
23import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
24import net.kuujo.copycat.resource.Resource;
25
26/**
27 * Database.
28 */
29public interface Database extends DatabaseProxy<String, byte[]>, Resource<Database> {
30
31 /**
32 * Creates a new database with the default cluster configuration.<p>
33 *
34 * The database will be constructed with the default cluster configuration. The default cluster configuration
35 * searches for two resources on the classpath - {@code cluster} and {cluster-defaults} - in that order. Configuration
36 * options specified in {@code cluster.conf} will override those in {cluster-defaults.conf}.<p>
37 *
38 * Additionally, the database will be constructed with an database configuration that searches the classpath for
39 * three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
40 * {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
41 * as the map resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
42 * configurations will be loaded according to namespaces as well; for example, `databases.conf`.
43 *
44 * @param name The database name.
45 * @return The database.
46 */
47 static Database create(String name) {
48 return create(name, new ClusterConfig(), new DatabaseConfig());
49 }
50
51 /**
52 * Creates a new database.<p>
53 *
54 * The database will be constructed with an database configuration that searches the classpath for
55 * three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
56 * {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
57 * as the database resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
58 * configurations will be loaded according to namespaces as well; for example, `databases.conf`.
59 *
60 * @param name The database name.
61 * @param cluster The cluster configuration.
62 * @return The database.
63 */
64 static Database create(String name, ClusterConfig cluster) {
65 return create(name, cluster, new DatabaseConfig());
66 }
67
68 /**
69 * Creates a new database.
70 *
71 * @param name The database name.
72 * @param cluster The cluster configuration.
73 * @param config The database configuration.
74
75 * @return The database.
76 */
77 static Database create(String name, ClusterConfig cluster, DatabaseConfig config) {
78 ClusterCoordinator coordinator =
79 new DefaultClusterCoordinator(new CoordinatorConfig().withName(name).withClusterConfig(cluster));
80 return coordinator.<Database>getResource(name, config.resolve(cluster))
81 .addStartupTask(() -> coordinator.open().thenApply(v -> null))
82 .addShutdownTask(coordinator::close);
83 }
Madan Jampani94c23532015-02-05 17:40:01 -080084}