Committed by
Gerrit Code Review
Binary incompatible serializer changes
- If the field type is fixed and the type is final, Class info can be omitted - Annotations serializer to use optimization based on the fact Map<String, String> and non-null key/value - Reduce number of Map copy required for ImmutableMap serializer - Reduce number of array copy behind Immutable{List, Set} serializer Change-Id: Ie467a943a33fbfb43b289b8b71ad91ee5890bfb0
Showing
13 changed files
with
116 additions
and
49 deletions
... | @@ -15,11 +15,12 @@ | ... | @@ -15,11 +15,12 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.device.impl; | 16 | package org.onosproject.store.device.impl; |
17 | 17 | ||
18 | +import static org.onosproject.store.serializers.DeviceIdSerializer.deviceIdSerializer; | ||
19 | + | ||
18 | import org.onosproject.net.DeviceId; | 20 | import org.onosproject.net.DeviceId; |
19 | import org.onosproject.net.device.DeviceDescription; | 21 | import org.onosproject.net.device.DeviceDescription; |
20 | import org.onosproject.net.provider.ProviderId; | 22 | import org.onosproject.net.provider.ProviderId; |
21 | import org.onosproject.store.impl.Timestamped; | 23 | import org.onosproject.store.impl.Timestamped; |
22 | - | ||
23 | import com.esotericsoftware.kryo.Kryo; | 24 | import com.esotericsoftware.kryo.Kryo; |
24 | import com.esotericsoftware.kryo.Serializer; | 25 | import com.esotericsoftware.kryo.Serializer; |
25 | import com.esotericsoftware.kryo.io.Input; | 26 | import com.esotericsoftware.kryo.io.Input; |
... | @@ -41,7 +42,7 @@ public class InternalDeviceEventSerializer extends Serializer<InternalDeviceEven | ... | @@ -41,7 +42,7 @@ public class InternalDeviceEventSerializer extends Serializer<InternalDeviceEven |
41 | @Override | 42 | @Override |
42 | public void write(Kryo kryo, Output output, InternalDeviceEvent event) { | 43 | public void write(Kryo kryo, Output output, InternalDeviceEvent event) { |
43 | kryo.writeClassAndObject(output, event.providerId()); | 44 | kryo.writeClassAndObject(output, event.providerId()); |
44 | - kryo.writeClassAndObject(output, event.deviceId()); | 45 | + kryo.writeObject(output, event.deviceId(), deviceIdSerializer()); |
45 | kryo.writeClassAndObject(output, event.deviceDescription()); | 46 | kryo.writeClassAndObject(output, event.deviceDescription()); |
46 | } | 47 | } |
47 | 48 | ||
... | @@ -49,7 +50,7 @@ public class InternalDeviceEventSerializer extends Serializer<InternalDeviceEven | ... | @@ -49,7 +50,7 @@ public class InternalDeviceEventSerializer extends Serializer<InternalDeviceEven |
49 | public InternalDeviceEvent read(Kryo kryo, Input input, | 50 | public InternalDeviceEvent read(Kryo kryo, Input input, |
50 | Class<InternalDeviceEvent> type) { | 51 | Class<InternalDeviceEvent> type) { |
51 | ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); | 52 | ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); |
52 | - DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); | 53 | + DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer()); |
53 | 54 | ||
54 | @SuppressWarnings("unchecked") | 55 | @SuppressWarnings("unchecked") |
55 | Timestamped<DeviceDescription> deviceDescription | 56 | Timestamped<DeviceDescription> deviceDescription | ... | ... |
... | @@ -15,9 +15,10 @@ | ... | @@ -15,9 +15,10 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.device.impl; | 16 | package org.onosproject.store.device.impl; |
17 | 17 | ||
18 | +import static org.onosproject.store.serializers.DeviceIdSerializer.deviceIdSerializer; | ||
19 | + | ||
18 | import org.onosproject.net.DeviceId; | 20 | import org.onosproject.net.DeviceId; |
19 | import org.onosproject.store.Timestamp; | 21 | import org.onosproject.store.Timestamp; |
20 | - | ||
21 | import com.esotericsoftware.kryo.Kryo; | 22 | import com.esotericsoftware.kryo.Kryo; |
22 | import com.esotericsoftware.kryo.Serializer; | 23 | import com.esotericsoftware.kryo.Serializer; |
23 | import com.esotericsoftware.kryo.io.Input; | 24 | import com.esotericsoftware.kryo.io.Input; |
... | @@ -38,14 +39,14 @@ public class InternalDeviceOfflineEventSerializer extends Serializer<InternalDev | ... | @@ -38,14 +39,14 @@ public class InternalDeviceOfflineEventSerializer extends Serializer<InternalDev |
38 | 39 | ||
39 | @Override | 40 | @Override |
40 | public void write(Kryo kryo, Output output, InternalDeviceOfflineEvent event) { | 41 | public void write(Kryo kryo, Output output, InternalDeviceOfflineEvent event) { |
41 | - kryo.writeClassAndObject(output, event.deviceId()); | 42 | + kryo.writeObject(output, event.deviceId(), deviceIdSerializer()); |
42 | kryo.writeClassAndObject(output, event.timestamp()); | 43 | kryo.writeClassAndObject(output, event.timestamp()); |
43 | } | 44 | } |
44 | 45 | ||
45 | @Override | 46 | @Override |
46 | public InternalDeviceOfflineEvent read(Kryo kryo, Input input, | 47 | public InternalDeviceOfflineEvent read(Kryo kryo, Input input, |
47 | Class<InternalDeviceOfflineEvent> type) { | 48 | Class<InternalDeviceOfflineEvent> type) { |
48 | - DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); | 49 | + DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer()); |
49 | Timestamp timestamp = (Timestamp) kryo.readClassAndObject(input); | 50 | Timestamp timestamp = (Timestamp) kryo.readClassAndObject(input); |
50 | 51 | ||
51 | return new InternalDeviceOfflineEvent(deviceId, timestamp); | 52 | return new InternalDeviceOfflineEvent(deviceId, timestamp); | ... | ... |
... | @@ -15,6 +15,8 @@ | ... | @@ -15,6 +15,8 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.device.impl; | 16 | package org.onosproject.store.device.impl; |
17 | 17 | ||
18 | +import static org.onosproject.store.serializers.DeviceIdSerializer.deviceIdSerializer; | ||
19 | + | ||
18 | import java.util.List; | 20 | import java.util.List; |
19 | 21 | ||
20 | import org.onosproject.net.DeviceId; | 22 | import org.onosproject.net.DeviceId; |
... | @@ -43,7 +45,7 @@ public class InternalPortEventSerializer extends Serializer<InternalPortEvent> { | ... | @@ -43,7 +45,7 @@ public class InternalPortEventSerializer extends Serializer<InternalPortEvent> { |
43 | @Override | 45 | @Override |
44 | public void write(Kryo kryo, Output output, InternalPortEvent event) { | 46 | public void write(Kryo kryo, Output output, InternalPortEvent event) { |
45 | kryo.writeClassAndObject(output, event.providerId()); | 47 | kryo.writeClassAndObject(output, event.providerId()); |
46 | - kryo.writeClassAndObject(output, event.deviceId()); | 48 | + kryo.writeObject(output, event.deviceId(), deviceIdSerializer()); |
47 | kryo.writeClassAndObject(output, event.portDescriptions()); | 49 | kryo.writeClassAndObject(output, event.portDescriptions()); |
48 | } | 50 | } |
49 | 51 | ||
... | @@ -51,7 +53,7 @@ public class InternalPortEventSerializer extends Serializer<InternalPortEvent> { | ... | @@ -51,7 +53,7 @@ public class InternalPortEventSerializer extends Serializer<InternalPortEvent> { |
51 | public InternalPortEvent read(Kryo kryo, Input input, | 53 | public InternalPortEvent read(Kryo kryo, Input input, |
52 | Class<InternalPortEvent> type) { | 54 | Class<InternalPortEvent> type) { |
53 | ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); | 55 | ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); |
54 | - DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); | 56 | + DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer()); |
55 | 57 | ||
56 | @SuppressWarnings("unchecked") | 58 | @SuppressWarnings("unchecked") |
57 | Timestamped<List<PortDescription>> portDescriptions | 59 | Timestamped<List<PortDescription>> portDescriptions | ... | ... |
... | @@ -15,6 +15,8 @@ | ... | @@ -15,6 +15,8 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.device.impl; | 16 | package org.onosproject.store.device.impl; |
17 | 17 | ||
18 | +import static org.onosproject.store.serializers.DeviceIdSerializer.deviceIdSerializer; | ||
19 | + | ||
18 | import org.onosproject.net.DeviceId; | 20 | import org.onosproject.net.DeviceId; |
19 | import org.onosproject.net.device.PortDescription; | 21 | import org.onosproject.net.device.PortDescription; |
20 | import org.onosproject.net.provider.ProviderId; | 22 | import org.onosproject.net.provider.ProviderId; |
... | @@ -41,7 +43,7 @@ public class InternalPortStatusEventSerializer extends Serializer<InternalPortSt | ... | @@ -41,7 +43,7 @@ public class InternalPortStatusEventSerializer extends Serializer<InternalPortSt |
41 | @Override | 43 | @Override |
42 | public void write(Kryo kryo, Output output, InternalPortStatusEvent event) { | 44 | public void write(Kryo kryo, Output output, InternalPortStatusEvent event) { |
43 | kryo.writeClassAndObject(output, event.providerId()); | 45 | kryo.writeClassAndObject(output, event.providerId()); |
44 | - kryo.writeClassAndObject(output, event.deviceId()); | 46 | + kryo.writeObject(output, event.deviceId(), deviceIdSerializer()); |
45 | kryo.writeClassAndObject(output, event.portDescription()); | 47 | kryo.writeClassAndObject(output, event.portDescription()); |
46 | } | 48 | } |
47 | 49 | ||
... | @@ -49,7 +51,7 @@ public class InternalPortStatusEventSerializer extends Serializer<InternalPortSt | ... | @@ -49,7 +51,7 @@ public class InternalPortStatusEventSerializer extends Serializer<InternalPortSt |
49 | public InternalPortStatusEvent read(Kryo kryo, Input input, | 51 | public InternalPortStatusEvent read(Kryo kryo, Input input, |
50 | Class<InternalPortStatusEvent> type) { | 52 | Class<InternalPortStatusEvent> type) { |
51 | ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); | 53 | ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); |
52 | - DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); | 54 | + DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer()); |
53 | @SuppressWarnings("unchecked") | 55 | @SuppressWarnings("unchecked") |
54 | Timestamped<PortDescription> portDescription = (Timestamped<PortDescription>) kryo.readClassAndObject(input); | 56 | Timestamped<PortDescription> portDescription = (Timestamped<PortDescription>) kryo.readClassAndObject(input); |
55 | 57 | ... | ... |
... | @@ -21,24 +21,48 @@ import com.esotericsoftware.kryo.Kryo; | ... | @@ -21,24 +21,48 @@ import com.esotericsoftware.kryo.Kryo; |
21 | import com.esotericsoftware.kryo.Serializer; | 21 | import com.esotericsoftware.kryo.Serializer; |
22 | import com.esotericsoftware.kryo.io.Input; | 22 | import com.esotericsoftware.kryo.io.Input; |
23 | import com.esotericsoftware.kryo.io.Output; | 23 | import com.esotericsoftware.kryo.io.Output; |
24 | +import com.esotericsoftware.kryo.serializers.DefaultSerializers; | ||
25 | +import com.esotericsoftware.kryo.serializers.DefaultSerializers.StringSerializer; | ||
26 | +import com.esotericsoftware.kryo.serializers.MapSerializer; | ||
24 | 27 | ||
25 | import java.util.HashMap; | 28 | import java.util.HashMap; |
29 | +import java.util.Map; | ||
26 | 30 | ||
27 | public class AnnotationsSerializer extends Serializer<DefaultAnnotations> { | 31 | public class AnnotationsSerializer extends Serializer<DefaultAnnotations> { |
28 | 32 | ||
33 | + private static final StringSerializer STR_SERIALIZER | ||
34 | + = new DefaultSerializers.StringSerializer(); | ||
35 | + | ||
36 | + private static final MapSerializer MAP_SERIALIZER = stringMapSerializer(); | ||
37 | + | ||
38 | + /** | ||
39 | + * Returns a MapSerializer for {@code Map<String, String>} with | ||
40 | + * no null key or value. | ||
41 | + * | ||
42 | + * @return serializer | ||
43 | + */ | ||
44 | + private static MapSerializer stringMapSerializer() { | ||
45 | + MapSerializer serializer = new MapSerializer(); | ||
46 | + serializer.setKeysCanBeNull(false); | ||
47 | + serializer.setKeyClass(String.class, STR_SERIALIZER); | ||
48 | + serializer.setValuesCanBeNull(false); | ||
49 | + serializer.setValueClass(String.class, STR_SERIALIZER); | ||
50 | + return serializer; | ||
51 | + } | ||
52 | + | ||
29 | public AnnotationsSerializer() { | 53 | public AnnotationsSerializer() { |
30 | super(false, true); | 54 | super(false, true); |
31 | } | 55 | } |
32 | 56 | ||
33 | @Override | 57 | @Override |
34 | public void write(Kryo kryo, Output output, DefaultAnnotations object) { | 58 | public void write(Kryo kryo, Output output, DefaultAnnotations object) { |
35 | - kryo.writeObject(output, object.asMap()); | 59 | + kryo.writeObject(output, object.asMap(), MAP_SERIALIZER); |
36 | } | 60 | } |
37 | 61 | ||
38 | @Override | 62 | @Override |
39 | public DefaultAnnotations read(Kryo kryo, Input input, Class<DefaultAnnotations> type) { | 63 | public DefaultAnnotations read(Kryo kryo, Input input, Class<DefaultAnnotations> type) { |
40 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); | 64 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
41 | - HashMap<String, String> map = kryo.readObject(input, HashMap.class); | 65 | + Map<String, String> map = kryo.readObject(input, HashMap.class, MAP_SERIALIZER); |
42 | map.forEach((k, v) -> b.set(k, v)); | 66 | map.forEach((k, v) -> b.set(k, v)); |
43 | 67 | ||
44 | return b.build(); | 68 | return b.build(); | ... | ... |
... | @@ -36,14 +36,14 @@ public final class DefaultApplicationIdSerializer extends Serializer<DefaultAppl | ... | @@ -36,14 +36,14 @@ public final class DefaultApplicationIdSerializer extends Serializer<DefaultAppl |
36 | 36 | ||
37 | @Override | 37 | @Override |
38 | public void write(Kryo kryo, Output output, DefaultApplicationId object) { | 38 | public void write(Kryo kryo, Output output, DefaultApplicationId object) { |
39 | - kryo.writeObject(output, object.id()); | 39 | + output.writeShort(object.id()); |
40 | - kryo.writeObject(output, object.name()); | 40 | + output.writeString(object.name()); |
41 | } | 41 | } |
42 | 42 | ||
43 | @Override | 43 | @Override |
44 | public DefaultApplicationId read(Kryo kryo, Input input, Class<DefaultApplicationId> type) { | 44 | public DefaultApplicationId read(Kryo kryo, Input input, Class<DefaultApplicationId> type) { |
45 | - short id = kryo.readObject(input, Short.class); | 45 | + short id = input.readShort(); |
46 | - String name = kryo.readObject(input, String.class); | 46 | + String name = input.readString(); |
47 | return new DefaultApplicationId(id, name); | 47 | return new DefaultApplicationId(id, name); |
48 | } | 48 | } |
49 | } | 49 | } | ... | ... |
... | @@ -27,6 +27,12 @@ import com.esotericsoftware.kryo.io.Output; | ... | @@ -27,6 +27,12 @@ import com.esotericsoftware.kryo.io.Output; |
27 | */ | 27 | */ |
28 | public final class DeviceIdSerializer extends Serializer<DeviceId> { | 28 | public final class DeviceIdSerializer extends Serializer<DeviceId> { |
29 | 29 | ||
30 | + private static final DeviceIdSerializer INSTANCE = new DeviceIdSerializer(); | ||
31 | + | ||
32 | + public static final DeviceIdSerializer deviceIdSerializer() { | ||
33 | + return INSTANCE; | ||
34 | + } | ||
35 | + | ||
30 | /** | 36 | /** |
31 | * Creates {@link DeviceId} serializer instance. | 37 | * Creates {@link DeviceId} serializer instance. |
32 | */ | 38 | */ | ... | ... |
... | @@ -20,7 +20,6 @@ import com.esotericsoftware.kryo.Serializer; | ... | @@ -20,7 +20,6 @@ import com.esotericsoftware.kryo.Serializer; |
20 | import com.esotericsoftware.kryo.io.Input; | 20 | import com.esotericsoftware.kryo.io.Input; |
21 | import com.esotericsoftware.kryo.io.Output; | 21 | import com.esotericsoftware.kryo.io.Output; |
22 | import com.google.common.collect.ImmutableList; | 22 | import com.google.common.collect.ImmutableList; |
23 | -import com.google.common.collect.ImmutableList.Builder; | ||
24 | 23 | ||
25 | /** | 24 | /** |
26 | * Creates {@link ImmutableList} serializer instance. | 25 | * Creates {@link ImmutableList} serializer instance. |
... | @@ -44,12 +43,19 @@ public class ImmutableListSerializer extends Serializer<ImmutableList<?>> { | ... | @@ -44,12 +43,19 @@ public class ImmutableListSerializer extends Serializer<ImmutableList<?>> { |
44 | 43 | ||
45 | @Override | 44 | @Override |
46 | public ImmutableList<?> read(Kryo kryo, Input input, | 45 | public ImmutableList<?> read(Kryo kryo, Input input, |
47 | - Class<ImmutableList<?>> type) { | 46 | + Class<ImmutableList<?>> type) { |
48 | final int size = input.readInt(); | 47 | final int size = input.readInt(); |
49 | - Builder<Object> builder = ImmutableList.builder(); | 48 | + switch (size) { |
50 | - for (int i = 0; i < size; ++i) { | 49 | + case 0: |
51 | - builder.add(kryo.readClassAndObject(input)); | 50 | + return ImmutableList.of(); |
51 | + case 1: | ||
52 | + return ImmutableList.of(kryo.readClassAndObject(input)); | ||
53 | + default: | ||
54 | + Object[] elms = new Object[size]; | ||
55 | + for (int i = 0; i < size; ++i) { | ||
56 | + elms[i] = kryo.readClassAndObject(input); | ||
57 | + } | ||
58 | + return ImmutableList.copyOf(elms); | ||
52 | } | 59 | } |
53 | - return builder.build(); | ||
54 | } | 60 | } |
55 | } | 61 | } | ... | ... |
... | @@ -15,24 +15,20 @@ | ... | @@ -15,24 +15,20 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.serializers; | 16 | package org.onosproject.store.serializers; |
17 | 17 | ||
18 | -import java.util.Collections; | 18 | +import java.util.Map.Entry; |
19 | -import java.util.HashMap; | ||
20 | -import java.util.Map; | ||
21 | 19 | ||
22 | import com.esotericsoftware.kryo.Kryo; | 20 | import com.esotericsoftware.kryo.Kryo; |
23 | import com.esotericsoftware.kryo.Serializer; | 21 | import com.esotericsoftware.kryo.Serializer; |
24 | import com.esotericsoftware.kryo.io.Input; | 22 | import com.esotericsoftware.kryo.io.Input; |
25 | import com.esotericsoftware.kryo.io.Output; | 23 | import com.esotericsoftware.kryo.io.Output; |
26 | -import com.esotericsoftware.kryo.serializers.MapSerializer; | ||
27 | import com.google.common.collect.ImmutableMap; | 24 | import com.google.common.collect.ImmutableMap; |
25 | +import com.google.common.collect.ImmutableMap.Builder; | ||
28 | 26 | ||
29 | /** | 27 | /** |
30 | * Kryo Serializer for {@link ImmutableMap}. | 28 | * Kryo Serializer for {@link ImmutableMap}. |
31 | */ | 29 | */ |
32 | public class ImmutableMapSerializer extends Serializer<ImmutableMap<?, ?>> { | 30 | public class ImmutableMapSerializer extends Serializer<ImmutableMap<?, ?>> { |
33 | 31 | ||
34 | - private final MapSerializer mapSerializer = new MapSerializer(); | ||
35 | - | ||
36 | /** | 32 | /** |
37 | * Creates {@link ImmutableMap} serializer instance. | 33 | * Creates {@link ImmutableMap} serializer instance. |
38 | */ | 34 | */ |
... | @@ -43,16 +39,31 @@ public class ImmutableMapSerializer extends Serializer<ImmutableMap<?, ?>> { | ... | @@ -43,16 +39,31 @@ public class ImmutableMapSerializer extends Serializer<ImmutableMap<?, ?>> { |
43 | 39 | ||
44 | @Override | 40 | @Override |
45 | public void write(Kryo kryo, Output output, ImmutableMap<?, ?> object) { | 41 | public void write(Kryo kryo, Output output, ImmutableMap<?, ?> object) { |
46 | - // wrapping with unmodifiableMap proxy | 42 | + output.writeInt(object.size()); |
47 | - // to avoid Kryo from writing only the reference marker of this instance, | 43 | + for (Entry<?, ?> e : object.entrySet()) { |
48 | - // which will be embedded right before this method call. | 44 | + kryo.writeClassAndObject(output, e.getKey()); |
49 | - kryo.writeObject(output, Collections.unmodifiableMap(object), mapSerializer); | 45 | + kryo.writeClassAndObject(output, e.getValue()); |
46 | + } | ||
50 | } | 47 | } |
51 | 48 | ||
52 | @Override | 49 | @Override |
53 | public ImmutableMap<?, ?> read(Kryo kryo, Input input, | 50 | public ImmutableMap<?, ?> read(Kryo kryo, Input input, |
54 | Class<ImmutableMap<?, ?>> type) { | 51 | Class<ImmutableMap<?, ?>> type) { |
55 | - Map<?, ?> map = kryo.readObject(input, HashMap.class, mapSerializer); | 52 | + final int size = input.readInt(); |
56 | - return ImmutableMap.copyOf(map); | 53 | + switch (size) { |
54 | + case 0: | ||
55 | + return ImmutableMap.of(); | ||
56 | + case 1: | ||
57 | + return ImmutableMap.of(kryo.readClassAndObject(input), | ||
58 | + kryo.readClassAndObject(input)); | ||
59 | + | ||
60 | + default: | ||
61 | + Builder<Object, Object> builder = ImmutableMap.builder(); | ||
62 | + for (int i = 0; i < size; ++i) { | ||
63 | + builder.put(kryo.readClassAndObject(input), | ||
64 | + kryo.readClassAndObject(input)); | ||
65 | + } | ||
66 | + return builder.build(); | ||
67 | + } | ||
57 | } | 68 | } |
58 | } | 69 | } | ... | ... |
... | @@ -15,14 +15,10 @@ | ... | @@ -15,14 +15,10 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.serializers; | 16 | package org.onosproject.store.serializers; |
17 | 17 | ||
18 | -import java.util.ArrayList; | ||
19 | -import java.util.List; | ||
20 | - | ||
21 | import com.esotericsoftware.kryo.Kryo; | 18 | import com.esotericsoftware.kryo.Kryo; |
22 | import com.esotericsoftware.kryo.Serializer; | 19 | import com.esotericsoftware.kryo.Serializer; |
23 | import com.esotericsoftware.kryo.io.Input; | 20 | import com.esotericsoftware.kryo.io.Input; |
24 | import com.esotericsoftware.kryo.io.Output; | 21 | import com.esotericsoftware.kryo.io.Output; |
25 | -import com.esotericsoftware.kryo.serializers.CollectionSerializer; | ||
26 | import com.google.common.collect.ImmutableSet; | 22 | import com.google.common.collect.ImmutableSet; |
27 | 23 | ||
28 | /** | 24 | /** |
... | @@ -30,26 +26,37 @@ import com.google.common.collect.ImmutableSet; | ... | @@ -30,26 +26,37 @@ import com.google.common.collect.ImmutableSet; |
30 | */ | 26 | */ |
31 | public class ImmutableSetSerializer extends Serializer<ImmutableSet<?>> { | 27 | public class ImmutableSetSerializer extends Serializer<ImmutableSet<?>> { |
32 | 28 | ||
33 | - private final CollectionSerializer serializer = new CollectionSerializer(); | ||
34 | - | ||
35 | /** | 29 | /** |
36 | * Creates {@link ImmutableSet} serializer instance. | 30 | * Creates {@link ImmutableSet} serializer instance. |
37 | */ | 31 | */ |
38 | public ImmutableSetSerializer() { | 32 | public ImmutableSetSerializer() { |
39 | // non-null, immutable | 33 | // non-null, immutable |
40 | super(false, true); | 34 | super(false, true); |
41 | - serializer.setElementsCanBeNull(false); | ||
42 | } | 35 | } |
43 | 36 | ||
44 | @Override | 37 | @Override |
45 | public void write(Kryo kryo, Output output, ImmutableSet<?> object) { | 38 | public void write(Kryo kryo, Output output, ImmutableSet<?> object) { |
46 | - kryo.writeObject(output, object.asList(), serializer); | 39 | + output.writeInt(object.size()); |
40 | + for (Object e : object) { | ||
41 | + kryo.writeClassAndObject(output, e); | ||
42 | + } | ||
47 | } | 43 | } |
48 | 44 | ||
49 | @Override | 45 | @Override |
50 | public ImmutableSet<?> read(Kryo kryo, Input input, | 46 | public ImmutableSet<?> read(Kryo kryo, Input input, |
51 | Class<ImmutableSet<?>> type) { | 47 | Class<ImmutableSet<?>> type) { |
52 | - List<?> elms = kryo.readObject(input, ArrayList.class, serializer); | 48 | + final int size = input.readInt(); |
53 | - return ImmutableSet.copyOf(elms); | 49 | + switch (size) { |
50 | + case 0: | ||
51 | + return ImmutableSet.of(); | ||
52 | + case 1: | ||
53 | + return ImmutableSet.of(kryo.readClassAndObject(input)); | ||
54 | + default: | ||
55 | + Object[] elms = new Object[size]; | ||
56 | + for (int i = 0; i < size; ++i) { | ||
57 | + elms[i] = kryo.readClassAndObject(input); | ||
58 | + } | ||
59 | + return ImmutableSet.copyOf(elms); | ||
60 | + } | ||
54 | } | 61 | } |
55 | } | 62 | } | ... | ... |
core/store/serializers/src/main/java/org/onosproject/store/serializers/MastershipTermSerializer.java
... | @@ -15,6 +15,8 @@ | ... | @@ -15,6 +15,8 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.serializers; | 16 | package org.onosproject.store.serializers; |
17 | 17 | ||
18 | +import static org.onosproject.store.serializers.NodeIdSerializer.nodeIdSerializer; | ||
19 | + | ||
18 | import org.onosproject.cluster.NodeId; | 20 | import org.onosproject.cluster.NodeId; |
19 | import org.onosproject.mastership.MastershipTerm; | 21 | import org.onosproject.mastership.MastershipTerm; |
20 | 22 | ||
... | @@ -26,7 +28,7 @@ import com.esotericsoftware.kryo.io.Output; | ... | @@ -26,7 +28,7 @@ import com.esotericsoftware.kryo.io.Output; |
26 | /** | 28 | /** |
27 | * Kryo Serializer for {@link org.onosproject.mastership.MastershipTerm}. | 29 | * Kryo Serializer for {@link org.onosproject.mastership.MastershipTerm}. |
28 | */ | 30 | */ |
29 | -public class MastershipTermSerializer extends Serializer<MastershipTerm> { | 31 | +public final class MastershipTermSerializer extends Serializer<MastershipTerm> { |
30 | 32 | ||
31 | /** | 33 | /** |
32 | * Creates {@link MastershipTerm} serializer instance. | 34 | * Creates {@link MastershipTerm} serializer instance. |
... | @@ -38,14 +40,14 @@ public class MastershipTermSerializer extends Serializer<MastershipTerm> { | ... | @@ -38,14 +40,14 @@ public class MastershipTermSerializer extends Serializer<MastershipTerm> { |
38 | 40 | ||
39 | @Override | 41 | @Override |
40 | public MastershipTerm read(Kryo kryo, Input input, Class<MastershipTerm> type) { | 42 | public MastershipTerm read(Kryo kryo, Input input, Class<MastershipTerm> type) { |
41 | - final NodeId node = (NodeId) kryo.readClassAndObject(input); | 43 | + final NodeId node = kryo.readObjectOrNull(input, NodeId.class, nodeIdSerializer()); |
42 | final long term = input.readLong(); | 44 | final long term = input.readLong(); |
43 | return MastershipTerm.of(node, term); | 45 | return MastershipTerm.of(node, term); |
44 | } | 46 | } |
45 | 47 | ||
46 | @Override | 48 | @Override |
47 | public void write(Kryo kryo, Output output, MastershipTerm object) { | 49 | public void write(Kryo kryo, Output output, MastershipTerm object) { |
48 | - kryo.writeClassAndObject(output, object.master()); | 50 | + kryo.writeObjectOrNull(output, object.master(), nodeIdSerializer()); |
49 | output.writeLong(object.termNumber()); | 51 | output.writeLong(object.termNumber()); |
50 | } | 52 | } |
51 | } | 53 | } | ... | ... |
... | @@ -27,6 +27,12 @@ import org.onosproject.cluster.NodeId; | ... | @@ -27,6 +27,12 @@ import org.onosproject.cluster.NodeId; |
27 | */ | 27 | */ |
28 | public final class NodeIdSerializer extends Serializer<NodeId> { | 28 | public final class NodeIdSerializer extends Serializer<NodeId> { |
29 | 29 | ||
30 | + private static final NodeIdSerializer INSTANCE = new NodeIdSerializer(); | ||
31 | + | ||
32 | + public static final NodeIdSerializer nodeIdSerializer() { | ||
33 | + return INSTANCE; | ||
34 | + } | ||
35 | + | ||
30 | /** | 36 | /** |
31 | * Creates {@link NodeId} serializer instance. | 37 | * Creates {@link NodeId} serializer instance. |
32 | */ | 38 | */ | ... | ... |
... | @@ -25,8 +25,7 @@ import com.esotericsoftware.kryo.io.Output; | ... | @@ -25,8 +25,7 @@ import com.esotericsoftware.kryo.io.Output; |
25 | /** | 25 | /** |
26 | * Serializer for {@link PortNumber}. | 26 | * Serializer for {@link PortNumber}. |
27 | */ | 27 | */ |
28 | -public final class PortNumberSerializer extends | 28 | +public final class PortNumberSerializer extends Serializer<PortNumber> { |
29 | - Serializer<PortNumber> { | ||
30 | 29 | ||
31 | /** | 30 | /** |
32 | * Creates {@link PortNumber} serializer instance. | 31 | * Creates {@link PortNumber} serializer instance. | ... | ... |
-
Please register or login to post a comment