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

org.gradle.api.internal.changedetection.state.OutputFilesCollectionSnapshot Maven / Gradle / Ivy

/*
 * Copyright 2016 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.api.internal.changedetection.state;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Iterators;
import org.gradle.api.internal.cache.StringInterner;
import org.gradle.api.internal.changedetection.rules.ChangeType;
import org.gradle.api.internal.changedetection.rules.FileChange;
import org.gradle.api.internal.changedetection.rules.TaskStateChange;
import org.gradle.api.internal.tasks.cache.TaskCacheKeyBuilder;
import org.gradle.internal.serialize.Decoder;
import org.gradle.internal.serialize.Encoder;
import org.gradle.internal.serialize.Serializer;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class OutputFilesCollectionSnapshot implements FileCollectionSnapshot {
    private final Map roots;
    private final FileCollectionSnapshot filesSnapshot;

    OutputFilesCollectionSnapshot(Map roots, FileCollectionSnapshot filesSnapshot) {
        this.roots = roots;
        this.filesSnapshot = filesSnapshot;
    }

    public Collection getFiles() {
        return filesSnapshot.getFiles();
    }

    @Override
    public Map getSnapshots() {
        return filesSnapshot.getSnapshots();
    }

    @Override
    public boolean isEmpty() {
        return filesSnapshot.isEmpty();
    }

    @Override
    public Iterator iterateContentChangesSince(FileCollectionSnapshot oldSnapshot, String fileType) {
        final OutputFilesCollectionSnapshot other = (OutputFilesCollectionSnapshot) oldSnapshot;
        final Iterator rootFileIdIterator = iterateRootFileIdChanges(other, fileType);
        final Iterator fileIterator = filesSnapshot.iterateContentChangesSince(other.filesSnapshot, fileType);
        return Iterators.concat(rootFileIdIterator, fileIterator);
    }

    @VisibleForTesting
    Map getRoots() {
        return roots;
    }

    FileCollectionSnapshot getFilesSnapshot() {
        return filesSnapshot;
    }

    private Iterator iterateRootFileIdChanges(final OutputFilesCollectionSnapshot other, final String fileType) {
        Map added = new HashMap(roots);
        added.keySet().removeAll(other.roots.keySet());
        final Iterator addedIterator = added.keySet().iterator();

        Map removed = new HashMap(other.roots);
        removed.keySet().removeAll(roots.keySet());
        final Iterator removedIterator = removed.keySet().iterator();

        Set changed = new HashSet();
        for (Map.Entry current : roots.entrySet()) {
            Boolean otherValue = other.roots.get(current.getKey());
            // Only care about roots that used to exist and have been removed
            if (otherValue != null && otherValue && !otherValue.equals(current.getValue())) {
                changed.add(current.getKey());
            }
        }
        final Iterator changedIterator = changed.iterator();

        return new AbstractIterator() {
            @Override
            protected TaskStateChange computeNext() {
                if (addedIterator.hasNext()) {
                    return new FileChange(addedIterator.next(), ChangeType.ADDED, fileType);
                }
                if (removedIterator.hasNext()) {
                    return new FileChange(removedIterator.next(), ChangeType.REMOVED, fileType);
                }
                if (changedIterator.hasNext()) {
                    return new FileChange(changedIterator.next(), ChangeType.MODIFIED, fileType);
                }

                return endOfData();
            }
        };
    }

    @Override
    public void appendToCacheKey(TaskCacheKeyBuilder builder) {
        filesSnapshot.appendToCacheKey(builder);
    }

    public static class SerializerImpl implements Serializer {
        private final Serializer serializer;
        private final StringInterner stringInterner;

        public SerializerImpl(Serializer serializer, StringInterner stringInterner) {
            this.serializer = serializer;
            this.stringInterner = stringInterner;
        }

        public OutputFilesCollectionSnapshot read(Decoder decoder) throws Exception {
            Map roots = new HashMap();
            int rootFileIdsCount = decoder.readSmallInt();
            for (int i = 0; i < rootFileIdsCount; i++) {
                String path = stringInterner.intern(decoder.readString());
                boolean exists = decoder.readBoolean();
                roots.put(path, exists);
            }
            FileCollectionSnapshot snapshot = serializer.read(decoder);
            return new OutputFilesCollectionSnapshot(roots, snapshot);
        }

        public void write(Encoder encoder, OutputFilesCollectionSnapshot value) throws Exception {
            int roots = value.roots.size();
            encoder.writeSmallInt(roots);
            for (Map.Entry entry : value.roots.entrySet()) {
                String path = entry.getKey();
                Boolean exists = entry.getValue();
                encoder.writeString(path);
                encoder.writeBoolean(exists);
            }
            serializer.write(encoder, value.filesSnapshot);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy