Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 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 | package org.onosproject.store.service; |
| 17 | |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 18 | import org.onosproject.store.primitives.DefaultAtomicCounterMap; |
| 19 | |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 20 | import java.util.concurrent.CompletableFuture; |
| 21 | |
| 22 | /** |
| 23 | * An async atomic counter map dispenses monotonically increasing values associated with key. |
| 24 | */ |
Jihwan Kim | cf8f6b2 | 2016-11-12 13:07:04 +0900 | [diff] [blame] | 25 | public interface AsyncAtomicCounterMap<K> extends DistributedPrimitive { |
| 26 | |
| 27 | @Override |
| 28 | default DistributedPrimitive.Type primitiveType() { |
| 29 | return DistributedPrimitive.Type.COUNTER_MAP; |
| 30 | } |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Increments by one the value currently associated with key, and returns the new value. |
| 34 | * |
| 35 | * @param key key with which the specified value is to be associated |
Ray Milkey | d7909ca | 2016-03-04 08:46:13 -0800 | [diff] [blame] | 36 | * @return incremented value |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 37 | */ |
| 38 | CompletableFuture<Long> incrementAndGet(K key); |
| 39 | |
| 40 | /** |
| 41 | * Decrements by one the value currently associated with key, and returns the new value. |
| 42 | * |
| 43 | * @param key key with which the specified value is to be associated |
| 44 | * @return updated value |
| 45 | */ |
| 46 | CompletableFuture<Long> decrementAndGet(K key); |
| 47 | |
| 48 | /** |
| 49 | * Increments by one the value currently associated with key, and returns the old value. |
| 50 | * |
| 51 | * @param key key with which the specified value is to be associated |
| 52 | * @return previous value |
| 53 | */ |
| 54 | CompletableFuture<Long> getAndIncrement(K key); |
| 55 | |
| 56 | /** |
| 57 | * Decrements by one the value currently associated with key, and returns the old value. |
| 58 | * |
| 59 | * @param key key with which the specified value is to be associated |
| 60 | * @return previous value |
| 61 | */ |
| 62 | CompletableFuture<Long> getAndDecrement(K key); |
| 63 | |
| 64 | /** |
| 65 | * Adds delta to the value currently associated with key, and returns the new value. |
| 66 | * |
| 67 | * @param key key with which the specified value is to be associated |
| 68 | * @param delta the value to add |
| 69 | * @return updated value |
| 70 | */ |
| 71 | CompletableFuture<Long> addAndGet(K key, long delta); |
| 72 | |
| 73 | /** |
| 74 | * Adds delta to the value currently associated with key, and returns the old value. |
| 75 | * |
| 76 | * @param key key with which the specified value is to be associated |
| 77 | * @param delta the value to add |
| 78 | * @return previous value |
| 79 | */ |
| 80 | CompletableFuture<Long> getAndAdd(K key, long delta); |
| 81 | |
| 82 | /** |
| 83 | * Returns the value associated with key, or zero if there is no value associated with key. |
| 84 | * |
| 85 | * @param key key with which the specified value is to be associated |
| 86 | * @return current value |
| 87 | */ |
| 88 | CompletableFuture<Long> get(K key); |
| 89 | |
| 90 | /** |
| 91 | * Associates ewValue with key in this map, and returns the value previously |
| 92 | * associated with key, or zero if there was no such value. |
| 93 | * |
| 94 | * @param key key with which the specified value is to be associated |
| 95 | * @param newValue the value to put |
| 96 | * @return previous value or zero |
| 97 | */ |
| 98 | CompletableFuture<Long> put(K key, long newValue); |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * If key is not already associated with a value or if key is associated with |
| 103 | * zero, associate it with newValue. Returns the previous value associated with |
| 104 | * key, or zero if there was no mapping for key. |
| 105 | * |
| 106 | * @param key key with which the specified value is to be associated |
| 107 | * @param newValue the value to put |
| 108 | * @return previous value or zero |
| 109 | */ |
| 110 | CompletableFuture<Long> putIfAbsent(K key, long newValue); |
| 111 | |
| 112 | /** |
| 113 | * If (key, expectedOldValue) is currently in the map, this method replaces |
| 114 | * expectedOldValue with newValue and returns true; otherwise, this method return false. |
| 115 | * |
| 116 | * If expectedOldValue is zero, this method will succeed if (key, zero) |
| 117 | * is currently in the map, or if key is not in the map at all. |
| 118 | * |
| 119 | * @param key key with which the specified value is to be associated |
| 120 | * @param expectedOldValue the expected value |
| 121 | * @param newValue the value to replace |
| 122 | * @return true if the value was replaced, false otherwise |
| 123 | */ |
| 124 | CompletableFuture<Boolean> replace(K key, long expectedOldValue, long newValue); |
| 125 | |
| 126 | /** |
| 127 | * Removes and returns the value associated with key. If key is not |
| 128 | * in the map, this method has no effect and returns zero. |
| 129 | * |
| 130 | * @param key key with which the specified value is to be associated |
| 131 | * @return the previous value associated with the specified key or null |
| 132 | */ |
| 133 | CompletableFuture<Long> remove(K key); |
| 134 | |
| 135 | /** |
| 136 | * If (key, value) is currently in the map, this method removes it and returns |
| 137 | * true; otherwise, this method returns false. |
| 138 | * |
| 139 | * @param key key with which the specified value is to be associated |
| 140 | * @param value the value to remove |
| 141 | * @return true if the value was removed, false otherwise |
| 142 | */ |
| 143 | CompletableFuture<Boolean> remove(K key, long value); |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Returns the number of entries in the map. |
| 147 | * |
| 148 | * @return the number of entries in the map |
| 149 | */ |
| 150 | CompletableFuture<Integer> size(); |
| 151 | |
| 152 | /** |
| 153 | * Returns a boolean indicating whether the map is empty. |
| 154 | * |
| 155 | * @return true if the map is empty, false otherwise |
| 156 | */ |
| 157 | CompletableFuture<Boolean> isEmpty(); |
| 158 | |
| 159 | /** |
| 160 | * Removes all entries from the map. |
| 161 | * |
| 162 | * @return void |
| 163 | */ |
| 164 | CompletableFuture<Void> clear(); |
| 165 | |
| 166 | /** |
| 167 | * Returns a new {@link AtomicCounterMap} that is backed by this instance. |
| 168 | * |
| 169 | * @return new {@code AtomicCounterMap} instance |
| 170 | */ |
| 171 | default AtomicCounterMap<K> asAtomicCounterMap() { |
Jordan Halterman | 6440b09 | 2017-05-24 17:48:08 -0700 | [diff] [blame] | 172 | return asAtomicCounterMap(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS); |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns a new {@link AtomicCounterMap} that is backed by this instance. |
| 177 | * |
| 178 | * @param timeoutMillis timeout duration for the returned ConsistentMap operations |
| 179 | * @return new {@code AtomicCounterMap} instance |
| 180 | */ |
| 181 | default AtomicCounterMap<K> asAtomicCounterMap(long timeoutMillis) { |
| 182 | return new DefaultAtomicCounterMap<>(this, timeoutMillis); |
| 183 | } |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 184 | } |