// 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.collect.nestedset;
import com.google.common.base.Preconditions;
import com.google.common.hash.Hashing;
import com.google.common.hash.HashingOutputStream;
import com.google.devtools.build.lib.skyframe.serialization.EnumCodec;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
/**
* Codec for {@link NestedSet}.
*
*
Nested sets are serialized by sorting the sub-graph in topological order, then writing the
* nodes in that order. As a node is written we remember its digest. When serializing a node higher
* in the graph, we replace any edge to another nested set with its digest.
*/
public class NestedSetCodec implements ObjectCodec> {
private static final EnumCodec orderCodec = new EnumCodec<>(Order.class);
private final ObjectCodec objectCodec;
public NestedSetCodec(ObjectCodec objectCodec) {
this.objectCodec = objectCodec;
}
@SuppressWarnings("unchecked")
@Override
public Class> getEncodedClass() {
// Compiler doesn't like cast from Class -> Class>, but it
// does allow what we see below. Type is lost at runtime anyway, so while gross this works.
return (Class>) ((Class>) NestedSet.class);
}
@Override
public void serialize(NestedSet obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
// Topo sort the nested set to ensure digests are available for children at time of writing
Collection