blob: 6b641398b4051bac83277a1af8f0fb345a1a811b [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
Thomas Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
Jordan Haltermanbc982392018-08-07 15:02:37 -070018import java.time.Duration;
Jordan Halterman00e92da2018-05-22 23:05:52 -070019import java.util.Collection;
20import java.util.Map;
21import java.util.NavigableMap;
22import java.util.NavigableSet;
23import java.util.Set;
24import java.util.concurrent.CompletableFuture;
25import java.util.concurrent.Executor;
26import java.util.function.BiFunction;
27import java.util.function.Predicate;
28import java.util.stream.Collectors;
29
30import com.google.common.base.Throwables;
31import com.google.common.collect.Maps;
32import io.atomix.core.collection.impl.TranscodingAsyncDistributedCollection;
33import io.atomix.core.map.impl.DelegatingAsyncDistributedNavigableMap;
34import org.onosproject.store.primitives.MapUpdate;
35import org.onosproject.store.primitives.TransactionId;
36import org.onosproject.store.service.AsyncConsistentTreeMap;
37import org.onosproject.store.service.AsyncIterator;
38import org.onosproject.store.service.ConsistentMapException;
39import org.onosproject.store.service.MapEvent;
40import org.onosproject.store.service.MapEventListener;
41import org.onosproject.store.service.TransactionLog;
42import org.onosproject.store.service.Version;
43import org.onosproject.store.service.Versioned;
44
45/**
46 * Atomix consistent tree map.
47 */
48public class AtomixConsistentTreeMap<V> implements AsyncConsistentTreeMap<V> {
49 private final io.atomix.core.map.AsyncAtomicNavigableMap<String, V> atomixTreeMap;
50 private final Map<MapEventListener<String, V>, io.atomix.core.map.AtomicMapEventListener<String, V>> listenerMap =
51 Maps.newIdentityHashMap();
52
53 public AtomixConsistentTreeMap(io.atomix.core.map.AsyncAtomicNavigableMap<String, V> atomixTreeMap) {
54 this.atomixTreeMap = atomixTreeMap;
55 }
56
57 @Override
58 public String name() {
59 return atomixTreeMap.name();
60 }
61
62 @Override
63 public CompletableFuture<Integer> size() {
64 return atomixTreeMap.size();
65 }
66
67 @Override
68 public CompletableFuture<String> firstKey() {
69 return atomixTreeMap.firstKey();
70 }
71
72 @Override
73 public CompletableFuture<String> lastKey() {
74 return atomixTreeMap.lastKey();
75 }
76
77 @Override
78 public CompletableFuture<Map.Entry<String, Versioned<V>>> ceilingEntry(String key) {
79 return atomixTreeMap.ceilingEntry(key).thenApply(this::toVersioned);
80 }
81
82 @Override
83 public CompletableFuture<Map.Entry<String, Versioned<V>>> floorEntry(String key) {
84 return atomixTreeMap.floorEntry(key).thenApply(this::toVersioned);
85 }
86
87 @Override
88 public CompletableFuture<Map.Entry<String, Versioned<V>>> higherEntry(String key) {
89 return atomixTreeMap.higherEntry(key).thenApply(this::toVersioned);
90 }
91
92 @Override
93 public CompletableFuture<Map.Entry<String, Versioned<V>>> lowerEntry(String key) {
94 return atomixTreeMap.lowerEntry(key).thenApply(this::toVersioned);
95 }
96
97 @Override
98 public CompletableFuture<Map.Entry<String, Versioned<V>>> firstEntry() {
99 return atomixTreeMap.firstEntry().thenApply(this::toVersioned);
100 }
101
102 @Override
103 public CompletableFuture<Map.Entry<String, Versioned<V>>> lastEntry() {
104 return atomixTreeMap.lastEntry().thenApply(this::toVersioned);
105 }
106
107 @Override
108 public CompletableFuture<Map.Entry<String, Versioned<V>>> pollFirstEntry() {
109 throw new UnsupportedOperationException();
110 }
111
112 @Override
113 public CompletableFuture<Map.Entry<String, Versioned<V>>> pollLastEntry() {
114 throw new UnsupportedOperationException();
115 }
116
117 @Override
118 public CompletableFuture<Boolean> containsKey(String key) {
119 return atomixTreeMap.containsKey(key);
120 }
121
122 @Override
123 public CompletableFuture<String> lowerKey(String key) {
124 return atomixTreeMap.lowerKey(key);
125 }
126
127 @Override
128 public CompletableFuture<String> floorKey(String key) {
129 return atomixTreeMap.floorKey(key);
130 }
131
132 @Override
133 public CompletableFuture<String> ceilingKey(String key) {
134 return atomixTreeMap.ceilingKey(key);
135 }
136
137 @Override
138 public CompletableFuture<Versioned<V>> get(String key) {
139 return atomixTreeMap.get(key).thenApply(this::toVersioned);
140 }
141
142 @Override
143 public CompletableFuture<String> higherKey(String key) {
144 return atomixTreeMap.higherKey(key);
145 }
146
147 @Override
148 public CompletableFuture<NavigableSet<String>> navigableKeySet() {
Jordan Haltermanbc982392018-08-07 15:02:37 -0700149 return CompletableFuture.completedFuture(atomixTreeMap.navigableKeySet()
150 .sync(Duration.ofMillis(DEFAULT_OPERATION_TIMEOUT_MILLIS)));
Jordan Halterman00e92da2018-05-22 23:05:52 -0700151 }
152
153 @Override
154 public CompletableFuture<Versioned<V>> getOrDefault(String key, V defaultValue) {
155 return atomixTreeMap.getOrDefault(key, defaultValue).thenApply(this::toVersioned);
156 }
157
158 @Override
159 public CompletableFuture<NavigableMap<String, V>> subMap(
160 String upperKey, String lowerKey, boolean inclusiveUpper, boolean inclusiveLower) {
161 return CompletableFuture.completedFuture(
162 new DelegatingAsyncDistributedNavigableMap<>(
Jordan Haltermanbc982392018-08-07 15:02:37 -0700163 atomixTreeMap.subMap(lowerKey, inclusiveLower, upperKey, inclusiveUpper))
164 .sync(Duration.ofMillis(DEFAULT_OPERATION_TIMEOUT_MILLIS)));
Jordan Halterman00e92da2018-05-22 23:05:52 -0700165 }
166
167 @Override
168 public CompletableFuture<Versioned<V>> computeIf(
169 String key,
170 Predicate<? super V> condition, BiFunction<? super String, ? super V, ? extends V> remappingFunction) {
171 return adapt(atomixTreeMap.computeIf(key, condition, remappingFunction)).thenApply(this::toVersioned);
172 }
173
174 @Override
175 public CompletableFuture<Versioned<V>> put(String key, V value) {
176 return adapt(atomixTreeMap.put(key, value)).thenApply(this::toVersioned);
177 }
178
179 @Override
180 public CompletableFuture<Versioned<V>> putAndGet(String key, V value) {
181 return adapt(atomixTreeMap.putAndGet(key, value)).thenApply(this::toVersioned);
182 }
183
184 @Override
185 public CompletableFuture<Versioned<V>> remove(String key) {
186 return adapt(atomixTreeMap.remove(key)).thenApply(this::toVersioned);
187 }
188
189 @Override
190 public CompletableFuture<Set<String>> keySet() {
Jordan Haltermanbc982392018-08-07 15:02:37 -0700191 return CompletableFuture.completedFuture(atomixTreeMap.keySet()
192 .sync(Duration.ofMillis(DEFAULT_OPERATION_TIMEOUT_MILLIS)));
Jordan Halterman00e92da2018-05-22 23:05:52 -0700193 }
194
195 @Override
196 public CompletableFuture<Set<Map.Entry<String, Versioned<V>>>> entrySet() {
197 return CompletableFuture.completedFuture(atomixTreeMap.entrySet().stream()
198 .map(this::toVersioned)
199 .collect(Collectors.toSet()));
200 }
201
202 @Override
203 public CompletableFuture<Versioned<V>> putIfAbsent(String key, V value) {
204 return adapt(atomixTreeMap.putIfAbsent(key, value)).thenApply(this::toVersioned);
205 }
206
207 @Override
208 public CompletableFuture<Boolean> remove(String key, V value) {
209 return adapt(atomixTreeMap.remove(key, value));
210 }
211
212 @Override
213 public CompletableFuture<Boolean> remove(String key, long version) {
214 return adapt(atomixTreeMap.remove(key, version));
215 }
216
217 @Override
218 public CompletableFuture<Versioned<V>> replace(String key, V value) {
219 return adapt(atomixTreeMap.replace(key, value)).thenApply(this::toVersioned);
220 }
221
222 @Override
223 public CompletableFuture<Boolean> replace(String key, V oldValue, V newValue) {
224 return adapt(atomixTreeMap.replace(key, oldValue, newValue));
225 }
226
227 @Override
228 public CompletableFuture<Boolean> replace(String key, long oldVersion, V newValue) {
229 return adapt(atomixTreeMap.replace(key, oldVersion, newValue));
230 }
231
232 @Override
233 public CompletableFuture<Boolean> containsValue(V value) {
234 return atomixTreeMap.containsValue(value);
235 }
236
237 @Override
238 public CompletableFuture<AsyncIterator<Map.Entry<String, Versioned<V>>>> iterator() {
239 io.atomix.core.iterator.AsyncIterator<Map.Entry<String, io.atomix.utils.time.Versioned<V>>> atomixIterator
240 = atomixTreeMap.entrySet().iterator();
241 return CompletableFuture.completedFuture(new AsyncIterator<Map.Entry<String, Versioned<V>>>() {
242 @Override
243 public CompletableFuture<Boolean> hasNext() {
244 return atomixIterator.hasNext();
245 }
246
247 @Override
248 public CompletableFuture<Map.Entry<String, Versioned<V>>> next() {
249 return atomixIterator.next()
250 .thenApply(entry -> Maps.immutableEntry(entry.getKey(), toVersioned(entry.getValue())));
251 }
252 });
253 }
254
255 @Override
256 public CompletableFuture<Void> clear() {
257 return atomixTreeMap.clear();
258 }
259
260 @Override
261 public CompletableFuture<Collection<Versioned<V>>> values() {
262 return CompletableFuture.completedFuture(
263 new TranscodingAsyncDistributedCollection<Versioned<V>, io.atomix.utils.time.Versioned<V>>(
264 atomixTreeMap.values(),
265 e -> new io.atomix.utils.time.Versioned<>(e.value(), e.version()),
Jordan Haltermanbc982392018-08-07 15:02:37 -0700266 e -> new Versioned<>(e.value(), e.version()))
267 .sync(Duration.ofMillis(DEFAULT_OPERATION_TIMEOUT_MILLIS)));
Jordan Halterman00e92da2018-05-22 23:05:52 -0700268 }
269
270 @Override
271 public synchronized CompletableFuture<Void> addListener(MapEventListener<String, V> listener, Executor executor) {
272 io.atomix.core.map.AtomicMapEventListener<String, V> atomixListener = event ->
273 listener.event(new MapEvent<String, V>(
274 MapEvent.Type.valueOf(event.type().name()),
275 name(),
276 event.key(),
277 toVersioned(event.newValue()),
278 toVersioned(event.oldValue())));
279 listenerMap.put(listener, atomixListener);
280 return atomixTreeMap.addListener(atomixListener, executor);
281 }
282
283 @Override
284 public CompletableFuture<Void> removeListener(MapEventListener<String, V> listener) {
285 io.atomix.core.map.AtomicMapEventListener<String, V> atomixListener = listenerMap.remove(listener);
286 if (atomixListener != null) {
287 return atomixTreeMap.removeListener(atomixListener);
288 }
289 return CompletableFuture.completedFuture(null);
290 }
291
292 @Override
293 public CompletableFuture<Version> begin(TransactionId transactionId) {
294 throw new UnsupportedOperationException();
295 }
296
297 @Override
298 public CompletableFuture<Boolean> prepare(TransactionLog<MapUpdate<String, V>> transactionLog) {
299 throw new UnsupportedOperationException();
300 }
301
302 @Override
303 public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<MapUpdate<String, V>> transactionLog) {
304 throw new UnsupportedOperationException();
305 }
306
307 @Override
308 public CompletableFuture<Void> commit(TransactionId transactionId) {
309 throw new UnsupportedOperationException();
310 }
311
312 @Override
313 public CompletableFuture<Void> rollback(TransactionId transactionId) {
314 throw new UnsupportedOperationException();
315 }
316
317 private <T> CompletableFuture<T> adapt(CompletableFuture<T> future) {
318 CompletableFuture<T> newFuture = new CompletableFuture<>();
319 future.whenComplete((result, error) -> {
320 if (error == null) {
321 newFuture.complete(result);
322 } else {
323 Throwable cause = Throwables.getRootCause(error);
324 if (cause instanceof io.atomix.primitive.PrimitiveException.ConcurrentModification) {
325 newFuture.completeExceptionally(
326 new ConsistentMapException.ConcurrentModification(error.getMessage()));
327 } else if (cause instanceof io.atomix.primitive.PrimitiveException.Timeout) {
328 newFuture.completeExceptionally(new ConsistentMapException.Timeout(error.getMessage()));
329 } else if (cause instanceof io.atomix.primitive.PrimitiveException.Interrupted) {
330 newFuture.completeExceptionally(new ConsistentMapException.Interrupted());
331 } else if (cause instanceof io.atomix.primitive.PrimitiveException.Unavailable) {
332 newFuture.completeExceptionally(new ConsistentMapException.Unavailable());
333 } else if (cause instanceof io.atomix.primitive.PrimitiveException) {
334 newFuture.completeExceptionally(new ConsistentMapException(cause.getMessage()));
335 }
336 }
337 });
338 return newFuture;
339 }
340
341 private Versioned<V> toVersioned(io.atomix.utils.time.Versioned<V> versioned) {
342 return versioned != null
343 ? new Versioned<>(versioned.value(), versioned.version(), versioned.creationTime())
344 : null;
345 }
346
347 private Map.Entry<String, Versioned<V>> toVersioned(Map.Entry<String, io.atomix.utils.time.Versioned<V>> entry) {
348 return entry != null ? Maps.immutableEntry(entry.getKey(), toVersioned(entry.getValue())) : null;
349 }
350}