blob: 9b209ba5438311ec10786406dc77aabc48ab54ee [file] [log] [blame]
Madan Jampani551d0d22016-02-01 12:51:48 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani551d0d22016-02-01 12:51:48 -08003 *
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
Jordan Halterman400bbe52018-04-05 23:07:47 -070018import java.util.function.BiFunction;
19import java.util.function.Function;
20
Jordan Halterman45008172018-03-19 16:40:31 -070021import org.onosproject.core.Version;
Jordan Haltermanc955df72017-02-04 20:43:28 -080022import org.onosproject.store.service.AsyncAtomicCounterMap;
Jordan Halterman400bbe52018-04-05 23:07:47 -070023import org.onosproject.store.service.AsyncAtomicValue;
Madan Jampani551d0d22016-02-01 12:51:48 -080024import org.onosproject.store.service.AsyncConsistentMap;
Aaron Kruglikov61582a02016-09-06 13:18:58 -070025import org.onosproject.store.service.AsyncConsistentMultimap;
Aaron Kruglikoved88ff62016-08-01 16:02:09 -070026import org.onosproject.store.service.AsyncConsistentTreeMap;
Madan Jampani551d0d22016-02-01 12:51:48 -080027import org.onosproject.store.service.AsyncDistributedSet;
Jordan Halterman8d8da592017-08-28 14:45:19 -070028import org.onosproject.store.service.AsyncDocumentTree;
Madan Jampani551d0d22016-02-01 12:51:48 -080029
30/**
31 * Misc utilities for working with {@code DistributedPrimitive}s.
32 */
33public final class DistributedPrimitives {
34
35 private DistributedPrimitives() {}
36
37 /**
38 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
39 *
40 * @param map backing map
41 * @return set
42 * @param <E> set element type
43 */
44 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
45 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
46 }
47
48 /**
49 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
50 *
51 * @param map map whose operations are to be metered
52 * @return metered map
53 * @param <K> map key type
54 * @param <V> map value type
55 */
56 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
57 return new MeteredAsyncConsistentMap<>(map);
58 }
59
60 /**
61 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
62 *
63 * @param map backing map
64 * @return caching map
65 * @param <K> map key type
66 * @param <V> map value type
67 */
68 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
69 return new CachingAsyncConsistentMap<>(map);
70 }
71
72 /**
73 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
74 *
75 * @param map backing map
76 * @return unmodifiable map
77 * @param <K> map key type
78 * @param <V> map value type
79 */
80 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
81 return new UnmodifiableAsyncConsistentMap<>(map);
82 }
83
84 /**
Jordan Halterman4922a062017-07-31 15:55:36 -070085 * Creates an instance of {@code AsyncConsistentMap} that disallows null values.
86 *
87 * @param map backing map
88 * @return not null map
89 * @param <K> map key type
90 * @param <V> map value type
91 */
92 public static <K, V> AsyncConsistentMap<K, V> newNotNullMap(AsyncConsistentMap<K, V> map) {
93 return new NotNullAsyncConsistentMap<>(map);
94 }
95
96 /**
Jordan Halterman45008172018-03-19 16:40:31 -070097 * Creates an instance of {@code AsyncConsistentMap} that converts values from other versions.
98 *
99 * @param map backing map
100 * @param compatibilityFunction the compatibility function
101 * @param version local node version
102 * @param <K> map key type
103 * @param <V> map value type
104 * @return compatible map
105 */
106 public static <K, V> AsyncConsistentMap<K, V> newCompatibleMap(
107 AsyncConsistentMap<K, CompatibleValue<V>> map,
108 BiFunction<V, Version, V> compatibilityFunction,
109 Version version) {
110 Function<V, CompatibleValue<V>> encoder = value -> new CompatibleValue<>(value, version);
111 Function<CompatibleValue<V>, V> decoder = value -> {
112 if (!value.version().equals(version)) {
113 return compatibilityFunction.apply(value.value(), value.version());
114 }
115 return value.value();
116 };
117 return new TranscodingAsyncConsistentMap<>(map, k -> k, k -> k, encoder, decoder);
118 }
119
120 /**
Jordan Halterman400bbe52018-04-05 23:07:47 -0700121 * Creates an instance of {@code AsyncAtomicValue} that transforms value types.
122 *
123 * @param value backing value
124 * @param valueEncoder transformer for value type of returned value to value type of input value
125 * @param valueDecoder transformer for value type of input value to value type of returned value
126 * @param <V1> returned value type
127 * @param <V2> input value type
128 * @return new counter map
129 */
130 public static <V1, V2> AsyncAtomicValue<V1> newTranscodingAtomicValue(AsyncAtomicValue<V2> value,
131 Function<V1, V2> valueEncoder,
132 Function<V2, V1> valueDecoder) {
133 return new TranscodingAsyncAtomicValue<>(value, valueEncoder, valueDecoder);
134 }
135
136 /**
137 * Creates an instance of {@code AsyncAtomicValue} that converts values from other versions.
138 *
139 * @param atomicValue backing value
140 * @param compatibilityFunction the compatibility function
141 * @param version local node version
142 * @param <V> value type
143 * @return compatible map
144 */
145 public static <V> AsyncAtomicValue<V> newCompatibleAtomicValue(
146 AsyncAtomicValue<CompatibleValue<V>> atomicValue,
147 BiFunction<V, Version, V> compatibilityFunction,
148 Version version) {
149 Function<V, CompatibleValue<V>> encoder = value -> new CompatibleValue<>(value, version);
150 Function<CompatibleValue<V>, V> decoder = value -> {
151 if (!value.version().equals(version)) {
152 return compatibilityFunction.apply(value.value(), value.version());
153 }
154 return value.value();
155 };
156 return new TranscodingAsyncAtomicValue<>(atomicValue, encoder, decoder);
157 }
158
159 /**
Jordan Haltermanc955df72017-02-04 20:43:28 -0800160 * Creates an instance of {@code AsyncAtomicCounterMap} that transforms key types.
161 *
162 * @param map backing map
163 * @param keyEncoder transformer for key type of returned map to key type of input map
164 * @param keyDecoder transformer for key type of input map to key type of returned map
165 * @param <K1> returned map key type
166 * @param <K2> input map key type
167 * @return new counter map
168 */
169 public static <K1, K2> AsyncAtomicCounterMap<K1> newTranscodingAtomicCounterMap(AsyncAtomicCounterMap<K2> map,
Jordan Halterman400bbe52018-04-05 23:07:47 -0700170 Function<K1, K2> keyEncoder,
171 Function<K2, K1> keyDecoder) {
Jordan Haltermanc955df72017-02-04 20:43:28 -0800172 return new TranscodingAsyncAtomicCounterMap<>(map, keyEncoder, keyDecoder);
173 }
174
175 /**
Madan Jampani551d0d22016-02-01 12:51:48 -0800176 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
177 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
178 *
179 * @param map backing map
180 * @param keyEncoder transformer for key type of returned map to key type of input map
181 * @param keyDecoder transformer for key type of input map to key type of returned map
182 * @param valueEncoder transformer for value type of returned map to value type of input map
183 * @param valueDecoder transformer for value type of input map to value type of returned map
184 * @param <K1> returned map key type
185 * @param <K2> input map key type
186 * @param <V1> returned map value type
187 * @param <V2> input map key type
188 * @return new map
189 */
190 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
191 Function<K1, K2> keyEncoder,
192 Function<K2, K1> keyDecoder,
193 Function<V1, V2> valueEncoder,
194 Function<V2, V1> valueDecoder) {
195 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
196 keyEncoder,
197 keyDecoder,
198 valueEncoder,
199 valueDecoder);
200 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700201
202 /**
203 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
204 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
205 *
206 * @param map backing map
207 * @param valueEncoder transformer for value type of returned map to value type of input map
208 * @param valueDecoder transformer for value type of input map to value type of returned map
209 * @param <V1> returned map value type
210 * @param <V2> input map key type
211 * @return new map
212 */
213 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
214 AsyncConsistentTreeMap<V2> map,
215 Function<V1, V2> valueEncoder,
216 Function<V2, V1> valueDecoder) {
217 return new TranscodingAsyncConsistentTreeMap<>(map,
218 valueEncoder,
219 valueDecoder);
220 }
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700221
222 /**
223 * Creates an instance of {@code AsyncConsistentMultimap} that transforms
224 * operations inputs and applies them to corresponding operation in a
225 * differently typed map and returns the output after reverse transforming
226 * it.
227 *
228 * @param multimap backing multimap
229 * @param keyEncoder transformer for key type of returned map to key type
230 * of input map
231 * @param keyDecoder transformer for key type of input map to key type of
232 * returned map
233 * @param valueEncoder transformer for value type of returned map to value
234 * type of input map
235 * @param valueDecoder transformer for value type of input map to value
236 * type of returned map
237 * @param <K1> returned map key type
238 * @param <K2> input map key type
239 * @param <V1> returned map value type
240 * @param <V2> input map key type
241 * @return new map
242 */
243 public static <K1, V1, K2, V2> AsyncConsistentMultimap<K1, V1>
244 newTranscodingMultimap(AsyncConsistentMultimap<K2, V2> multimap,
245 Function<K1, K2> keyEncoder,
246 Function<K2, K1> keyDecoder,
247 Function<V1, V2> valueEncoder,
248 Function<V2, V1> valueDecoder) {
249 return new TranscodingAsyncConsistentMultimap<>(multimap,
250 keyEncoder,
251 keyDecoder,
252 valueDecoder,
253 valueEncoder);
254 }
255
Jordan Halterman8d8da592017-08-28 14:45:19 -0700256 /**
257 * Creates an instance of {@code AsyncDocumentTree} that caches values on get.
258 *
259 * @param tree backing tree
260 * @return caching tree
261 * @param <V> tree value type
262 */
263 public static <V> AsyncDocumentTree<V> newCachingDocumentTree(AsyncDocumentTree<V> tree) {
264 return new CachingAsyncDocumentTree<V>(tree);
265 }
266
Madan Jampani551d0d22016-02-01 12:51:48 -0800267}