blob: c14e57cdb2ad05144e08d282610241d2980ee23c [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;
26import java.util.List;
27import java.util.Map;
28import java.util.Set;
29import java.util.concurrent.CompletableFuture;
30import java.util.function.Supplier;
31
Madan Jampani393e0f02015-02-12 07:35:39 +053032import org.onosproject.store.service.UpdateOperation;
33import org.onosproject.store.service.Versioned;
34
Madan Jampani94c23532015-02-05 17:40:01 -080035/**
36 * Default database.
37 */
38public class DefaultDatabase extends AbstractResource<Database> implements Database {
Madan Jampanif1b8e172015-03-23 11:42:02 -070039 private final StateMachine<DatabaseState<String, byte[]>> stateMachine;
40 private DatabaseProxy<String, byte[]> proxy;
Madan Jampani94c23532015-02-05 17:40:01 -080041
Madan Jampanif1b8e172015-03-23 11:42:02 -070042 @SuppressWarnings("unchecked")
43 public DefaultDatabase(ResourceContext context) {
44 super(context);
45 this.stateMachine = new DefaultStateMachine(context, DatabaseState.class, DefaultDatabaseState.class);
Madan Jampani94c23532015-02-05 17:40:01 -080046 }
Madan Jampani94c23532015-02-05 17:40:01 -080047
Madan Jampanif1b8e172015-03-23 11:42:02 -070048 /**
49 * If the database is closed, returning a failed CompletableFuture. Otherwise, calls the given supplier to
50 * return the completed future result.
51 *
52 * @param supplier The supplier to call if the database is open.
53 * @param <T> The future result type.
54 * @return A completable future that if this database is closed is immediately failed.
55 */
56 protected <T> CompletableFuture<T> checkOpen(Supplier<CompletableFuture<T>> supplier) {
57 if (proxy == null) {
58 return Futures.exceptionalFuture(new IllegalStateException("Database closed"));
59 }
60 return supplier.get();
61 }
Madan Jampani94c23532015-02-05 17:40:01 -080062
Madan Jampanif1b8e172015-03-23 11:42:02 -070063 @Override
Madan Jampania89f8f92015-04-01 14:39:54 -070064 public CompletableFuture<Set<String>> tableNames() {
65 return checkOpen(() -> proxy.tableNames());
66 }
67
68 @Override
Madan Jampanif1b8e172015-03-23 11:42:02 -070069 public CompletableFuture<Integer> size(String tableName) {
70 return checkOpen(() -> proxy.size(tableName));
71 }
Madan Jampani94c23532015-02-05 17:40:01 -080072
Madan Jampanif1b8e172015-03-23 11:42:02 -070073 @Override
74 public CompletableFuture<Boolean> isEmpty(String tableName) {
75 return checkOpen(() -> proxy.isEmpty(tableName));
76 }
Madan Jampani94c23532015-02-05 17:40:01 -080077
Madan Jampanif1b8e172015-03-23 11:42:02 -070078 @Override
79 public CompletableFuture<Boolean> containsKey(String tableName, String key) {
80 return checkOpen(() -> proxy.containsKey(tableName, key));
81 }
Madan Jampani94c23532015-02-05 17:40:01 -080082
Madan Jampanif1b8e172015-03-23 11:42:02 -070083 @Override
84 public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) {
85 return checkOpen(() -> proxy.containsValue(tableName, value));
86 }
Madan Jampani94c23532015-02-05 17:40:01 -080087
Madan Jampanif1b8e172015-03-23 11:42:02 -070088 @Override
89 public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) {
90 return checkOpen(() -> proxy.get(tableName, key));
91 }
Madan Jampani94c23532015-02-05 17:40:01 -080092
Madan Jampanif1b8e172015-03-23 11:42:02 -070093 @Override
94 public CompletableFuture<Versioned<byte[]>> put(String tableName, String key, byte[] value) {
95 return checkOpen(() -> proxy.put(tableName, key, value));
96 }
Madan Jampani94c23532015-02-05 17:40:01 -080097
Madan Jampanif1b8e172015-03-23 11:42:02 -070098 @Override
99 public CompletableFuture<Versioned<byte[]>> remove(String tableName, String key) {
100 return checkOpen(() -> proxy.remove(tableName, key));
101 }
Madan Jampani94c23532015-02-05 17:40:01 -0800102
Madan Jampanif1b8e172015-03-23 11:42:02 -0700103 @Override
104 public CompletableFuture<Void> clear(String tableName) {
105 return checkOpen(() -> proxy.clear(tableName));
106 }
Madan Jampani94c23532015-02-05 17:40:01 -0800107
Madan Jampanif1b8e172015-03-23 11:42:02 -0700108 @Override
109 public CompletableFuture<Set<String>> keySet(String tableName) {
110 return checkOpen(() -> proxy.keySet(tableName));
111 }
Madan Jampani94c23532015-02-05 17:40:01 -0800112
Madan Jampanif1b8e172015-03-23 11:42:02 -0700113 @Override
114 public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) {
115 return checkOpen(() -> proxy.values(tableName));
116 }
Madan Jampani94c23532015-02-05 17:40:01 -0800117
Madan Jampanif1b8e172015-03-23 11:42:02 -0700118 @Override
119 public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet(String tableName) {
120 return checkOpen(() -> proxy.entrySet(tableName));
121 }
Madan Jampani94c23532015-02-05 17:40:01 -0800122
Madan Jampanif1b8e172015-03-23 11:42:02 -0700123 @Override
124 public CompletableFuture<Versioned<byte[]>> putIfAbsent(String tableName, String key, byte[] value) {
125 return checkOpen(() -> proxy.putIfAbsent(tableName, key, value));
126 }
Madan Jampani94c23532015-02-05 17:40:01 -0800127
Madan Jampanif1b8e172015-03-23 11:42:02 -0700128 @Override
129 public CompletableFuture<Boolean> remove(String tableName, String key, byte[] value) {
130 return checkOpen(() -> proxy.remove(tableName, key, value));
131 }
Madan Jampani94c23532015-02-05 17:40:01 -0800132
Madan Jampanif1b8e172015-03-23 11:42:02 -0700133 @Override
134 public CompletableFuture<Boolean> remove(String tableName, String key, long version) {
135 return checkOpen(() -> proxy.remove(tableName, key, version));
136 }
Madan Jampani94c23532015-02-05 17:40:01 -0800137
Madan Jampanif1b8e172015-03-23 11:42:02 -0700138 @Override
139 public CompletableFuture<Boolean> replace(String tableName, String key, byte[] oldValue, byte[] newValue) {
140 return checkOpen(() -> proxy.replace(tableName, key, oldValue, newValue));
141 }
Madan Jampani94c23532015-02-05 17:40:01 -0800142
Madan Jampanif1b8e172015-03-23 11:42:02 -0700143 @Override
144 public CompletableFuture<Boolean> replace(String tableName, String key, long oldVersion, byte[] newValue) {
145 return checkOpen(() -> proxy.replace(tableName, key, oldVersion, newValue));
146 }
Madan Jampani94c23532015-02-05 17:40:01 -0800147
Madan Jampanif1b8e172015-03-23 11:42:02 -0700148 @Override
149 public CompletableFuture<Boolean> atomicBatchUpdate(List<UpdateOperation<String, byte[]>> updates) {
150 return checkOpen(() -> proxy.atomicBatchUpdate(updates));
151 }
Madan Jampani94c23532015-02-05 17:40:01 -0800152
Madan Jampanif1b8e172015-03-23 11:42:02 -0700153 @Override
154 @SuppressWarnings("unchecked")
155 public synchronized CompletableFuture<Database> open() {
156 return runStartupTasks()
157 .thenCompose(v -> stateMachine.open())
158 .thenRun(() -> {
159 this.proxy = stateMachine.createProxy(DatabaseProxy.class, this.getClass().getClassLoader());
160 })
161 .thenApply(v -> null);
162 }
163
164 @Override
165 public synchronized CompletableFuture<Void> close() {
166 proxy = null;
167 return stateMachine.close()
168 .thenCompose(v -> runShutdownTasks());
169 }
170
171 @Override
172 public int hashCode() {
173 return name().hashCode();
174 }
175
176 @Override
177 public boolean equals(Object other) {
178 if (other instanceof Database) {
179 return name().equals(((Database) other).name());
180 }
181 return false;
182 }
183}