Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 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 | package org.onosproject.store.primitives.impl; |
| 17 | |
Jordan Halterman | 400bbe5 | 2018-04-05 23:07:47 -0700 | [diff] [blame] | 18 | import java.util.function.BiFunction; |
| 19 | import java.util.function.Function; |
| 20 | |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame] | 21 | import org.onosproject.core.Version; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 22 | import org.onosproject.store.service.AsyncAtomicCounterMap; |
Jordan Halterman | 400bbe5 | 2018-04-05 23:07:47 -0700 | [diff] [blame] | 23 | import org.onosproject.store.service.AsyncAtomicValue; |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 24 | import org.onosproject.store.service.AsyncConsistentMap; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.AsyncConsistentMultimap; |
Aaron Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 26 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 27 | import org.onosproject.store.service.AsyncDistributedSet; |
Jordan Halterman | 8d8da59 | 2017-08-28 14:45:19 -0700 | [diff] [blame] | 28 | import org.onosproject.store.service.AsyncDocumentTree; |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Misc utilities for working with {@code DistributedPrimitive}s. |
| 32 | */ |
| 33 | public 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 Halterman | 4922a06 | 2017-07-31 15:55:36 -0700 | [diff] [blame] | 85 | * 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 Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame] | 97 | * 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 Halterman | 400bbe5 | 2018-04-05 23:07:47 -0700 | [diff] [blame] | 121 | * 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 Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 160 | * 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 Halterman | 400bbe5 | 2018-04-05 23:07:47 -0700 | [diff] [blame] | 170 | Function<K1, K2> keyEncoder, |
| 171 | Function<K2, K1> keyDecoder) { |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 172 | return new TranscodingAsyncAtomicCounterMap<>(map, keyEncoder, keyDecoder); |
| 173 | } |
| 174 | |
| 175 | /** |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 176 | * 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 Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 201 | |
| 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 Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 221 | |
| 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 Halterman | 8d8da59 | 2017-08-28 14:45:19 -0700 | [diff] [blame] | 256 | /** |
| 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 Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 267 | } |