blob: 309043698c9634e502e99cbc110fa560e09cc28b [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;
31
32import org.onosproject.cluster.PartitionId;
33import org.onosproject.store.service.AsyncConsistentMap;
34import org.onosproject.store.service.MapEventListener;
35import org.onosproject.store.service.Versioned;
36
37import com.google.common.collect.Lists;
38import com.google.common.collect.Maps;
39import com.google.common.collect.Sets;
40
41/**
42 * {@link AsyncConsistentMap} that has its entries partitioned horizontally across
43 * several {@link AsyncConsistentMap maps}.
44 *
45 * @param <K> key type
46 * @param <V> value type
47 */
48public class PartitionedAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> {
49
50 private final String name;
51 private final TreeMap<PartitionId, AsyncConsistentMap<K, V>> partitions = Maps.newTreeMap();
52 private final Hasher<K> keyHasher;
53
54 public PartitionedAsyncConsistentMap(String name,
55 Map<PartitionId, AsyncConsistentMap<K, V>> partitions,
56 Hasher<K> keyHasher) {
57 this.name = name;
58 this.partitions.putAll(checkNotNull(partitions));
59 this.keyHasher = checkNotNull(keyHasher);
60 }
61
62 @Override
63 public String name() {
64 return name;
65 }
66
67 @Override
68 public CompletableFuture<Integer> size() {
69 AtomicInteger totalSize = new AtomicInteger(0);
70 return CompletableFuture.allOf(getMaps()
71 .stream()
72 .map(map -> map.size().thenAccept(totalSize::addAndGet))
73 .toArray(CompletableFuture[]::new))
74 .thenApply(v -> totalSize.get());
75 }
76
77 @Override
78 public CompletableFuture<Boolean> isEmpty() {
79 return size().thenApply(size -> size == 0);
80 }
81
82 @Override
83 public CompletableFuture<Boolean> containsKey(K key) {
84 return getMap(key).containsKey(key);
85 }
86
87 @Override
88 public CompletableFuture<Boolean> containsValue(V value) {
89 AtomicBoolean contains = new AtomicBoolean(false);
90 return CompletableFuture.allOf(getMaps().stream()
91 .map(map -> map.containsValue(value)
92 .thenAccept(v -> contains.set(contains.get() || v)))
93 .toArray(CompletableFuture[]::new))
94 .thenApply(v -> contains.get());
95 }
96 @Override
97 public CompletableFuture<Versioned<V>> get(K key) {
98 return getMap(key).get(key);
99 }
100
101 @Override
102 public CompletableFuture<Versioned<V>> computeIf(K key,
103 Predicate<? super V> condition,
104 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
105 return getMap(key).computeIf(key, condition, remappingFunction);
106 }
107
108 @Override
109 public CompletableFuture<Versioned<V>> put(K key, V value) {
110 return getMap(key).put(key, value);
111 }
112
113 @Override
114 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
115 return getMap(key).putAndGet(key, value);
116 }
117
118 @Override
119 public CompletableFuture<Versioned<V>> remove(K key) {
120 return getMap(key).remove(key);
121 }
122
123 @Override
124 public CompletableFuture<Void> clear() {
125 return CompletableFuture.allOf(getMaps().stream()
126 .map(map -> map.clear())
127 .toArray(CompletableFuture[]::new));
128 }
129
130 @Override
131 public CompletableFuture<Set<K>> keySet() {
132 Set<K> allKeys = Sets.newConcurrentHashSet();
133 return CompletableFuture.allOf(getMaps().stream()
134 .map(map -> map.keySet().thenAccept(allKeys::addAll))
135 .toArray(CompletableFuture[]::new))
136 .thenApply(v -> allKeys);
137 }
138
139 @Override
140 public CompletableFuture<Collection<Versioned<V>>> values() {
141 List<Versioned<V>> allValues = Lists.newCopyOnWriteArrayList();
142 return CompletableFuture.allOf(getMaps().stream()
143 .map(map -> map.values().thenAccept(allValues::addAll))
144 .toArray(CompletableFuture[]::new))
145 .thenApply(v -> allValues);
146 }
147
148 @Override
149 public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() {
150 Set<Entry<K, Versioned<V>>> allEntries = Sets.newConcurrentHashSet();
151 return CompletableFuture.allOf(getMaps().stream()
152 .map(map -> map.entrySet().thenAccept(allEntries::addAll))
153 .toArray(CompletableFuture[]::new))
154 .thenApply(v -> allEntries);
155 }
156
157 @Override
158 public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) {
159 return getMap(key).putIfAbsent(key, value);
160 }
161
162 @Override
163 public CompletableFuture<Boolean> remove(K key, V value) {
164 return getMap(key).remove(key, value);
165 }
166
167 @Override
168 public CompletableFuture<Boolean> remove(K key, long version) {
169 return getMap(key).remove(key, version);
170 }
171
172 @Override
173 public CompletableFuture<Versioned<V>> replace(K key, V value) {
174 return getMap(key).replace(key, value);
175 }
176
177 @Override
178 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
179 return getMap(key).replace(key, oldValue, newValue);
180 }
181
182 @Override
183 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
184 return getMap(key).replace(key, oldVersion, newValue);
185 }
186
187 @Override
188 public CompletableFuture<Void> addListener(MapEventListener<K, V> listener) {
189 return CompletableFuture.allOf(getMaps().stream()
190 .map(map -> map.addListener(listener))
191 .toArray(CompletableFuture[]::new));
192 }
193
194 @Override
195 public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) {
196 return CompletableFuture.allOf(getMaps().stream()
197 .map(map -> map.removeListener(listener))
198 .toArray(CompletableFuture[]::new));
199 }
200
201 /**
202 * Returns the map (partition) to which the specified key maps.
203 * @param key key
204 * @return AsyncConsistentMap to which key maps
205 */
206 private AsyncConsistentMap<K, V> getMap(K key) {
207 return partitions.get(keyHasher.hash(key));
208 }
209
210 /**
211 * Returns all the constituent maps.
212 * @return collection of maps.
213 */
214 private Collection<AsyncConsistentMap<K, V>> getMaps() {
215 return partitions.values();
216 }
217}