blob: ab5f987d6e62522a1c68a798ddee9bb67d47f5fd [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
Madan Jampanif2f086c2016-01-13 16:15:39 -080019import org.onlab.util.Match;
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070020import org.onosproject.store.service.Transaction;
21import org.onosproject.store.service.Versioned;
22
Madan Jampani94c23532015-02-05 17:40:01 -080023import java.util.Collection;
Madan Jampani94c23532015-02-05 17:40:01 -080024import java.util.Map;
25import java.util.Set;
26import java.util.concurrent.CompletableFuture;
27
Madan Jampani94c23532015-02-05 17:40:01 -080028/**
29 * Database proxy.
30 */
31public interface DatabaseProxy<K, V> {
32
Madan Jampania89f8f92015-04-01 14:39:54 -070033 /**
Madan Jampani7804c992015-07-20 13:20:19 -070034 * Returns a set of all map names.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070035 *
Madan Jampania89f8f92015-04-01 14:39:54 -070036 * @return A completable future to be completed with the result once complete.
37 */
Madan Jampani7804c992015-07-20 13:20:19 -070038 CompletableFuture<Set<String>> maps();
Madan Jampania89f8f92015-04-01 14:39:54 -070039
Madan Jampanib5d72d52015-04-03 16:53:50 -070040 /**
41 * Returns a mapping from counter name to next value.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070042 *
Madan Jampanib5d72d52015-04-03 16:53:50 -070043 * @return A completable future to be completed with the result once complete.
44 */
45 CompletableFuture<Map<String, Long>> counters();
46
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070047 /**
Madan Jampani3ca9cb62015-07-21 11:35:44 -070048 * Returns the number of entries in map.
andreafd912ac2015-10-02 14:58:35 -070049 *
Madan Jampani7804c992015-07-20 13:20:19 -070050 * @param mapName map name
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070051 * @return A completable future to be completed with the result once complete.
52 */
Madan Jampani7804c992015-07-20 13:20:19 -070053 CompletableFuture<Integer> mapSize(String mapName);
Madan Jampani94c23532015-02-05 17:40:01 -080054
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070055 /**
Madan Jampani7804c992015-07-20 13:20:19 -070056 * Checks whether the map is empty.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070057 *
Madan Jampani7804c992015-07-20 13:20:19 -070058 * @param mapName map name
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070059 * @return A completable future to be completed with the result once complete.
60 */
Madan Jampani7804c992015-07-20 13:20:19 -070061 CompletableFuture<Boolean> mapIsEmpty(String mapName);
Madan Jampani94c23532015-02-05 17:40:01 -080062
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070063 /**
Madan Jampani7804c992015-07-20 13:20:19 -070064 * Checks whether the map contains a key.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070065 *
Madan Jampani7804c992015-07-20 13:20:19 -070066 * @param mapName map name
andreafd912ac2015-10-02 14:58:35 -070067 * @param key key to check.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070068 * @return A completable future to be completed with the result once complete.
69 */
Madan Jampani7804c992015-07-20 13:20:19 -070070 CompletableFuture<Boolean> mapContainsKey(String mapName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -080071
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070072 /**
Madan Jampani7804c992015-07-20 13:20:19 -070073 * Checks whether the map contains a value.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070074 *
Madan Jampani7804c992015-07-20 13:20:19 -070075 * @param mapName map name
andreafd912ac2015-10-02 14:58:35 -070076 * @param value The value to check.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070077 * @return A completable future to be completed with the result once complete.
78 */
Madan Jampani7804c992015-07-20 13:20:19 -070079 CompletableFuture<Boolean> mapContainsValue(String mapName, V value);
Madan Jampani94c23532015-02-05 17:40:01 -080080
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070081 /**
Madan Jampani7804c992015-07-20 13:20:19 -070082 * Gets a value from the map.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070083 *
Madan Jampani7804c992015-07-20 13:20:19 -070084 * @param mapName map name
andreafd912ac2015-10-02 14:58:35 -070085 * @param key The key to get.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070086 * @return A completable future to be completed with the result once complete.
87 */
Madan Jampani7804c992015-07-20 13:20:19 -070088 CompletableFuture<Versioned<V>> mapGet(String mapName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -080089
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070090 /**
Madan Jampani7804c992015-07-20 13:20:19 -070091 * Updates the map.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070092 *
andreafd912ac2015-10-02 14:58:35 -070093 * @param mapName map name
94 * @param key The key to set
95 * @param valueMatch match for checking existing value
96 * @param versionMatch match for checking existing version
97 * @param value new value
Madan Jampani7804c992015-07-20 13:20:19 -070098 * @return A completable future to be completed with the result once complete
99 */
100 CompletableFuture<Result<UpdateResult<K, V>>> mapUpdate(
101 String mapName, K key, Match<V> valueMatch, Match<Long> versionMatch, V value);
102
103 /**
104 * Clears the map.
105 *
106 * @param mapName map name
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700107 * @return A completable future to be completed with the result once complete.
108 */
Madan Jampani7804c992015-07-20 13:20:19 -0700109 CompletableFuture<Result<Void>> mapClear(String mapName);
Madan Jampani94c23532015-02-05 17:40:01 -0800110
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700111 /**
Madan Jampani7804c992015-07-20 13:20:19 -0700112 * Gets a set of keys in the map.
Madan Jampani346d4f52015-05-04 11:09:39 -0700113 *
Madan Jampani7804c992015-07-20 13:20:19 -0700114 * @param mapName map name
Madan Jampani346d4f52015-05-04 11:09:39 -0700115 * @return A completable future to be completed with the result once complete.
116 */
Madan Jampani7804c992015-07-20 13:20:19 -0700117 CompletableFuture<Set<K>> mapKeySet(String mapName);
Madan Jampani346d4f52015-05-04 11:09:39 -0700118
119 /**
Madan Jampani7804c992015-07-20 13:20:19 -0700120 * Gets a collection of values in the map.
Madan Jampani346d4f52015-05-04 11:09:39 -0700121 *
Madan Jampani7804c992015-07-20 13:20:19 -0700122 * @param mapName map name
Madan Jampani346d4f52015-05-04 11:09:39 -0700123 * @return A completable future to be completed with the result once complete.
124 */
Madan Jampani7804c992015-07-20 13:20:19 -0700125 CompletableFuture<Collection<Versioned<V>>> mapValues(String mapName);
Madan Jampani346d4f52015-05-04 11:09:39 -0700126
127 /**
Madan Jampani7804c992015-07-20 13:20:19 -0700128 * Gets a set of entries in the map.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700129 *
Madan Jampani7804c992015-07-20 13:20:19 -0700130 * @param mapName map name
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700131 * @return A completable future to be completed with the result once complete.
132 */
Madan Jampani7804c992015-07-20 13:20:19 -0700133 CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> mapEntrySet(String mapName);
Madan Jampani94c23532015-02-05 17:40:01 -0800134
andreafd912ac2015-10-02 14:58:35 -0700135 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700136 * Atomically add the given value to current value of the specified counter.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700137 *
138 * @param counterName counter name
andreafd912ac2015-10-02 14:58:35 -0700139 * @param delta value to add
Madan Jampani04aeb452015-05-02 16:12:24 -0700140 * @return updated value
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700141 */
Madan Jampani04aeb452015-05-02 16:12:24 -0700142 CompletableFuture<Long> counterAddAndGet(String counterName, long delta);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700143
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700144 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700145 * Atomically add the given value to current value of the specified counter.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700146 *
147 * @param counterName counter name
andreafd912ac2015-10-02 14:58:35 -0700148 * @param delta value to add
Madan Jampani04aeb452015-05-02 16:12:24 -0700149 * @return previous value
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700150 */
Madan Jampani04aeb452015-05-02 16:12:24 -0700151 CompletableFuture<Long> counterGetAndAdd(String counterName, long delta);
152
andreafd912ac2015-10-02 14:58:35 -0700153
154 /**
155 * Atomically sets the given value to current value of the specified counter.
156 *
157 * @param counterName counter name
158 * @param value value to set
159 * @return void future
160 */
161 CompletableFuture<Void> counterSet(String counterName, long value);
162
Madan Jampani04aeb452015-05-02 16:12:24 -0700163 /**
Aaron Kruglikov82fd6322015-10-06 12:02:46 -0700164 * Atomically sets the given counter to the specified update value if and only if the current value is equal to the
165 * expected value.
166 * @param counterName counter name
167 * @param expectedValue value to use for equivalence check
168 * @param update value to set if expected value is current value
169 * @return true if an update occurred, false otherwise
170 */
171 CompletableFuture<Boolean> counterCompareAndSet(String counterName, long expectedValue, long update);
172
173 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700174 * Returns the current value of the specified atomic counter.
175 *
176 * @param counterName counter name
177 * @return current value
178 */
179 CompletableFuture<Long> counterGet(String counterName);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700180
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700181 /**
Madan Jampani63c659f2015-06-11 00:52:58 -0700182 * Returns the size of queue.
andreafd912ac2015-10-02 14:58:35 -0700183 *
Madan Jampani63c659f2015-06-11 00:52:58 -0700184 * @param queueName queue name
185 * @return queue size
186 */
187 CompletableFuture<Long> queueSize(String queueName);
188
189 /**
190 * Inserts an entry into the queue.
andreafd912ac2015-10-02 14:58:35 -0700191 *
Madan Jampani63c659f2015-06-11 00:52:58 -0700192 * @param queueName queue name
andreafd912ac2015-10-02 14:58:35 -0700193 * @param entry queue entry
Madan Jampania6d787b2015-08-11 11:02:02 -0700194 * @return void future
Madan Jampani63c659f2015-06-11 00:52:58 -0700195 */
Madan Jampania6d787b2015-08-11 11:02:02 -0700196 CompletableFuture<Void> queuePush(String queueName, byte[] entry);
Madan Jampani63c659f2015-06-11 00:52:58 -0700197
198 /**
199 * Removes an entry from the queue if the queue is non-empty.
andreafd912ac2015-10-02 14:58:35 -0700200 *
Madan Jampani63c659f2015-06-11 00:52:58 -0700201 * @param queueName queue name
Madan Jampania6d787b2015-08-11 11:02:02 -0700202 * @return entry future. Can be completed with null if queue is empty
Madan Jampani63c659f2015-06-11 00:52:58 -0700203 */
Madan Jampania6d787b2015-08-11 11:02:02 -0700204 CompletableFuture<byte[]> queuePop(String queueName);
Madan Jampani63c659f2015-06-11 00:52:58 -0700205
206 /**
207 * Returns but does not remove an entry from the queue.
andreafd912ac2015-10-02 14:58:35 -0700208 *
Madan Jampani63c659f2015-06-11 00:52:58 -0700209 * @param queueName queue name
210 * @return entry. Can be null if queue is empty
211 */
212 CompletableFuture<byte[]> queuePeek(String queueName);
213
214 /**
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700215 * Prepare and commit the specified transaction.
216 *
217 * @param transaction transaction to commit (after preparation)
218 * @return A completable future to be completed with the result once complete
219 */
Madan Jampanibab51a42015-08-10 13:53:35 -0700220 CompletableFuture<CommitResponse> prepareAndCommit(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700221
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700222 /**
223 * Prepare the specified transaction for commit. A successful prepare implies
224 * all the affected resources are locked thus ensuring no concurrent updates can interfere.
225 *
226 * @param transaction transaction to prepare (for commit)
227 * @return A completable future to be completed with the result once complete. The future is completed
228 * with true if the transaction is successfully prepared i.e. all pre-conditions are met and
229 * applicable resources locked.
230 */
231 CompletableFuture<Boolean> prepare(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700232
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700233 /**
234 * Commit the specified transaction. A successful commit implies
235 * all the updates are applied, are now durable and are now visible externally.
236 *
237 * @param transaction transaction to commit
238 * @return A completable future to be completed with the result once complete
239 */
Madan Jampanibab51a42015-08-10 13:53:35 -0700240 CompletableFuture<CommitResponse> commit(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700241
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700242 /**
243 * Rollback the specified transaction. A successful rollback implies
244 * all previously acquired locks for the affected resources are released.
245 *
246 * @param transaction transaction to rollback
247 * @return A completable future to be completed with the result once complete
248 */
249 CompletableFuture<Boolean> rollback(Transaction transaction);
Madan Jampani94c23532015-02-05 17:40:01 -0800250}