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; |
| 20 | import java.util.Map.Entry; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 21 | import java.util.Optional; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 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 | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 48 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 49 | public String name() { |
| 50 | return asyncMap.name(); |
| 51 | } |
| 52 | |
| 53 | public DefaultConsistentMap(DefaultAsyncConsistentMap<K, V> asyncMap) { |
| 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 |
| 118 | public Optional<Versioned<V>> putIfAbsentAndGet(K key, V value) { |
| 119 | return complete(asyncMap.putIfAbsentAndGet(key, value)); |
| 120 | } |
| 121 | |
| 122 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 123 | public Versioned<V> remove(K key) { |
| 124 | return complete(asyncMap.remove(key)); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void clear() { |
| 129 | complete(asyncMap.clear()); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Set<K> keySet() { |
| 134 | return complete(asyncMap.keySet()); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Collection<Versioned<V>> values() { |
| 139 | return complete(asyncMap.values()); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Set<Entry<K, Versioned<V>>> entrySet() { |
| 144 | return complete(asyncMap.entrySet()); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Versioned<V> putIfAbsent(K key, V value) { |
| 149 | return complete(asyncMap.putIfAbsent(key, value)); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean remove(K key, V value) { |
| 154 | return complete(asyncMap.remove(key, value)); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean remove(K key, long version) { |
| 159 | return complete(asyncMap.remove(key, version)); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 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 | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 172 | @Override |
| 173 | public Optional<Versioned<V>> replaceAndGet(K key, long oldVersion, V newValue) { |
| 174 | return complete(asyncMap.replaceAndGet(key, oldVersion, newValue)); |
| 175 | } |
| 176 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 177 | private static <T> T complete(CompletableFuture<T> future) { |
| 178 | try { |
| 179 | return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 180 | } catch (InterruptedException e) { |
| 181 | Thread.currentThread().interrupt(); |
| 182 | throw new ConsistentMapException.Interrupted(); |
| 183 | } catch (TimeoutException e) { |
| 184 | throw new ConsistentMapException.Timeout(); |
| 185 | } catch (ExecutionException e) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 186 | if (e.getCause() instanceof ConsistentMapException) { |
| 187 | throw (ConsistentMapException) e.getCause(); |
| 188 | } else { |
| 189 | throw new ConsistentMapException(e.getCause()); |
| 190 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 193 | |
| 194 | @Override |
| 195 | public void addListener(MapEventListener<K, V> listener) { |
| 196 | asyncMap.addListener(listener); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void removeListener(MapEventListener<K, V> listener) { |
| 201 | asyncMap.addListener(listener); |
| 202 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 203 | } |