// Copyright 2017 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.common.collect.ImmutableMap;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
/**
* Wrapper for the minutiae of serializing and deserializing objects using {@link ObjectCodec}s,
* serving as a layer between the streaming-oriented {@link ObjectCodec} interface and users.
* Handles the mapping and selection of custom serialization implementations, falling back on less
* performant java serialization by default when no better option is available and it is allowed by
* the configuration.
*
*
To use, create a {@link ObjectCodecs.Builder} and add custom classifier to {@link ObjectCodec}
* mappings using {@link ObjectCodecs.Builder#add} directly or by using one of the convenience
* builders returned by {@link ObjectCodecs.Builder#asSkyFunctionNameKeyedBuilder()} or
* {@link ObjectCodecs.Builder#asClassKeyedBuilder()}. The provided mappings are then used to
* determine serialization/deserialization logic. For example:
*
*
{@code
* // Create an instance for which anything identified as "foo" will use FooCodec.
* ObjectCodecs objectCodecs = ObjectCodecs.newBuilder()
* .add("foo", new FooCodec())
* .build();
*
* // This will use the custom supplied FooCodec to serialize obj:
* ByteString serialized = objectCodecs.serialize("foo", obj);
* Object deserialized = objectCodecs.deserialize(ByteString.copyFromUtf8("foo"), serialized);
*
* // This will use default java object serialization to serialize obj:
* ByteString serialized = objectCodecs.serialize("bar", obj);
* Object deserialized = objectCodecs.deserialize(ByteString.copyFromUtf8("bar"), serialized);
* }
*
*
Classifiers will typically be class names or SkyFunction names.
*/
public class ObjectCodecs {
private static final ObjectCodec