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