Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | import java.util.concurrent.CompletableFuture; |
| 22 | import java.util.concurrent.Executor; |
| 23 | import java.util.function.Function; |
| 24 | |
| 25 | import com.google.common.collect.Maps; |
| 26 | import com.google.common.collect.Multiset; |
| 27 | import io.atomix.core.map.impl.TranscodingAsyncDistributedMap; |
| 28 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 29 | import org.onosproject.store.service.AsyncIterator; |
| 30 | import org.onosproject.store.service.MultimapEvent; |
| 31 | import org.onosproject.store.service.MultimapEventListener; |
| 32 | import org.onosproject.store.service.Versioned; |
| 33 | |
| 34 | /** |
| 35 | * Atomix consistent map. |
| 36 | */ |
| 37 | public class AtomixConsistentMultimap<K, V> implements AsyncConsistentMultimap<K, V> { |
| 38 | private final io.atomix.core.multimap.AsyncAtomicMultimap<K, V> atomixMultimap; |
| 39 | private final Map<MultimapEventListener<K, V>, io.atomix.core.multimap.AtomicMultimapEventListener<K, V>> |
| 40 | listenerMap = Maps.newIdentityHashMap(); |
| 41 | |
| 42 | public AtomixConsistentMultimap(io.atomix.core.multimap.AsyncAtomicMultimap<K, V> atomixMultimap) { |
| 43 | this.atomixMultimap = atomixMultimap; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String name() { |
| 48 | return atomixMultimap.name(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public CompletableFuture<Integer> size() { |
| 53 | return atomixMultimap.size(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public CompletableFuture<Boolean> containsKey(K key) { |
| 58 | return atomixMultimap.containsKey(key); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public CompletableFuture<Boolean> containsValue(V value) { |
| 63 | return atomixMultimap.containsValue(value); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public CompletableFuture<Boolean> isEmpty() { |
| 68 | return atomixMultimap.isEmpty(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<Boolean> containsEntry(K key, V value) { |
| 73 | return atomixMultimap.containsEntry(key, value); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public CompletableFuture<Boolean> put(K key, V value) { |
| 78 | return atomixMultimap.put(key, value); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public CompletableFuture<Versioned<Collection<? extends V>>> putAndGet(K key, V value) { |
| 83 | return atomixMultimap.put(key, value).thenCompose(v -> atomixMultimap.get(key)).thenApply(this::toVersioned); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 88 | return atomixMultimap.remove(key, value); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<Versioned<Collection<? extends V>>> removeAndGet(K key, V value) { |
| 93 | return atomixMultimap.remove(key, value).thenCompose(v -> atomixMultimap.get(key)).thenApply(this::toVersioned); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Boolean> removeAll(K key, Collection<? extends V> values) { |
| 98 | return atomixMultimap.removeAll(key, values); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key) { |
| 103 | return atomixMultimap.removeAll(key).thenApply(this::toVersioned); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public CompletableFuture<Boolean> putAll(K key, Collection<? extends V> values) { |
| 108 | return atomixMultimap.putAll(key, values); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | @SuppressWarnings("unchecked") |
| 113 | public CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(K key, Collection<V> values) { |
| 114 | return atomixMultimap.replaceValues(key, values).thenApply(this::toVersioned); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public CompletableFuture<Void> clear() { |
| 119 | return atomixMultimap.clear(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | @SuppressWarnings("unchecked") |
| 124 | public CompletableFuture<Versioned<Collection<? extends V>>> get(K key) { |
| 125 | return atomixMultimap.get(key).thenApply(this::toVersioned); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public CompletableFuture<Set<K>> keySet() { |
| 130 | return CompletableFuture.completedFuture(atomixMultimap.keySet().sync()); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Multiset<K>> keys() { |
| 135 | return CompletableFuture.completedFuture(atomixMultimap.keys().sync()); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public CompletableFuture<Multiset<V>> values() { |
| 140 | return CompletableFuture.completedFuture(atomixMultimap.values().sync()); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<Collection<Map.Entry<K, V>>> entries() { |
| 145 | return CompletableFuture.completedFuture(atomixMultimap.entries().sync()); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public CompletableFuture<AsyncIterator<Map.Entry<K, V>>> iterator() { |
| 150 | return CompletableFuture.completedFuture(new AtomixIterator<>(atomixMultimap.entries().iterator())); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public CompletableFuture<Map<K, Collection<V>>> asMap() { |
| 155 | return CompletableFuture.completedFuture( |
| 156 | new TranscodingAsyncDistributedMap<K, Collection<V>, K, io.atomix.utils.time.Versioned<Collection<V>>>( |
| 157 | atomixMultimap.asMap(), |
| 158 | Function.identity(), |
| 159 | Function.identity(), |
| 160 | v -> new io.atomix.utils.time.Versioned<>(v, 0), |
| 161 | v -> v.value()).sync()); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public synchronized CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener, Executor executor) { |
| 166 | io.atomix.core.multimap.AtomicMultimapEventListener<K, V> atomixListener = event -> |
| 167 | listener.event(new MultimapEvent<K, V>( |
| 168 | name(), |
| 169 | event.key(), |
| 170 | event.newValue(), |
| 171 | event.oldValue())); |
| 172 | listenerMap.put(listener, atomixListener); |
| 173 | return atomixMultimap.addListener(atomixListener, executor); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public CompletableFuture<Void> removeListener(MultimapEventListener<K, V> listener) { |
| 178 | io.atomix.core.multimap.AtomicMultimapEventListener<K, V> atomixListener = listenerMap.remove(listener); |
| 179 | if (atomixListener != null) { |
| 180 | return atomixMultimap.removeListener(atomixListener); |
| 181 | } |
| 182 | return CompletableFuture.completedFuture(null); |
| 183 | } |
| 184 | |
| 185 | private Versioned<Collection<? extends V>> toVersioned( |
| 186 | io.atomix.utils.time.Versioned<Collection<V>> versioned) { |
| 187 | return versioned != null |
| 188 | ? new Versioned<>(versioned.value(), versioned.version(), versioned.creationTime()) |
| 189 | : null; |
| 190 | } |
| 191 | } |