Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -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 | */ |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 16 | package org.onosproject.store.primitives; |
| 17 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 19 | import java.util.Map; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 20 | import java.util.Map.Entry; |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 22 | import java.util.Set; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 23 | import java.util.concurrent.CompletableFuture; |
| 24 | import java.util.concurrent.ExecutionException; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | import java.util.concurrent.TimeoutException; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 27 | import java.util.function.BiFunction; |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 28 | import java.util.function.Consumer; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 29 | import java.util.function.Function; |
| 30 | import java.util.function.Predicate; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 31 | |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 32 | import org.onlab.util.Tools; |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 33 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.ConsistentMap; |
| 35 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 36 | import org.onosproject.store.service.ConsistentMapException.ConcurrentModification; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 37 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 38 | import org.onosproject.store.service.Synchronous; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.Versioned; |
| 40 | |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 41 | import com.google.common.base.Throwables; |
| 42 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 43 | /** |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 44 | * Default implementation of {@code ConsistentMap}. |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 45 | * |
| 46 | * @param <K> type of key. |
| 47 | * @param <V> type of value. |
| 48 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 49 | public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K, V>> implements ConsistentMap<K, V> { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 50 | |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 51 | private static final int MAX_DELAY_BETWEEN_RETY_MILLS = 50; |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 52 | private final AsyncConsistentMap<K, V> asyncMap; |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 53 | private final long operationTimeoutMillis; |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 54 | private Map<K, V> javaMap; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 55 | |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 56 | public DefaultConsistentMap(AsyncConsistentMap<K, V> asyncMap, long operationTimeoutMillis) { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 57 | super(asyncMap); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 58 | this.asyncMap = asyncMap; |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 59 | this.operationTimeoutMillis = operationTimeoutMillis; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public int size() { |
| 64 | return complete(asyncMap.size()); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean isEmpty() { |
| 69 | return complete(asyncMap.isEmpty()); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean containsKey(K key) { |
| 74 | return complete(asyncMap.containsKey(key)); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean containsValue(V value) { |
| 79 | return complete(asyncMap.containsValue(value)); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Versioned<V> get(K key) { |
| 84 | return complete(asyncMap.get(key)); |
| 85 | } |
| 86 | |
| 87 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 88 | public Versioned<V> computeIfAbsent(K key, |
| 89 | Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 90 | return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k)); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Versioned<V> computeIfPresent(K key, |
| 95 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 96 | return computeIf(key, Objects::nonNull, remappingFunction); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Versioned<V> compute(K key, |
| 101 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 102 | return computeIf(key, v -> true, remappingFunction); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Versioned<V> computeIf(K key, |
| 107 | Predicate<? super V> condition, |
| 108 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 109 | return Tools.retryable(() -> complete(asyncMap.computeIf(key, condition, remappingFunction)), |
| 110 | ConcurrentModification.class, |
| 111 | Integer.MAX_VALUE, |
| 112 | MAX_DELAY_BETWEEN_RETY_MILLS).get(); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 116 | public Versioned<V> put(K key, V value) { |
| 117 | return complete(asyncMap.put(key, value)); |
| 118 | } |
| 119 | |
| 120 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 121 | public Versioned<V> putAndGet(K key, V value) { |
| 122 | return complete(asyncMap.putAndGet(key, value)); |
| 123 | } |
| 124 | |
| 125 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 126 | public Versioned<V> remove(K key) { |
| 127 | return complete(asyncMap.remove(key)); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void clear() { |
| 132 | complete(asyncMap.clear()); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Set<K> keySet() { |
| 137 | return complete(asyncMap.keySet()); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Collection<Versioned<V>> values() { |
| 142 | return complete(asyncMap.values()); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Set<Entry<K, Versioned<V>>> entrySet() { |
| 147 | return complete(asyncMap.entrySet()); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Versioned<V> putIfAbsent(K key, V value) { |
| 152 | return complete(asyncMap.putIfAbsent(key, value)); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean remove(K key, V value) { |
| 157 | return complete(asyncMap.remove(key, value)); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean remove(K key, long version) { |
| 162 | return complete(asyncMap.remove(key, version)); |
| 163 | } |
| 164 | |
| 165 | @Override |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 166 | public Versioned<V> replace(K key, V value) { |
| 167 | return complete(asyncMap.replace(key, value)); |
| 168 | } |
| 169 | |
| 170 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 171 | public boolean replace(K key, V oldValue, V newValue) { |
| 172 | return complete(asyncMap.replace(key, oldValue, newValue)); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean replace(K key, long oldVersion, V newValue) { |
| 177 | return complete(asyncMap.replace(key, oldVersion, newValue)); |
| 178 | } |
| 179 | |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 180 | @Override |
| 181 | public void addListener(MapEventListener<K, V> listener) { |
| 182 | complete(asyncMap.addListener(listener)); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void removeListener(MapEventListener<K, V> listener) { |
| 187 | complete(asyncMap.addListener(listener)); |
| 188 | } |
| 189 | |
| 190 | @Override |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 191 | public void addStatusChangeListener(Consumer<Status> listener) { |
| 192 | asyncMap.addStatusChangeListener(listener); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void removeStatusChangeListener(Consumer<Status> listener) { |
| 197 | asyncMap.removeStatusChangeListener(listener); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Collection<Consumer<Status>> statusChangeListeners() { |
| 202 | return asyncMap.statusChangeListeners(); |
| 203 | } |
| 204 | |
| 205 | @Override |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 206 | public Map<K, V> asJavaMap() { |
| 207 | synchronized (this) { |
| 208 | if (javaMap == null) { |
| 209 | javaMap = new ConsistentMapBackedJavaMap<>(this); |
| 210 | } |
| 211 | } |
| 212 | return javaMap; |
| 213 | } |
| 214 | |
HIGUCHI Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 215 | @Override |
| 216 | public String toString() { |
| 217 | return asJavaMap().toString(); |
| 218 | } |
| 219 | |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 220 | private <T> T complete(CompletableFuture<T> future) { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 221 | try { |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 222 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 223 | } catch (InterruptedException e) { |
| 224 | Thread.currentThread().interrupt(); |
| 225 | throw new ConsistentMapException.Interrupted(); |
| 226 | } catch (TimeoutException e) { |
| 227 | throw new ConsistentMapException.Timeout(); |
| 228 | } catch (ExecutionException e) { |
Madan Jampani | f6c973a | 2016-01-30 22:20:48 -0800 | [diff] [blame] | 229 | Throwables.propagateIfPossible(e.getCause()); |
| 230 | throw new ConsistentMapException(e.getCause()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 233 | } |