diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecRegistry.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecRegistry.java | 116 |
1 files changed, 24 insertions, 92 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecRegistry.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecRegistry.java index 920a7e2a64..80ec3281ae 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecRegistry.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ObjectCodecRegistry.java @@ -20,14 +20,13 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.skyframe.serialization.Memoizer.MemoizingCodec; -import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; import com.google.protobuf.CodedOutputStream; import java.io.IOException; +import java.util.Collection; import java.util.Comparator; import java.util.IdentityHashMap; import java.util.Map; -import java.util.Map.Entry; import javax.annotation.Nullable; /** @@ -41,8 +40,7 @@ public class ObjectCodecRegistry { return new Builder(); } - private final ImmutableMap<String, CodecDescriptor> stringMappedCodecs; - private final ImmutableMap<ByteString, CodecDescriptor> byteStringMappedCodecs; + private final ImmutableMap<Class<?>, CodecDescriptor> classMappedCodecs; private final ImmutableList<CodecDescriptor> tagMappedCodecs; @Nullable private final CodecDescriptor defaultCodecDescriptor; @@ -56,25 +54,24 @@ public class ObjectCodecRegistry { private final int constantsStartTag; private ObjectCodecRegistry( - Map<String, CodecHolder> codecs, + Map<Class<?>, CodecHolder> codecs, ImmutableSet<MemoizingCodec<?>> memoizingCodecs, ImmutableList<Object> constants, boolean allowDefaultCodec) { - ImmutableMap.Builder<String, CodecDescriptor> codecMappingsBuilder = ImmutableMap.builder(); + ImmutableMap.Builder<Class<?>, CodecDescriptor> codecMappingsBuilder = ImmutableMap.builder(); int nextTag = 1; // 0 is reserved for null. - for (String classifier : ImmutableList.sortedCopyOf(codecs.keySet())) { - codecMappingsBuilder.put(classifier, codecs.get(classifier).createDescriptor(nextTag)); + for (Class<?> clazz : + ImmutableList.sortedCopyOf(Comparator.comparing(Class::getName), codecs.keySet())) { + codecMappingsBuilder.put(clazz, codecs.get(clazz).createDescriptor(nextTag)); nextTag++; } - this.stringMappedCodecs = codecMappingsBuilder.build(); - - this.byteStringMappedCodecs = makeByteStringMappedCodecs(stringMappedCodecs); + this.classMappedCodecs = codecMappingsBuilder.build(); this.defaultCodecDescriptor = allowDefaultCodec ? new TypedCodecDescriptor<>(nextTag++, new JavaSerializableCodec()) : null; - this.tagMappedCodecs = makeTagMappedCodecs(stringMappedCodecs, defaultCodecDescriptor); + this.tagMappedCodecs = makeTagMappedCodecs(classMappedCodecs.values(), defaultCodecDescriptor); this.memoizingCodecsStartTag = nextTag; ImmutableMap.Builder<Class<?>, MemoizingCodecDescriptor<?>> memoizingCodecsBuilder = @@ -95,34 +92,6 @@ public class ObjectCodecRegistry { this.constants = constants; } - /** Returns the {@link CodecDescriptor} associated with the supplied classifier. */ - public CodecDescriptor getCodecDescriptor(String classifier) - throws SerializationException.NoCodecException { - CodecDescriptor result = stringMappedCodecs.getOrDefault(classifier, defaultCodecDescriptor); - if (result != null) { - return result; - } else { - throw new SerializationException.NoCodecException( - "No codec available for " + classifier + " and default fallback disabled"); - } - } - - /** - * Returns the {@link CodecDescriptor} associated with the supplied classifier. This method is a - * specialization of {@link #getCodecDescriptor(String)} for performance purposes. - */ - public CodecDescriptor getCodecDescriptor(ByteString classifier) - throws SerializationException.NoCodecException { - CodecDescriptor result = - byteStringMappedCodecs.getOrDefault(classifier, defaultCodecDescriptor); - if (result != null) { - return result; - } else { - throw new SerializationException.NoCodecException( - "No codec available for " + classifier.toStringUtf8() + " and default fallback disabled"); - } - } - /** * Returns a {@link CodecDescriptor} for the given type. * @@ -133,7 +102,7 @@ public class ObjectCodecRegistry { throws SerializationException.NoCodecException { // TODO(blaze-team): consider caching this traversal. for (Class<?> nextType = type; nextType != null; nextType = nextType.getSuperclass()) { - CodecDescriptor result = stringMappedCodecs.get(nextType.getName()); + CodecDescriptor result = classMappedCodecs.get(nextType); if (result != null) { return result; } @@ -205,8 +174,9 @@ public class ObjectCodecRegistry { public Builder getBuilder() { Builder builder = newBuilder(); builder.setAllowDefaultCodec(defaultCodecDescriptor != null); - for (Map.Entry<String, CodecDescriptor> entry : stringMappedCodecs.entrySet()) { - builder.add(entry.getKey(), entry.getValue().getCodec()); + for (Map.Entry<Class<?>, CodecDescriptor> entry : classMappedCodecs.entrySet()) { + // Cast here is safe because the original #add in the Builder was type-checked. + builder.add(entry.getKey(), getObjectCodec(entry.getValue())); } for (MemoizingCodecDescriptor<?> descriptor : tagMappedMemoizingCodecs) { @@ -219,6 +189,11 @@ public class ObjectCodecRegistry { return builder; } + @SuppressWarnings("unchecked") + private static ObjectCodec<Object> getObjectCodec(CodecDescriptor descriptor) { + return (ObjectCodec<Object>) descriptor.getCodec(); + } + /** Describes encoding logic. */ interface CodecDescriptor { void serialize(SerializationContext context, Object obj, CodedOutputStream codedOut) @@ -326,25 +301,15 @@ public class ObjectCodecRegistry { /** Builder for {@link ObjectCodecRegistry}. */ public static class Builder { - private final ImmutableMap.Builder<String, CodecHolder> codecsBuilder = ImmutableMap.builder(); + private final ImmutableMap.Builder<Class<?>, CodecHolder> codecsBuilder = + ImmutableMap.builder(); private final ImmutableSet.Builder<MemoizingCodec<?>> memoizingCodecBuilder = ImmutableSet.builder(); private final ImmutableList.Builder<Object> constantsBuilder = ImmutableList.builder(); private boolean allowDefaultCodec = true; - /** - * Add custom serialization strategy ({@code codec}) for {@code classifier}. - * - * <p>Intended for package-internal usage only. Consider using the specialized build types - * returned by {@link #asClassKeyedBuilder()} before using this method. - */ - <T> Builder add(String classifier, ObjectCodec<T> codec) { - codecsBuilder.put(classifier, new TypedCodecHolder<>(codec)); - return this; - } - public <T> Builder add(Class<? extends T> type, ObjectCodec<T> codec) { - add(type.getName(), codec); + codecsBuilder.put(type, new TypedCodecHolder<>(codec)); return this; } @@ -366,11 +331,6 @@ public class ObjectCodecRegistry { return this; } - /** Wrap this builder with a {@link ClassKeyedBuilder}. */ - public ClassKeyedBuilder asClassKeyedBuilder() { - return new ClassKeyedBuilder(this); - } - public ObjectCodecRegistry build() { return new ObjectCodecRegistry( codecsBuilder.build(), @@ -380,40 +340,12 @@ public class ObjectCodecRegistry { } } - /** Convenience builder for adding codecs classified by class name. */ - static class ClassKeyedBuilder { - private final Builder underlying; - - private ClassKeyedBuilder(Builder underlying) { - this.underlying = underlying; - } - - public <T> ClassKeyedBuilder add(Class<? extends T> clazz, ObjectCodec<T> codec) { - underlying.add(clazz, codec); - return this; - } - - public ObjectCodecRegistry build() { - return underlying.build(); - } - } - - private static ImmutableMap<ByteString, CodecDescriptor> makeByteStringMappedCodecs( - Map<String, CodecDescriptor> stringMappedCodecs) { - ImmutableMap.Builder<ByteString, CodecDescriptor> result = ImmutableMap.builder(); - for (Entry<String, CodecDescriptor> entry : stringMappedCodecs.entrySet()) { - result.put(ByteString.copyFromUtf8(entry.getKey()), entry.getValue()); - } - return result.build(); - } - private static ImmutableList<CodecDescriptor> makeTagMappedCodecs( - Map<String, CodecDescriptor> codecs, - @Nullable CodecDescriptor defaultCodecDescriptor) { + Collection<CodecDescriptor> codecs, @Nullable CodecDescriptor defaultCodecDescriptor) { CodecDescriptor[] codecTable = new CodecDescriptor[codecs.size() + (defaultCodecDescriptor != null ? 1 : 0)]; - for (Entry<String, CodecDescriptor> entry : codecs.entrySet()) { - codecTable[entry.getValue().getTag() - 1] = entry.getValue(); + for (CodecDescriptor codecDescriptor : codecs) { + codecTable[codecDescriptor.getTag() - 1] = codecDescriptor; } if (defaultCodecDescriptor != null) { |