All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.internal.execution.history.impl.DefaultPreviousExecutionStateSerializer Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2018 the original author or authors.
 *
 * 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 org.gradle.internal.execution.history.impl;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import org.gradle.caching.internal.origin.OriginMetadata;
import org.gradle.internal.execution.history.AfterPreviousExecutionState;
import org.gradle.internal.fingerprint.FileCollectionFingerprint;
import org.gradle.internal.id.UniqueId;
import org.gradle.internal.serialize.AbstractSerializer;
import org.gradle.internal.serialize.Decoder;
import org.gradle.internal.serialize.Encoder;
import org.gradle.internal.serialize.Serializer;
import org.gradle.internal.snapshot.ValueSnapshot;
import org.gradle.internal.snapshot.impl.ImplementationSnapshot;
import org.gradle.internal.snapshot.impl.SnapshotSerializer;

import java.util.Map;

public class DefaultPreviousExecutionStateSerializer extends AbstractSerializer {
    private final Serializer fileCollectionFingerprintSerializer;
    private final Serializer implementationSnapshotSerializer;
    private final Serializer valueSnapshotSerializer = new SnapshotSerializer();

    public DefaultPreviousExecutionStateSerializer(Serializer fileCollectionFingerprintSerializer) {
        this.fileCollectionFingerprintSerializer = fileCollectionFingerprintSerializer;
        this.implementationSnapshotSerializer = new ImplementationSnapshot.SerializerImpl();
    }

    public AfterPreviousExecutionState read(Decoder decoder) throws Exception {
        OriginMetadata originMetadata = new OriginMetadata(
            UniqueId.from(decoder.readString()),
            decoder.readLong()
        );

        ImplementationSnapshot taskImplementation = implementationSnapshotSerializer.read(decoder);

        // We can't use an immutable list here because some hashes can be null
        int taskActionsCount = decoder.readSmallInt();
        ImmutableList.Builder taskActionImplementationsBuilder = ImmutableList.builder();
        for (int j = 0; j < taskActionsCount; j++) {
            ImplementationSnapshot actionImpl = implementationSnapshotSerializer.read(decoder);
            taskActionImplementationsBuilder.add(actionImpl);
        }
        ImmutableList taskActionImplementations = taskActionImplementationsBuilder.build();

        ImmutableSortedMap inputProperties = readInputProperties(decoder);
        ImmutableSortedMap inputFilesFingerprints = readFingerprints(decoder);
        ImmutableSortedMap outputFilesFingerprints = readFingerprints(decoder);

        boolean successful = decoder.readBoolean();

        return new DefaultAfterPreviousExecutionState(
            originMetadata,
            taskImplementation,
            taskActionImplementations,
            inputProperties,
            inputFilesFingerprints,
            outputFilesFingerprints,
            successful
        );
    }

    public void write(Encoder encoder, AfterPreviousExecutionState execution) throws Exception {
        OriginMetadata originMetadata = execution.getOriginMetadata();
        encoder.writeString(originMetadata.getBuildInvocationId().asString());
        encoder.writeLong(originMetadata.getExecutionTime());

        implementationSnapshotSerializer.write(encoder, execution.getImplementation());
        ImmutableList additionalImplementations = execution.getAdditionalImplementations();
        encoder.writeSmallInt(additionalImplementations.size());
        for (ImplementationSnapshot actionImpl : additionalImplementations) {
            implementationSnapshotSerializer.write(encoder, actionImpl);
        }

        writeInputProperties(encoder, execution.getInputProperties());
        writeFingerprints(encoder, execution.getInputFileProperties());
        writeFingerprints(encoder, execution.getOutputFileProperties());

        encoder.writeBoolean(execution.isSuccessful());
    }

    public ImmutableSortedMap readInputProperties(Decoder decoder) throws Exception {
        int size = decoder.readSmallInt();
        if (size == 0) {
            return ImmutableSortedMap.of();
        }
        if (size == 1) {
            return ImmutableSortedMap.of(decoder.readString(), readValueSnapshot(decoder));
        }

        ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder();
        for (int i = 0; i < size; i++) {
            builder.put(decoder.readString(), readValueSnapshot(decoder));
        }
        return builder.build();
    }

    public void writeInputProperties(Encoder encoder, ImmutableMap properties) throws Exception {
        encoder.writeSmallInt(properties.size());
        for (Map.Entry entry : properties.entrySet()) {
            encoder.writeString(entry.getKey());
            writeValueSnapshot(encoder, entry.getValue());
        }
    }

    private ImmutableSortedMap readFingerprints(Decoder decoder) throws Exception {
        int count = decoder.readSmallInt();
        ImmutableSortedMap.Builder builder = ImmutableSortedMap.naturalOrder();
        for (int fingerprintIdx = 0; fingerprintIdx < count; fingerprintIdx++) {
            String property = decoder.readString();
            FileCollectionFingerprint fingerprint = fileCollectionFingerprintSerializer.read(decoder);
            builder.put(property, fingerprint);
        }
        return builder.build();
    }

    private void writeFingerprints(Encoder encoder, Map fingerprints) throws Exception {
        encoder.writeSmallInt(fingerprints.size());
        for (Map.Entry entry : fingerprints.entrySet()) {
            encoder.writeString(entry.getKey());
            fileCollectionFingerprintSerializer.write(encoder, entry.getValue());
        }
    }

    private ValueSnapshot readValueSnapshot(Decoder decoder) throws Exception {
        return valueSnapshotSerializer.read(decoder);
    }

    private void writeValueSnapshot(Encoder encoder, ValueSnapshot snapshot) throws Exception {
        valueSnapshotSerializer.write(encoder, snapshot);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy