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