Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -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.Collection; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.ExecutorService; |
| 25 | import java.util.concurrent.ScheduledExecutorService; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import java.util.function.BiFunction; |
| 28 | |
| 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.cluster.NodeId; |
| 31 | import org.onosproject.store.Timestamp; |
| 32 | |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 33 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 34 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Testing version of an Eventually Consistent Map. |
| 38 | */ |
| 39 | |
| 40 | public final class TestEventuallyConsistentMap<K, V> extends EventuallyConsistentMapAdapter<K, V> { |
| 41 | |
| 42 | private final HashMap<K, V> map; |
| 43 | private final String mapName; |
| 44 | private final List<EventuallyConsistentMapListener<K, V>> listeners; |
| 45 | private final BiFunction<K, V, Collection<NodeId>> peerUpdateFunction; |
| 46 | |
| 47 | private TestEventuallyConsistentMap(String mapName, |
| 48 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) { |
| 49 | map = new HashMap<>(); |
| 50 | listeners = new LinkedList<>(); |
| 51 | this.mapName = mapName; |
| 52 | this.peerUpdateFunction = peerUpdateFunction; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Notify all listeners of an event. |
| 57 | */ |
| 58 | private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) { |
| 59 | listeners.forEach( |
| 60 | listener -> listener.event(event) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int size() { |
| 66 | return map.size(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean isEmpty() { |
| 71 | return map.isEmpty(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean containsKey(K key) { |
| 76 | return map.containsKey(key); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean containsValue(V value) { |
| 81 | return map.containsValue(value); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public V get(K key) { |
| 86 | return map.get(key); |
| 87 | } |
| 88 | |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 89 | @SuppressWarnings("ReturnValueIgnored") |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void put(K key, V value) { |
| 92 | map.put(key, value); |
| 93 | EventuallyConsistentMapEvent<K, V> addEvent = |
| 94 | new EventuallyConsistentMapEvent<>(mapName, PUT, key, value); |
| 95 | notifyListeners(addEvent); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 96 | if (peerUpdateFunction != null) { |
| 97 | peerUpdateFunction.apply(key, value); |
| 98 | } |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public V remove(K key) { |
| 103 | V result = map.remove(key); |
| 104 | if (result != null) { |
| 105 | EventuallyConsistentMapEvent<K, V> removeEvent = |
| 106 | new EventuallyConsistentMapEvent<>(mapName, REMOVE, |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 107 | key, result); |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 108 | notifyListeners(removeEvent); |
| 109 | } |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void remove(K key, V value) { |
| 115 | boolean removed = map.remove(key, value); |
| 116 | if (removed) { |
| 117 | EventuallyConsistentMapEvent<K, V> removeEvent = |
| 118 | new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, value); |
| 119 | notifyListeners(removeEvent); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public V compute(K key, BiFunction<K, V, V> recomputeFunction) { |
| 125 | return map.compute(key, recomputeFunction); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void putAll(Map<? extends K, ? extends V> m) { |
| 130 | map.putAll(m); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void clear() { |
| 135 | map.clear(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Set<K> keySet() { |
| 140 | return map.keySet(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Collection<V> values() { |
| 145 | return map.values(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Set<Map.Entry<K, V>> entrySet() { |
| 150 | return map.entrySet(); |
| 151 | } |
| 152 | |
| 153 | public static <K, V> Builder<K, V> builder() { |
| 154 | return new Builder<>(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void addListener(EventuallyConsistentMapListener<K, V> listener) { |
| 159 | listeners.add(listener); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void removeListener(EventuallyConsistentMapListener<K, V> listener) { |
| 164 | listeners.remove(listener); |
| 165 | } |
| 166 | |
| 167 | public static class Builder<K, V> implements EventuallyConsistentMapBuilder<K, V> { |
| 168 | private String name; |
| 169 | private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction; |
| 170 | |
| 171 | @Override |
| 172 | public EventuallyConsistentMapBuilder<K, V> withName(String name) { |
| 173 | this.name = name; |
| 174 | return this; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace.Builder serializerBuilder) { |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | @Override |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 183 | public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) { |
| 184 | return this; |
| 185 | } |
| 186 | |
| 187 | @Override |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 188 | public EventuallyConsistentMapBuilder<K, V> |
| 189 | withTimestampProvider(BiFunction<K, V, Timestamp> timestampProvider) { |
| 190 | return this; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) { |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(ExecutorService executor) { |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) { |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public EventuallyConsistentMapBuilder<K, V> |
| 210 | withPeerUpdateFunction(BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) { |
| 211 | this.peerUpdateFunction = peerUpdateFunction; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() { |
| 217 | return this; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) { |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() { |
| 227 | return this; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public EventuallyConsistentMapBuilder<K, V> withPersistence() { |
| 232 | return this; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public EventuallyConsistentMap<K, V> build() { |
| 237 | if (name == null) { |
| 238 | name = "test"; |
| 239 | } |
| 240 | return new TestEventuallyConsistentMap<>(name, peerUpdateFunction); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |