From a35488e8ee09ca83623422f2dca3f0adc4507155 Mon Sep 17 00:00:00 2001 From: shahan Date: Mon, 29 Jan 2018 08:19:01 -0800 Subject: Additional Serializers and Serializer bugfixes needed to serialize BuildConfiguration. PiperOrigin-RevId: 183667795 --- .../serialization/serializers/ClassSerializer.java | 44 +++++++++++++++ .../serializers/ImmutableListSerializer.java | 1 + .../serializers/ImmutableMultimapSerializer.java | 2 +- .../serializers/ImmutableSortedMapSerializer.java | 53 ++++++++++++++++++ .../serialization/serializers/KryoConfigUtil.java | 4 ++ .../serializers/MultimapSerializer.java | 1 + .../serializers/UnmodifiableListSerializer.java | 65 ++++++++++++++++++++++ .../serializers/UnmodifiableMapSerializer.java | 52 +++++++++++++++++ 8 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ClassSerializer.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableSortedMapSerializer.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableListSerializer.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableMapSerializer.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ClassSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ClassSerializer.java new file mode 100644 index 0000000000..a815b24de8 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ClassSerializer.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.KryoException; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** A {@link Serializer} for {@link Class}. */ +@SuppressWarnings("rawtypes") +class ClassSerializer extends Serializer { + + @Override + public void write(Kryo unusedKryo, Output output, Class object) { + output.writeAscii(object.getName()); + } + + @Override + public Class read(Kryo unusedKryo, Input input, Class type) { + try { + return Class.forName(input.readString()); + } catch (ClassNotFoundException e) { + throw new KryoException(e); + } + } + + static void registerSerializers(Kryo kryo) { + kryo.register(Class.class, new ClassSerializer()); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableListSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableListSerializer.java index 0556d02615..98b23c7244 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableListSerializer.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableListSerializer.java @@ -81,6 +81,7 @@ class ImmutableListSerializer extends Serializer> { kryo.register(ImmutableList.of().getClass(), serializer); kryo.register(ImmutableList.of(1).getClass(), serializer); + kryo.register(ImmutableList.of(1, 2).getClass(), serializer); kryo.register(ImmutableList.of(1, 2, 3, 4).subList(1, 3).getClass(), serializer); kryo.register(ImmutableList.of(1, 2).reverse().getClass(), serializer); diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableMultimapSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableMultimapSerializer.java index 98b4e7b970..cec2cbb9ea 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableMultimapSerializer.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableMultimapSerializer.java @@ -42,7 +42,7 @@ class ImmutableMultimapSerializer extends Serializer> type) { ImmutableMultimap.Builder builder; if (type.equals(ImmutableListMultimap.class)) { - builder = ImmutableMultimap.builder(); + builder = ImmutableListMultimap.builder(); } else if (type.equals(ImmutableSetMultimap.class)) { builder = ImmutableSetMultimap.builder(); } else { diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableSortedMapSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableSortedMapSerializer.java new file mode 100644 index 0000000000..b25a840261 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/ImmutableSortedMapSerializer.java @@ -0,0 +1,53 @@ +// 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.ImmutableSortedMap; +import java.util.Comparator; +import java.util.Map.Entry; + +/** Custom Kryo serializer for {@link ImmutableSortedMap}. */ +class ImmutableSortedMapSerializer extends Serializer> { + + @Override + public void write(Kryo kryo, Output output, ImmutableSortedMap map) { + kryo.writeClassAndObject(output, map.comparator()); + output.writeInt(map.size(), true); + for (Entry elt : map.entrySet()) { + kryo.writeClassAndObject(output, elt.getKey()); + kryo.writeClassAndObject(output, elt.getValue()); + } + } + + @Override + @SuppressWarnings({"unchecked", "rawtypes"}) + public ImmutableSortedMap read( + Kryo kryo, Input input, Class> type) { + ImmutableSortedMap.Builder builder = + ImmutableSortedMap.orderedBy((Comparator) kryo.readClassAndObject(input)); + int size = input.readInt(true); + for (int i = 0; i < size; ++i) { + builder.put(kryo.readClassAndObject(input), kryo.readClassAndObject(input)); + } + return builder.build(); + } + + static void registerSerializers(Kryo kryo) { + kryo.register(ImmutableSortedMap.class, new ImmutableSortedMapSerializer()); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/KryoConfigUtil.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/KryoConfigUtil.java index d0adf345fb..d19eca930d 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/KryoConfigUtil.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/KryoConfigUtil.java @@ -34,17 +34,21 @@ public class KryoConfigUtil { kryo.register(Ordering.natural().getClass()); kryo.register(Collections.reverseOrder().getClass()); + ClassSerializer.registerSerializers(kryo); HashCodeSerializer.registerSerializers(kryo); ImmutableListSerializer.registerSerializers(kryo); ImmutableMapSerializer.registerSerializers(kryo); ImmutableMultimapSerializer.registerSerializers(kryo); ImmutableSetSerializer.registerSerializers(kryo); + ImmutableSortedMapSerializer.registerSerializers(kryo); ImmutableSortedSetSerializer.registerSerializers(kryo); MapEntrySerializer.registerSerializers(kryo); MultimapSerializer.registerSerializers(kryo); PatternSerializer.registerSerializers(kryo); ReverseListSerializer.registerSerializers(kryo); OptionalSerializer.registerSerializers(kryo); + UnmodifiableListSerializer.registerSerializers(kryo); + UnmodifiableMapSerializer.registerSerializers(kryo); UnmodifiableNavigableSetSerializer.registerSerializers(kryo); } } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MultimapSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MultimapSerializer.java index 40b9570c51..5c45b8ad52 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MultimapSerializer.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/MultimapSerializer.java @@ -54,6 +54,7 @@ class MultimapSerializer> extends Serializer { @Override public T read(Kryo kryo, Input input, Class unusedType) { T multimap = create.get(); + kryo.reference(multimap); int size = input.readInt(true); for (int i = 0; i < size; ++i) { multimap.put((E) kryo.readClassAndObject(input), (E) kryo.readClassAndObject(input)); diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableListSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableListSerializer.java new file mode 100644 index 0000000000..cdc3463382 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableListSerializer.java @@ -0,0 +1,65 @@ +// 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 java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** A {@link Serializer} for objects produced by {@link Collections#unmodifiableList}. */ +class UnmodifiableListSerializer extends Serializer> { + + private static final Class RANDOM_ACCESS_TYPE = + Collections.unmodifiableList(new ArrayList()).getClass(); + private static final Class SEQUENTIAL_ACCESS_TYPE = + Collections.unmodifiableList(new LinkedList()).getClass(); + + @Override + public void write(Kryo kryo, Output output, List object) { + output.writeInt(object.size(), true); + for (Object elt : object) { + kryo.writeClassAndObject(output, elt); + } + } + + @Override + public List read(Kryo kryo, Input input, Class> type) { + int size = input.readInt(true); + List list = null; + if (type.equals(RANDOM_ACCESS_TYPE)) { + list = new ArrayList<>(size); + } else if (type.equals(SEQUENTIAL_ACCESS_TYPE)) { + list = new LinkedList<>(); + } else { + throw new IllegalArgumentException( + "UnmodifiableListSerializer.read called with type: " + type); + } + for (int i = 0; i < size; ++i) { + list.add(kryo.readClassAndObject(input)); + } + return Collections.unmodifiableList(list); + } + + static void registerSerializers(Kryo kryo) { + UnmodifiableListSerializer serializer = new UnmodifiableListSerializer(); + kryo.register(RANDOM_ACCESS_TYPE, serializer); + kryo.register(SEQUENTIAL_ACCESS_TYPE, serializer); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableMapSerializer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableMapSerializer.java new file mode 100644 index 0000000000..0dccc9593f --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/serializers/UnmodifiableMapSerializer.java @@ -0,0 +1,52 @@ +// 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 java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** {@link Serializer} for objects produced by {@link Collections#unmodifiableMap}. */ +class UnmodifiableMapSerializer extends Serializer> { + + @Override + public void write(Kryo kryo, Output output, Map map) { + output.writeInt(map.size(), true); + for (Map.Entry entry : map.entrySet()) { + kryo.writeClassAndObject(output, entry.getKey()); + kryo.writeClassAndObject(output, entry.getValue()); + } + } + + @Override + public Map read(Kryo kryo, Input input, Class> unusedType) { + LinkedHashMap map = new LinkedHashMap<>(); + int length = input.readInt(true); + for (int i = 0; i < length; ++i) { + map.put(kryo.readClassAndObject(input), kryo.readClassAndObject(input)); + } + return Collections.unmodifiableMap(map); + } + + static void registerSerializers(Kryo kryo) { + kryo.register( + Collections.unmodifiableMap(new LinkedHashMap()).getClass(), + new UnmodifiableMapSerializer()); + } +} -- cgit v1.2.3