Skip to content

Commit

Permalink
Add explicit AsyncObjectCodec, clarifying its usage.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aoeui authored and copybara-github committed Nov 13, 2023
1 parent 0b1be1c commit 99c56a5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<T> implements ObjectCodec<T> {

@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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Comparator;

/** A codec that serializes arbitrary types. */
public final class DynamicCodec implements ObjectCodec<Object> {
public final class DynamicCodec extends AsyncObjectCodec<Object> {

private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();

Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 99c56a5

Please sign in to comment.