Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [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 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 20 | import org.onosproject.store.primitives.DefaultAtomicCounter; |
| 21 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 22 | /** |
| 23 | * An async atomic counter dispenses monotonically increasing values. |
| 24 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 25 | public interface AsyncAtomicCounter extends DistributedPrimitive { |
| 26 | |
| 27 | @Override |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 28 | default DistributedPrimitive.Type primitiveType() { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 29 | return DistributedPrimitive.Type.COUNTER; |
| 30 | } |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Atomically increment by one the current value. |
| 34 | * |
| 35 | * @return updated value |
| 36 | */ |
| 37 | CompletableFuture<Long> incrementAndGet(); |
| 38 | |
| 39 | /** |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 40 | * Atomically increment by one the current value. |
| 41 | * |
| 42 | * @return previous value |
| 43 | */ |
| 44 | CompletableFuture<Long> getAndIncrement(); |
| 45 | |
| 46 | /** |
| 47 | * Atomically adds the given value to the current value. |
| 48 | * |
| 49 | * @param delta the value to add |
| 50 | * @return previous value |
| 51 | */ |
| 52 | CompletableFuture<Long> getAndAdd(long delta); |
| 53 | |
| 54 | /** |
| 55 | * Atomically adds the given value to the current value. |
| 56 | * |
| 57 | * @param delta the value to add |
| 58 | * @return updated value |
| 59 | */ |
| 60 | CompletableFuture<Long> addAndGet(long delta); |
| 61 | |
| 62 | /** |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 63 | * Returns the current value of the counter without modifying it. |
| 64 | * |
| 65 | * @return current value |
| 66 | */ |
| 67 | CompletableFuture<Long> get(); |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | /** |
| 71 | * Atomically sets the given value to the current value. |
| 72 | * |
Brian O'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 73 | * @param value new value |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 74 | * @return future void |
| 75 | */ |
| 76 | CompletableFuture<Void> set(long value); |
Aaron Kruglikov | 82fd632 | 2015-10-06 12:02:46 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Atomically sets the given counter to the updated value if the current value is the expected value, otherwise |
| 80 | * no change occurs. |
| 81 | * @param expectedValue the expected current value of the counter |
| 82 | * @param updateValue the new value to be set |
| 83 | * @return true if the update occurred and the expected value was equal to the current value, false otherwise |
| 84 | */ |
| 85 | CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue); |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Returns a new {@link AtomicCounter} that is backed by this instance. |
| 89 | * |
| 90 | * @param timeoutMillis timeout duration for the returned ConsistentMap operations |
| 91 | * @return new {@code ConsistentMap} instance |
| 92 | */ |
| 93 | default AtomicCounter asAtomicCounter(long timeoutMillis) { |
| 94 | return new DefaultAtomicCounter(this, timeoutMillis); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns a new {@link AtomicCounter} that is backed by this instance and with a default operation timeout. |
| 99 | * |
| 100 | * @return new {@code ConsistentMap} instance |
| 101 | */ |
| 102 | default AtomicCounter asAtomicCounter() { |
Jordan Halterman | 6440b09 | 2017-05-24 17:48:08 -0700 | [diff] [blame] | 103 | return new DefaultAtomicCounter(this, DEFAULT_OPERATION_TIMEOUT_MILLIS); |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 104 | } |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 105 | } |