@Override
 public Iterator<TaskStateChange> iterateContentChangesSince(
     FileCollectionSnapshot oldSnapshot, String fileType) {
   final OutputFilesSnapshot other = (OutputFilesSnapshot) oldSnapshot;
   final Iterator<TaskStateChange> rootFileIdIterator =
       iterateRootFileIdChanges(other, fileType);
   final Iterator<TaskStateChange> fileIterator =
       filesSnapshot.iterateContentChangesSince(other.filesSnapshot, fileType);
   return Iterators.concat(rootFileIdIterator, fileIterator);
 }
  /**
   * Returns a new snapshot that filters out entries that should not be considered outputs of the
   * task.
   */
  public OutputFilesCollectionSnapshot createOutputSnapshot(
      FileCollectionSnapshot afterPreviousExecution,
      FileCollectionSnapshot beforeExecution,
      FileCollectionSnapshot afterExecution,
      FileCollection roots) {
    FileCollectionSnapshot filesSnapshot;
    Map<String, NormalizedFileSnapshot> afterSnapshots = afterExecution.getSnapshots();
    if (!beforeExecution.getSnapshots().isEmpty() && !afterSnapshots.isEmpty()) {
      Map<String, NormalizedFileSnapshot> beforeSnapshots = beforeExecution.getSnapshots();
      Map<String, NormalizedFileSnapshot> afterPreviousSnapshots =
          afterPreviousExecution != null
              ? afterPreviousExecution.getSnapshots()
              : new HashMap<String, NormalizedFileSnapshot>();
      int newEntryCount = 0;
      ImmutableMap.Builder<String, NormalizedFileSnapshot> outputEntries = ImmutableMap.builder();

      for (Map.Entry<String, NormalizedFileSnapshot> entry : afterSnapshots.entrySet()) {
        final String path = entry.getKey();
        NormalizedFileSnapshot fileSnapshot = entry.getValue();
        if (isOutputEntry(path, fileSnapshot, beforeSnapshots, afterPreviousSnapshots)) {
          outputEntries.put(entry.getKey(), fileSnapshot);
          newEntryCount++;
        }
      }
      // Are all files snapshot after execution accounted for as new entries?
      if (newEntryCount == afterSnapshots.size()) {
        filesSnapshot = unwrap(afterExecution);
      } else {
        filesSnapshot =
            new DefaultFileCollectionSnapshot(
                outputEntries.build(), TaskFilePropertyCompareType.OUTPUT);
      }
    } else {
      filesSnapshot = unwrap(afterExecution);
    }
    return new OutputFilesCollectionSnapshot(getRoots(roots), filesSnapshot);
  }
  /** Returns a new snapshot that ignores new files between 2 previous snapshots */
  public OutputFilesSnapshot createOutputSnapshot(
      FileCollectionSnapshot afterPreviousExecution,
      FileCollectionSnapshot beforeExecution,
      FileCollectionSnapshot afterExecution,
      FileCollection roots) {
    FileCollectionSnapshot filesSnapshot;
    if (!beforeExecution.getSnapshots().isEmpty() && !afterExecution.getSnapshots().isEmpty()) {
      Map<String, IncrementalFileSnapshot> beforeSnapshots = beforeExecution.getSnapshots();
      Map<String, IncrementalFileSnapshot> previousSnapshots =
          afterPreviousExecution != null
              ? afterPreviousExecution.getSnapshots()
              : new HashMap<String, IncrementalFileSnapshot>();
      List<Map.Entry<String, IncrementalFileSnapshot>> newEntries =
          new ArrayList<Map.Entry<String, IncrementalFileSnapshot>>(
              afterExecution.getSnapshots().size());

      for (Map.Entry<String, IncrementalFileSnapshot> entry :
          afterExecution.getSnapshots().entrySet()) {
        final String path = entry.getKey();
        IncrementalFileSnapshot otherFile = beforeSnapshots.get(path);
        if (otherFile == null
            || !entry.getValue().isContentAndMetadataUpToDate(otherFile)
            || previousSnapshots.containsKey(path)) {
          newEntries.add(entry);
        }
      }
      if (newEntries.size() == afterExecution.getSnapshots().size()) {
        filesSnapshot = afterExecution;
      } else {
        ImmutableMap.Builder<String, IncrementalFileSnapshot> newSnapshots = ImmutableMap.builder();
        for (Map.Entry<String, IncrementalFileSnapshot> entry : newEntries) {
          newSnapshots.put(entry.getKey(), entry.getValue());
        }
        filesSnapshot =
            new FileCollectionSnapshotImpl(
                newSnapshots.build(), TaskFilePropertyCompareType.OUTPUT);
      }
    } else {
      filesSnapshot = afterExecution;
    }
    if (filesSnapshot instanceof OutputFilesSnapshot) {
      filesSnapshot = ((OutputFilesSnapshot) filesSnapshot).filesSnapshot;
    }
    return new OutputFilesSnapshot(getRoots(roots), filesSnapshot);
  }
Exemplo n.º 4
0
  public TaskUpToDateState(
      TaskInternal task,
      TaskHistoryRepository.History history,
      FileCollectionSnapshotter outputFilesSnapshotter,
      FileCollectionSnapshotter inputFilesSnapshotter,
      FileCollectionSnapshotter discoveredFilesSnapshotter) {
    TaskExecution thisExecution = history.getCurrentExecution();
    TaskExecution lastExecution = history.getPreviousExecution();

    noHistoryState = NoHistoryStateChangeRule.create(task, lastExecution);
    taskTypeState = TaskTypeStateChangeRule.create(task, lastExecution, thisExecution);
    inputPropertiesState =
        InputPropertiesStateChangeRule.create(task, lastExecution, thisExecution);

    // Capture outputs state
    try {
      outputFilesState =
          caching(
              OutputFilesStateChangeRule.create(
                  task, lastExecution, thisExecution, outputFilesSnapshotter));
    } catch (UncheckedIOException e) {
      throw new UncheckedIOException(
          String.format(
              "Failed to capture snapshot of output files for task '%s' during up-to-date check.",
              task.getName()),
          e);
    }

    // Capture inputs state
    try {
      FileCollectionSnapshot inputFilesSnapshot =
          inputFilesSnapshotter.snapshot(task.getInputs().getFiles());
      this.inputFilesSnapshot = inputFilesSnapshot.getSnapshot();
      inputFilesState =
          caching(
              InputFilesStateChangeRule.create(lastExecution, thisExecution, inputFilesSnapshot));
    } catch (UncheckedIOException e) {
      throw new UncheckedIOException(
          String.format(
              "Failed to capture snapshot of input files for task '%s' during up-to-date check.",
              task.getName()),
          e);
    }

    // Capture discovered inputs state from previous execution
    try {
      discoveredInputFilesState =
          DiscoveredInputFilesStateChangeRule.create(
              lastExecution, thisExecution, discoveredFilesSnapshotter);
    } catch (UncheckedIOException e) {
      throw new UncheckedIOException(
          String.format(
              "Failed to capture snapshot of input files for task '%s' during up-to-date check.",
              task.getName()),
          e);
    }

    allTaskChanges =
        new SummaryTaskStateChanges(
            MAX_OUT_OF_DATE_MESSAGES,
            noHistoryState,
            taskTypeState,
            inputPropertiesState,
            outputFilesState,
            inputFilesState,
            caching(discoveredInputFilesState));
    rebuildChanges =
        new SummaryTaskStateChanges(
            1, noHistoryState, taskTypeState, inputPropertiesState, outputFilesState);
  }
 @Override
 public void appendToCacheKey(TaskCacheKeyBuilder builder) {
   filesSnapshot.appendToCacheKey(builder);
 }
 @Override
 public boolean isEmpty() {
   return filesSnapshot.isEmpty();
 }
 @Override
 public Collection<Long> getTreeSnapshotIds() {
   return filesSnapshot.getTreeSnapshotIds();
 }
 public FilesSnapshotSet getSnapshot() {
   return filesSnapshot.getSnapshot();
 }
 @Override
 public Map<String, IncrementalFileSnapshot> getSnapshots() {
   return filesSnapshot.getSnapshots();
 }
 public Collection<File> getFiles() {
   return filesSnapshot.getFiles();
 }