diff options
author | shahan <shahan@google.com> | 2018-03-14 17:08:08 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-14 17:09:34 -0700 |
commit | a1c2826e0fe95959d498b18d38eb8d2a7d45e55d (patch) | |
tree | 5310b0b9d18f5bd71dd443342d7b68af30332223 /src/main/java/com/google/devtools/build/lib/collect | |
parent | 92044990cdc318fa49cd769181ad9866a9d86ef6 (diff) |
Splits the iterableMarshaller into runtime codecs.
Makes NestedSetCodec into a runtime codec instead of a Marshaller.
PiperOrigin-RevId: 189110883
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/collect')
5 files changed, 102 insertions, 72 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/collect/BUILD b/src/main/java/com/google/devtools/build/lib/collect/BUILD index f8c7071081..e54e3266d1 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/BUILD +++ b/src/main/java/com/google/devtools/build/lib/collect/BUILD @@ -15,6 +15,7 @@ java_library( deps = [ "//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/skyframe/serialization/autocodec", "//third_party:guava", "//third_party:jsr305", ], diff --git a/src/main/java/com/google/devtools/build/lib/collect/IterableCodecs.java b/src/main/java/com/google/devtools/build/lib/collect/IterableCodecs.java new file mode 100644 index 0000000000..e61055ecfc --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/collect/IterableCodecs.java @@ -0,0 +1,97 @@ +// 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.collect; + +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +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.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import java.io.IOException; + +@SuppressWarnings("rawtypes") +class IterableCodecs { + static class FluentIterableCodec implements ObjectCodec<FluentIterable> { + @Override + public Class<FluentIterable> getEncodedClass() { + return FluentIterable.class; + } + + @Override + public void serialize( + SerializationContext context, FluentIterable obj, CodedOutputStream codedOut) + throws SerializationException, IOException { + IterableCodecs.serialize(context, obj, codedOut); + } + + @Override + public FluentIterable deserialize(DeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException { + return FluentIterable.from(IterableCodecs.deserialize(context, codedIn)); + } + } + + static class IterablesChainCodec implements ObjectCodec<IterablesChain> { + @Override + public Class<IterablesChain> getEncodedClass() { + return IterablesChain.class; + } + + @Override + public void serialize( + SerializationContext context, IterablesChain obj, CodedOutputStream codedOut) + throws SerializationException, IOException { + IterableCodecs.serialize(context, obj, codedOut); + } + + @Override + public IterablesChain deserialize(DeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException { + return new IterablesChain<>(IterableCodecs.deserialize(context, codedIn)); + } + } + + private static void serialize( + SerializationContext context, Iterable obj, CodedOutputStream codedOut) + throws SerializationException, IOException { + for (Object elt : obj) { + context.serialize(elt, codedOut); + } + context.serialize(DONE, codedOut); + } + + private static ImmutableList<Object> deserialize( + DeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException { + ImmutableList.Builder<Object> builder = ImmutableList.builder(); + for (Object next = context.deserialize(codedIn); + next != DONE; + next = context.deserialize(codedIn)) { + builder.add(next); + } + return builder.build(); + } + + /** + * An object used to mark the end of the sequence of elements. + * + * <p>We use this instead of emitting a count to avoid possibly running deduplication twice. + */ + @AutoCodec @AutoCodec.VisibleForSerialization static final Object DONE = new Object(); +} diff --git a/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java b/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java index 5fa4f8621e..fe0f4bbe0c 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java +++ b/src/main/java/com/google/devtools/build/lib/collect/IterablesChain.java @@ -40,7 +40,7 @@ public final class IterablesChain<T> implements Iterable<T> { private final Iterable<T> chain; - private IterablesChain(Iterable<T> chain) { + IterablesChain(Iterable<T> chain) { this.chain = chain; } @@ -103,7 +103,7 @@ public final class IterablesChain<T> implements Iterable<T> { } /** - * If this is called, the the resulting {@link IterablesChain} object uses a hash set to remove + * If this is called, the resulting {@link IterablesChain} object uses a hash set to remove * duplicate elements. */ public Builder<T> deduplicate() { diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/BUILD b/src/main/java/com/google/devtools/build/lib/collect/nestedset/BUILD index dc51b15368..bd372ff043 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/BUILD +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/BUILD @@ -12,6 +12,7 @@ java_library( srcs = [ "NestedSet.java", "NestedSetBuilder.java", + "NestedSetCodec.java", "NestedSetView.java", "NestedSetVisitor.java", "Order.java", @@ -19,22 +20,9 @@ java_library( deps = [ "//src/main/java/com/google/devtools/build/lib/collect/compacthashset", "//src/main/java/com/google/devtools/build/lib/concurrent", - "//third_party:guava", - "//third_party:jsr305", - ], -) - -java_library( - name = "serialization", - srcs = [ - "NestedSetCodec.java", - "NestedSetSerializer.java", - ], - deps = [ - ":nestedset", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization", - "//src/main/java/com/google/devtools/build/lib/skyframe/serialization:kryo", "//third_party:guava", + "//third_party:jsr305", "//third_party/protobuf:protobuf_java", ], ) diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetSerializer.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetSerializer.java deleted file mode 100644 index 3bb2552915..0000000000 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetSerializer.java +++ /dev/null @@ -1,56 +0,0 @@ -// 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.collect.nestedset; - -import com.esotericsoftware.kryo.Kryo; -import com.esotericsoftware.kryo.Serializer; -import com.esotericsoftware.kryo.io.Input; -import com.esotericsoftware.kryo.io.Output; - -/** - * {@link Serializer} for {@link NestedSet}. - * - * <p>Needed to handle {@link NestedSet}'s sentinel values correctly. - */ -public class NestedSetSerializer extends Serializer<NestedSet<Object>> { - - @Override - public void write(Kryo kryo, Output output, NestedSet<Object> nestedSet) { - kryo.writeObject(output, nestedSet.getOrder()); - Object children = nestedSet.rawChildren(); - if (children == NestedSet.EMPTY_CHILDREN) { - output.writeBoolean(false); - } else { - output.writeBoolean(true); - kryo.writeClassAndObject(output, children); - } - } - - @Override - public NestedSet<Object> read(Kryo kryo, Input input, Class<NestedSet<Object>> unusedType) { - Order order = kryo.readObject(input, Order.class); - if (input.readBoolean()) { - return new NestedSet<>(order, kryo.readClassAndObject(input)); - } else { - return new NestedSet<>(order, NestedSet.EMPTY_CHILDREN); - } - } - - public static void registerSerializers(Kryo kryo) { - kryo.register(NestedSet.class, new NestedSetSerializer()); - kryo.register(Order.class); - kryo.register(Object[].class); - } -} |