Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -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 | 6f44033 | 2015-07-31 10:40:53 -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; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 23 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 24 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 25 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicReference; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 27 | import java.util.function.BiFunction; |
| 28 | import java.util.function.Function; |
| 29 | import java.util.function.Predicate; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 32 | import org.onosproject.store.primitives.ConsistentMapBackedJavaMap; |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 33 | |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 34 | import com.google.common.base.Objects; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Test implementation of the consistent map. |
| 38 | */ |
| 39 | public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> { |
| 40 | |
| 41 | private final List<MapEventListener<K, V>> listeners; |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 42 | private final Map<K, Versioned<V>> map; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 43 | private final String mapName; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 44 | private final AtomicLong counter = new AtomicLong(0); |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 45 | private final Serializer serializer; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 46 | |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 47 | private TestConsistentMap(String mapName, Serializer serializer) { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 48 | map = new ConcurrentHashMap<>(); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 49 | listeners = new LinkedList<>(); |
| 50 | this.mapName = mapName; |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 51 | this.serializer = serializer; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private Versioned<V> version(V v) { |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 55 | return new Versioned<>(v, counter.incrementAndGet(), System.currentTimeMillis()); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Notify all listeners of an event. |
| 60 | */ |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 61 | private void notifyListeners(String mapName, |
| 62 | K key, Versioned<V> newvalue, Versioned<V> oldValue) { |
| 63 | MapEvent<K, V> event = new MapEvent<>(mapName, key, newvalue, oldValue); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 64 | listeners.forEach( |
| 65 | listener -> listener.event(event) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int size() { |
| 71 | return map.size(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean isEmpty() { |
| 76 | return map.isEmpty(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean containsKey(K key) { |
| 81 | return map.containsKey(key); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean containsValue(V value) { |
| 86 | return map.containsValue(value); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Versioned<V> get(K key) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 91 | return map.get(key); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Versioned<V> computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 96 | AtomicBoolean updated = new AtomicBoolean(false); |
| 97 | Versioned<V> result = map.compute(key, (k, v) -> { |
| 98 | if (v == null) { |
| 99 | updated.set(true); |
| 100 | return version(mappingFunction.apply(key)); |
| 101 | } |
| 102 | return v; |
| 103 | }); |
| 104 | if (updated.get()) { |
| 105 | notifyListeners(mapName, key, result, null); |
| 106 | } |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 107 | return result; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Versioned<V> compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 112 | AtomicBoolean updated = new AtomicBoolean(false); |
| 113 | AtomicReference<Versioned<V>> previousValue = new AtomicReference<>(); |
| 114 | Versioned<V> result = map.compute(key, (k, v) -> { |
| 115 | updated.set(true); |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 116 | previousValue.set(serializer.decode(serializer.encode(v))); |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 117 | return version(remappingFunction.apply(k, Versioned.valueOrNull(v))); |
| 118 | }); |
| 119 | if (updated.get()) { |
| 120 | notifyListeners(mapName, key, result, previousValue.get()); |
| 121 | } |
| 122 | return result; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Versioned<V> computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 127 | AtomicBoolean updated = new AtomicBoolean(false); |
| 128 | AtomicReference<Versioned<V>> previousValue = new AtomicReference<>(); |
| 129 | Versioned<V> result = map.compute(key, (k, v) -> { |
| 130 | if (v != null) { |
| 131 | updated.set(true); |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 132 | previousValue.set(serializer.decode(serializer.encode(v))); |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 133 | return version(remappingFunction.apply(k, v.value())); |
Brian O'Connor | b2a383f | 2015-12-07 20:05:14 -0800 | [diff] [blame] | 134 | } |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 135 | return v; |
| 136 | }); |
| 137 | if (updated.get()) { |
| 138 | notifyListeners(mapName, key, result, previousValue.get()); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 139 | } |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 140 | return result; |
| 141 | } |
| 142 | |
| 143 | @Override |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 144 | public Versioned<V> computeIf(K key, Predicate<? super V> condition, |
| 145 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 146 | AtomicBoolean updated = new AtomicBoolean(false); |
| 147 | AtomicReference<Versioned<V>> previousValue = new AtomicReference<>(); |
| 148 | Versioned<V> result = map.compute(key, (k, v) -> { |
| 149 | if (condition.test(Versioned.valueOrNull(v))) { |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 150 | previousValue.set(serializer.decode(serializer.encode(v))); |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 151 | updated.set(true); |
| 152 | return version(remappingFunction.apply(k, Versioned.valueOrNull(v))); |
| 153 | } |
| 154 | return v; |
| 155 | }); |
| 156 | if (updated.get()) { |
| 157 | notifyListeners(mapName, key, result, previousValue.get()); |
| 158 | } |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 159 | return result; |
| 160 | } |
| 161 | |
| 162 | @Override |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 163 | public Versioned<V> put(K key, V value) { |
| 164 | Versioned<V> newValue = version(value); |
| 165 | Versioned<V> previousValue = map.put(key, newValue); |
| 166 | notifyListeners(mapName, key, newValue, previousValue); |
| 167 | return previousValue; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Versioned<V> putAndGet(K key, V value) { |
| 172 | Versioned<V> newValue = version(value); |
| 173 | Versioned<V> previousValue = map.put(key, newValue); |
| 174 | notifyListeners(mapName, key, newValue, previousValue); |
| 175 | return newValue; |
| 176 | } |
| 177 | |
| 178 | @Override |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 179 | public Versioned<V> remove(K key) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 180 | Versioned<V> result = map.remove(key); |
| 181 | notifyListeners(mapName, key, null, result); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 182 | return result; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void clear() { |
Thiago Santos | 01c9338 | 2017-10-20 14:22:57 -0700 | [diff] [blame] | 187 | for (K key : map.keySet().stream().collect(Collectors.toList())) { |
| 188 | remove(key); |
| 189 | } |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Set<K> keySet() { |
| 194 | return map.keySet(); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Collection<Versioned<V>> values() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 199 | return map.values() |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 200 | .stream() |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 201 | .collect(Collectors.toList()); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Set<Map.Entry<K, Versioned<V>>> entrySet() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 206 | return map.entrySet(); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Versioned<V> putIfAbsent(K key, V value) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 211 | Versioned<V> newValue = version(value); |
| 212 | Versioned<V> result = map.putIfAbsent(key, newValue); |
| 213 | if (result == null) { |
| 214 | notifyListeners(mapName, key, newValue, result); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 215 | } |
| 216 | return result; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public boolean remove(K key, V value) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 221 | Versioned<V> existingValue = map.get(key); |
| 222 | if (Objects.equal(Versioned.valueOrNull(existingValue), value)) { |
| 223 | map.remove(key); |
| 224 | notifyListeners(mapName, key, null, existingValue); |
| 225 | return true; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 226 | } |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 227 | return false; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean remove(K key, long version) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 232 | Versioned<V> existingValue = map.get(key); |
| 233 | if (existingValue == null) { |
| 234 | return false; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 235 | } |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 236 | if (existingValue.version() == version) { |
| 237 | map.remove(key); |
| 238 | notifyListeners(mapName, key, null, existingValue); |
| 239 | return true; |
| 240 | } |
| 241 | return false; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 245 | public Versioned<V> replace(K key, V value) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 246 | Versioned<V> existingValue = map.get(key); |
| 247 | if (existingValue == null) { |
| 248 | return null; |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 249 | } |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 250 | Versioned<V> newValue = version(value); |
| 251 | Versioned<V> result = map.put(key, newValue); |
| 252 | notifyListeners(mapName, key, newValue, result); |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | |
| 256 | @Override |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 257 | public boolean replace(K key, V oldValue, V newValue) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 258 | Versioned<V> existingValue = map.get(key); |
| 259 | if (existingValue == null || !existingValue.value().equals(oldValue)) { |
| 260 | return false; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 261 | } |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 262 | Versioned<V> value = version(newValue); |
| 263 | Versioned<V> result = map.put(key, value); |
| 264 | notifyListeners(mapName, key, value, result); |
| 265 | return true; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public boolean replace(K key, long oldVersion, V newValue) { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 270 | Versioned<V> existingValue = map.get(key); |
| 271 | if (existingValue == null || existingValue.version() != oldVersion) { |
| 272 | return false; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 273 | } |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 274 | Versioned<V> value = version(newValue); |
| 275 | Versioned<V> result = map.put(key, value); |
| 276 | notifyListeners(mapName, key, value, result); |
| 277 | return true; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void addListener(MapEventListener<K, V> listener) { |
| 282 | listeners.add(listener); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public void removeListener(MapEventListener<K, V> listener) { |
| 287 | listeners.remove(listener); |
| 288 | } |
| 289 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 290 | @Override |
| 291 | public Map<K, V> asJavaMap() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 292 | return new ConsistentMapBackedJavaMap<>(this); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 295 | public static Builder builder() { |
| 296 | return new Builder(); |
| 297 | } |
| 298 | |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 299 | public static class Builder<K, V> extends ConsistentMapBuilder<K, V> { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 300 | |
| 301 | @Override |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 302 | public ConsistentMap<K, V> build() { |
Charles Chan | 593acf9 | 2017-11-22 13:55:41 -0800 | [diff] [blame] | 303 | return new TestConsistentMap<>(name(), serializer()); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public AsyncConsistentMap<K, V> buildAsyncMap() { |
| 308 | return null; |
| 309 | } |
| 310 | |
| 311 | } |
| 312 | |
| 313 | } |