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 | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 18 | import org.onosproject.core.Version; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 19 | import org.onosproject.store.service.AsyncAtomicCounterMap; |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 20 | import org.onosproject.store.service.AsyncConsistentMap; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 21 | import org.onosproject.store.service.AsyncConsistentMultimap; |
Aaron Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.AsyncDistributedSet; |
Jordan Halterman | 8d8da59 | 2017-08-28 14:45:19 -0700 | [diff] [blame] | 24 | import org.onosproject.store.service.AsyncDocumentTree; |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 25 | |
Jordan Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 26 | import java.util.function.BiFunction; |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 27 | import java.util.function.Function; |
| 28 | |
Madan Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Misc utilities for working with {@code DistributedPrimitive}s. |
| 31 | */ |
| 32 | public 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 Halterman | 4922a06 | 2017-07-31 15:55:36 -0700 | [diff] [blame] | 84 | * 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 Halterman | 4500817 | 2018-03-19 16:40:31 -0700 | [diff] [blame^] | 96 | * 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 Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 120 | * 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 Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 136 | * 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 Kruglikov | ed88ff6 | 2016-08-01 16:02:09 -0700 | [diff] [blame] | 161 | |
| 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 Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 181 | |
| 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 Halterman | 8d8da59 | 2017-08-28 14:45:19 -0700 | [diff] [blame] | 216 | /** |
| 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 Jampani | 551d0d2 | 2016-02-01 12:51:48 -0800 | [diff] [blame] | 227 | } |