diff options
author | 2018-01-25 19:36:29 -0800 | |
---|---|---|
committer | 2018-01-25 19:38:33 -0800 | |
commit | b1b710728dff392da8d8b76e229ae0b6347cf0c3 (patch) | |
tree | cacb821f755ac37fe3c1c1cfc69d2e704c5b6786 /src/main/java/com | |
parent | 7989840dbaefc7fcfd3dbef8dc7a88f926be2ca5 (diff) |
Serializers for HashCode, Map.Entry and Optional
This should bring Serializers to parity with AutoCodec Marshallers.
PiperOrigin-RevId: 183330231
Diffstat (limited to 'src/main/java/com')
5 files changed, 134 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/HashCodeSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/HashCodeSerializer.java new file mode 100644 index 0000000000..8cbb52136f --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/HashCodeSerializer.java @@ -0,0 +1,44 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.skyframe.serialization.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.google.common.hash.HashCode; + +class HashCodeSerializer extends Serializer<HashCode> { + + @Override + public void write(Kryo unusedKryo, Output output, HashCode hashCode) { + byte[] bytes = hashCode.asBytes(); + output.writeInt(bytes.length, true); + output.write(bytes); + } + + @Override + public HashCode read(Kryo kryo, Input input, Class<HashCode> unusedType) { + return HashCode.fromBytes(input.readBytes(input.readInt(true))); + } + + static void registerSerializers(Kryo kryo) { + HashCodeSerializer serializer = new HashCodeSerializer(); + kryo.register(HashCode.class, serializer); + kryo.register(HashCode.fromInt(0).getClass(), serializer); + kryo.register(HashCode.fromLong(0).getClass(), serializer); + kryo.register(HashCode.fromBytes(new byte[1]).getClass(), serializer); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MapEntrySerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MapEntrySerializer.java new file mode 100644 index 0000000000..402780dbcd --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MapEntrySerializer.java @@ -0,0 +1,44 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.skyframe.serialization.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.google.common.collect.Maps; +import java.util.Map.Entry; + +/** {@link Serializer} for {@link Entry}. */ +class MapEntrySerializer extends Serializer<Entry<Object, Object>> { + + @Override + public void write(Kryo kryo, Output output, Entry<Object, Object> entry) { + kryo.writeClassAndObject(output, entry.getKey()); + kryo.writeClassAndObject(output, entry.getValue()); + } + + @Override + public Entry<Object, Object> read( + Kryo kryo, Input input, Class<Entry<Object, Object>> unusedType) { + return Maps.immutableEntry(kryo.readClassAndObject(input), kryo.readClassAndObject(input)); + } + + static void registerSerializers(Kryo kryo) { + MapEntrySerializer serializer = new MapEntrySerializer(); + kryo.register(Entry.class, serializer); + kryo.register(Maps.immutableEntry(null, null).getClass(), serializer); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/OptionalSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/OptionalSerializer.java new file mode 100644 index 0000000000..f285c10e02 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/OptionalSerializer.java @@ -0,0 +1,42 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.skyframe.serialization.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.google.common.base.Optional; + +/** {@link Serializer} for {@link Optional}. */ +class OptionalSerializer extends Serializer<Optional<Object>> { + + @Override + public void write(Kryo kryo, Output output, Optional<Object> optional) { + kryo.writeClassAndObject(output, optional.orNull()); + } + + @Override + public Optional<Object> read(Kryo kryo, Input input, Class<Optional<Object>> unusedType) { + return Optional.fromNullable(kryo.readClassAndObject(input)); + } + + static void registerSerializers(Kryo kryo) { + OptionalSerializer serializer = new OptionalSerializer(); + kryo.register(Optional.class, serializer); + kryo.register(Optional.absent().getClass(), serializer); + kryo.register(Optional.of(0).getClass(), serializer); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/RegistrationUtil.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/RegistrationUtil.java index c6f4efe8c0..ce2668c1b8 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/RegistrationUtil.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/RegistrationUtil.java @@ -25,14 +25,17 @@ public interface RegistrationUtil { kryo.register(Ordering.natural().getClass()); kryo.register(Collections.reverseOrder().getClass()); + HashCodeSerializer.registerSerializers(kryo); ImmutableListSerializer.registerSerializers(kryo); ImmutableMapSerializer.registerSerializers(kryo); ImmutableMultimapSerializer.registerSerializers(kryo); ImmutableSetSerializer.registerSerializers(kryo); ImmutableSortedSetSerializer.registerSerializers(kryo); + MapEntrySerializer.registerSerializers(kryo); MultimapSerializer.registerSerializers(kryo); PatternSerializer.registerSerializers(kryo); ReverseListSerializer.registerSerializers(kryo); + OptionalSerializer.registerSerializers(kryo); UnmodifiableNavigableSetSerializer.registerSerializers(kryo); } } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils/SerializerTester.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils/SerializerTester.java index 6780b80107..52a68a9a2b 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils/SerializerTester.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils/SerializerTester.java @@ -90,7 +90,7 @@ public class SerializerTester<SubjectT, SerializerT extends SubjectT> { SubjectT deserialized = fromBytes(serialized); verificationFunction.verifyDeserialized(subject, deserialized); } - logger.log(Level.INFO, "total serialized bytes = " + totalBytes); + logger.log(Level.INFO, type.getSimpleName() + " total serialized bytes = " + totalBytes); } /** Runs serialized bytes stability tests. */ |