Jihwan Kim
Committed by Madan Jampani

[Falcon][ONOS-2696] support method "replace" in ConsistentMap and method "notNull" in Match

Change-Id: I7c7d4644f963e52297785c2f9a5f21eff8723e61
...@@ -245,6 +245,16 @@ public interface AsyncConsistentMap<K, V> { ...@@ -245,6 +245,16 @@ public interface AsyncConsistentMap<K, V> {
245 CompletableFuture<Boolean> remove(K key, long version); 245 CompletableFuture<Boolean> remove(K key, long version);
246 246
247 /** 247 /**
248 + * Replaces the entry for the specified key only if there is any value
249 + * which associated with specified key.
250 + *
251 + * @param key key with which the specified value is associated
252 + * @param value value expected to be associated with the specified key
253 + * @return the previous value associated with the specified key or null
254 + */
255 + CompletableFuture<Versioned<V>> replace(K key, V value);
256 +
257 + /**
248 * Replaces the entry for the specified key only if currently mapped 258 * Replaces the entry for the specified key only if currently mapped
249 * to the specified value. 259 * to the specified value.
250 * 260 *
......
...@@ -247,6 +247,16 @@ public interface ConsistentMap<K, V> { ...@@ -247,6 +247,16 @@ public interface ConsistentMap<K, V> {
247 boolean remove(K key, long version); 247 boolean remove(K key, long version);
248 248
249 /** 249 /**
250 + * Replaces the entry for the specified key only if there is any value
251 + * which associated with specified key.
252 + *
253 + * @param key key with which the specified value is associated
254 + * @param value value expected to be associated with the specified key
255 + * @return the previous value associated with the specified key or null
256 + */
257 + Versioned<V> replace(K key, V value);
258 +
259 + /**
250 * Replaces the entry for the specified key only if currently mapped 260 * Replaces the entry for the specified key only if currently mapped
251 * to the specified value. 261 * to the specified value.
252 * 262 *
......
...@@ -123,6 +123,11 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> { ...@@ -123,6 +123,11 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> {
123 } 123 }
124 124
125 @Override 125 @Override
126 + public Versioned<V> replace(K key, V value) {
127 + return null;
128 + }
129 +
130 + @Override
126 public boolean replace(K key, V oldValue, V newValue) { 131 public boolean replace(K key, V oldValue, V newValue) {
127 return false; 132 return false;
128 } 133 }
......
...@@ -198,6 +198,15 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> { ...@@ -198,6 +198,15 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
198 } 198 }
199 199
200 @Override 200 @Override
201 + public Versioned<V> replace(K key, V value) {
202 + Versioned<V> result = version(map.replace(key, value));
203 + if (map.get(key).equals(value)) {
204 + notifyListeners(mapName, UPDATE, key, result);
205 + }
206 + return result;
207 + }
208 +
209 + @Override
201 public boolean replace(K key, V oldValue, V newValue) { 210 public boolean replace(K key, V oldValue, V newValue) {
202 boolean replaced = map.replace(key, oldValue, newValue); 211 boolean replaced = map.replace(key, oldValue, newValue);
203 if (replaced) { 212 if (replaced) {
......
...@@ -392,6 +392,16 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V ...@@ -392,6 +392,16 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V
392 } 392 }
393 393
394 @Override 394 @Override
395 + public CompletableFuture<Versioned<V>> replace(K key, V value) {
396 + checkNotNull(key, ERROR_NULL_KEY);
397 + checkNotNull(value, ERROR_NULL_VALUE);
398 + final MeteringAgent.Context timer = monitor.startTimer(REPLACE);
399 + return updateAndGet(key, Match.ifNotNull(), Match.any(), value)
400 + .whenComplete((r, e) -> timer.stop(e))
401 + .thenApply(v -> v.oldValue());
402 + }
403 +
404 + @Override
395 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { 405 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
396 checkNotNull(key, ERROR_NULL_KEY); 406 checkNotNull(key, ERROR_NULL_KEY);
397 checkNotNull(oldValue, ERROR_NULL_VALUE); 407 checkNotNull(oldValue, ERROR_NULL_VALUE);
......
...@@ -156,6 +156,11 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> { ...@@ -156,6 +156,11 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
156 } 156 }
157 157
158 @Override 158 @Override
159 + public Versioned<V> replace(K key, V value) {
160 + return complete(asyncMap.replace(key, value));
161 + }
162 +
163 + @Override
159 public boolean replace(K key, V oldValue, V newValue) { 164 public boolean replace(K key, V oldValue, V newValue) {
160 return complete(asyncMap.replace(key, oldValue, newValue)); 165 return complete(asyncMap.replace(key, oldValue, newValue));
161 } 166 }
......