2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.service;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.LinkedList;
21 import java.util.List;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import java.util.function.BiFunction;
29 import org.onlab.util.KryoNamespace;
30 import org.onosproject.cluster.NodeId;
31 import org.onosproject.store.Timestamp;
33 import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.*;
36 * Testing version of an Eventually Consistent Map.
39 public final class TestEventuallyConsistentMap<K, V> extends EventuallyConsistentMapAdapter<K, V> {
41 private final HashMap<K, V> map;
42 private final String mapName;
43 private final List<EventuallyConsistentMapListener<K, V>> listeners;
44 private final BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
46 private TestEventuallyConsistentMap(String mapName,
47 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
48 map = new HashMap<>();
49 listeners = new LinkedList<>();
50 this.mapName = mapName;
51 this.peerUpdateFunction = peerUpdateFunction;
55 * Notify all listeners of an event.
57 private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) {
59 listener -> listener.event(event)
69 public boolean isEmpty() {
74 public boolean containsKey(K key) {
75 return map.containsKey(key);
79 public boolean containsValue(V value) {
80 return map.containsValue(value);
89 public void put(K key, V value) {
91 EventuallyConsistentMapEvent<K, V> addEvent =
92 new EventuallyConsistentMapEvent<>(mapName, PUT, key, value);
93 notifyListeners(addEvent);
94 if (peerUpdateFunction != null) {
95 peerUpdateFunction.apply(key, value);
100 public V remove(K key) {
101 V result = map.remove(key);
102 if (result != null) {
103 EventuallyConsistentMapEvent<K, V> removeEvent =
104 new EventuallyConsistentMapEvent<>(mapName, REMOVE,
106 notifyListeners(removeEvent);
112 public void remove(K key, V value) {
113 boolean removed = map.remove(key, value);
115 EventuallyConsistentMapEvent<K, V> removeEvent =
116 new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, value);
117 notifyListeners(removeEvent);
122 public V compute(K key, BiFunction<K, V, V> recomputeFunction) {
123 return map.compute(key, recomputeFunction);
127 public void putAll(Map<? extends K, ? extends V> m) {
132 public void clear() {
137 public Set<K> keySet() {
142 public Collection<V> values() {
147 public Set<Map.Entry<K, V>> entrySet() {
148 return map.entrySet();
151 public static <K, V> Builder<K, V> builder() {
152 return new Builder<>();
156 public void addListener(EventuallyConsistentMapListener<K, V> listener) {
157 listeners.add(listener);
161 public void removeListener(EventuallyConsistentMapListener<K, V> listener) {
162 listeners.remove(listener);
165 public static class Builder<K, V> implements EventuallyConsistentMapBuilder<K, V> {
167 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
170 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
176 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace.Builder serializerBuilder) {
181 public EventuallyConsistentMapBuilder<K, V>
182 withTimestampProvider(BiFunction<K, V, Timestamp> timestampProvider) {
187 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
192 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(ExecutorService executor) {
197 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
202 public EventuallyConsistentMapBuilder<K, V>
203 withPeerUpdateFunction(BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
204 this.peerUpdateFunction = peerUpdateFunction;
209 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
214 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
219 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
224 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
229 public EventuallyConsistentMap<K, V> build() {
233 return new TestEventuallyConsistentMap<>(name, peerUpdateFunction);