blob: 815b14218e1b2821b213702dbe05873f564e582b [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 Jampanibff6d8f2015-03-31 16:53:47 -070024import org.onosproject.store.service.Transaction;
Madan Jampani393e0f02015-02-12 07:35:39 +053025import org.onosproject.store.service.Versioned;
26
Madan Jampani94c23532015-02-05 17:40:01 -080027/**
28 * Database proxy.
29 */
30public interface DatabaseProxy<K, V> {
31
Madan Jampania89f8f92015-04-01 14:39:54 -070032 /**
33 * Returns a set of all tables names.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070034 *
Madan Jampania89f8f92015-04-01 14:39:54 -070035 * @return A completable future to be completed with the result once complete.
36 */
37 CompletableFuture<Set<String>> tableNames();
38
Madan Jampanib5d72d52015-04-03 16:53:50 -070039 /**
40 * Returns a mapping from counter name to next value.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070041 *
Madan Jampanib5d72d52015-04-03 16:53:50 -070042 * @return A completable future to be completed with the result once complete.
43 */
44 CompletableFuture<Map<String, Long>> counters();
45
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070046 /**
47 * Gets the table size.
48 *
49 * @param tableName table name
50 * @return A completable future to be completed with the result once complete.
51 */
52 CompletableFuture<Integer> size(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -080053
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070054 /**
55 * Checks whether the table is empty.
56 *
57 * @param tableName table name
58 * @return A completable future to be completed with the result once complete.
59 */
60 CompletableFuture<Boolean> isEmpty(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -080061
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070062 /**
63 * Checks whether the table contains a key.
64 *
65 * @param tableName table name
66 * @param key The key to check.
67 * @return A completable future to be completed with the result once complete.
68 */
69 CompletableFuture<Boolean> containsKey(String tableName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -080070
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070071 /**
72 * Checks whether the table contains a value.
73 *
74 * @param tableName table name
75 * @param value The value to check.
76 * @return A completable future to be completed with the result once complete.
77 */
78 CompletableFuture<Boolean> containsValue(String tableName, V value);
Madan Jampani94c23532015-02-05 17:40:01 -080079
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070080 /**
81 * Gets a value from the table.
82 *
83 * @param tableName table name
84 * @param key The key to get.
85 * @return A completable future to be completed with the result once complete.
86 */
87 CompletableFuture<Versioned<V>> get(String tableName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -080088
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070089 /**
90 * Puts a value in the table.
91 *
92 * @param tableName table name
93 * @param key The key to set.
94 * @param value The value to set.
95 * @return A completable future to be completed with the result once complete.
96 */
97 CompletableFuture<Result<Versioned<V>>> put(String tableName, K key, V value);
Madan Jampani94c23532015-02-05 17:40:01 -080098
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -070099 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700100 * Puts a value in the table.
101 *
102 * @param tableName table name
103 * @param key The key to set.
104 * @param value The value to set.
105 * @return A completable future to be completed with the result once complete.
106 */
107 CompletableFuture<Result<UpdateResult<Versioned<V>>>> putAndGet(String tableName, K key, V value);
108
109 /**
110 * Puts a value in the table.
111 *
112 * @param tableName table name
113 * @param key The key to set.
114 * @param value The value to set.
115 * @return A completable future to be completed with the result once complete.
116 */
117 CompletableFuture<Result<UpdateResult<Versioned<V>>>> putIfAbsentAndGet(String tableName, K key, V value);
118
119 /**
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700120 * Removes a value from the table.
121 *
122 * @param tableName table name
123 * @param key The key to remove.
124 * @return A completable future to be completed with the result once complete.
125 */
126 CompletableFuture<Result<Versioned<V>>> remove(String tableName, K key);
Madan Jampani94c23532015-02-05 17:40:01 -0800127
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700128 /**
129 * Clears the table.
130 *
131 * @param tableName table name
132 * @return A completable future to be completed with the result once complete.
133 */
134 CompletableFuture<Result<Void>> clear(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800135
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700136 /**
137 * Gets a set of keys in the table.
138 *
139 * @param tableName table name
140 * @return A completable future to be completed with the result once complete.
141 */
142 CompletableFuture<Set<K>> keySet(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800143
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700144 /**
145 * Gets a collection of values in the table.
146 *
147 * @param tableName table name
148 * @return A completable future to be completed with the result once complete.
149 */
150 CompletableFuture<Collection<Versioned<V>>> values(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800151
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700152 /**
153 * Gets a set of entries in the table.
154 *
155 * @param tableName table name
156 * @return A completable future to be completed with the result once complete.
157 */
158 CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet(String tableName);
Madan Jampani94c23532015-02-05 17:40:01 -0800159
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700160 /**
161 * Puts a value in the table if the given key does not exist.
162 *
163 * @param tableName table name
164 * @param key The key to set.
165 * @param value The value to set if the given key does not exist.
166 * @return A completable future to be completed with the result once complete.
167 */
168 CompletableFuture<Result<Versioned<V>>> putIfAbsent(String tableName, K key, V value);
Madan Jampani94c23532015-02-05 17:40:01 -0800169
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700170 /**
171 * Removes a key and if the existing value for that key matches the specified value.
172 *
173 * @param tableName table name
174 * @param key The key to remove.
175 * @param value The value to remove.
176 * @return A completable future to be completed with the result once complete.
177 */
178 CompletableFuture<Result<Boolean>> remove(String tableName, K key, V value);
Madan Jampani94c23532015-02-05 17:40:01 -0800179
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700180 /**
181 * Removes a key and if the existing version for that key matches the specified version.
182 *
183 * @param tableName table name
184 * @param key The key to remove.
185 * @param version The expected version.
186 * @return A completable future to be completed with the result once complete.
187 */
188 CompletableFuture<Result<Boolean>> remove(String tableName, K key, long version);
Madan Jampani94c23532015-02-05 17:40:01 -0800189
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700190 /**
191 * Replaces the entry for the specified key only if currently mapped to the specified value.
192 *
193 * @param tableName table name
194 * @param key The key to replace.
195 * @param oldValue The value to replace.
196 * @param newValue The value with which to replace the given key and value.
197 * @return A completable future to be completed with the result once complete.
198 */
199 CompletableFuture<Result<Boolean>> replace(String tableName, K key, V oldValue, V newValue);
Madan Jampani94c23532015-02-05 17:40:01 -0800200
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700201 /**
202 * Replaces the entry for the specified key only if currently mapped to the specified version.
203 *
204 * @param tableName table name
205 * @param key The key to update
206 * @param oldVersion existing version in the map for this replace to succeed.
207 * @param newValue The value with which to replace the given key and version.
208 * @return A completable future to be completed with the result once complete.
209 */
210 CompletableFuture<Result<Boolean>> replace(String tableName, K key, long oldVersion, V newValue);
Madan Jampani94c23532015-02-05 17:40:01 -0800211
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700212 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700213 * Replaces the entry for the specified key only if currently mapped to the specified version.
214 *
215 * @param tableName table name
216 * @param key The key to update
217 * @param oldVersion existing version in the map for this replace to succeed.
218 * @param newValue The value with which to replace the given key and version.
219 * @return A completable future to be completed with the result once complete.
220 */
221 CompletableFuture<Result<UpdateResult<Versioned<V>>>> replaceAndGet(String tableName,
222 K key, long oldVersion,
223 V newValue);
224
225 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700226 * Atomically add the given value to current value of the specified counter.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700227 *
228 * @param counterName counter name
Madan Jampani04aeb452015-05-02 16:12:24 -0700229 * @param delta value to add
230 * @return updated value
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700231 */
Madan Jampani04aeb452015-05-02 16:12:24 -0700232 CompletableFuture<Long> counterAddAndGet(String counterName, long delta);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700233
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700234 /**
Madan Jampani04aeb452015-05-02 16:12:24 -0700235 * Atomically add the given value to current value of the specified counter.
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700236 *
237 * @param counterName counter name
Madan Jampani04aeb452015-05-02 16:12:24 -0700238 * @param delta value to add
239 * @return previous value
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700240 */
Madan Jampani04aeb452015-05-02 16:12:24 -0700241 CompletableFuture<Long> counterGetAndAdd(String counterName, long delta);
242
243 /**
244 * Returns the current value of the specified atomic counter.
245 *
246 * @param counterName counter name
247 * @return current value
248 */
249 CompletableFuture<Long> counterGet(String counterName);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700250
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700251 /**
252 * Prepare and commit the specified transaction.
253 *
254 * @param transaction transaction to commit (after preparation)
255 * @return A completable future to be completed with the result once complete
256 */
257 CompletableFuture<Boolean> prepareAndCommit(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700258
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700259 /**
260 * Prepare the specified transaction for commit. A successful prepare implies
261 * all the affected resources are locked thus ensuring no concurrent updates can interfere.
262 *
263 * @param transaction transaction to prepare (for commit)
264 * @return A completable future to be completed with the result once complete. The future is completed
265 * with true if the transaction is successfully prepared i.e. all pre-conditions are met and
266 * applicable resources locked.
267 */
268 CompletableFuture<Boolean> prepare(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700269
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700270 /**
271 * Commit the specified transaction. A successful commit implies
272 * all the updates are applied, are now durable and are now visible externally.
273 *
274 * @param transaction transaction to commit
275 * @return A completable future to be completed with the result once complete
276 */
277 CompletableFuture<Boolean> commit(Transaction transaction);
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700278
Sho SHIMIZUd91c4b02015-04-27 20:07:23 -0700279 /**
280 * Rollback the specified transaction. A successful rollback implies
281 * all previously acquired locks for the affected resources are released.
282 *
283 * @param transaction transaction to rollback
284 * @return A completable future to be completed with the result once complete
285 */
286 CompletableFuture<Boolean> rollback(Transaction transaction);
Madan Jampani94c23532015-02-05 17:40:01 -0800287}