blob: b85dfa2e586efec40e103f02a3555a75a0b460d0 [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;
Madan Jampani346d4f52015-05-04 11:09:39 -070021import java.util.Optional;
Madan Jampani7c521002015-03-23 12:23:01 -070022import 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
31import org.onosproject.store.service.AsyncConsistentMap;
32import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.ConsistentMapException;
34import org.onosproject.store.service.Serializer;
35import org.onosproject.store.service.Versioned;
36
37/**
38 * ConsistentMap implementation that is backed by a Raft consensus
39 * based database.
40 *
41 * @param <K> type of key.
42 * @param <V> type of value.
43 */
44public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
45
46 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
47
48 private final AsyncConsistentMap<K, V> asyncMap;
49
50 public DefaultConsistentMap(String name,
Madan Jampanif1b8e172015-03-23 11:42:02 -070051 Database database,
Madan Jampani02b7fb82015-05-01 13:01:20 -070052 Serializer serializer,
53 boolean readOnly) {
54 asyncMap = new DefaultAsyncConsistentMap<>(name, database, serializer, readOnly);
Madan Jampani7c521002015-03-23 12:23:01 -070055 }
56
57 @Override
58 public int size() {
59 return complete(asyncMap.size());
60 }
61
62 @Override
63 public boolean isEmpty() {
64 return complete(asyncMap.isEmpty());
65 }
66
67 @Override
68 public boolean containsKey(K key) {
69 return complete(asyncMap.containsKey(key));
70 }
71
72 @Override
73 public boolean containsValue(V value) {
74 return complete(asyncMap.containsValue(value));
75 }
76
77 @Override
78 public Versioned<V> get(K key) {
79 return complete(asyncMap.get(key));
80 }
81
82 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -070083 public Versioned<V> computeIfAbsent(K key,
84 Function<? super K, ? extends V> mappingFunction) {
85 return complete(asyncMap.computeIfAbsent(key, mappingFunction));
86 }
87
88 @Override
89 public Versioned<V> computeIfPresent(K key,
90 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
91 return complete(asyncMap.computeIfPresent(key, remappingFunction));
92 }
93
94 @Override
95 public Versioned<V> compute(K key,
96 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
97 return complete(asyncMap.compute(key, remappingFunction));
98 }
99
100 @Override
101 public Versioned<V> computeIf(K key,
102 Predicate<? super V> condition,
103 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
104 return complete(asyncMap.computeIf(key, condition, remappingFunction));
105 }
106
107 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700108 public Versioned<V> put(K key, V value) {
109 return complete(asyncMap.put(key, value));
110 }
111
112 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700113 public Versioned<V> putAndGet(K key, V value) {
114 return complete(asyncMap.putAndGet(key, value));
115 }
116
117 @Override
118 public Optional<Versioned<V>> putIfAbsentAndGet(K key, V value) {
119 return complete(asyncMap.putIfAbsentAndGet(key, value));
120 }
121
122 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700123 public Versioned<V> remove(K key) {
124 return complete(asyncMap.remove(key));
125 }
126
127 @Override
128 public void clear() {
129 complete(asyncMap.clear());
130 }
131
132 @Override
133 public Set<K> keySet() {
134 return complete(asyncMap.keySet());
135 }
136
137 @Override
138 public Collection<Versioned<V>> values() {
139 return complete(asyncMap.values());
140 }
141
142 @Override
143 public Set<Entry<K, Versioned<V>>> entrySet() {
144 return complete(asyncMap.entrySet());
145 }
146
147 @Override
148 public Versioned<V> putIfAbsent(K key, V value) {
149 return complete(asyncMap.putIfAbsent(key, value));
150 }
151
152 @Override
153 public boolean remove(K key, V value) {
154 return complete(asyncMap.remove(key, value));
155 }
156
157 @Override
158 public boolean remove(K key, long version) {
159 return complete(asyncMap.remove(key, version));
160 }
161
162 @Override
163 public boolean replace(K key, V oldValue, V newValue) {
164 return complete(asyncMap.replace(key, oldValue, newValue));
165 }
166
167 @Override
168 public boolean replace(K key, long oldVersion, V newValue) {
169 return complete(asyncMap.replace(key, oldVersion, newValue));
170 }
171
Madan Jampani346d4f52015-05-04 11:09:39 -0700172 @Override
173 public Optional<Versioned<V>> replaceAndGet(K key, long oldVersion, V newValue) {
174 return complete(asyncMap.replaceAndGet(key, oldVersion, newValue));
175 }
176
Madan Jampani7c521002015-03-23 12:23:01 -0700177 private static <T> T complete(CompletableFuture<T> future) {
178 try {
179 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
180 } catch (InterruptedException e) {
181 Thread.currentThread().interrupt();
182 throw new ConsistentMapException.Interrupted();
183 } catch (TimeoutException e) {
184 throw new ConsistentMapException.Timeout();
185 } catch (ExecutionException e) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700186 if (e.getCause() instanceof ConsistentMapException) {
187 throw (ConsistentMapException) e.getCause();
188 } else {
189 throw new ConsistentMapException(e.getCause());
190 }
Madan Jampani7c521002015-03-23 12:23:01 -0700191 }
192 }
193}