blob: 188a10e63bcd4db2686f0b96a5a34dfce060b6bc [file] [log] [blame]
Madan Jampani7c521002015-03-23 12:23:01 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani7c521002015-03-23 12:23:01 -07003 *
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'Connor7cbbbb72016-04-09 02:13:23 -070016package org.onosproject.store.primitives;
17
Madan Jampani7c521002015-03-23 12:23:01 -070018import java.util.Collection;
Madan Jampanie1065222015-08-17 16:21:51 -070019import java.util.Map;
Madan Jampani7c521002015-03-23 12:23:01 -070020import java.util.Map.Entry;
Madan Jampanif6c973a2016-01-30 22:20:48 -080021import java.util.Objects;
Madan Jampanifa242182016-01-22 13:42:54 -080022import java.util.Set;
Madan Jampani7c521002015-03-23 12:23:01 -070023import java.util.concurrent.CompletableFuture;
24import java.util.concurrent.ExecutionException;
Madan Jampani0463cf92016-05-04 14:46:08 -070025import java.util.concurrent.Executor;
Madan Jampani7c521002015-03-23 12:23:01 -070026import java.util.concurrent.TimeUnit;
27import java.util.concurrent.TimeoutException;
Madan Jampani346d4f52015-05-04 11:09:39 -070028import java.util.function.BiFunction;
Madan Jampani1d3b6172016-04-28 13:22:57 -070029import java.util.function.Consumer;
Madan Jampani346d4f52015-05-04 11:09:39 -070030import java.util.function.Function;
31import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070032
Madan Jampanif6c973a2016-01-30 22:20:48 -080033import org.onlab.util.Tools;
Madan Jampania090a112016-01-18 16:38:17 -080034import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampani7c521002015-03-23 12:23:01 -070035import org.onosproject.store.service.ConsistentMap;
36import org.onosproject.store.service.ConsistentMapException;
Madan Jampanif6c973a2016-01-30 22:20:48 -080037import org.onosproject.store.service.ConsistentMapException.ConcurrentModification;
Madan Jampani50589ac2015-06-08 11:38:46 -070038import org.onosproject.store.service.MapEventListener;
Madan Jampania090a112016-01-18 16:38:17 -080039import org.onosproject.store.service.Synchronous;
Madan Jampani7c521002015-03-23 12:23:01 -070040import org.onosproject.store.service.Versioned;
41
Madan Jampanif6c973a2016-01-30 22:20:48 -080042import com.google.common.base.Throwables;
43
Madan Jampani7c521002015-03-23 12:23:01 -070044/**
Madan Jampanifa242182016-01-22 13:42:54 -080045 * Default implementation of {@code ConsistentMap}.
Madan Jampani7c521002015-03-23 12:23:01 -070046 *
47 * @param <K> type of key.
48 * @param <V> type of value.
49 */
Madan Jampania090a112016-01-18 16:38:17 -080050public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K, V>> implements ConsistentMap<K, V> {
Madan Jampani7c521002015-03-23 12:23:01 -070051
Madan Jampanif6c973a2016-01-30 22:20:48 -080052 private static final int MAX_DELAY_BETWEEN_RETY_MILLS = 50;
Madan Jampanifa242182016-01-22 13:42:54 -080053 private final AsyncConsistentMap<K, V> asyncMap;
Madan Jampani28b2cd62016-02-02 10:49:52 -080054 private final long operationTimeoutMillis;
Madan Jampanie1065222015-08-17 16:21:51 -070055 private Map<K, V> javaMap;
Madan Jampani7c521002015-03-23 12:23:01 -070056
Madan Jampani28b2cd62016-02-02 10:49:52 -080057 public DefaultConsistentMap(AsyncConsistentMap<K, V> asyncMap, long operationTimeoutMillis) {
Madan Jampania090a112016-01-18 16:38:17 -080058 super(asyncMap);
Madan Jampani50589ac2015-06-08 11:38:46 -070059 this.asyncMap = asyncMap;
Madan Jampani28b2cd62016-02-02 10:49:52 -080060 this.operationTimeoutMillis = operationTimeoutMillis;
Madan Jampani7c521002015-03-23 12:23:01 -070061 }
62
63 @Override
64 public int size() {
65 return complete(asyncMap.size());
66 }
67
68 @Override
69 public boolean isEmpty() {
70 return complete(asyncMap.isEmpty());
71 }
72
73 @Override
74 public boolean containsKey(K key) {
75 return complete(asyncMap.containsKey(key));
76 }
77
78 @Override
79 public boolean containsValue(V value) {
80 return complete(asyncMap.containsValue(value));
81 }
82
83 @Override
84 public Versioned<V> get(K key) {
85 return complete(asyncMap.get(key));
86 }
87
88 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -070089 public Versioned<V> computeIfAbsent(K key,
90 Function<? super K, ? extends V> mappingFunction) {
Madan Jampanif6c973a2016-01-30 22:20:48 -080091 return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
Madan Jampani346d4f52015-05-04 11:09:39 -070092 }
93
94 @Override
95 public Versioned<V> computeIfPresent(K key,
96 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanif6c973a2016-01-30 22:20:48 -080097 return computeIf(key, Objects::nonNull, remappingFunction);
Madan Jampani346d4f52015-05-04 11:09:39 -070098 }
99
100 @Override
101 public Versioned<V> compute(K key,
102 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanif6c973a2016-01-30 22:20:48 -0800103 return computeIf(key, v -> true, remappingFunction);
Madan Jampani346d4f52015-05-04 11:09:39 -0700104 }
105
106 @Override
107 public Versioned<V> computeIf(K key,
108 Predicate<? super V> condition,
109 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampanif6c973a2016-01-30 22:20:48 -0800110 return Tools.retryable(() -> complete(asyncMap.computeIf(key, condition, remappingFunction)),
111 ConcurrentModification.class,
112 Integer.MAX_VALUE,
113 MAX_DELAY_BETWEEN_RETY_MILLS).get();
Madan Jampani346d4f52015-05-04 11:09:39 -0700114 }
115
116 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700117 public Versioned<V> put(K key, V value) {
118 return complete(asyncMap.put(key, value));
119 }
120
121 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700122 public Versioned<V> putAndGet(K key, V value) {
123 return complete(asyncMap.putAndGet(key, value));
124 }
125
126 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700127 public Versioned<V> remove(K key) {
128 return complete(asyncMap.remove(key));
129 }
130
131 @Override
132 public void clear() {
133 complete(asyncMap.clear());
134 }
135
136 @Override
137 public Set<K> keySet() {
138 return complete(asyncMap.keySet());
139 }
140
141 @Override
142 public Collection<Versioned<V>> values() {
143 return complete(asyncMap.values());
144 }
145
146 @Override
147 public Set<Entry<K, Versioned<V>>> entrySet() {
148 return complete(asyncMap.entrySet());
149 }
150
151 @Override
152 public Versioned<V> putIfAbsent(K key, V value) {
153 return complete(asyncMap.putIfAbsent(key, value));
154 }
155
156 @Override
157 public boolean remove(K key, V value) {
158 return complete(asyncMap.remove(key, value));
159 }
160
161 @Override
162 public boolean remove(K key, long version) {
163 return complete(asyncMap.remove(key, version));
164 }
165
166 @Override
Jihwan Kim9887ad92015-12-12 00:23:57 +0900167 public Versioned<V> replace(K key, V value) {
168 return complete(asyncMap.replace(key, value));
169 }
170
171 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700172 public boolean replace(K key, V oldValue, V newValue) {
173 return complete(asyncMap.replace(key, oldValue, newValue));
174 }
175
176 @Override
177 public boolean replace(K key, long oldVersion, V newValue) {
178 return complete(asyncMap.replace(key, oldVersion, newValue));
179 }
180
Madan Jampanifa242182016-01-22 13:42:54 -0800181 @Override
Madan Jampani0463cf92016-05-04 14:46:08 -0700182 public void addListener(MapEventListener<K, V> listener, Executor executor) {
183 complete(asyncMap.addListener(listener, executor));
Madan Jampanifa242182016-01-22 13:42:54 -0800184 }
185
186 @Override
187 public void removeListener(MapEventListener<K, V> listener) {
Hyunsun Moon65b38cb2016-05-06 18:27:37 -0700188 complete(asyncMap.removeListener(listener));
Madan Jampanifa242182016-01-22 13:42:54 -0800189 }
190
191 @Override
Madan Jampani1d3b6172016-04-28 13:22:57 -0700192 public void addStatusChangeListener(Consumer<Status> listener) {
193 asyncMap.addStatusChangeListener(listener);
194 }
195
196 @Override
197 public void removeStatusChangeListener(Consumer<Status> listener) {
198 asyncMap.removeStatusChangeListener(listener);
199 }
200
201 @Override
202 public Collection<Consumer<Status>> statusChangeListeners() {
203 return asyncMap.statusChangeListeners();
204 }
205
206 @Override
Madan Jampanifa242182016-01-22 13:42:54 -0800207 public Map<K, V> asJavaMap() {
208 synchronized (this) {
209 if (javaMap == null) {
210 javaMap = new ConsistentMapBackedJavaMap<>(this);
211 }
212 }
213 return javaMap;
214 }
215
HIGUCHI Yutadc4394c2016-01-29 15:35:10 -0800216 @Override
217 public String toString() {
218 return asJavaMap().toString();
219 }
220
Madan Jampani28b2cd62016-02-02 10:49:52 -0800221 private <T> T complete(CompletableFuture<T> future) {
Madan Jampani7c521002015-03-23 12:23:01 -0700222 try {
Madan Jampani28b2cd62016-02-02 10:49:52 -0800223 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
Madan Jampani7c521002015-03-23 12:23:01 -0700224 } catch (InterruptedException e) {
225 Thread.currentThread().interrupt();
226 throw new ConsistentMapException.Interrupted();
227 } catch (TimeoutException e) {
Madan Jampani42755872016-09-06 19:31:06 -0700228 throw new ConsistentMapException.Timeout(name());
Madan Jampani7c521002015-03-23 12:23:01 -0700229 } catch (ExecutionException e) {
Madan Jampanif6c973a2016-01-30 22:20:48 -0800230 Throwables.propagateIfPossible(e.getCause());
231 throw new ConsistentMapException(e.getCause());
Madan Jampani7c521002015-03-23 12:23:01 -0700232 }
233 }
Madan Jampani7c521002015-03-23 12:23:01 -0700234}