From 99c56a5fa5d77e5db86686e3e4f2501c7760c26e Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 13 Nov 2023 09:40:08 -0800 Subject: [PATCH] Add explicit AsyncObjectCodec, clarifying its usage. Codecs that derive from AsyncObjectCodec may only call the subset of methods in DeserializationContext that are also in AsyncDeserializationContext, which designates them as being compatible with async serialization. Nothing is actually asynchronous yet. PiperOrigin-RevId: 581993442 Change-Id: I866eb33fd104ea863933d24d3baebed94052f652 --- .../serialization/AsyncObjectCodec.java | 40 +++++++++++++++++++ .../skyframe/serialization/DynamicCodec.java | 9 +---- 2 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/serialization/AsyncObjectCodec.java diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/AsyncObjectCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/AsyncObjectCodec.java new file mode 100644 index 00000000000000..ba66b6e67e83d7 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/AsyncObjectCodec.java @@ -0,0 +1,40 @@ +// Copyright 2023 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 java.io.IOException; + +/** {@link ObjectCodec} that uses only {@link AsyncDeserializationContext}. */ +public abstract class AsyncObjectCodec implements ObjectCodec { + + @Override + public final MemoizationStrategy getStrategy() { + return MemoizationStrategy.MEMOIZE_BEFORE; + } + + /** Adapter for synchronous contexts. */ + @Override + public final T deserialize(DeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException { + return deserializeAsync(context, codedIn); + } + + /** + * This has the same contract as {@link #deserialize}, but narrows the {@code context} API to + * methods that are compatible with async deserialization. + */ + public abstract T deserializeAsync(AsyncDeserializationContext context, CodedInputStream codedIn) + throws SerializationException, IOException; +} diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java index ede98139cd88cb..e739fcf5d915d4 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java @@ -30,7 +30,7 @@ import java.util.Comparator; /** A codec that serializes arbitrary types. */ -public final class DynamicCodec implements ObjectCodec { +public final class DynamicCodec extends AsyncObjectCodec { private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); @@ -47,11 +47,6 @@ public Class getEncodedClass() { return type; } - @Override - public MemoizationStrategy getStrategy() { - return ObjectCodec.MemoizationStrategy.MEMOIZE_BEFORE; - } - @Override @SuppressWarnings("LogAndThrow") // Want the full stack trace. public void serialize(SerializationContext context, Object obj, CodedOutputStream codedOut) @@ -70,7 +65,7 @@ public void serialize(SerializationContext context, Object obj, CodedOutputStrea @Override @SuppressWarnings("LogAndThrow") // Want the full stack trace. - public Object deserialize(DeserializationContext context, CodedInputStream codedIn) + public Object deserializeAsync(AsyncDeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { Object instance; try {