From a16887edd766989af3b50dd3b42afab8338c6d4b Mon Sep 17 00:00:00 2001 From: shahan Date: Thu, 15 Mar 2018 16:02:08 -0700 Subject: Makes leaf level AutoCodec marshallers into runtime codecs. Next change will convert containers. PiperOrigin-RevId: 189261293 --- .../BuildEventStreamProtoCodecRegisterer.java | 28 +++ .../cpp/CrosstoolConfigProtoCodecRegisterer.java | 31 ++++ .../skyframe/serialization/CodecRegisterer.java | 8 +- .../lib/skyframe/serialization/CodecScanner.java | 9 +- .../skyframe/serialization/EnumRuntimeCodec.java | 58 ++++++ .../skyframe/serialization/MessageLiteCodec.java | 64 +++++++ .../lib/skyframe/serialization/ObjectCodec.java | 2 +- .../lib/skyframe/serialization/UUIDCodec.java | 41 +++++ .../serialization/autocodec/Marshallers.java | 195 +-------------------- .../serialization/strings/CharsetCodec.java | 44 +++++ .../serialization/strings/PatternCodec.java | 45 +++++ .../serialization/strings/StringCodecs.java | 12 +- 12 files changed, 330 insertions(+), 207 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java create mode 100644 src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java (limited to 'src/main/java/com/google/devtools/build/lib') diff --git a/src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java b/src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java new file mode 100644 index 0000000000..8c37ab46ef --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/buildeventstream/BuildEventStreamProtoCodecRegisterer.java @@ -0,0 +1,28 @@ +// 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.buildeventstream; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer; +import com.google.devtools.build.lib.skyframe.serialization.MessageLiteCodec; + +class BuildEventStreamProtoCodecRegisterer implements CodecRegisterer { + @Override + public Iterable getCodecsToRegister() { + return ImmutableList.of( + new MessageLiteCodec(BuildEventStreamProtos.BuildEventId::newBuilder), + new MessageLiteCodec(BuildEventStreamProtos.BuildEvent::newBuilder)); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java new file mode 100644 index 0000000000..bad9dc0319 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigProtoCodecRegisterer.java @@ -0,0 +1,31 @@ +// 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.rules.cpp; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer; +import com.google.devtools.build.lib.skyframe.serialization.MessageLiteCodec; +import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig; + +class CrosstoolConfigProtoCodecRegisterer implements CodecRegisterer { + @Override + public Iterable getCodecsToRegister() { + return ImmutableList.of( + new MessageLiteCodec(CrosstoolConfig.CrosstoolRelease::newBuilder), + new MessageLiteCodec(CrosstoolConfig.CToolchain::newBuilder), + new MessageLiteCodec(CrosstoolConfig.CToolchain.OptionalFlag::newBuilder), + new MessageLiteCodec(CrosstoolConfig.CToolchain.Tool::newBuilder)); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java index 72d99191e3..9176a978d2 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecRegisterer.java @@ -14,6 +14,8 @@ package com.google.devtools.build.lib.skyframe.serialization; +import java.util.Collections; + /** * Custom registration behavior for a codec. * @@ -31,8 +33,6 @@ package com.google.devtools.build.lib.skyframe.serialization; * *

Implementations must have a default constructor. * - *

Multiple {@link CodecRegisterer} implementations for the same {@link ObjectCodec} is illegal. - * *

Inheriting {@link CodecRegisterer} through a superclass is illegal. It must be directly * implemented. Also, the generic parameter of {@link CodecRegisterer} must be reified. * @@ -40,5 +40,7 @@ package com.google.devtools.build.lib.skyframe.serialization; */ public interface CodecRegisterer> { - default void register(ObjectCodecRegistry.Builder builder) {} + default Iterable> getCodecsToRegister() { + return Collections.emptyList(); + } } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java index 1adb15047f..635b5ad755 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/CodecScanner.java @@ -122,15 +122,14 @@ public class CodecScanner { HashSet>> registered = new HashSet<>(); for (Class> registererType : registerers) { Class> objectCodecType = getObjectCodecType(registererType); - Preconditions.checkState( - !registered.contains(objectCodecType), - "%s has multiple associated CodecRegisterer definitions!", - objectCodecType); registered.add(objectCodecType); Constructor> constructor = (Constructor>) registererType.getDeclaredConstructor(); constructor.setAccessible(true); - constructor.newInstance().register(builder); + CodecRegisterer registerer = constructor.newInstance(); + for (ObjectCodec codec : registerer.getCodecsToRegister()) { + builder.add(codec); + } } return registered; } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java new file mode 100644 index 0000000000..d9f2ea4aed --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/EnumRuntimeCodec.java @@ -0,0 +1,58 @@ +// 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; + +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.nio.ByteBuffer; + +@SuppressWarnings("rawtypes") +class EnumRuntimeCodec implements ObjectCodec { + + @Override + public Class getEncodedClass() { + return Enum.class; + } + + @Override + public void serialize(SerializationContext unusedContext, Enum value, CodedOutputStream codedOut) + throws IOException, SerializationException { + // We're using Java serialization below because Enums are serializable by default and a + // hand-rolled version is going to be very similar. + ByteString.Output out = ByteString.newOutput(); + ObjectOutputStream objOut = new ObjectOutputStream(out); + objOut.writeObject(value); + codedOut.writeBytesNoTag(out.toByteString()); + } + + @Override + public Enum deserialize(DeserializationContext unusedContext, CodedInputStream codedIn) + throws SerializationException, IOException { + ByteBuffer buffer = codedIn.readByteBuffer(); + ObjectInputStream objIn = + new ObjectInputStream( + new ByteArrayInputStream(buffer.array(), buffer.arrayOffset(), buffer.remaining())); + try { + return (Enum) objIn.readObject(); + } catch (ClassNotFoundException e) { + throw new SerializationException("Couldn't find class for Enum deserialization?", e); + } + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java new file mode 100644 index 0000000000..e7cce534c8 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/MessageLiteCodec.java @@ -0,0 +1,64 @@ +// 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; + +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import com.google.protobuf.ExtensionRegistryLite; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.MessageLite; +import java.io.IOException; +import java.util.function.Supplier; + +/** Codec for protos. */ +public class MessageLiteCodec implements ObjectCodec { + + private final Supplier builderSupplier; + private final Class type; + + /** + * Constructor. + * + * @param builderSupplier reference to a proto's newBuilder method + */ + public MessageLiteCodec(Supplier builderSupplier) { + this.builderSupplier = builderSupplier; + this.type = builderSupplier.get().buildPartial().getClass(); + } + + @Override + public Class getEncodedClass() { + return type; + } + + @Override + public void serialize( + SerializationContext unusedContext, MessageLite message, CodedOutputStream codedOut) + throws IOException, SerializationException { + codedOut.writeMessageNoTag(message); + } + + @Override + public MessageLite deserialize(DeserializationContext unusedContext, CodedInputStream codedIn) + throws IOException, SerializationException { + try { + MessageLite.Builder builder = builderSupplier.get(); + codedIn.readMessage(builder, ExtensionRegistryLite.getEmptyRegistry()); + return builder.build(); + } catch (InvalidProtocolBufferException e) { + throw new SerializationException("Failed to parse proto of type " + type, e); + } + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java index 4b47b4bad0..a754246db6 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodec.java @@ -31,7 +31,7 @@ public interface ObjectCodec { *

This is useful for automatically dispatching to the correct codec, e.g. in {@link * ObjectCodecs}. */ - Class getEncodedClass(); + Class getEncodedClass(); /** * Returns additional subtypes of {@code T} that may be serialized/deserialized using this codec diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java new file mode 100644 index 0000000000..7e0faa0bdb --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/UUIDCodec.java @@ -0,0 +1,41 @@ +// 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; + +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; +import java.util.UUID; + +class UUIDCodec implements ObjectCodec { + + @Override + public Class getEncodedClass() { + return UUID.class; + } + + @Override + public void serialize(SerializationContext unusedContext, UUID uuid, CodedOutputStream codedOut) + throws SerializationException, IOException { + codedOut.writeInt64NoTag(uuid.getMostSignificantBits()); + codedOut.writeInt64NoTag(uuid.getLeastSignificantBits()); + } + + @Override + public UUID deserialize(DeserializationContext unusedContext, CodedInputStream codedIn) + throws SerializationException, IOException { + return new UUID(codedIn.readInt64(), codedIn.readInt64()); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java index 692fb9904a..330c2e4429 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/Marshallers.java @@ -26,11 +26,7 @@ import com.google.devtools.build.lib.skyframe.serialization.autocodec.Serializat import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationCodeGenerator.Marshaller; import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationCodeGenerator.PrimitiveValueSerializationCodeGenerator; import com.google.devtools.build.lib.skyframe.serialization.strings.StringCodecs; -import com.google.protobuf.AbstractMessage; -import com.google.protobuf.ExtensionRegistryLite; -import com.google.protobuf.ProtocolMessageEnum; import com.squareup.javapoet.TypeName; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -38,10 +34,7 @@ import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.UUID; -import java.util.regex.Pattern; import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; import javax.lang.model.type.ArrayType; import javax.lang.model.type.DeclaredType; @@ -331,70 +324,6 @@ class Marshallers { } }; - private final Marshaller enumMarshaller = - new Marshaller() { - @Override - public boolean matches(DeclaredType type) { - return env.getTypeUtils().asElement(type).getKind() == ElementKind.ENUM; - } - - @Override - public void addSerializationCode(Context context) { - if (isProtoEnum(context.getDeclaredType())) { - context.builder.addStatement("codedOut.writeInt32NoTag($L.getNumber())", context.name); - } else { - context.builder.addStatement("codedOut.writeInt32NoTag($L.ordinal())", context.name); - } - } - - @Override - public void addDeserializationCode(Context context) { - if (isProtoEnum(context.getDeclaredType())) { - context.builder.addStatement( - "$L = $T.forNumber(codedIn.readInt32())", context.name, context.getTypeName()); - } else { - // TODO(shahan): memoize this expensive call to values(). - context.builder.addStatement( - "$L = $T.values()[codedIn.readInt32()]", context.name, context.getTypeName()); - } - } - - private boolean isProtoEnum(DeclaredType type) { - return env.getTypeUtils() - .isSubtype( - type, - env.getElementUtils() - .getTypeElement(ProtocolMessageEnum.class.getCanonicalName()) - .asType()); - } - }; - - private static void addStringDeserializationCode(Context context) { - context.builder.addStatement( - "$L = $T.asciiOptimized().deserialize(context, codedIn)", context.name, StringCodecs.class); - } - - private final Marshaller stringMarshaller = - new Marshaller() { - @Override - public boolean matches(DeclaredType type) { - return matchesType(type, String.class); - } - - @Override - public void addSerializationCode(Context context) { - context.builder.addStatement( - "$T.asciiOptimized().serialize(context, $L, codedOut)", - StringCodecs.class, - context.name); - } - - @Override - public void addDeserializationCode(Context context) { - addStringDeserializationCode(context); - } - }; - private final Marshaller charSequenceMarshaller = new Marshaller() { @Override @@ -412,37 +341,10 @@ class Marshallers { @Override public void addDeserializationCode(Context context) { - addStringDeserializationCode(context); - } - }; - - private final Marshaller uuidMarshller = - new Marshaller() { - @Override - public boolean matches(DeclaredType type) { - return matchesType(type, UUID.class); - } - - @Override - public void addSerializationCode(Context context) { - context.builder.addStatement( - "codedOut.writeInt64NoTag($L.getMostSignificantBits())", context.name); - context.builder.addStatement( - "codedOut.writeInt64NoTag($L.getLeastSignificantBits())", context.name); - } - - @Override - public void addDeserializationCode(Context context) { - String mostSignificantBitsName = context.makeName("mostSignificantBits"); - String leastSignificantBitsName = context.makeName("leastSignificantBits"); - context.builder.addStatement("long $L = codedIn.readInt64()", mostSignificantBitsName); - context.builder.addStatement("long $L = codedIn.readInt64()", leastSignificantBitsName); context.builder.addStatement( - "$L = new $T($L, $L)", + "$L = $T.asciiOptimized().deserialize(context, codedIn)", context.name, - UUID.class, - mostSignificantBitsName, - leastSignificantBitsName); + StringCodecs.class); } }; @@ -737,63 +639,6 @@ class Marshallers { } }; - /** Since we cannot add a codec to {@link Pattern}, it needs to be supported natively. */ - private final Marshaller patternMarshaller = - new Marshaller() { - @Override - public boolean matches(DeclaredType type) { - return matchesType(type, Pattern.class); - } - - @Override - public void addSerializationCode(Context context) { - context.builder.addStatement( - "$T.asciiOptimized().serialize(context, $L.pattern(), codedOut)", - StringCodecs.class, - context.name); - context.builder.addStatement("codedOut.writeInt32NoTag($L.flags())", context.name); - } - - @Override - public void addDeserializationCode(Context context) { - context.builder.addStatement( - "$L = $T.compile(" - + "$T.asciiOptimized().deserialize(context, codedIn), " - + "codedIn.readInt32())", - context.name, - Pattern.class, - StringCodecs.class); - } - }; - - private final Marshaller protoMarshaller = - new Marshaller() { - @Override - public boolean matches(DeclaredType type) { - return isSubtype(type, AbstractMessage.class); - } - - @Override - public void addSerializationCode(Context context) { - context.builder.addStatement("codedOut.writeMessageNoTag($L)", context.name); - } - - @Override - public void addDeserializationCode(Context context) { - String builderName = context.makeName("builder"); - context.builder.addStatement( - "$T.Builder $L = $T.newBuilder()", - context.getTypeName(), - builderName, - context.getTypeName()); - context.builder.addStatement( - "codedIn.readMessage($L, $T.getEmptyRegistry())", - builderName, - ExtensionRegistryLite.class); - context.builder.addStatement("$L = $L.build()", context.name, builderName); - } - }; - /** Delegates marshalling back to the context. */ private final Marshaller contextMarshaller = new Marshaller() { @@ -813,31 +658,6 @@ class Marshallers { } }; - private final Marshaller charsetMarshaller = - new Marshaller() { - @Override - public boolean matches(DeclaredType type) { - return matchesType(type, Charset.class); - } - - @Override - public void addSerializationCode(Context context) { - context.builder.addStatement( - "$T.asciiOptimized().serialize(context, $L.name(), codedOut)", - StringCodecs.class, - context.name); - } - - @Override - public void addDeserializationCode(Context context) { - context.builder.addStatement( - "$L = $T.forName($T.asciiOptimized().deserialize(context, codedIn))", - context.name, - Charset.class, - StringCodecs.class); - } - }; - private final ImmutableList primitiveGenerators = ImmutableList.of( INT_CODE_GENERATOR, @@ -848,20 +668,14 @@ class Marshallers { private final ImmutableList marshallers = ImmutableList.of( - enumMarshaller, - stringMarshaller, charSequenceMarshaller, supplierMarshaller, - uuidMarshller, mapEntryMarshaller, listMarshaller, immutableSetMarshaller, immutableSortedSetMarshaller, mapMarshaller, multimapMarshaller, - patternMarshaller, - protoMarshaller, - charsetMarshaller, contextMarshaller); /** True when {@code type} has the same type as {@code clazz}. */ @@ -869,11 +683,6 @@ class Marshallers { return env.getTypeUtils().isSameType(type, getType(clazz)); } - /** True when {@code type} is a subtype of {@code clazz}. */ - private boolean isSubtype(TypeMirror type, Class clazz) { - return env.getTypeUtils().isSubtype(type, getType(clazz)); - } - /** True when erasure of {@code type} matches erasure of {@code clazz}. */ private boolean matchesErased(TypeMirror type, Class clazz) { return env.getTypeUtils() diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.java new file mode 100644 index 0000000000..f011669f17 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/CharsetCodec.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.strings; + +import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext; +import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; +import com.google.devtools.build.lib.skyframe.serialization.SerializationContext; +import com.google.devtools.build.lib.skyframe.serialization.SerializationException; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +class CharsetCodec implements ObjectCodec { + + @Override + public Class getEncodedClass() { + return Charset.class; + } + + @Override + public void serialize(SerializationContext context, Charset charset, CodedOutputStream codedOut) + throws SerializationException, IOException { + context.serialize(charset.name(), codedOut); + } + + @Override + public Charset deserialize(DeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException { + return Charset.forName(context.deserialize(codedIn)); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java new file mode 100644 index 0000000000..3d1d495ef5 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/PatternCodec.java @@ -0,0 +1,45 @@ +// 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.strings; + +import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext; +import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; +import com.google.devtools.build.lib.skyframe.serialization.SerializationContext; +import com.google.devtools.build.lib.skyframe.serialization.SerializationException; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; +import java.util.regex.Pattern; + +class PatternCodec implements ObjectCodec { + + @Override + public Class getEncodedClass() { + return Pattern.class; + } + + @Override + public void serialize(SerializationContext context, Pattern pattern, CodedOutputStream codedOut) + throws SerializationException, IOException { + context.serialize(pattern.pattern(), codedOut); + codedOut.writeInt32NoTag(pattern.flags()); + } + + @Override + public Pattern deserialize(DeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException { + return Pattern.compile(context.deserialize(codedIn), codedIn.readInt32()); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java index a5516a75a6..f9b294dac3 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecs.java @@ -16,7 +16,7 @@ package com.google.devtools.build.lib.skyframe.serialization.strings; import com.google.devtools.build.lib.skyframe.serialization.CodecRegisterer; import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; -import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecRegistry; +import java.util.Collections; import java.util.logging.Logger; /** Utility for accessing (potentially platform-specific) {@link String} {@link ObjectCodec}s. */ @@ -74,10 +74,11 @@ public final class StringCodecs { */ static class StringCodecRegisterer implements CodecRegisterer { @Override - public void register(ObjectCodecRegistry.Builder builder) { + public Iterable> getCodecsToRegister() { if (!supportsOptimizedAscii()) { - builder.add(simple()); + return Collections.singletonList(simple()); } + return Collections.emptyList(); } } @@ -88,10 +89,11 @@ public final class StringCodecs { */ static class FastStringCodecRegisterer implements CodecRegisterer { @Override - public void register(ObjectCodecRegistry.Builder builder) { + public Iterable> getCodecsToRegister() { if (supportsOptimizedAscii()) { - builder.add(asciiOptimized()); + return Collections.singletonList(asciiOptimized()); } + return Collections.emptyList(); } } } -- cgit v1.2.3