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> {
CompletableFuture<Boolean> remove(K key, long version);
/**
* Replaces the entry for the specified key only if there is any value
* which associated with specified key.
*
* @param key key with which the specified value is associated
* @param value value expected to be associated with the specified key
* @return the previous value associated with the specified key or null
*/
CompletableFuture<Versioned<V>> replace(K key, V value);
/**
* Replaces the entry for the specified key only if currently mapped
* to the specified value.
*
......
......@@ -247,6 +247,16 @@ public interface ConsistentMap<K, V> {
boolean remove(K key, long version);
/**
* Replaces the entry for the specified key only if there is any value
* which associated with specified key.
*
* @param key key with which the specified value is associated
* @param value value expected to be associated with the specified key
* @return the previous value associated with the specified key or null
*/
Versioned<V> replace(K key, V value);
/**
* Replaces the entry for the specified key only if currently mapped
* to the specified value.
*
......
......@@ -123,6 +123,11 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> {
}
@Override
public Versioned<V> replace(K key, V value) {
return null;
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return false;
}
......
......@@ -198,6 +198,15 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
}
@Override
public Versioned<V> replace(K key, V value) {
Versioned<V> result = version(map.replace(key, value));
if (map.get(key).equals(value)) {
notifyListeners(mapName, UPDATE, key, result);
}
return result;
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
boolean replaced = map.replace(key, oldValue, newValue);
if (replaced) {
......
......@@ -392,6 +392,16 @@ public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V
}
@Override
public CompletableFuture<Versioned<V>> replace(K key, V value) {
checkNotNull(key, ERROR_NULL_KEY);
checkNotNull(value, ERROR_NULL_VALUE);
final MeteringAgent.Context timer = monitor.startTimer(REPLACE);
return updateAndGet(key, Match.ifNotNull(), Match.any(), value)
.whenComplete((r, e) -> timer.stop(e))
.thenApply(v -> v.oldValue());
}
@Override
public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
checkNotNull(key, ERROR_NULL_KEY);
checkNotNull(oldValue, ERROR_NULL_VALUE);
......
......@@ -156,6 +156,11 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
}
@Override
public Versioned<V> replace(K key, V value) {
return complete(asyncMap.replace(key, value));
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return complete(asyncMap.replace(key, oldValue, newValue));
}
......