Adds a CODECs for Package and PackageValue. PiperOrigin-RevId: 181624201
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java index 1bceb6b..985dedf 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/Package.java +++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java
@@ -35,11 +35,15 @@ import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.AttributeMap.AcceptsLabelAttribute; import com.google.devtools.build.lib.packages.License.DistributionType; +import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodec; +import com.google.devtools.build.lib.skyframe.serialization.SerializationException; import com.google.devtools.build.lib.syntax.SkylarkSemantics; import com.google.devtools.build.lib.util.SpellChecker; import com.google.devtools.build.lib.vfs.Canonicalizer; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; @@ -53,16 +57,18 @@ import javax.annotation.Nullable; /** - * A package, which is a container of {@link Rule}s, each of - * which contains a dictionary of named attributes. + * A package, which is a container of {@link Rule}s, each of which contains a dictionary of named + * attributes. * - * <p>Package instances are intended to be immutable and for all practical - * purposes can be treated as such. Note, however, that some member variables - * exposed via the public interface are not strictly immutable, so until their - * types are guaranteed immutable we're not applying the {@code @Immutable} - * annotation here. + * <p>Package instances are intended to be immutable and for all practical purposes can be treated + * as such. Note, however, that some member variables exposed via the public interface are not + * strictly immutable, so until their types are guaranteed immutable we're not applying the + * {@code @Immutable} annotation here. */ +@SuppressWarnings("JavaLangClash") public class Package { + public static final InjectingObjectCodec<Package, PackageCodecDependencies> CODEC = + new PackageCodec(); /** * Common superclass for all name-conflict exceptions. @@ -1552,4 +1558,33 @@ return message + ", defined at " + target.getLocation(); } } + + /** Package codec implementation. */ + private static final class PackageCodec + implements InjectingObjectCodec<Package, PackageCodecDependencies> { + @Override + public Class<Package> getEncodedClass() { + return Package.class; + } + + @Override + public void serialize( + PackageCodecDependencies codecDeps, Package input, CodedOutputStream codedOut) + throws IOException { + codecDeps.getPackageSerializer().serialize(input, codedOut); + } + + @Override + public Package deserialize(PackageCodecDependencies codecDeps, CodedInputStream codedIn) + throws SerializationException, IOException { + try { + return codecDeps.getPackageDeserializer().deserialize(codedIn); + } catch (PackageDeserializationException e) { + throw new SerializationException("Failed to deserialize Package", e); + } catch (InterruptedException e) { + throw new IllegalStateException( + "Unexpected InterruptedException during Package deserialization", e); + } + } + } }
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageCodecDependencies.java b/src/main/java/com/google/devtools/build/lib/packages/PackageCodecDependencies.java new file mode 100644 index 0000000..52dc91b --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageCodecDependencies.java
@@ -0,0 +1,46 @@ +// 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.packages; + +/** Dependencies of {@value Package#Codec}. */ +public interface PackageCodecDependencies { + + PackageSerializerInterface getPackageSerializer(); + + PackageDeserializerInterface getPackageDeserializer(); + + /** Simplest implementation of PackageCodecDependencies. */ + public static class SimplePackageCodecDependencies implements PackageCodecDependencies { + private final PackageSerializerInterface packageSerializer; + private final PackageDeserializerInterface packageDeserializer; + + public SimplePackageCodecDependencies( + PackageSerializerInterface packageSerializer, + PackageDeserializerInterface packageDeserializer) { + this.packageSerializer = packageSerializer; + this.packageDeserializer = packageDeserializer; + } + + @Override + public PackageSerializerInterface getPackageSerializer() { + return packageSerializer; + } + + @Override + public PackageDeserializerInterface getPackageDeserializer() { + return packageDeserializer; + } + } +}
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializationException.java b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializationException.java new file mode 100644 index 0000000..992c2ab --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializationException.java
@@ -0,0 +1,30 @@ +// 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.packages; + +/** Exception thrown when something goes wrong during package deserialization. */ +public class PackageDeserializationException extends Exception { + PackageDeserializationException(String message) { + super(message); + } + + PackageDeserializationException(String message, Exception reason) { + super(message, reason); + } + + PackageDeserializationException(Exception reason) { + super(reason); + } +}
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializerInterface.java b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializerInterface.java new file mode 100644 index 0000000..ea74a4d --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageDeserializerInterface.java
@@ -0,0 +1,39 @@ +// 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.packages; + +import com.google.protobuf.CodedInputStream; +import java.io.IOException; + +/** + * Interface for Package deserialization. + * + * <p>Provides a layer of indirection for breaking circular dependencies. + */ +public interface PackageDeserializerInterface { + + /** + * Deserializes a {@link Package} from {@code codedIn}. The inverse of {@link + * PackageSerializer#serialize}. + * + * @param codedIn stream to read from + * @return a new {@link Package} as read from {@code codedIn} + * @throws PackageDeserializationException on failures deserializing the input + * @throws IOException on failures reading from {@code codedIn} + * @throws InterruptedException + */ + Package deserialize(CodedInputStream codedIn) + throws PackageDeserializationException, IOException, InterruptedException; +}
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageSerializerInterface.java b/src/main/java/com/google/devtools/build/lib/packages/PackageSerializerInterface.java new file mode 100644 index 0000000..af25075 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageSerializerInterface.java
@@ -0,0 +1,35 @@ +// 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.packages; + +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; + +/** + * Abstraction layer for Package serialization. + * + * <p>Provides a layer of indirection for breaking circular dependencies. + */ +public interface PackageSerializerInterface { + /** + * Serialize a package to {@code codedOut}. The inverse of {@link + * PackageDeserializer#deserialize}. + * + * @param pkg the {@link Package} to be serialized + * @param codedOut the stream to write {@code pkg}'s serialized representation to + * @throws IOException on failure writing to {@code codedOut} + */ + void serialize(Package pkg, CodedOutputStream codedOut) throws IOException; +}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageValue.java index c5d9094..f31bf09 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageValue.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageValue.java
@@ -19,18 +19,22 @@ import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; import com.google.devtools.build.lib.packages.Package; +import com.google.devtools.build.lib.packages.PackageCodecDependencies; +import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodec; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.skyframe.LegacySkyKey; import com.google.devtools.build.skyframe.NotComparableSkyValue; import com.google.devtools.build.skyframe.SkyKey; import java.util.ArrayList; import java.util.List; -/** - * A Skyframe value representing a package. - */ +/** A Skyframe value representing a package. */ +@AutoCodec(dependency = PackageCodecDependencies.class) @Immutable @ThreadSafe public class PackageValue implements NotComparableSkyValue { + public static final InjectingObjectCodec<PackageValue, PackageCodecDependencies> CODEC = + new PackageValue_AutoCodec(); private final Package pkg; @@ -65,5 +69,4 @@ } return keys; } - }
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/BUILD b/src/test/java/com/google/devtools/build/lib/skyframe/BUILD index a4b01da..e9cfbfd 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/BUILD +++ b/src/test/java/com/google/devtools/build/lib/skyframe/BUILD
@@ -78,6 +78,7 @@ "//src/main/java/com/google/devtools/build/lib/collect/nestedset", "//src/main/java/com/google/devtools/build/lib/concurrent", "//src/main/java/com/google/devtools/build/lib/rules/cpp", + "//src/main/java/com/google/devtools/build/lib/skyframe/serialization", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs", @@ -97,6 +98,7 @@ "//third_party:junit4", "//third_party:mockito", "//third_party:truth", + "//third_party/protobuf:protobuf_java", ], )
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/PackageValueTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/PackageValueTest.java new file mode 100644 index 0000000..2947249 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skyframe/PackageValueTest.java
@@ -0,0 +1,85 @@ +// 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; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.devtools.build.lib.packages.Package; +import com.google.devtools.build.lib.packages.PackageCodecDependencies.SimplePackageCodecDependencies; +import com.google.devtools.build.lib.packages.PackageDeserializationException; +import com.google.devtools.build.lib.packages.PackageDeserializerInterface; +import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodecAdapter; +import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; +import com.google.devtools.build.lib.skyframe.serialization.SerializationException; +import com.google.protobuf.CodedInputStream; +import java.io.IOException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.ArgumentCaptor; + +/** Basic tests for {@link PackageValue}. */ +@RunWith(JUnit4.class) +public class PackageValueTest { + + private PackageDeserializerInterface mockDeserializer; + private ObjectCodec<PackageValue> underTest; + + @Before + public void setUp() { + this.mockDeserializer = mock(PackageDeserializerInterface.class); + this.underTest = + new InjectingObjectCodecAdapter<>( + PackageValue.CODEC, new SimplePackageCodecDependencies(null, mockDeserializer)); + } + + @Test + public void testDeserializationIsDelegatedToPackageDeserializer() + throws SerializationException, IOException, PackageDeserializationException, + InterruptedException { + // Mock because all we need is to verify that we're properly delegating to Package deserializer. + Package mockPackage = mock(Package.class); + + when(mockDeserializer.deserialize(ArgumentCaptor.forClass(CodedInputStream.class).capture())) + .thenReturn(mockPackage); + + CodedInputStream codedIn = CodedInputStream.newInstance(new byte[] {1, 2, 3, 4}); + PackageValue result = underTest.deserialize(codedIn); + + assertThat(result.getPackage()).isSameAs(mockPackage); + } + + @Test + public void testInterruptedExceptionRaisesIllegalStateException() throws Exception { + InterruptedException staged = new InterruptedException("Stop that!"); + doThrow(staged).when(mockDeserializer).deserialize(any(CodedInputStream.class)); + + try { + underTest.deserialize(CodedInputStream.newInstance(new byte[] {1, 2, 3, 4})); + fail("Expected exception"); + } catch (IllegalStateException e) { + assertThat(e) + .hasMessageThat() + .isEqualTo("Unexpected InterruptedException during Package deserialization"); + assertThat(e).hasCauseThat().isSameAs(staged); + } + } +}