blob: 78c41e87c1e4bee451fa18240c01ebc64a261cb6 [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 Halterman45008172018-03-19 16:40:31 -070018import org.onosproject.core.Version;
Jordan Haltermanc955df72017-02-04 20:43:28 -080019import org.onosproject.store.service.AsyncAtomicCounterMap;
Madan Jampani551d0d22016-02-01 12:51:48 -080020import org.onosproject.store.service.AsyncConsistentMap;
Aaron Kruglikov61582a02016-09-06 13:18:58 -070021import org.onosproject.store.service.AsyncConsistentMultimap;
Aaron Kruglikoved88ff62016-08-01 16:02:09 -070022import org.onosproject.store.service.AsyncConsistentTreeMap;
Madan Jampani551d0d22016-02-01 12:51:48 -080023import org.onosproject.store.service.AsyncDistributedSet;
Jordan Halterman8d8da592017-08-28 14:45:19 -070024import org.onosproject.store.service.AsyncDocumentTree;
Madan Jampani551d0d22016-02-01 12:51:48 -080025
Jordan Halterman45008172018-03-19 16:40:31 -070026import java.util.function.BiFunction;
Jordan Haltermanc955df72017-02-04 20:43:28 -080027import java.util.function.Function;
28
Madan Jampani551d0d22016-02-01 12:51:48 -080029/**
30 * Misc utilities for working with {@code DistributedPrimitive}s.
31 */
32public final class DistributedPrimitives {
33
34 private DistributedPrimitives() {}
35
36 /**
37 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
38 *
39 * @param map backing map
40 * @return set
41 * @param <E> set element type
42 */
43 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
44 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
45 }
46
47 /**
48 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
49 *
50 * @param map map whose operations are to be metered
51 * @return metered map
52 * @param <K> map key type
53 * @param <V> map value type
54 */
55 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
56 return new MeteredAsyncConsistentMap<>(map);
57 }
58
59 /**
60 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
61 *
62 * @param map backing map
63 * @return caching map
64 * @param <K> map key type
65 * @param <V> map value type
66 */
67 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
68 return new CachingAsyncConsistentMap<>(map);
69 }
70
71 /**
72 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
73 *
74 * @param map backing map
75 * @return unmodifiable map
76 * @param <K> map key type
77 * @param <V> map value type
78 */
79 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
80 return new UnmodifiableAsyncConsistentMap<>(map);
81 }
82
83 /**
Jordan Halterman4922a062017-07-31 15:55:36 -070084 * Creates an instance of {@code AsyncConsistentMap} that disallows null values.
85 *
86 * @param map backing map
87 * @return not null map
88 * @param <K> map key type
89 * @param <V> map value type
90 */
91 public static <K, V> AsyncConsistentMap<K, V> newNotNullMap(AsyncConsistentMap<K, V> map) {
92 return new NotNullAsyncConsistentMap<>(map);
93 }
94
95 /**
Jordan Halterman45008172018-03-19 16:40:31 -070096 * Creates an instance of {@code AsyncConsistentMap} that converts values from other versions.
97 *
98 * @param map backing map
99 * @param compatibilityFunction the compatibility function
100 * @param version local node version
101 * @param <K> map key type
102 * @param <V> map value type
103 * @return compatible map
104 */
105 public static <K, V> AsyncConsistentMap<K, V> newCompatibleMap(
106 AsyncConsistentMap<K, CompatibleValue<V>> map,
107 BiFunction<V, Version, V> compatibilityFunction,
108 Version version) {
109 Function<V, CompatibleValue<V>> encoder = value -> new CompatibleValue<>(value, version);
110 Function<CompatibleValue<V>, V> decoder = value -> {
111 if (!value.version().equals(version)) {
112 return compatibilityFunction.apply(value.value(), value.version());
113 }
114 return value.value();
115 };
116 return new TranscodingAsyncConsistentMap<>(map, k -> k, k -> k, encoder, decoder);
117 }
118
119 /**
Jordan Haltermanc955df72017-02-04 20:43:28 -0800120 * Creates an instance of {@code AsyncAtomicCounterMap} that transforms key types.
121 *
122 * @param map backing map
123 * @param keyEncoder transformer for key type of returned map to key type of input map
124 * @param keyDecoder transformer for key type of input map to key type of returned map
125 * @param <K1> returned map key type
126 * @param <K2> input map key type
127 * @return new counter map
128 */
129 public static <K1, K2> AsyncAtomicCounterMap<K1> newTranscodingAtomicCounterMap(AsyncAtomicCounterMap<K2> map,
130 Function<K1, K2> keyEncoder,
131 Function<K2, K1> keyDecoder) {
132 return new TranscodingAsyncAtomicCounterMap<>(map, keyEncoder, keyDecoder);
133 }
134
135 /**
Madan Jampani551d0d22016-02-01 12:51:48 -0800136 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
137 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
138 *
139 * @param map backing map
140 * @param keyEncoder transformer for key type of returned map to key type of input map
141 * @param keyDecoder transformer for key type of input map to key type of returned map
142 * @param valueEncoder transformer for value type of returned map to value type of input map
143 * @param valueDecoder transformer for value type of input map to value type of returned map
144 * @param <K1> returned map key type
145 * @param <K2> input map key type
146 * @param <V1> returned map value type
147 * @param <V2> input map key type
148 * @return new map
149 */
150 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
151 Function<K1, K2> keyEncoder,
152 Function<K2, K1> keyDecoder,
153 Function<V1, V2> valueEncoder,
154 Function<V2, V1> valueDecoder) {
155 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
156 keyEncoder,
157 keyDecoder,
158 valueEncoder,
159 valueDecoder);
160 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700161
162 /**
163 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
164 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
165 *
166 * @param map backing map
167 * @param valueEncoder transformer for value type of returned map to value type of input map
168 * @param valueDecoder transformer for value type of input map to value type of returned map
169 * @param <V1> returned map value type
170 * @param <V2> input map key type
171 * @return new map
172 */
173 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
174 AsyncConsistentTreeMap<V2> map,
175 Function<V1, V2> valueEncoder,
176 Function<V2, V1> valueDecoder) {
177 return new TranscodingAsyncConsistentTreeMap<>(map,
178 valueEncoder,
179 valueDecoder);
180 }
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700181
182 /**
183 * Creates an instance of {@code AsyncConsistentMultimap} that transforms
184 * operations inputs and applies them to corresponding operation in a
185 * differently typed map and returns the output after reverse transforming
186 * it.
187 *
188 * @param multimap backing multimap
189 * @param keyEncoder transformer for key type of returned map to key type
190 * of input map
191 * @param keyDecoder transformer for key type of input map to key type of
192 * returned map
193 * @param valueEncoder transformer for value type of returned map to value
194 * type of input map
195 * @param valueDecoder transformer for value type of input map to value
196 * type of returned map
197 * @param <K1> returned map key type
198 * @param <K2> input map key type
199 * @param <V1> returned map value type
200 * @param <V2> input map key type
201 * @return new map
202 */
203 public static <K1, V1, K2, V2> AsyncConsistentMultimap<K1, V1>
204 newTranscodingMultimap(AsyncConsistentMultimap<K2, V2> multimap,
205 Function<K1, K2> keyEncoder,
206 Function<K2, K1> keyDecoder,
207 Function<V1, V2> valueEncoder,
208 Function<V2, V1> valueDecoder) {
209 return new TranscodingAsyncConsistentMultimap<>(multimap,
210 keyEncoder,
211 keyDecoder,
212 valueDecoder,
213 valueEncoder);
214 }
215
Jordan Halterman8d8da592017-08-28 14:45:19 -0700216 /**
217 * Creates an instance of {@code AsyncDocumentTree} that caches values on get.
218 *
219 * @param tree backing tree
220 * @return caching tree
221 * @param <V> tree value type
222 */
223 public static <V> AsyncDocumentTree<V> newCachingDocumentTree(AsyncDocumentTree<V> tree) {
224 return new CachingAsyncDocumentTree<V>(tree);
225 }
226
Madan Jampani551d0d22016-02-01 12:51:48 -0800227}