alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.service; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 18 | import java.util.Map; |
| 19 | |
Madan Jampani | 37c2e70 | 2014-11-04 18:11:10 -0800 | [diff] [blame] | 20 | /** |
| 21 | * Service interface for a strongly consistent and durable |
| 22 | * key value data store. |
| 23 | */ |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 24 | public interface DatabaseService { |
| 25 | |
| 26 | /** |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 27 | * Reads the specified key. |
| 28 | * @param tableName name of the table associated with this operation. |
Thomas Vachuska | 2292567 | 2014-11-11 17:57:53 -0800 | [diff] [blame] | 29 | * @param key key to read. |
| 30 | * @return value (and version) associated with this key. This calls returns null if the key does not exist. |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 31 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 32 | VersionedValue get(String tableName, String key); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 33 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 34 | /** |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 35 | * Reads the whole table. |
| 36 | * |
| 37 | * @param tableName name of the table associated with this operation. |
| 38 | * @return the whole table |
| 39 | */ |
| 40 | Map<String, VersionedValue> getAll(String tableName); |
| 41 | |
| 42 | /** |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 43 | * Associate the key with a value. |
| 44 | * @param tableName table name in which this key/value resides. |
| 45 | * @param key key with which the specified value is to be associated |
| 46 | * @param value value to be associated with the specified key |
| 47 | * @return the previous value associated with the specified key, or null if there was no mapping for the key. |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 48 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 49 | VersionedValue put(String tableName, String key, byte[] value); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 50 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 51 | /** |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 52 | * If the specified key is not already associated with a value, associate it with the given value. |
| 53 | * @param tableName table name in which this key/value resides. |
| 54 | * @param key key with which the specified value is to be associated |
| 55 | * @param value value to be associated with the specified key |
| 56 | * @return true if put was successful, false if there is already a value associated with this key |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 57 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 58 | boolean putIfAbsent(String tableName, String key, byte[] value); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 59 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 60 | /** |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 61 | * Sets the key to the specified value if the version in the database (for that key) |
| 62 | * matches the specified version. |
| 63 | * @param tableName name of table associated with this operation. |
| 64 | * @param key key |
| 65 | * @param value value |
| 66 | * @param version version that should present in the database for the put to be successful. |
| 67 | * @return true if put was successful, false if there version in database is different from what is specified. |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 68 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 69 | boolean putIfVersionMatches(String tableName, String key, byte[] value, long version); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 70 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 71 | /** |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 72 | * Replaces the entry for a key only if currently mapped to a given value. |
| 73 | * @param tableName name of table associated with this operation. |
| 74 | * @param key with which the specified value is associated |
| 75 | * @param oldValue value expected to be associated with the specified key |
| 76 | * @param newValue value to be associated with the specified key |
| 77 | * @return true if put was successful, false if there version in database is different from what is specified. |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 78 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 79 | boolean putIfValueMatches(String tableName, String key, byte[] oldValue, byte[] newValue); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 80 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Removes the key (and associated value). |
| 83 | * @param tableName name of table associated with this operation. |
| 84 | * @param key key to remove |
| 85 | * @return value previously associated with the key. This call returns null if the key does not exist. |
| 86 | */ |
| 87 | VersionedValue remove(String tableName, String key); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 88 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Removes the key (and associated value) if the version in the database matches specified version. |
| 91 | * @param tableName name of table associated with this operation. |
| 92 | * @param key key to remove |
| 93 | * @param version version that should present in the database for the remove to be successful. |
| 94 | * @return true if remove was successful, false if there version in database is different from what is specified. |
| 95 | */ |
| 96 | boolean removeIfVersionMatches(String tableName, String key, long version); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 97 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Removes the key (and associated value) if the value in the database matches specified value. |
| 100 | * @param tableName name of table associated with this operation. |
| 101 | * @param key key to remove |
| 102 | * @param value value that should present in the database for the remove to be successful. |
| 103 | * @return true if remove was successful, false if there value in database is different from what is specified. |
| 104 | */ |
| 105 | boolean removeIfValueMatches(String tableName, String key, byte[] value); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 106 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 107 | /** |
| 108 | * Performs a batch read operation and returns the results. |
| 109 | * @param batchRequest batch request. |
| 110 | * @return result of the batch operation. |
| 111 | */ |
| 112 | BatchReadResult batchRead(BatchReadRequest batchRequest); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 113 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Performs a batch write operation and returns the results. |
| 116 | * This method provides transactional semantics. Either all writes succeed or none do. |
| 117 | * Even a single write failure would cause the entire batch to be aborted. |
| 118 | * In the case of unsuccessful operation, the batch result can be inspected to determine |
| 119 | * which operation(s) caused the batch to fail. |
| 120 | * @param batchRequest batch request. |
| 121 | * @return result of the batch operation. |
| 122 | */ |
| 123 | BatchWriteResult batchWrite(BatchWriteRequest batchRequest); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 124 | } |