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