blob: 518b526b3c4f5624e5cb227ac7d02513037fb9ac [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 java.util.Collection;
Madan Jampani94c23532015-02-05 17:40:01 -080020import java.util.Map;
21import java.util.Set;
22import java.util.concurrent.CompletableFuture;
23
Madan Jampani63c659f2015-06-11 00:52:58 -070024import org.onosproject.cluster.NodeId;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070025import org.onosproject.store.service.Transaction;
Madan Jampani393e0f02015-02-12 07:35:39 +053026import org.onosproject.store.service.Versioned;
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 /**
34 * Returns a set of all tables 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 */
38 CompletableFuture<Set<String>> tableNames();
39
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 /**
48 * Gets the table size.
49 *
50 * @param tableName table name
51 * @return A completable future to be completed with the result once complete.
52 */
53 CompletableFuture<Integer> size(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -080054
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070055 /**
56 * Checks whether the table is empty.
57 *
58 * @param tableName table name
59 * @return A completable future to be completed with the result once complete.
60 */
61 CompletableFuture<Boolean> isEmpty(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -080062
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070063 /**
64 * Checks whether the table contains a key.
65 *
66 * @param tableName table name
67 * @param key The key to check.
68 * @return A completable future to be completed with the result once complete.
69 */
70 CompletableFuture<Boolean> containsKey(String tableName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -080071
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070072 /**
73 * Checks whether the table contains a value.
74 *
75 * @param tableName table name
76 * @param value The value to check.
77 * @return A completable future to be completed with the result once complete.
78 */
79 CompletableFuture<Boolean> containsValue(String tableName, V value);
Madan Jampani94c23532015-02-05 17:40:01 -080080
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070081 /**
82 * Gets a value from the table.
83 *
84 * @param tableName table name
85 * @param key The key to get.
86 * @return A completable future to be completed with the result once complete.
87 */
88 CompletableFuture<Versioned<V>> get(String tableName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -080089
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070090 /**
91 * Puts a value in the table.
92 *
93 * @param tableName table name
94 * @param key The key to set.
95 * @param value The value to set.
96 * @return A completable future to be completed with the result once complete.
97 */
98 CompletableFuture<Result<Versioned<V>>> put(String tableName, K key, V value);
Madan Jampani94c23532015-02-05 17:40:01 -080099
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700100 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700101 * Puts a value in the table.
102 *
103 * @param tableName table name
104 * @param key The key to set.
105 * @param value The value to set.
106 * @return A completable future to be completed with the result once complete.
107 */
108 CompletableFuture<Result<UpdateResult<Versioned<V>>>> putAndGet(String tableName, K key, V value);
109
110 /**
111 * Puts a value in the table.
112 *
113 * @param tableName table name
114 * @param key The key to set.
115 * @param value The value to set.
116 * @return A completable future to be completed with the result once complete.
117 */
118 CompletableFuture<Result<UpdateResult<Versioned<V>>>> putIfAbsentAndGet(String tableName, K key, V value);
119
120 /**
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700121 * Removes a value from the table.
122 *
123 * @param tableName table name
124 * @param key The key to remove.
125 * @return A completable future to be completed with the result once complete.
126 */
127 CompletableFuture<Result<Versioned<V>>> remove(String tableName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -0800128
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700129 /**
130 * Clears the table.
131 *
132 * @param tableName table name
133 * @return A completable future to be completed with the result once complete.
134 */
135 CompletableFuture<Result<Void>> clear(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800136
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700137 /**
138 * Gets a set of keys in the table.
139 *
140 * @param tableName table name
141 * @return A completable future to be completed with the result once complete.
142 */
143 CompletableFuture<Set<K>> keySet(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800144
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700145 /**
146 * Gets a collection of values in the table.
147 *
148 * @param tableName table name
149 * @return A completable future to be completed with the result once complete.
150 */
151 CompletableFuture<Collection<Versioned<V>>> values(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800152
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700153 /**
154 * Gets a set of entries in the table.
155 *
156 * @param tableName table name
157 * @return A completable future to be completed with the result once complete.
158 */
159 CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800160
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700161 /**
162 * Puts a value in the table if the given key does not exist.
163 *
164 * @param tableName table name
165 * @param key The key to set.
166 * @param value The value to set if the given key does not exist.
167 * @return A completable future to be completed with the result once complete.
168 */
169 CompletableFuture<Result<Versioned<V>>> putIfAbsent(String tableName, K key, V value);
Madan Jampani94c23532015-02-05 17:40:01 -0800170
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700171 /**
172 * Removes a key and if the existing value for that key matches the specified value.
173 *
174 * @param tableName table name
175 * @param key The key to remove.
176 * @param value The value to remove.
177 * @return A completable future to be completed with the result once complete.
178 */
179 CompletableFuture<Result<Boolean>> remove(String tableName, K key, V value);
Madan Jampani94c23532015-02-05 17:40:01 -0800180
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700181 /**
182 * Removes a key and if the existing version for that key matches the specified version.
183 *
184 * @param tableName table name
185 * @param key The key to remove.
186 * @param version The expected version.
187 * @return A completable future to be completed with the result once complete.
188 */
189 CompletableFuture<Result<Boolean>> remove(String tableName, K key, long version);
Madan Jampani94c23532015-02-05 17:40:01 -0800190
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700191 /**
192 * Replaces the entry for the specified key only if currently mapped to the specified value.
193 *
194 * @param tableName table name
195 * @param key The key to replace.
196 * @param oldValue The value to replace.
197 * @param newValue The value with which to replace the given key and value.
198 * @return A completable future to be completed with the result once complete.
199 */
200 CompletableFuture<Result<Boolean>> replace(String tableName, K key, V oldValue, V newValue);
Madan Jampani94c23532015-02-05 17:40:01 -0800201
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700202 /**
203 * Replaces the entry for the specified key only if currently mapped to the specified version.
204 *
205 * @param tableName table name
206 * @param key The key to update
207 * @param oldVersion existing version in the map for this replace to succeed.
208 * @param newValue The value with which to replace the given key and version.
209 * @return A completable future to be completed with the result once complete.
210 */
211 CompletableFuture<Result<Boolean>> replace(String tableName, K key, long oldVersion, V newValue);
Madan Jampani94c23532015-02-05 17:40:01 -0800212
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700213 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700214 * Replaces the entry for the specified key only if currently mapped to the specified version.
215 *
216 * @param tableName table name
217 * @param key The key to update
218 * @param oldVersion existing version in the map for this replace to succeed.
219 * @param newValue The value with which to replace the given key and version.
220 * @return A completable future to be completed with the result once complete.
221 */
222 CompletableFuture<Result<UpdateResult<Versioned<V>>>> replaceAndGet(String tableName,
223 K key, long oldVersion,
224 V newValue);
225
226 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700227 * Atomically add the given value to current value of the specified counter.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700228 *
229 * @param counterName counter name
Madan Jampani04aeb452015-05-02 16:12:24 -0700230 * @param delta value to add
231 * @return updated value
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700232 */
Madan Jampani04aeb452015-05-02 16:12:24 -0700233 CompletableFuture<Long> counterAddAndGet(String counterName, long delta);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700234
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700235 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700236 * Atomically add the given value to current value of the specified counter.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700237 *
238 * @param counterName counter name
Madan Jampani04aeb452015-05-02 16:12:24 -0700239 * @param delta value to add
240 * @return previous value
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700241 */
Madan Jampani04aeb452015-05-02 16:12:24 -0700242 CompletableFuture<Long> counterGetAndAdd(String counterName, long delta);
243
244 /**
245 * Returns the current value of the specified atomic counter.
246 *
247 * @param counterName counter name
248 * @return current value
249 */
250 CompletableFuture<Long> counterGet(String counterName);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700251
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700252 /**
Madan Jampani63c659f2015-06-11 00:52:58 -0700253 * Returns the size of queue.
254 * @param queueName queue name
255 * @return queue size
256 */
257 CompletableFuture<Long> queueSize(String queueName);
258
259 /**
260 * Inserts an entry into the queue.
261 * @param queueName queue name
262 * @param entry queue entry
263 * @return set of nodes to notify about the queue update
264 */
265 CompletableFuture<Set<NodeId>> queuePush(String queueName, byte[] entry);
266
267 /**
268 * Removes an entry from the queue if the queue is non-empty.
269 * @param queueName queue name
270 * @param nodeId If the queue is empty the identifier of node to notify when an entry becomes available
271 * @return entry. Can be null if queue is empty
272 */
273 CompletableFuture<byte[]> queuePop(String queueName, NodeId nodeId);
274
275 /**
276 * Returns but does not remove an entry from the queue.
277 * @param queueName queue name
278 * @return entry. Can be null if queue is empty
279 */
280 CompletableFuture<byte[]> queuePeek(String queueName);
281
282 /**
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700283 * Prepare and commit the specified transaction.
284 *
285 * @param transaction transaction to commit (after preparation)
286 * @return A completable future to be completed with the result once complete
287 */
288 CompletableFuture<Boolean> prepareAndCommit(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700289
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700290 /**
291 * Prepare the specified transaction for commit. A successful prepare implies
292 * all the affected resources are locked thus ensuring no concurrent updates can interfere.
293 *
294 * @param transaction transaction to prepare (for commit)
295 * @return A completable future to be completed with the result once complete. The future is completed
296 * with true if the transaction is successfully prepared i.e. all pre-conditions are met and
297 * applicable resources locked.
298 */
299 CompletableFuture<Boolean> prepare(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700300
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700301 /**
302 * Commit the specified transaction. A successful commit implies
303 * all the updates are applied, are now durable and are now visible externally.
304 *
305 * @param transaction transaction to commit
306 * @return A completable future to be completed with the result once complete
307 */
308 CompletableFuture<Boolean> commit(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700309
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700310 /**
311 * Rollback the specified transaction. A successful rollback implies
312 * all previously acquired locks for the affected resources are released.
313 *
314 * @param transaction transaction to rollback
315 * @return A completable future to be completed with the result once complete
316 */
317 CompletableFuture<Boolean> rollback(Transaction transaction);
Madan Jampani94c23532015-02-05 17:40:01 -0800318}