blob: d6b94418f1d97299cb5689ad18c641213d85f209 [file] [log] [blame]
Madan Jampani7e55c662016-02-15 21:13:53 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani7e55c662016-02-15 21:13:53 -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
18import org.onosproject.store.primitives.DistributedPrimitiveCreator;
Jordan Halterman45008172018-03-19 16:40:31 -070019import org.onosproject.store.serializers.KryoNamespaces;
Madan Jampani7e55c662016-02-15 21:13:53 -080020import org.onosproject.store.service.AsyncConsistentMap;
21import org.onosproject.store.service.ConsistentMap;
22import org.onosproject.store.service.ConsistentMapBuilder;
Jordan Halterman45008172018-03-19 16:40:31 -070023import org.onosproject.store.service.Serializer;
Madan Jampani7e55c662016-02-15 21:13:53 -080024
25/**
26 * Default {@link AsyncConsistentMap} builder.
27 *
28 * @param <K> type for map key
29 * @param <V> type for map value
30 */
Madan Jampani832686d2016-04-04 21:57:26 -070031public class DefaultConsistentMapBuilder<K, V> extends ConsistentMapBuilder<K, V> {
Madan Jampani7e55c662016-02-15 21:13:53 -080032
Madan Jampani931e97d2016-02-26 12:20:44 -080033 private final DistributedPrimitiveCreator primitiveCreator;
Madan Jampani7e55c662016-02-15 21:13:53 -080034
Jordan Halterman400bbe52018-04-05 23:07:47 -070035 public DefaultConsistentMapBuilder(DistributedPrimitiveCreator primitiveCreator) {
Madan Jampani931e97d2016-02-26 12:20:44 -080036 this.primitiveCreator = primitiveCreator;
Madan Jampani7e55c662016-02-15 21:13:53 -080037 }
38
39 @Override
40 public ConsistentMap<K, V> build() {
41 return buildAsyncMap().asConsistentMap();
42 }
43
44 @Override
45 public AsyncConsistentMap<K, V> buildAsyncMap() {
Jordan Halterman45008172018-03-19 16:40:31 -070046 AsyncConsistentMap<K, V> map;
47
48 // If a compatibility function is defined, we don't assume CompatibleValue and Version is registered in
49 // the user-provided serializer since it's an implementation detail. Instead, we use the user-provided
50 // serializer to convert the CompatibleValue value to a raw byte[] and use a separate serializer to encode
51 // the CompatibleValue to binary.
52 if (compatibilityFunction != null) {
53 Serializer serializer = serializer();
54
55 // Convert the byte[] value to CompatibleValue<byte[]>
56 AsyncConsistentMap<K, CompatibleValue<byte[]>> rawMap = primitiveCreator.newAsyncConsistentMap(
57 withSerializer(Serializer.using(KryoNamespaces.API, CompatibleValue.class)));
58
59 // Convert the CompatibleValue<byte[]> value to CompatibleValue<V> using the user-provided serializer.
60 AsyncConsistentMap<K, CompatibleValue<V>> compatibleMap =
61 DistributedPrimitives.newTranscodingMap(
62 rawMap,
63 key -> key,
64 key -> key,
65 value -> value == null ? null :
66 new CompatibleValue<byte[]>(serializer.encode(value.value()), value.version()),
67 value -> value == null ? null :
68 new CompatibleValue<V>(serializer.decode(value.value()), value.version()));
69 map = DistributedPrimitives.newCompatibleMap(compatibleMap, compatibilityFunction, version());
70 } else {
71 map = primitiveCreator.newAsyncConsistentMap(name(), serializer());
72 }
73
Jordan Halterman4922a062017-07-31 15:55:36 -070074 map = nullValues() ? map : DistributedPrimitives.newNotNullMap(map);
Madan Jampani7e55c662016-02-15 21:13:53 -080075 map = relaxedReadConsistency() ? DistributedPrimitives.newCachingMap(map) : map;
76 map = readOnly() ? DistributedPrimitives.newUnmodifiableMap(map) : map;
77 return meteringEnabled() ? DistributedPrimitives.newMeteredMap(map) : map;
78 }
79}