blob: e1af47ac74883ac63a615b2ef9fadd2b34658db3 [file] [log] [blame]
Madan Jampani7c521002015-03-23 12:23:01 -07001/*
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
Madan Jampanif4c88502016-01-21 12:35:36 -080017package org.onosproject.store.primitives.impl;
Madan Jampani7c521002015-03-23 12:23:01 -070018
19import java.util.Collection;
Madan Jampanie1065222015-08-17 16:21:51 -070020import java.util.Map;
Madan Jampani7c521002015-03-23 12:23:01 -070021import java.util.Map.Entry;
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;
25import java.util.concurrent.TimeUnit;
26import java.util.concurrent.TimeoutException;
Madan Jampani346d4f52015-05-04 11:09:39 -070027import java.util.function.BiFunction;
28import java.util.function.Function;
29import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070030
Madan Jampania090a112016-01-18 16:38:17 -080031import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampani7c521002015-03-23 12:23:01 -070032import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.ConsistentMapException;
Madan Jampani50589ac2015-06-08 11:38:46 -070034import org.onosproject.store.service.MapEventListener;
Madan Jampania090a112016-01-18 16:38:17 -080035import org.onosproject.store.service.Synchronous;
Madan Jampani7c521002015-03-23 12:23:01 -070036import org.onosproject.store.service.Versioned;
37
38/**
Madan Jampanifa242182016-01-22 13:42:54 -080039 * Default implementation of {@code ConsistentMap}.
Madan Jampani7c521002015-03-23 12:23:01 -070040 *
41 * @param <K> type of key.
42 * @param <V> type of value.
43 */
Madan Jampania090a112016-01-18 16:38:17 -080044public class DefaultConsistentMap<K, V> extends Synchronous<AsyncConsistentMap<K, V>> implements ConsistentMap<K, V> {
Madan Jampani7c521002015-03-23 12:23:01 -070045
46 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
47
Madan Jampanifa242182016-01-22 13:42:54 -080048 private final AsyncConsistentMap<K, V> asyncMap;
Madan Jampanie1065222015-08-17 16:21:51 -070049 private Map<K, V> javaMap;
Madan Jampani7c521002015-03-23 12:23:01 -070050
Madan Jampanifa242182016-01-22 13:42:54 -080051 public DefaultConsistentMap(AsyncConsistentMap<K, V> asyncMap) {
Madan Jampania090a112016-01-18 16:38:17 -080052 super(asyncMap);
Madan Jampani50589ac2015-06-08 11:38:46 -070053 this.asyncMap = asyncMap;
Madan Jampani7c521002015-03-23 12:23:01 -070054 }
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 Jampani346d4f52015-05-04 11:09:39 -070082 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 Jampani7c521002015-03-23 12:23:01 -0700107 public Versioned<V> put(K key, V value) {
108 return complete(asyncMap.put(key, value));
109 }
110
111 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700112 public Versioned<V> putAndGet(K key, V value) {
113 return complete(asyncMap.putAndGet(key, value));
114 }
115
116 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700117 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
Jihwan Kim9887ad92015-12-12 00:23:57 +0900157 public Versioned<V> replace(K key, V value) {
158 return complete(asyncMap.replace(key, value));
159 }
160
161 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700162 public boolean replace(K key, V oldValue, V newValue) {
163 return complete(asyncMap.replace(key, oldValue, newValue));
164 }
165
166 @Override
167 public boolean replace(K key, long oldVersion, V newValue) {
168 return complete(asyncMap.replace(key, oldVersion, newValue));
169 }
170
Madan Jampanifa242182016-01-22 13:42:54 -0800171 @Override
172 public void addListener(MapEventListener<K, V> listener) {
173 complete(asyncMap.addListener(listener));
174 }
175
176 @Override
177 public void removeListener(MapEventListener<K, V> listener) {
178 complete(asyncMap.addListener(listener));
179 }
180
181 @Override
182 public Map<K, V> asJavaMap() {
183 synchronized (this) {
184 if (javaMap == null) {
185 javaMap = new ConsistentMapBackedJavaMap<>(this);
186 }
187 }
188 return javaMap;
189 }
190
Madan Jampani7c521002015-03-23 12:23:01 -0700191 private static <T> T complete(CompletableFuture<T> future) {
192 try {
193 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
194 } catch (InterruptedException e) {
195 Thread.currentThread().interrupt();
196 throw new ConsistentMapException.Interrupted();
197 } catch (TimeoutException e) {
198 throw new ConsistentMapException.Timeout();
199 } catch (ExecutionException e) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700200 if (e.getCause() instanceof ConsistentMapException) {
201 throw (ConsistentMapException) e.getCause();
202 } else {
203 throw new ConsistentMapException(e.getCause());
204 }
Madan Jampani7c521002015-03-23 12:23:01 -0700205 }
206 }
Madan Jampani7c521002015-03-23 12:23:01 -0700207}