blob: 0bc90e845620e13e9bb08e607e1e2ccc7c347ed6 [file] [log] [blame]
Madan Jampani10073672016-01-21 19:13:59 -08001/*
2 * Copyright 2016 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 */
16package org.onosproject.store.primitives.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.List;
22import java.util.Map;
23import java.util.Map.Entry;
24import java.util.Set;
25import java.util.TreeMap;
26import java.util.concurrent.CompletableFuture;
27import java.util.concurrent.atomic.AtomicBoolean;
28import java.util.concurrent.atomic.AtomicInteger;
29import java.util.function.BiFunction;
30import java.util.function.Predicate;
Madan Jampani74da78b2016-02-09 21:18:36 -080031import java.util.stream.Collectors;
Madan Jampani10073672016-01-21 19:13:59 -080032
Madan Jampani74da78b2016-02-09 21:18:36 -080033import org.onlab.util.Tools;
Madan Jampani10073672016-01-21 19:13:59 -080034import org.onosproject.cluster.PartitionId;
Madan Jampani74da78b2016-02-09 21:18:36 -080035import org.onosproject.store.primitives.MapUpdate;
36import org.onosproject.store.primitives.TransactionId;
Madan Jampani10073672016-01-21 19:13:59 -080037import org.onosproject.store.service.AsyncConsistentMap;
38import org.onosproject.store.service.MapEventListener;
Madan Jampani74da78b2016-02-09 21:18:36 -080039import org.onosproject.store.service.MapTransaction;
Madan Jampani10073672016-01-21 19:13:59 -080040import org.onosproject.store.service.Versioned;
41
42import com.google.common.collect.Lists;
43import com.google.common.collect.Maps;
44import com.google.common.collect.Sets;
45
46/**
47 * {@link AsyncConsistentMap} that has its entries partitioned horizontally across
48 * several {@link AsyncConsistentMap maps}.
49 *
50 * @param <K> key type
51 * @param <V> value type
52 */
53public class PartitionedAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> {
54
55 private final String name;
56 private final TreeMap<PartitionId, AsyncConsistentMap<K, V>> partitions = Maps.newTreeMap();
57 private final Hasher<K> keyHasher;
58
59 public PartitionedAsyncConsistentMap(String name,
60 Map<PartitionId, AsyncConsistentMap<K, V>> partitions,
61 Hasher<K> keyHasher) {
62 this.name = name;
63 this.partitions.putAll(checkNotNull(partitions));
64 this.keyHasher = checkNotNull(keyHasher);
65 }
66
67 @Override
68 public String name() {
69 return name;
70 }
71
72 @Override
73 public CompletableFuture<Integer> size() {
74 AtomicInteger totalSize = new AtomicInteger(0);
75 return CompletableFuture.allOf(getMaps()
76 .stream()
77 .map(map -> map.size().thenAccept(totalSize::addAndGet))
78 .toArray(CompletableFuture[]::new))
79 .thenApply(v -> totalSize.get());
80 }
81
82 @Override
83 public CompletableFuture<Boolean> isEmpty() {
84 return size().thenApply(size -> size == 0);
85 }
86
87 @Override
88 public CompletableFuture<Boolean> containsKey(K key) {
89 return getMap(key).containsKey(key);
90 }
91
92 @Override
93 public CompletableFuture<Boolean> containsValue(V value) {
94 AtomicBoolean contains = new AtomicBoolean(false);
95 return CompletableFuture.allOf(getMaps().stream()
96 .map(map -> map.containsValue(value)
97 .thenAccept(v -> contains.set(contains.get() || v)))
98 .toArray(CompletableFuture[]::new))
99 .thenApply(v -> contains.get());
100 }
101 @Override
102 public CompletableFuture<Versioned<V>> get(K key) {
103 return getMap(key).get(key);
104 }
105
106 @Override
107 public CompletableFuture<Versioned<V>> computeIf(K key,
108 Predicate<? super V> condition,
109 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
110 return getMap(key).computeIf(key, condition, remappingFunction);
111 }
112
113 @Override
114 public CompletableFuture<Versioned<V>> put(K key, V value) {
115 return getMap(key).put(key, value);
116 }
117
118 @Override
119 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
120 return getMap(key).putAndGet(key, value);
121 }
122
123 @Override
124 public CompletableFuture<Versioned<V>> remove(K key) {
125 return getMap(key).remove(key);
126 }
127
128 @Override
129 public CompletableFuture<Void> clear() {
130 return CompletableFuture.allOf(getMaps().stream()
131 .map(map -> map.clear())
132 .toArray(CompletableFuture[]::new));
133 }
134
135 @Override
136 public CompletableFuture<Set<K>> keySet() {
137 Set<K> allKeys = Sets.newConcurrentHashSet();
138 return CompletableFuture.allOf(getMaps().stream()
139 .map(map -> map.keySet().thenAccept(allKeys::addAll))
140 .toArray(CompletableFuture[]::new))
141 .thenApply(v -> allKeys);
142 }
143
144 @Override
145 public CompletableFuture<Collection<Versioned<V>>> values() {
146 List<Versioned<V>> allValues = Lists.newCopyOnWriteArrayList();
147 return CompletableFuture.allOf(getMaps().stream()
148 .map(map -> map.values().thenAccept(allValues::addAll))
149 .toArray(CompletableFuture[]::new))
150 .thenApply(v -> allValues);
151 }
152
153 @Override
154 public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() {
155 Set<Entry<K, Versioned<V>>> allEntries = Sets.newConcurrentHashSet();
156 return CompletableFuture.allOf(getMaps().stream()
157 .map(map -> map.entrySet().thenAccept(allEntries::addAll))
158 .toArray(CompletableFuture[]::new))
159 .thenApply(v -> allEntries);
160 }
161
162 @Override
163 public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) {
164 return getMap(key).putIfAbsent(key, value);
165 }
166
167 @Override
168 public CompletableFuture<Boolean> remove(K key, V value) {
169 return getMap(key).remove(key, value);
170 }
171
172 @Override
173 public CompletableFuture<Boolean> remove(K key, long version) {
174 return getMap(key).remove(key, version);
175 }
176
177 @Override
178 public CompletableFuture<Versioned<V>> replace(K key, V value) {
179 return getMap(key).replace(key, value);
180 }
181
182 @Override
183 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
184 return getMap(key).replace(key, oldValue, newValue);
185 }
186
187 @Override
188 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
189 return getMap(key).replace(key, oldVersion, newValue);
190 }
191
192 @Override
193 public CompletableFuture<Void> addListener(MapEventListener<K, V> listener) {
194 return CompletableFuture.allOf(getMaps().stream()
195 .map(map -> map.addListener(listener))
196 .toArray(CompletableFuture[]::new));
197 }
198
199 @Override
200 public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) {
201 return CompletableFuture.allOf(getMaps().stream()
202 .map(map -> map.removeListener(listener))
203 .toArray(CompletableFuture[]::new));
204 }
205
Madan Jampani74da78b2016-02-09 21:18:36 -0800206 @Override
207 public CompletableFuture<Boolean> prepare(MapTransaction<K, V> transaction) {
208
209 Map<AsyncConsistentMap<K, V>, List<MapUpdate<K, V>>> updatesGroupedByMap = Maps.newIdentityHashMap();
210 transaction.updates().forEach(update -> {
211 AsyncConsistentMap<K, V> map = getMap(update.key());
212 updatesGroupedByMap.computeIfAbsent(map, k -> Lists.newLinkedList()).add(update);
213 });
214 Map<AsyncConsistentMap<K, V>, MapTransaction<K, V>> transactionsByMap =
215 Maps.transformValues(updatesGroupedByMap,
216 list -> new MapTransaction<>(transaction.transactionId(), list));
217
218 return Tools.allOf(transactionsByMap.entrySet()
219 .stream()
220 .map(e -> e.getKey().prepare(e.getValue()))
221 .collect(Collectors.toList()))
222 .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true));
223 }
224
225 @Override
226 public CompletableFuture<Void> commit(TransactionId transactionId) {
227 return CompletableFuture.allOf(getMaps().stream()
228 .map(e -> e.commit(transactionId))
229 .toArray(CompletableFuture[]::new));
230 }
231
232 @Override
233 public CompletableFuture<Void> rollback(TransactionId transactionId) {
234 return CompletableFuture.allOf(getMaps().stream()
235 .map(e -> e.rollback(transactionId))
236 .toArray(CompletableFuture[]::new));
237 }
238
Madan Jampani542d9e22016-04-05 15:39:55 -0700239 @Override
240 public CompletableFuture<Boolean> prepareAndCommit(MapTransaction<K, V> transaction) {
241 Map<AsyncConsistentMap<K, V>, List<MapUpdate<K, V>>> updatesGroupedByMap = Maps.newIdentityHashMap();
242 transaction.updates().forEach(update -> {
243 AsyncConsistentMap<K, V> map = getMap(update.key());
244 updatesGroupedByMap.computeIfAbsent(map, k -> Lists.newLinkedList()).add(update);
245 });
246 Map<AsyncConsistentMap<K, V>, MapTransaction<K, V>> transactionsByMap =
247 Maps.transformValues(updatesGroupedByMap,
248 list -> new MapTransaction<>(transaction.transactionId(), list));
249
250 return Tools.allOf(transactionsByMap.entrySet()
251 .stream()
252 .map(e -> e.getKey().prepareAndCommit(e.getValue()))
253 .collect(Collectors.toList()))
254 .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true));
255 }
256
Madan Jampani10073672016-01-21 19:13:59 -0800257 /**
258 * Returns the map (partition) to which the specified key maps.
259 * @param key key
260 * @return AsyncConsistentMap to which key maps
261 */
262 private AsyncConsistentMap<K, V> getMap(K key) {
263 return partitions.get(keyHasher.hash(key));
264 }
265
266 /**
267 * Returns all the constituent maps.
268 * @return collection of maps.
269 */
270 private Collection<AsyncConsistentMap<K, V>> getMaps() {
271 return partitions.values();
272 }
273}