blob: 8ed76700db5a3f65b1c341cfac1848e0eb181367 [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
19import net.kuujo.copycat.resource.internal.ResourceContext;
20import net.kuujo.copycat.state.StateMachine;
21import net.kuujo.copycat.resource.internal.AbstractResource;
22import net.kuujo.copycat.state.internal.DefaultStateMachine;
23import net.kuujo.copycat.util.concurrent.Futures;
24
25import java.util.Collection;
Madan Jampani94c23532015-02-05 17:40:01 -080026import java.util.Map;
27import java.util.Set;
28import java.util.concurrent.CompletableFuture;
29import java.util.function.Supplier;
30
Madan Jampanibff6d8f2015-03-31 16:53:47 -070031import org.onosproject.store.service.Transaction;
Madan Jampani393e0f02015-02-12 07:35:39 +053032import org.onosproject.store.service.Versioned;
33
Madan Jampani94c23532015-02-05 17:40:01 -080034/**
35 * Default database.
36 */
37public class DefaultDatabase extends AbstractResource<Database> implements Database {
Madan Jampanif1b8e172015-03-23 11:42:02 -070038 private final StateMachine<DatabaseState<String, byte[]>> stateMachine;
39 private DatabaseProxy<String, byte[]> proxy;
Madan Jampani94c23532015-02-05 17:40:01 -080040
Madan Jampanibff6d8f2015-03-31 16:53:47 -070041 @SuppressWarnings({ "unchecked", "rawtypes" })
Madan Jampanif1b8e172015-03-23 11:42:02 -070042 public DefaultDatabase(ResourceContext context) {
43 super(context);
44 this.stateMachine = new DefaultStateMachine(context, DatabaseState.class, DefaultDatabaseState.class);
Madan Jampani94c23532015-02-05 17:40:01 -080045 }
Madan Jampani94c23532015-02-05 17:40:01 -080046
Madan Jampanif1b8e172015-03-23 11:42:02 -070047 /**
48 * If the database is closed, returning a failed CompletableFuture. Otherwise, calls the given supplier to
49 * return the completed future result.
50 *
51 * @param supplier The supplier to call if the database is open.
52 * @param <T> The future result type.
53 * @return A completable future that if this database is closed is immediately failed.
54 */
55 protected <T> CompletableFuture<T> checkOpen(Supplier<CompletableFuture<T>> supplier) {
56 if (proxy == null) {
57 return Futures.exceptionalFuture(new IllegalStateException("Database closed"));
58 }
59 return supplier.get();
60 }
Madan Jampani94c23532015-02-05 17:40:01 -080061
Madan Jampanif1b8e172015-03-23 11:42:02 -070062 @Override
Madan Jampania89f8f92015-04-01 14:39:54 -070063 public CompletableFuture<Set<String>> tableNames() {
64 return checkOpen(() -> proxy.tableNames());
65 }
66
67 @Override
Madan Jampanif1b8e172015-03-23 11:42:02 -070068 public CompletableFuture<Integer> size(String tableName) {
69 return checkOpen(() -> proxy.size(tableName));
70 }
Madan Jampani94c23532015-02-05 17:40:01 -080071
Madan Jampanif1b8e172015-03-23 11:42:02 -070072 @Override
73 public CompletableFuture<Boolean> isEmpty(String tableName) {
74 return checkOpen(() -> proxy.isEmpty(tableName));
75 }
Madan Jampani94c23532015-02-05 17:40:01 -080076
Madan Jampanif1b8e172015-03-23 11:42:02 -070077 @Override
78 public CompletableFuture<Boolean> containsKey(String tableName, String key) {
79 return checkOpen(() -> proxy.containsKey(tableName, key));
80 }
Madan Jampani94c23532015-02-05 17:40:01 -080081
Madan Jampanif1b8e172015-03-23 11:42:02 -070082 @Override
83 public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) {
84 return checkOpen(() -> proxy.containsValue(tableName, value));
85 }
Madan Jampani94c23532015-02-05 17:40:01 -080086
Madan Jampanif1b8e172015-03-23 11:42:02 -070087 @Override
88 public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) {
89 return checkOpen(() -> proxy.get(tableName, key));
90 }
Madan Jampani94c23532015-02-05 17:40:01 -080091
Madan Jampanif1b8e172015-03-23 11:42:02 -070092 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -070093 public CompletableFuture<Result<Versioned<byte[]>>> put(String tableName, String key, byte[] value) {
Madan Jampanif1b8e172015-03-23 11:42:02 -070094 return checkOpen(() -> proxy.put(tableName, key, value));
95 }
Madan Jampani94c23532015-02-05 17:40:01 -080096
Madan Jampanif1b8e172015-03-23 11:42:02 -070097 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -070098 public CompletableFuture<Result<Versioned<byte[]>>> remove(String tableName, String key) {
Madan Jampanif1b8e172015-03-23 11:42:02 -070099 return checkOpen(() -> proxy.remove(tableName, key));
100 }
Madan Jampani94c23532015-02-05 17:40:01 -0800101
Madan Jampanif1b8e172015-03-23 11:42:02 -0700102 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700103 public CompletableFuture<Result<Void>> clear(String tableName) {
Madan Jampanif1b8e172015-03-23 11:42:02 -0700104 return checkOpen(() -> proxy.clear(tableName));
105 }
Madan Jampani94c23532015-02-05 17:40:01 -0800106
Madan Jampanif1b8e172015-03-23 11:42:02 -0700107 @Override
108 public CompletableFuture<Set<String>> keySet(String tableName) {
109 return checkOpen(() -> proxy.keySet(tableName));
110 }
Madan Jampani94c23532015-02-05 17:40:01 -0800111
Madan Jampanif1b8e172015-03-23 11:42:02 -0700112 @Override
113 public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) {
114 return checkOpen(() -> proxy.values(tableName));
115 }
Madan Jampani94c23532015-02-05 17:40:01 -0800116
Madan Jampanif1b8e172015-03-23 11:42:02 -0700117 @Override
118 public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet(String tableName) {
119 return checkOpen(() -> proxy.entrySet(tableName));
120 }
Madan Jampani94c23532015-02-05 17:40:01 -0800121
Madan Jampanif1b8e172015-03-23 11:42:02 -0700122 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700123 public CompletableFuture<Result<Versioned<byte[]>>> putIfAbsent(String tableName, String key, byte[] value) {
Madan Jampanif1b8e172015-03-23 11:42:02 -0700124 return checkOpen(() -> proxy.putIfAbsent(tableName, key, value));
125 }
Madan Jampani94c23532015-02-05 17:40:01 -0800126
Madan Jampanif1b8e172015-03-23 11:42:02 -0700127 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700128 public CompletableFuture<Result<Boolean>> remove(String tableName, String key, byte[] value) {
Madan Jampanif1b8e172015-03-23 11:42:02 -0700129 return checkOpen(() -> proxy.remove(tableName, key, value));
130 }
Madan Jampani94c23532015-02-05 17:40:01 -0800131
Madan Jampanif1b8e172015-03-23 11:42:02 -0700132 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700133 public CompletableFuture<Result<Boolean>> remove(String tableName, String key, long version) {
Madan Jampanif1b8e172015-03-23 11:42:02 -0700134 return checkOpen(() -> proxy.remove(tableName, key, version));
135 }
Madan Jampani94c23532015-02-05 17:40:01 -0800136
Madan Jampanif1b8e172015-03-23 11:42:02 -0700137 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700138 public CompletableFuture<Result<Boolean>> replace(String tableName, String key, byte[] oldValue, byte[] newValue) {
Madan Jampanif1b8e172015-03-23 11:42:02 -0700139 return checkOpen(() -> proxy.replace(tableName, key, oldValue, newValue));
140 }
Madan Jampani94c23532015-02-05 17:40:01 -0800141
Madan Jampanif1b8e172015-03-23 11:42:02 -0700142 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700143 public CompletableFuture<Result<Boolean>> replace(String tableName, String key, long oldVersion, byte[] newValue) {
Madan Jampanif1b8e172015-03-23 11:42:02 -0700144 return checkOpen(() -> proxy.replace(tableName, key, oldVersion, newValue));
145 }
Madan Jampani94c23532015-02-05 17:40:01 -0800146
Madan Jampanif1b8e172015-03-23 11:42:02 -0700147 @Override
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700148 public CompletableFuture<Boolean> prepareAndCommit(Transaction transaction) {
149 return checkOpen(() -> proxy.prepareAndCommit(transaction));
150 }
151
152 @Override
153 public CompletableFuture<Boolean> prepare(Transaction transaction) {
154 return checkOpen(() -> proxy.prepare(transaction));
155 }
156
157 @Override
158 public CompletableFuture<Boolean> commit(Transaction transaction) {
159 return checkOpen(() -> proxy.commit(transaction));
160 }
161
162 @Override
163 public CompletableFuture<Boolean> rollback(Transaction transaction) {
164 return checkOpen(() -> proxy.rollback(transaction));
Madan Jampanif1b8e172015-03-23 11:42:02 -0700165 }
Madan Jampani94c23532015-02-05 17:40:01 -0800166
Madan Jampanif1b8e172015-03-23 11:42:02 -0700167 @Override
168 @SuppressWarnings("unchecked")
169 public synchronized CompletableFuture<Database> open() {
170 return runStartupTasks()
171 .thenCompose(v -> stateMachine.open())
172 .thenRun(() -> {
173 this.proxy = stateMachine.createProxy(DatabaseProxy.class, this.getClass().getClassLoader());
174 })
175 .thenApply(v -> null);
176 }
177
178 @Override
179 public synchronized CompletableFuture<Void> close() {
180 proxy = null;
181 return stateMachine.close()
182 .thenCompose(v -> runShutdownTasks());
183 }
184
185 @Override
186 public int hashCode() {
187 return name().hashCode();
188 }
189
190 @Override
191 public boolean equals(Object other) {
192 if (other instanceof Database) {
193 return name().equals(((Database) other).name());
194 }
195 return false;
196 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700197}