blob: ec93df3bbc8daabeb004b7dd286caf8d829a2f81 [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
18import java.util.Collection;
19import java.util.Map;
20import java.util.Set;
21import java.util.concurrent.CompletableFuture;
22import java.util.concurrent.Executor;
23import java.util.function.BiFunction;
24import java.util.function.Predicate;
25
26import com.google.common.base.Throwables;
27import com.google.common.collect.Maps;
28import io.atomix.core.collection.impl.TranscodingAsyncDistributedCollection;
29import io.atomix.core.set.impl.TranscodingAsyncDistributedSet;
30import org.onosproject.store.primitives.MapUpdate;
31import org.onosproject.store.primitives.TransactionId;
32import org.onosproject.store.service.AsyncConsistentMap;
33import org.onosproject.store.service.AsyncIterator;
34import org.onosproject.store.service.ConsistentMapException;
35import org.onosproject.store.service.MapEvent;
36import org.onosproject.store.service.MapEventListener;
37import org.onosproject.store.service.TransactionLog;
38import org.onosproject.store.service.Version;
39import org.onosproject.store.service.Versioned;
40
41/**
42 * Atomix consistent map.
43 */
44public class AtomixConsistentMap<K, V> implements AsyncConsistentMap<K, V> {
45 private final io.atomix.core.map.AsyncAtomicMap<K, V> atomixMap;
46 private final Map<MapEventListener<K, V>, io.atomix.core.map.AtomicMapEventListener<K, V>> listenerMap =
47 Maps.newIdentityHashMap();
48
49 public AtomixConsistentMap(io.atomix.core.map.AsyncAtomicMap<K, V> atomixMap) {
50 this.atomixMap = atomixMap;
51 }
52
53 @Override
54 public String name() {
55 return atomixMap.name();
56 }
57
58 @Override
59 public CompletableFuture<Integer> size() {
60 return atomixMap.size();
61 }
62
63 @Override
64 public CompletableFuture<Boolean> containsKey(K key) {
65 return atomixMap.containsKey(key);
66 }
67
68 @Override
69 public CompletableFuture<Boolean> containsValue(V value) {
70 return atomixMap.containsValue(value);
71 }
72
73 @Override
74 public CompletableFuture<Versioned<V>> get(K key) {
75 return atomixMap.get(key).thenApply(this::toVersioned);
76 }
77
78 @Override
79 public CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue) {
80 return atomixMap.getOrDefault(key, defaultValue).thenApply(this::toVersioned);
81 }
82
83 @Override
84 public CompletableFuture<Versioned<V>> computeIf(
85 K key, Predicate<? super V> condition, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
86 return adapt(atomixMap.computeIf(key, condition, remappingFunction).thenApply(this::toVersioned));
87 }
88
89 @Override
90 public CompletableFuture<Versioned<V>> put(K key, V value) {
91 return adapt(atomixMap.put(key, value).thenApply(this::toVersioned));
92 }
93
94 @Override
95 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
96 return adapt(atomixMap.putAndGet(key, value).thenApply(this::toVersioned));
97 }
98
99 @Override
100 public CompletableFuture<Versioned<V>> remove(K key) {
101 return adapt(atomixMap.remove(key).thenApply(this::toVersioned));
102 }
103
104 @Override
105 public CompletableFuture<Void> clear() {
106 return atomixMap.clear();
107 }
108
109 @Override
110 public CompletableFuture<Set<K>> keySet() {
111 return CompletableFuture.completedFuture(atomixMap.keySet().sync());
112 }
113
114 @Override
115 public CompletableFuture<Collection<Versioned<V>>> values() {
116 return CompletableFuture.completedFuture(
117 new TranscodingAsyncDistributedCollection<Versioned<V>, io.atomix.utils.time.Versioned<V>>(
118 atomixMap.values(),
119 v -> new io.atomix.utils.time.Versioned<>(v.value(), v.version()),
120 v -> new Versioned<>(v.value(), v.version())).sync());
121 }
122
123 @Override
124 public CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet() {
125 return CompletableFuture.completedFuture(
126 new TranscodingAsyncDistributedSet<Map.Entry<K, Versioned<V>>,
127 Map.Entry<K, io.atomix.utils.time.Versioned<V>>>(
128 atomixMap.entrySet(),
129 e -> Maps.immutableEntry(e.getKey(),
130 new io.atomix.utils.time.Versioned<>(e.getValue().value(), e.getValue().version())),
131 e -> Maps.immutableEntry(e.getKey(), new Versioned<>(e.getValue().value(), e.getValue().version())))
132 .sync());
133 }
134
135 @Override
136 public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) {
137 return adapt(atomixMap.putIfAbsent(key, value).thenApply(this::toVersioned));
138 }
139
140 @Override
141 public CompletableFuture<Boolean> remove(K key, V value) {
142 return adapt(atomixMap.remove(key, value));
143 }
144
145 @Override
146 public CompletableFuture<Boolean> remove(K key, long version) {
147 return adapt(atomixMap.remove(key, version));
148 }
149
150 @Override
151 public CompletableFuture<Versioned<V>> replace(K key, V value) {
152 return adapt(atomixMap.replace(key, value).thenApply(this::toVersioned));
153 }
154
155 @Override
156 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
157 return adapt(atomixMap.replace(key, oldValue, newValue));
158 }
159
160 @Override
161 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
162 return adapt(atomixMap.replace(key, oldVersion, newValue));
163 }
164
165 @Override
166 public CompletableFuture<AsyncIterator<Map.Entry<K, Versioned<V>>>> iterator() {
167 io.atomix.core.iterator.AsyncIterator<Map.Entry<K, io.atomix.utils.time.Versioned<V>>> atomixIterator
168 = atomixMap.entrySet().iterator();
169 return CompletableFuture.completedFuture(new AsyncIterator<Map.Entry<K, Versioned<V>>>() {
170 @Override
171 public CompletableFuture<Boolean> hasNext() {
172 return atomixIterator.hasNext();
173 }
174
175 @Override
176 public CompletableFuture<Map.Entry<K, Versioned<V>>> next() {
177 return atomixIterator.next()
178 .thenApply(entry -> Maps.immutableEntry(entry.getKey(), toVersioned(entry.getValue())));
179 }
180 });
181 }
182
183 @Override
184 public synchronized CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor) {
185 io.atomix.core.map.AtomicMapEventListener<K, V> atomixListener = event ->
186 listener.event(new MapEvent<K, V>(
187 MapEvent.Type.valueOf(event.type().name()),
188 name(),
189 event.key(),
190 toVersioned(event.newValue()),
191 toVersioned(event.oldValue())));
192 listenerMap.put(listener, atomixListener);
193 return atomixMap.addListener(atomixListener, executor);
194 }
195
196 @Override
197 public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) {
198 io.atomix.core.map.AtomicMapEventListener<K, V> atomixListener = listenerMap.remove(listener);
199 if (atomixListener != null) {
200 return atomixMap.removeListener(atomixListener);
201 }
202 return CompletableFuture.completedFuture(null);
203 }
204
205 @Override
206 public CompletableFuture<Version> begin(TransactionId transactionId) {
207 throw new UnsupportedOperationException();
208 }
209
210 @Override
211 public CompletableFuture<Boolean> prepare(TransactionLog<MapUpdate<K, V>> transactionLog) {
212 throw new UnsupportedOperationException();
213 }
214
215 @Override
216 public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<MapUpdate<K, V>> transactionLog) {
217 throw new UnsupportedOperationException();
218 }
219
220 @Override
221 public CompletableFuture<Void> commit(TransactionId transactionId) {
222 throw new UnsupportedOperationException();
223 }
224
225 @Override
226 public CompletableFuture<Void> rollback(TransactionId transactionId) {
227 throw new UnsupportedOperationException();
228 }
229
230 private <T> CompletableFuture<T> adapt(CompletableFuture<T> future) {
231 CompletableFuture<T> newFuture = new CompletableFuture<>();
232 future.whenComplete((result, error) -> {
233 if (error == null) {
234 newFuture.complete(result);
235 } else {
236 Throwable cause = Throwables.getRootCause(error);
237 if (cause instanceof io.atomix.primitive.PrimitiveException.ConcurrentModification) {
238 newFuture.completeExceptionally(
239 new ConsistentMapException.ConcurrentModification(cause.getMessage()));
240 } else if (cause instanceof io.atomix.primitive.PrimitiveException.Timeout) {
241 newFuture.completeExceptionally(new ConsistentMapException.Timeout(cause.getMessage()));
242 } else if (cause instanceof io.atomix.primitive.PrimitiveException.Interrupted) {
243 newFuture.completeExceptionally(new ConsistentMapException.Interrupted());
244 } else if (cause instanceof io.atomix.primitive.PrimitiveException.Unavailable) {
245 newFuture.completeExceptionally(new ConsistentMapException.Unavailable());
246 } else if (cause instanceof io.atomix.primitive.PrimitiveException) {
247 newFuture.completeExceptionally(new ConsistentMapException(cause.getMessage()));
248 }
249 }
250 });
251 return newFuture;
252 }
253
254 private Versioned<V> toVersioned(io.atomix.utils.time.Versioned<V> versioned) {
255 return versioned != null
256 ? new Versioned<>(versioned.value(), versioned.version(), versioned.creationTime())
257 : null;
258 }
259}