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 | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
| 19 | import net.kuujo.copycat.resource.internal.ResourceContext; |
| 20 | import net.kuujo.copycat.state.StateMachine; |
| 21 | import net.kuujo.copycat.resource.internal.AbstractResource; |
| 22 | import net.kuujo.copycat.state.internal.DefaultStateMachine; |
| 23 | import net.kuujo.copycat.util.concurrent.Futures; |
| 24 | |
| 25 | import java.util.Collection; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | import java.util.concurrent.CompletableFuture; |
| 30 | import java.util.function.Supplier; |
| 31 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 32 | import org.onosproject.store.service.UpdateOperation; |
| 33 | import org.onosproject.store.service.Versioned; |
| 34 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Default database. |
| 37 | */ |
| 38 | public class DefaultDatabase extends AbstractResource<Database> implements Database { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 39 | private final StateMachine<DatabaseState<String, byte[]>> stateMachine; |
| 40 | private DatabaseProxy<String, byte[]> proxy; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 41 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 42 | @SuppressWarnings("unchecked") |
| 43 | public DefaultDatabase(ResourceContext context) { |
| 44 | super(context); |
| 45 | this.stateMachine = new DefaultStateMachine(context, DatabaseState.class, DefaultDatabaseState.class); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 46 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 47 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 48 | /** |
| 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 Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 62 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 63 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 64 | public CompletableFuture<Set<String>> tableNames() { |
| 65 | return checkOpen(() -> proxy.tableNames()); |
| 66 | } |
| 67 | |
| 68 | @Override |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 69 | public CompletableFuture<Integer> size(String tableName) { |
| 70 | return checkOpen(() -> proxy.size(tableName)); |
| 71 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 72 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public CompletableFuture<Boolean> isEmpty(String tableName) { |
| 75 | return checkOpen(() -> proxy.isEmpty(tableName)); |
| 76 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 77 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 78 | @Override |
| 79 | public CompletableFuture<Boolean> containsKey(String tableName, String key) { |
| 80 | return checkOpen(() -> proxy.containsKey(tableName, key)); |
| 81 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 82 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) { |
| 85 | return checkOpen(() -> proxy.containsValue(tableName, value)); |
| 86 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 87 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) { |
| 90 | return checkOpen(() -> proxy.get(tableName, key)); |
| 91 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 92 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public CompletableFuture<Versioned<byte[]>> put(String tableName, String key, byte[] value) { |
| 95 | return checkOpen(() -> proxy.put(tableName, key, value)); |
| 96 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 97 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public CompletableFuture<Versioned<byte[]>> remove(String tableName, String key) { |
| 100 | return checkOpen(() -> proxy.remove(tableName, key)); |
| 101 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 102 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public CompletableFuture<Void> clear(String tableName) { |
| 105 | return checkOpen(() -> proxy.clear(tableName)); |
| 106 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 107 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public CompletableFuture<Set<String>> keySet(String tableName) { |
| 110 | return checkOpen(() -> proxy.keySet(tableName)); |
| 111 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 112 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 113 | @Override |
| 114 | public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) { |
| 115 | return checkOpen(() -> proxy.values(tableName)); |
| 116 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 117 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet(String tableName) { |
| 120 | return checkOpen(() -> proxy.entrySet(tableName)); |
| 121 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 122 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String tableName, String key, byte[] value) { |
| 125 | return checkOpen(() -> proxy.putIfAbsent(tableName, key, value)); |
| 126 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 127 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public CompletableFuture<Boolean> remove(String tableName, String key, byte[] value) { |
| 130 | return checkOpen(() -> proxy.remove(tableName, key, value)); |
| 131 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 132 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public CompletableFuture<Boolean> remove(String tableName, String key, long version) { |
| 135 | return checkOpen(() -> proxy.remove(tableName, key, version)); |
| 136 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 137 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 138 | @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 Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 142 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 143 | @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 Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 147 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public CompletableFuture<Boolean> atomicBatchUpdate(List<UpdateOperation<String, byte[]>> updates) { |
| 150 | return checkOpen(() -> proxy.atomicBatchUpdate(updates)); |
| 151 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 152 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 153 | @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 | } |