blob: 30a8d6756539fea6c8d6afc3c1a756ffb93f13b7 [file] [log] [blame]
Aaron Kruglikov47047b22016-03-31 16:52:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikov47047b22016-03-31 16:52:48 -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 */
16
17package org.onosproject.store.primitives;
18
19import com.google.common.base.Throwables;
Madan Jampani0463cf92016-05-04 14:46:08 -070020
Aaron Kruglikov47047b22016-03-31 16:52:48 -070021import org.onosproject.store.service.ConsistentMapException;
22import org.onosproject.store.service.AsyncConsistentTreeMap;
23import org.onosproject.store.service.MapEventListener;
24import org.onosproject.store.service.Synchronous;
25import org.onosproject.store.service.ConsistentTreeMap;
26import org.onosproject.store.service.Versioned;
27
28import java.util.Collection;
29import java.util.Map;
Aaron Kruglikov6a164352016-07-25 11:46:17 -070030import java.util.NavigableMap;
Aaron Kruglikov47047b22016-03-31 16:52:48 -070031import java.util.NavigableSet;
32import java.util.Set;
33import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutionException;
Madan Jampani0463cf92016-05-04 14:46:08 -070035import java.util.concurrent.Executor;
Aaron Kruglikov47047b22016-03-31 16:52:48 -070036import java.util.concurrent.TimeUnit;
37import java.util.concurrent.TimeoutException;
38import java.util.function.BiFunction;
39import java.util.function.Function;
40import java.util.function.Predicate;
41
42/**
43 * Implementation of the {@link ConsistentTreeMap} interface.
44 */
Aaron Kruglikov6a164352016-07-25 11:46:17 -070045public class DefaultConsistentTreeMap<V>
46 extends Synchronous<AsyncConsistentTreeMap<V>>
47 implements ConsistentTreeMap<V> {
Aaron Kruglikov6a164352016-07-25 11:46:17 -070048 private final AsyncConsistentTreeMap<V> treeMap;
Aaron Kruglikov47047b22016-03-31 16:52:48 -070049 private final long operationTimeoutMillis;
Aaron Kruglikov6a164352016-07-25 11:46:17 -070050 private Map<String, V> javaMap;
Aaron Kruglikov47047b22016-03-31 16:52:48 -070051
Aaron Kruglikov6a164352016-07-25 11:46:17 -070052 public DefaultConsistentTreeMap(AsyncConsistentTreeMap<V> treeMap,
53 long operationTimeoutMillis) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070054 super(treeMap);
55 this.treeMap = treeMap;
56 this.operationTimeoutMillis = operationTimeoutMillis;
57 }
58
59 private <T> T complete(CompletableFuture<T> future) {
60 try {
61 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
62 } catch (InterruptedException e) {
63 Thread.currentThread().interrupt();
64 throw new ConsistentMapException.Interrupted();
65 } catch (ExecutionException e) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070066 Throwables.propagateIfPossible(e.getCause());
67 throw new ConsistentMapException(e.getCause());
Yuta HIGUCHIf13b3df2016-07-25 12:13:22 -070068 } catch (TimeoutException e) {
69 throw new ConsistentMapException.Timeout();
Aaron Kruglikov47047b22016-03-31 16:52:48 -070070 }
71 }
72
73 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -070074 public String firstKey() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070075 return complete(treeMap.firstKey());
76 }
77
78 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -070079 public String lastKey() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070080 return complete(treeMap.lastKey());
81 }
82
83 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -070084 public Map.Entry<String, Versioned<V>> ceilingEntry(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070085 return complete(treeMap.ceilingEntry(key));
86 }
87
88 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -070089 public Map.Entry<String, Versioned<V>> floorEntry(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070090 return complete(treeMap.floorEntry(key));
91 }
92
93 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -070094 public Map.Entry<String, Versioned<V>> higherEntry(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -070095 return complete(treeMap.higherEntry(key));
96 }
97
98 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -070099 public Map.Entry<String, Versioned<V>> lowerEntry(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700100 return complete(treeMap.lowerEntry(key));
101 }
102
103 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700104 public Map.Entry<String, Versioned<V>> firstEntry() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700105 return complete(treeMap.firstEntry());
106 }
107
108 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700109 public Map.Entry<String, Versioned<V>> lastEntry() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700110 return complete(treeMap.lastEntry());
111 }
112
113 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700114 public Map.Entry<String, Versioned<V>> pollFirstEntry() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700115 return complete(treeMap.pollFirstEntry());
116 }
117
118 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700119 public Map.Entry<String, Versioned<V>> pollLastEntry() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700120 return complete(treeMap.pollLastEntry());
121 }
122
123 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700124 public String lowerKey(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700125 return complete(treeMap.lowerKey(key));
126 }
127
128 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700129 public String floorKey(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700130 return complete(treeMap.floorKey(key));
131 }
132
133 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700134 public String ceilingKey(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700135 return complete(treeMap.ceilingKey(key));
136 }
137
138 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700139 public String higherKey(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700140 return complete(treeMap.higherKey(key));
141 }
142
143
144 @Override
145 /**
146 * {@inheritDoc}
147 * <p>This may be a long operation with greater risk of timeout.</p>
148 */
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700149 public NavigableSet<String> navigableKeySet() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700150 return complete(treeMap.navigableKeySet());
151 }
152
153 @Override
154 public int size() {
155 return complete(treeMap.size());
156 }
157
158 @Override
159 public boolean isEmpty() {
160 return complete(treeMap.isEmpty());
161 }
162
163 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700164 public boolean containsKey(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700165 return complete(treeMap.containsKey(key));
166 }
167
168 @Override
169 public boolean containsValue(V value) {
170 return complete(treeMap.containsValue(value));
171 }
172
173 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700174 public Versioned<V> get(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700175 return complete(treeMap.get(key));
176 }
177
178 @Override
Jordan Haltermanf6272442017-04-20 02:18:08 -0700179 public Versioned<V> getOrDefault(String key, V defaultValue) {
180 return complete(treeMap.getOrDefault(key, defaultValue));
181 }
182
183 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700184 public Versioned<V> computeIfAbsent(String key,
185 Function<? super String,
186 ? extends V> mappingFunction) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700187 return complete(treeMap.computeIfAbsent(key, mappingFunction));
188 }
189
190 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700191 public Versioned<V> compute(String key,
192 BiFunction<? super String,
193 ? super V,
194 ? extends V> remappingFunction) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700195 return complete(treeMap.compute(key, remappingFunction));
196 }
197
198 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700199 public Versioned<V> computeIfPresent(
200 String key,
201 BiFunction<? super String,
202 ? super V,
203 ? extends V> remappingFunction) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700204 return complete(treeMap.computeIfPresent(key, remappingFunction));
205 }
206
207 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700208 public Versioned<V> computeIf(String key, Predicate<? super V> condition,
209 BiFunction<? super String,
210 ? super V,
211 ? extends V> remappingFunction) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700212 return complete(treeMap.computeIf(key, condition, remappingFunction));
213 }
214
215 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700216 public Versioned<V> put(String key, V value) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700217 return complete(treeMap.put(key, value));
218 }
219
220 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700221 public Versioned<V> putAndGet(String key, V value) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700222 return complete(treeMap.putAndGet(key, value));
223 }
224
225 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700226 public Versioned<V> remove(String key) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700227 return complete(treeMap.remove(key));
228 }
229
230 @Override
231 public void clear() {
232 complete(treeMap.clear());
233 }
234
235 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700236 public Set<String> keySet() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700237 return complete(treeMap.keySet());
238 }
239
240 @Override
241 public Collection<Versioned<V>> values() {
242 return complete(treeMap.values());
243 }
244
245 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700246 public Set<Map.Entry<String, Versioned<V>>> entrySet() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700247 return complete(treeMap.entrySet());
248 }
249
250 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700251 public Versioned<V> putIfAbsent(String key, V value) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700252 return complete(treeMap.putIfAbsent(key, value));
253 }
254
255 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700256 public boolean remove(String key, V value) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700257 return complete(treeMap.remove(key, value));
258 }
259
260 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700261 public boolean remove(String key, long version) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700262 return complete(treeMap.remove(key, version));
263 }
264
265 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700266 public Versioned<V> replace(String key, V value) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700267 return complete(treeMap.replace(key, value));
268 }
269
270 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700271 public boolean replace(String key, V oldValue, V newValue) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700272 return complete(treeMap.replace(key, oldValue, newValue));
273 }
274
275 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700276 public boolean replace(String key, long oldVersion, V newValue) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700277 return complete(treeMap.replace(key, oldVersion, newValue));
278 }
279
280 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700281 public void addListener(MapEventListener<String, V> listener,
282 Executor executor) {
Madan Jampani0463cf92016-05-04 14:46:08 -0700283 complete(treeMap.addListener(listener, executor));
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700284 }
285
286 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700287 public void removeListener(MapEventListener<String, V> listener) {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700288 complete(treeMap.removeListener(listener));
289 }
290
291 @Override
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700292 public Map<String, V> asJavaMap() {
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700293 synchronized (this) {
294 if (javaMap == null) {
295 javaMap = new ConsistentMapBackedJavaMap<>(this);
296 }
297 }
298 return javaMap;
299 }
Aaron Kruglikov6a164352016-07-25 11:46:17 -0700300
301 @Override
302 public NavigableMap<String, V> subMap(String upperKey,
303 String lowerKey,
304 boolean inclusiveUpper,
305 boolean inclusiveLower) {
306 return complete(treeMap.subMap(upperKey, lowerKey,
307 inclusiveUpper, inclusiveLower));
308 }
Aaron Kruglikov47047b22016-03-31 16:52:48 -0700309}