diff options
author | 2018-02-27 13:38:36 -0800 | |
---|---|---|
committer | 2018-02-27 13:40:44 -0800 | |
commit | c5e36f668ae953dfec24850522b10f79bc686945 (patch) | |
tree | 92e51bcc828a873f4bd77e9df4730fcab3450334 | |
parent | 81a95c2f8bdb0080741cbc6ce6d007228f81bf37 (diff) |
Removes the need to map SkyFunctionName to codec in SkyKeySerializer
* FILE_SYMLINK* codecs need ImmutableList so this adds an ImmutableList runtime codec.
* Adds SkyFunction names where codecs exist for their SkyKeys.
* Adds a few @AutoCodec singleton codecs.
We cannot yet disable the ImmutableList marshaller because currently the
runtime ImmutableList codec requires runtime codecs for the contained elements, which doesn't hold everywhere (particularly for protos).
PiperOrigin-RevId: 187224736
4 files changed, 72 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/CollectTargetsInPackageValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/CollectTargetsInPackageValue.java index 83255267ee..1ed9592eb8 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/CollectTargetsInPackageValue.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/CollectTargetsInPackageValue.java @@ -40,8 +40,10 @@ public class CollectTargetsInPackageValue implements SkyValue { } /** {@link SkyKey} argument. */ + @AutoCodec @AutoValue public abstract static class CollectTargetsInPackageKey implements Serializable { + @AutoCodec.Instantiator public static CollectTargetsInPackageKey create( PackageIdentifier packageId, FilteringPolicy filteringPolicy) { return new AutoValue_CollectTargetsInPackageValue_CollectTargetsInPackageKey( diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameValue.java index 92204961ed..ea0bd94b3a 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameValue.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceNameValue.java @@ -14,6 +14,7 @@ package com.google.devtools.build.lib.skyframe; import com.google.common.base.Preconditions; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.skyframe.LegacySkyKey; import com.google.devtools.build.skyframe.SkyKey; import com.google.devtools.build.skyframe.SkyValue; @@ -76,7 +77,7 @@ public class WorkspaceNameValue implements SkyValue { /** Singleton class used as the {@link SkyKey#argument} for {@link WorkspaceNameValue#key}. */ public static final class DummyArgument { static final int HASHCODE = DummyArgument.class.getCanonicalName().hashCode(); - public static final DummyArgument INSTANCE = new DummyArgument(); + @AutoCodec public static final DummyArgument INSTANCE = new DummyArgument(); private DummyArgument() { } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableListRuntimeCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableListRuntimeCodec.java new file mode 100644 index 0000000000..e051af6331 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableListRuntimeCodec.java @@ -0,0 +1,56 @@ +// 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.common.collect.ImmutableList; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; + +/** + * A {@link ObjectCodec} for {@link ImmutableList}. + * + * <p>This differs from {@link ImmutableListCodec} in that this uses the runtime type of contained + * objects for serialization. + */ +@SuppressWarnings("rawtypes") // Intentional erasure of ImmutableList. +class ImmutableListRuntimeCodec implements ObjectCodec<ImmutableList> { + + @Override + public Class<ImmutableList> getEncodedClass() { + return ImmutableList.class; + } + + @Override + public void serialize( + SerializationContext context, ImmutableList object, CodedOutputStream codedOut) + throws IOException, SerializationException { + codedOut.writeInt32NoTag(object.size()); + for (Object obj : object) { + context.serialize(obj, codedOut); + } + } + + @Override + public ImmutableList deserialize(DeserializationContext context, CodedInputStream codedIn) + throws IOException, SerializationException { + int size = codedIn.readInt32(); + Object[] list = new Object[size]; + for (int i = 0; i < size; ++i) { + list[i] = context.deserialize(codedIn); + } + return ImmutableList.copyOf(list); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecs.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecs.java index db996325f2..cc7052501c 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecs.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecs.java @@ -55,6 +55,14 @@ public class ObjectCodecs { } } + public void serialize(Object subject, CodedOutputStream codedOut) throws SerializationException { + try { + serializationContext.serialize(subject, codedOut); + } catch (IOException e) { + throw new SerializationException("Failed to serialize " + subject, e); + } + } + /** * Serialize {@code subject}, using the serialization strategy determined by {@code classifier}, * returning a {@link ByteString} containing the serialized representation. @@ -92,7 +100,10 @@ public class ObjectCodecs { } public Object deserialize(ByteString data) throws SerializationException { - CodedInputStream codedIn = data.newCodedInput(); + return deserialize(data.newCodedInput()); + } + + public Object deserialize(CodedInputStream codedIn) throws SerializationException { try { return deserializationContext.deserialize(codedIn); } catch (IOException e) { |