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