blob: 6f7b548778b57d47aada6aae5a6fb9c495469bd3 [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
17package org.onosproject.store.consistent.impl;
18
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;
22import java.util.concurrent.CompletableFuture;
23import java.util.concurrent.ExecutionException;
24import java.util.concurrent.TimeUnit;
25import java.util.concurrent.TimeoutException;
Madan Jampani346d4f52015-05-04 11:09:39 -070026import java.util.function.BiFunction;
27import java.util.function.Function;
28import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070029import java.util.Set;
30
Madan Jampani7c521002015-03-23 12:23:01 -070031import org.onosproject.store.service.ConsistentMap;
32import org.onosproject.store.service.ConsistentMapException;
Madan Jampani50589ac2015-06-08 11:38:46 -070033import org.onosproject.store.service.MapEventListener;
Madan Jampani7c521002015-03-23 12:23:01 -070034import 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 */
43public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
44
45 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
46
Madan Jampani50589ac2015-06-08 11:38:46 -070047 private final DefaultAsyncConsistentMap<K, V> asyncMap;
Madan Jampanie1065222015-08-17 16:21:51 -070048 private Map<K, V> javaMap;
Madan Jampani7c521002015-03-23 12:23:01 -070049
Madan Jampani50589ac2015-06-08 11:38:46 -070050 public String name() {
51 return asyncMap.name();
52 }
53
54 public DefaultConsistentMap(DefaultAsyncConsistentMap<K, V> asyncMap) {
55 this.asyncMap = asyncMap;
Madan Jampani7c521002015-03-23 12:23:01 -070056 }
57
58 @Override
59 public int size() {
60 return complete(asyncMap.size());
61 }
62
63 @Override
64 public boolean isEmpty() {
65 return complete(asyncMap.isEmpty());
66 }
67
68 @Override
69 public boolean containsKey(K key) {
70 return complete(asyncMap.containsKey(key));
71 }
72
73 @Override
74 public boolean containsValue(V value) {
75 return complete(asyncMap.containsValue(value));
76 }
77
78 @Override
79 public Versioned<V> get(K key) {
80 return complete(asyncMap.get(key));
81 }
82
83 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -070084 public Versioned<V> computeIfAbsent(K key,
85 Function<? super K, ? extends V> mappingFunction) {
86 return complete(asyncMap.computeIfAbsent(key, mappingFunction));
87 }
88
89 @Override
90 public Versioned<V> computeIfPresent(K key,
91 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
92 return complete(asyncMap.computeIfPresent(key, remappingFunction));
93 }
94
95 @Override
96 public Versioned<V> compute(K key,
97 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
98 return complete(asyncMap.compute(key, remappingFunction));
99 }
100
101 @Override
102 public Versioned<V> computeIf(K key,
103 Predicate<? super V> condition,
104 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
105 return complete(asyncMap.computeIf(key, condition, remappingFunction));
106 }
107
108 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700109 public Versioned<V> put(K key, V value) {
110 return complete(asyncMap.put(key, value));
111 }
112
113 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700114 public Versioned<V> putAndGet(K key, V value) {
115 return complete(asyncMap.putAndGet(key, value));
116 }
117
118 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700119 public Versioned<V> remove(K key) {
120 return complete(asyncMap.remove(key));
121 }
122
123 @Override
124 public void clear() {
125 complete(asyncMap.clear());
126 }
127
128 @Override
129 public Set<K> keySet() {
130 return complete(asyncMap.keySet());
131 }
132
133 @Override
134 public Collection<Versioned<V>> values() {
135 return complete(asyncMap.values());
136 }
137
138 @Override
139 public Set<Entry<K, Versioned<V>>> entrySet() {
140 return complete(asyncMap.entrySet());
141 }
142
143 @Override
144 public Versioned<V> putIfAbsent(K key, V value) {
145 return complete(asyncMap.putIfAbsent(key, value));
146 }
147
148 @Override
149 public boolean remove(K key, V value) {
150 return complete(asyncMap.remove(key, value));
151 }
152
153 @Override
154 public boolean remove(K key, long version) {
155 return complete(asyncMap.remove(key, version));
156 }
157
158 @Override
159 public boolean replace(K key, V oldValue, V newValue) {
160 return complete(asyncMap.replace(key, oldValue, newValue));
161 }
162
163 @Override
164 public boolean replace(K key, long oldVersion, V newValue) {
165 return complete(asyncMap.replace(key, oldVersion, newValue));
166 }
167
168 private static <T> T complete(CompletableFuture<T> future) {
169 try {
170 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
171 } catch (InterruptedException e) {
172 Thread.currentThread().interrupt();
173 throw new ConsistentMapException.Interrupted();
174 } catch (TimeoutException e) {
175 throw new ConsistentMapException.Timeout();
176 } catch (ExecutionException e) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700177 if (e.getCause() instanceof ConsistentMapException) {
178 throw (ConsistentMapException) e.getCause();
179 } else {
180 throw new ConsistentMapException(e.getCause());
181 }
Madan Jampani7c521002015-03-23 12:23:01 -0700182 }
183 }
Madan Jampani50589ac2015-06-08 11:38:46 -0700184
185 @Override
186 public void addListener(MapEventListener<K, V> listener) {
187 asyncMap.addListener(listener);
188 }
189
190 @Override
191 public void removeListener(MapEventListener<K, V> listener) {
192 asyncMap.addListener(listener);
193 }
Madan Jampanie1065222015-08-17 16:21:51 -0700194
195 @Override
196 public Map<K, V> asJavaMap() {
197 synchronized (this) {
198 if (javaMap == null) {
199 javaMap = new ConsistentMapBackedJavaMap<>(this);
200 }
201 }
202 return javaMap;
203 }
Madan Jampani7c521002015-03-23 12:23:01 -0700204}