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