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 | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 17 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 18 | |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 19 | import org.onlab.util.Match; |
Aaron Kruglikov | 82fd632 | 2015-10-06 12:02:46 -0700 | [diff] [blame] | 20 | import org.onosproject.store.service.Versioned; |
| 21 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 22 | import java.util.Collection; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.CompletableFuture; |
| 26 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Database proxy. |
| 29 | */ |
| 30 | public interface DatabaseProxy<K, V> { |
| 31 | |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 32 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 33 | * Returns a set of all map names. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 34 | * |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 35 | * @return A completable future to be completed with the result once complete. |
| 36 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 37 | CompletableFuture<Set<String>> maps(); |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 38 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Returns a mapping from counter name to next value. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 41 | * |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 42 | * @return A completable future to be completed with the result once complete. |
| 43 | */ |
| 44 | CompletableFuture<Map<String, Long>> counters(); |
| 45 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 46 | /** |
Madan Jampani | 3ca9cb6 | 2015-07-21 11:35:44 -0700 | [diff] [blame] | 47 | * Returns the number of entries in map. |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 48 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 49 | * @param mapName map name |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 50 | * @return A completable future to be completed with the result once complete. |
| 51 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 52 | CompletableFuture<Integer> mapSize(String mapName); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 53 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 54 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 55 | * Checks whether the map is empty. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 56 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 57 | * @param mapName map name |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 58 | * @return A completable future to be completed with the result once complete. |
| 59 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 60 | CompletableFuture<Boolean> mapIsEmpty(String mapName); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 61 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 62 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 63 | * Checks whether the map contains a key. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 64 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 65 | * @param mapName map name |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 66 | * @param key key to check. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 67 | * @return A completable future to be completed with the result once complete. |
| 68 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 69 | CompletableFuture<Boolean> mapContainsKey(String mapName, K key); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 70 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 71 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 72 | * Checks whether the map contains a value. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 73 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 74 | * @param mapName map name |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 75 | * @param value The value to check. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 76 | * @return A completable future to be completed with the result once complete. |
| 77 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 78 | CompletableFuture<Boolean> mapContainsValue(String mapName, V value); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 79 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 80 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 81 | * Gets a value from the map. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 82 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 83 | * @param mapName map name |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 84 | * @param key The key to get. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 85 | * @return A completable future to be completed with the result once complete. |
| 86 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 87 | CompletableFuture<Versioned<V>> mapGet(String mapName, K key); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 88 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 89 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 90 | * Updates the map. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 91 | * |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 92 | * @param mapName map name |
| 93 | * @param key The key to set |
| 94 | * @param valueMatch match for checking existing value |
| 95 | * @param versionMatch match for checking existing version |
| 96 | * @param value new value |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 97 | * @return A completable future to be completed with the result once complete |
| 98 | */ |
| 99 | CompletableFuture<Result<UpdateResult<K, V>>> mapUpdate( |
| 100 | String mapName, K key, Match<V> valueMatch, Match<Long> versionMatch, V value); |
| 101 | |
| 102 | /** |
| 103 | * Clears the map. |
| 104 | * |
| 105 | * @param mapName map name |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 106 | * @return A completable future to be completed with the result once complete. |
| 107 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 108 | CompletableFuture<Result<Void>> mapClear(String mapName); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 109 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 110 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 111 | * Gets a set of keys in the map. |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 112 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 113 | * @param mapName map name |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 114 | * @return A completable future to be completed with the result once complete. |
| 115 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 116 | CompletableFuture<Set<K>> mapKeySet(String mapName); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 119 | * Gets a collection of values in the map. |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 120 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 121 | * @param mapName map name |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 122 | * @return A completable future to be completed with the result once complete. |
| 123 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 124 | CompletableFuture<Collection<Versioned<V>>> mapValues(String mapName); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 127 | * Gets a set of entries in the map. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 128 | * |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 129 | * @param mapName map name |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 130 | * @return A completable future to be completed with the result once complete. |
| 131 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 132 | CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> mapEntrySet(String mapName); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 133 | |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 134 | /** |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 135 | * Atomically add the given value to current value of the specified counter. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 136 | * |
| 137 | * @param counterName counter name |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 138 | * @param delta value to add |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 139 | * @return updated value |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 140 | */ |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 141 | CompletableFuture<Long> counterAddAndGet(String counterName, long delta); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 142 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 143 | /** |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 144 | * Atomically add the given value to current value of the specified counter. |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 145 | * |
| 146 | * @param counterName counter name |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 147 | * @param delta value to add |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 148 | * @return previous value |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 149 | */ |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 150 | CompletableFuture<Long> counterGetAndAdd(String counterName, long delta); |
| 151 | |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Atomically sets the given value to current value of the specified counter. |
| 155 | * |
| 156 | * @param counterName counter name |
| 157 | * @param value value to set |
| 158 | * @return void future |
| 159 | */ |
| 160 | CompletableFuture<Void> counterSet(String counterName, long value); |
| 161 | |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 162 | /** |
Aaron Kruglikov | 82fd632 | 2015-10-06 12:02:46 -0700 | [diff] [blame] | 163 | * Atomically sets the given counter to the specified update value if and only if the current value is equal to the |
| 164 | * expected value. |
| 165 | * @param counterName counter name |
| 166 | * @param expectedValue value to use for equivalence check |
| 167 | * @param update value to set if expected value is current value |
| 168 | * @return true if an update occurred, false otherwise |
| 169 | */ |
| 170 | CompletableFuture<Boolean> counterCompareAndSet(String counterName, long expectedValue, long update); |
| 171 | |
| 172 | /** |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 173 | * Returns the current value of the specified atomic counter. |
| 174 | * |
| 175 | * @param counterName counter name |
| 176 | * @return current value |
| 177 | */ |
| 178 | CompletableFuture<Long> counterGet(String counterName); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 179 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 180 | /** |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 181 | * Returns the size of queue. |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 182 | * |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 183 | * @param queueName queue name |
| 184 | * @return queue size |
| 185 | */ |
| 186 | CompletableFuture<Long> queueSize(String queueName); |
| 187 | |
| 188 | /** |
| 189 | * Inserts an entry into the queue. |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 190 | * |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 191 | * @param queueName queue name |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 192 | * @param entry queue entry |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 193 | * @return void future |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 194 | */ |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 195 | CompletableFuture<Void> queuePush(String queueName, byte[] entry); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Removes an entry from the queue if the queue is non-empty. |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 199 | * |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 200 | * @param queueName queue name |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 201 | * @return entry future. Can be completed with null if queue is empty |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 202 | */ |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 203 | CompletableFuture<byte[]> queuePop(String queueName); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Returns but does not remove an entry from the queue. |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 207 | * |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 208 | * @param queueName queue name |
| 209 | * @return entry. Can be null if queue is empty |
| 210 | */ |
| 211 | CompletableFuture<byte[]> queuePeek(String queueName); |
| 212 | |
| 213 | /** |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 214 | * Prepare and commit the specified transaction. |
| 215 | * |
| 216 | * @param transaction transaction to commit (after preparation) |
| 217 | * @return A completable future to be completed with the result once complete |
| 218 | */ |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 219 | CompletableFuture<CommitResponse> prepareAndCommit(Transaction transaction); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 220 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Prepare the specified transaction for commit. A successful prepare implies |
| 223 | * all the affected resources are locked thus ensuring no concurrent updates can interfere. |
| 224 | * |
| 225 | * @param transaction transaction to prepare (for commit) |
| 226 | * @return A completable future to be completed with the result once complete. The future is completed |
| 227 | * with true if the transaction is successfully prepared i.e. all pre-conditions are met and |
| 228 | * applicable resources locked. |
| 229 | */ |
| 230 | CompletableFuture<Boolean> prepare(Transaction transaction); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 231 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Commit the specified transaction. A successful commit implies |
| 234 | * all the updates are applied, are now durable and are now visible externally. |
| 235 | * |
| 236 | * @param transaction transaction to commit |
| 237 | * @return A completable future to be completed with the result once complete |
| 238 | */ |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 239 | CompletableFuture<CommitResponse> commit(Transaction transaction); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 240 | |
Sho SHIMIZU | d91c4b0 | 2015-04-27 20:07:23 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Rollback the specified transaction. A successful rollback implies |
| 243 | * all previously acquired locks for the affected resources are released. |
| 244 | * |
| 245 | * @param transaction transaction to rollback |
| 246 | * @return A completable future to be completed with the result once complete |
| 247 | */ |
| 248 | CompletableFuture<Boolean> rollback(Transaction transaction); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 249 | } |