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 { |
| 39 | private final StateMachine<DatabaseState<String, byte[]>> stateMachine; |
| 40 | private DatabaseProxy<String, byte[]> proxy; |
| 41 | |
| 42 | @SuppressWarnings("unchecked") |
| 43 | public DefaultDatabase(ResourceContext context) { |
| 44 | super(context); |
| 45 | this.stateMachine = new DefaultStateMachine(context, DatabaseState.class, DefaultDatabaseState.class); |
| 46 | } |
| 47 | |
| 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 | } |
| 62 | |
| 63 | @Override |
| 64 | public CompletableFuture<Integer> size(String tableName) { |
| 65 | return checkOpen(() -> proxy.size(tableName)); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public CompletableFuture<Boolean> isEmpty(String tableName) { |
| 70 | return checkOpen(() -> proxy.isEmpty(tableName)); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public CompletableFuture<Boolean> containsKey(String tableName, String key) { |
| 75 | return checkOpen(() -> proxy.containsKey(tableName, key)); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) { |
| 80 | return checkOpen(() -> proxy.containsValue(tableName, value)); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) { |
| 85 | return checkOpen(() -> proxy.get(tableName, key)); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public CompletableFuture<Versioned<byte[]>> put(String tableName, String key, byte[] value) { |
| 90 | return checkOpen(() -> proxy.put(tableName, key, value)); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public CompletableFuture<Versioned<byte[]>> remove(String tableName, String key) { |
| 95 | return checkOpen(() -> proxy.remove(tableName, key)); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<Void> clear(String tableName) { |
| 100 | return checkOpen(() -> proxy.clear(tableName)); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Set<String>> keySet(String tableName) { |
| 105 | return checkOpen(() -> proxy.keySet(tableName)); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) { |
| 110 | return checkOpen(() -> proxy.values(tableName)); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet(String tableName) { |
| 115 | return checkOpen(() -> proxy.entrySet(tableName)); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String tableName, String key, byte[] value) { |
| 120 | return checkOpen(() -> proxy.putIfAbsent(tableName, key, value)); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public CompletableFuture<Boolean> remove(String tableName, String key, byte[] value) { |
| 125 | return checkOpen(() -> proxy.remove(tableName, key, value)); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public CompletableFuture<Boolean> remove(String tableName, String key, long version) { |
| 130 | return checkOpen(() -> proxy.remove(tableName, key, version)); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Boolean> replace(String tableName, String key, byte[] oldValue, byte[] newValue) { |
| 135 | return checkOpen(() -> proxy.replace(tableName, key, oldValue, newValue)); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public CompletableFuture<Boolean> replace(String tableName, String key, long oldVersion, byte[] newValue) { |
| 140 | return checkOpen(() -> proxy.replace(tableName, key, oldVersion, newValue)); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<Boolean> atomicBatchUpdate(List<UpdateOperation<String, byte[]>> updates) { |
| 145 | return checkOpen(() -> proxy.atomicBatchUpdate(updates)); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | @SuppressWarnings("unchecked") |
| 150 | public synchronized CompletableFuture<Database> open() { |
| 151 | return runStartupTasks() |
| 152 | .thenCompose(v -> stateMachine.open()) |
| 153 | .thenRun(() -> { |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 154 | this.proxy = stateMachine.createProxy(DatabaseProxy.class, this.getClass().getClassLoader()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 155 | }) |
| 156 | .thenApply(v -> null); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public synchronized CompletableFuture<Void> close() { |
| 161 | proxy = null; |
| 162 | return stateMachine.close() |
| 163 | .thenCompose(v -> runShutdownTasks()); |
| 164 | } |
| 165 | } |