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