blob: d6a657cf65ebc7cbf80694271b3b2cdfbaf911c0 [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;
20import java.util.Map.Entry;
21import java.util.concurrent.CompletableFuture;
22import java.util.concurrent.ExecutionException;
23import java.util.concurrent.TimeUnit;
24import java.util.concurrent.TimeoutException;
Madan Jampani346d4f52015-05-04 11:09:39 -070025import java.util.function.BiFunction;
26import java.util.function.Function;
27import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070028import java.util.Set;
29
Madan Jampani7c521002015-03-23 12:23:01 -070030import org.onosproject.store.service.ConsistentMap;
31import org.onosproject.store.service.ConsistentMapException;
Madan Jampani50589ac2015-06-08 11:38:46 -070032import org.onosproject.store.service.MapEventListener;
Madan Jampani7c521002015-03-23 12:23:01 -070033import 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 */
42public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
43
44 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
45
Madan Jampani50589ac2015-06-08 11:38:46 -070046 private final DefaultAsyncConsistentMap<K, V> asyncMap;
Madan Jampani7c521002015-03-23 12:23:01 -070047
Madan Jampani50589ac2015-06-08 11:38:46 -070048 public String name() {
49 return asyncMap.name();
50 }
51
52 public DefaultConsistentMap(DefaultAsyncConsistentMap<K, V> asyncMap) {
53 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
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 Jampani346d4f52015-05-04 11:09:39 -0700175 if (e.getCause() instanceof ConsistentMapException) {
176 throw (ConsistentMapException) e.getCause();
177 } else {
178 throw new ConsistentMapException(e.getCause());
179 }
Madan Jampani7c521002015-03-23 12:23:01 -0700180 }
181 }
Madan Jampani50589ac2015-06-08 11:38:46 -0700182
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 Jampani7c521002015-03-23 12:23:01 -0700192}