blob: d9876fdd8bfb74aafcebd44fdf9c297a06d29b4a [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 static com.google.common.base.Preconditions.*;
20
21import java.util.Collection;
22import java.util.Map;
23import java.util.Map.Entry;
24import java.util.concurrent.CompletableFuture;
25import java.util.stream.Collectors;
26import java.util.Set;
27
28import org.apache.commons.lang3.tuple.Pair;
29import org.onlab.util.HexString;
30import org.onosproject.store.service.AsyncConsistentMap;
31import org.onosproject.store.service.Serializer;
32import org.onosproject.store.service.Versioned;
33
34import com.google.common.cache.CacheBuilder;
35import com.google.common.cache.CacheLoader;
36import com.google.common.cache.LoadingCache;
37
38/**
39 * AsyncConsistentMap implementation that is backed by a Raft consensus
40 * based database.
41 *
42 * @param <K> type of key.
43 * @param <V> type of value.
44 */
45public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> {
46
47 private final String name;
48 private final DatabaseProxy<String, byte[]> proxy;
49 private final Serializer serializer;
50
51 private static final String ERROR_NULL_KEY = "Key cannot be null";
52 private static final String ERROR_NULL_VALUE = "Null values are not allowed";
53
54 private final LoadingCache<K, String> keyCache = CacheBuilder.newBuilder()
55 .softValues()
56 .build(new CacheLoader<K, String>() {
57
58 @Override
59 public String load(K key) {
60 return HexString.toHexString(serializer.encode(key));
61 }
62 });
63
64 protected K dK(String key) {
65 return serializer.decode(HexString.fromHexString(key));
66 }
67
68 public DefaultAsyncConsistentMap(String name,
69 DatabaseProxy<String, byte[]> proxy,
70 Serializer serializer) {
71 this.name = checkNotNull(name, "map name cannot be null");
72 this.proxy = checkNotNull(proxy, "database proxy cannot be null");
73 this.serializer = checkNotNull(serializer, "serializer cannot be null");
74 }
75
76 @Override
77 public CompletableFuture<Integer> size() {
78 return proxy.size(name);
79 }
80
81 @Override
82 public CompletableFuture<Boolean> isEmpty() {
83 return proxy.isEmpty(name);
84 }
85
86 @Override
87 public CompletableFuture<Boolean> containsKey(K key) {
88 checkNotNull(key, ERROR_NULL_KEY);
89 return proxy.containsKey(name, keyCache.getUnchecked(key));
90 }
91
92 @Override
93 public CompletableFuture<Boolean> containsValue(V value) {
94 checkNotNull(value, ERROR_NULL_VALUE);
95 return proxy.containsValue(name, serializer.encode(value));
96 }
97
98 @Override
99 public CompletableFuture<Versioned<V>> get(K key) {
100 checkNotNull(key, ERROR_NULL_KEY);
101 return proxy.get(name, keyCache.getUnchecked(key))
102 .thenApply(v -> v != null
103 ? new Versioned<>(serializer.decode(v.value()), v.version(), v.creationTime()) : null);
104 }
105
106 @Override
107 public CompletableFuture<Versioned<V>> put(K key, V value) {
108 checkNotNull(key, ERROR_NULL_KEY);
109 checkNotNull(value, ERROR_NULL_VALUE);
110 return proxy.put(name, keyCache.getUnchecked(key), serializer.encode(value))
111 .thenApply(v -> v != null
112 ? new Versioned<>(serializer.decode(v.value()), v.version(), v.creationTime()) : null);
113 }
114
115 @Override
116 public CompletableFuture<Versioned<V>> remove(K key) {
117 checkNotNull(key, ERROR_NULL_KEY);
118 return proxy.remove(name, keyCache.getUnchecked(key))
119 .thenApply(v -> v != null
120 ? new Versioned<>(serializer.decode(v.value()), v.version(), v.creationTime()) : null);
121 }
122
123 @Override
124 public CompletableFuture<Void> clear() {
125 return proxy.clear(name);
126 }
127
128 @Override
129 public CompletableFuture<Set<K>> keySet() {
130 return proxy.keySet(name)
131 .thenApply(s -> s
132 .stream()
133 .map(this::dK)
134 .collect(Collectors.toSet()));
135 }
136
137 @Override
138 public CompletableFuture<Collection<Versioned<V>>> values() {
139 return proxy.values(name).thenApply(c -> c
140 .stream()
141 .map(v -> new Versioned<V>(serializer.decode(v.value()), v.version(), v.creationTime()))
142 .collect(Collectors.toList()));
143 }
144
145 @Override
146 public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() {
147 return proxy.entrySet(name).thenApply(s -> s
148 .stream()
149 .map(this::fromRawEntry)
150 .collect(Collectors.toSet()));
151 }
152
153 @Override
154 public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) {
155 checkNotNull(key, ERROR_NULL_KEY);
156 checkNotNull(value, ERROR_NULL_VALUE);
157 return proxy.putIfAbsent(
158 name, keyCache.getUnchecked(key), serializer.encode(value)).thenApply(v ->
159 v != null ?
160 new Versioned<>(serializer.decode(v.value()), v.version(), v.creationTime()) : null);
161 }
162
163 @Override
164 public CompletableFuture<Boolean> remove(K key, V value) {
165 checkNotNull(key, ERROR_NULL_KEY);
166 checkNotNull(value, ERROR_NULL_VALUE);
167 return proxy.remove(name, keyCache.getUnchecked(key), serializer.encode(value));
168 }
169
170 @Override
171 public CompletableFuture<Boolean> remove(K key, long version) {
172 checkNotNull(key, ERROR_NULL_KEY);
173 return proxy.remove(name, keyCache.getUnchecked(key), version);
174
175 }
176
177 @Override
178 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
179 checkNotNull(key, ERROR_NULL_KEY);
180 checkNotNull(newValue, ERROR_NULL_VALUE);
181 byte[] existing = oldValue != null ? serializer.encode(oldValue) : null;
182 return proxy.replace(name, keyCache.getUnchecked(key), existing, serializer.encode(newValue));
183 }
184
185 @Override
186 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
187 checkNotNull(key, ERROR_NULL_KEY);
188 checkNotNull(newValue, ERROR_NULL_VALUE);
189 return proxy.replace(name, keyCache.getUnchecked(key), oldVersion, serializer.encode(newValue));
190 }
191
192 private Map.Entry<K, Versioned<V>> fromRawEntry(Map.Entry<String, Versioned<byte[]>> e) {
193 return Pair.of(
194 dK(e.getKey()),
195 new Versioned<>(
196 serializer.decode(e.getValue().value()),
197 e.getValue().version(),
198 e.getValue().creationTime()));
199 }
200}