blob: 55004839052b885baef7e7b1826bd95e5f5d9941 [file] [log] [blame]
Jihwan Kim1f536272015-12-18 03:10:09 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jihwan Kim1f536272015-12-18 03:10:09 +09003 *
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 */
16package org.onosproject.store.service;
17
Jordan Haltermanc955df72017-02-04 20:43:28 -080018import org.onosproject.store.primitives.DefaultAtomicCounterMap;
19
Jihwan Kim1f536272015-12-18 03:10:09 +090020import java.util.concurrent.CompletableFuture;
21
22/**
23 * An async atomic counter map dispenses monotonically increasing values associated with key.
24 */
Jihwan Kimcf8f6b22016-11-12 13:07:04 +090025public interface AsyncAtomicCounterMap<K> extends DistributedPrimitive {
26
27 @Override
28 default DistributedPrimitive.Type primitiveType() {
29 return DistributedPrimitive.Type.COUNTER_MAP;
30 }
Jihwan Kim1f536272015-12-18 03:10:09 +090031
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 Milkeyd7909ca2016-03-04 08:46:13 -080036 * @return incremented value
Jihwan Kim1f536272015-12-18 03:10:09 +090037 */
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 Haltermanc955df72017-02-04 20:43:28 -0800144
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 Halterman6440b092017-05-24 17:48:08 -0700172 return asAtomicCounterMap(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
Jordan Haltermanc955df72017-02-04 20:43:28 -0800173 }
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 Kim1f536272015-12-18 03:10:09 +0900184}