// Copyright 2018 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.buildeventstream;

import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile;
import com.google.devtools.build.lib.vfs.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;

/** An uploader that simply turns paths into local file URIs. */
class LocalFilesArtifactUploader implements BuildEventArtifactUploader {
  private final ListeningExecutorService uploadExecutor =
      MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

  @Override
  public ListenableFuture<PathConverter> upload(Map<Path, LocalFile> files) {
    List<ListenableFuture<PathLookupResult>> lookups = new ArrayList<>();
    for (Path path : files.keySet()) {
      lookups.add(uploadExecutor.submit(() -> new PathLookupResult(path, path.isDirectory())));
    }
    return Futures.transform(
        Futures.allAsList(lookups),
        lookupList -> {
          ImmutableMap.Builder<Path, PathLookupResult> pathLookups = ImmutableMap.builder();
          for (PathLookupResult lookup : lookupList) {
            pathLookups.put(lookup.path, lookup);
          }
          return new PathConverterImpl(pathLookups.build());
        },
        MoreExecutors.directExecutor());
  }

  @Override
  public void shutdown() {
    // Intentionally left empty
  }

  private static class PathLookupResult {
    final Path path;
    final boolean isDirectory;

    private PathLookupResult(Path path, boolean isDirectory) {
      this.path = path;
      this.isDirectory = isDirectory;
    }
  }

  private static class PathConverterImpl implements PathConverter {
    private static final FileUriPathConverter FILE_URI_PATH_CONVERTER = new FileUriPathConverter();
    private final Map<Path, PathLookupResult> pathLookups;

    private PathConverterImpl(Map<Path, PathLookupResult> pathLookups) {
      this.pathLookups = pathLookups;
    }

    @Nullable
    @Override
    public String apply(Path path) {
      PathLookupResult result = pathLookups.get(path);
      if (result == null) {
        // We should throw here, the file wasn't declared in BuildEvent#referencedLocalFiles
        return null;
      }
      if (result.isDirectory) {
        return null;
      }
      return FILE_URI_PATH_CONVERTER.apply(result.path);
    }
  }
}

@@ -669,55 +669,8 @@
 m;%0A%0A
-import com.google.common.collect.ImmutableMap;%0A
 impo
@@ -778,131 +778,8 @@
 re;%0A
-import com.google.common.util.concurrent.ListeningExecutorService;%0Aimport com.google.common.util.concurrent.MoreExecutors;%0A
 impo
@@ -901,59 +901,8 @@
 th;%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A
 impo
@@ -940,28 +940,11 @@
 til.
-concurrent.Executors
+Set
 ;%0Aim
@@ -1120,1162 +1120,246 @@
 %7B%0A  
-private final ListeningExecutorService uploadExecutor =%0A      MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());%0A%0A  @Override%0A  public ListenableFuture%3CPathConverter%3E upload(Map%3CPath, LocalFile%3E files) %7B%0A    List%3CListenableFuture%3CPathLookupResult%3E%3E lookups = new ArrayList%3C%3E();%0A    for (Path path : files.keySet()) %7B%0A      lookups.add(uploadExecutor.submit(() -%3E new PathLookupResult(path, path.isDirectory())));%0A    %7D%0A    return Futures.transform(%0A        Futures.allAsList(lookups),%0A        lookupList -%3E %7B%0A          ImmutableMap.Builder%3CPath, PathLookupResult%3E pathLookups = ImmutableMap.builder();%0A          for (PathLookupResult lookup : lookupList) %7B%0A            pathLookups.put(lookup.path, lookup);%0A          %7D%0A          return new PathConverterImpl(pathLookups.build());%0A        %7D,%0A        MoreExecutors.directExecutor());%0A  %7D%0A%0A  @Override%0A  public void shutdown() %7B%0A    // Intentionally left empty%0A  %7D%0A%0A  private static class PathLookupResult %7B%0A    final Path path;%0A    final boolean isDirectory;%0A%0A    private PathLookupResult(Path path, boolean isDirectory) %7B%0A      this.path = path;%0A      this.isDirectory = isDirectory;%0A    %7D
+@Override%0A  public ListenableFuture%3CPathConverter%3E upload(Map%3CPath, LocalFile%3E files) %7B%0A    return Futures.immediateFuture(new PathConverterImpl(files.keySet()));%0A  %7D%0A%0A  @Override%0A  public void shutdown() %7B%0A    // Intentionally left empty
 %0A  %7D
@@ -1550,118 +1550,70 @@
 nal 
-Map%3CPath, PathLookupResult%3E pathLookups;%0A%0A    private PathConverterImpl(Map%3CPath, PathLookupResult%3E pathLookup
+Set%3CPath%3E paths;%0A%0A    private PathConverterImpl(Set%3CPath%3E path
 s) %7B
@@ -1632,22 +1632,16 @@
 path
-Lookup
 s = path
 Look
@@ -1636,22 +1636,16 @@
 s = path
-Lookup
 s;%0A    %7D
@@ -1721,81 +1721,33 @@
     
-PathLookupResult result = pathLookups.get(path);%0A      if (result == null
+if (!paths.contains(path)
 ) %7B%0A
@@ -1878,22 +1878,20 @@
     if (
-result
+path
 .isDirec
@@ -1894,16 +1894,18 @@
 irectory
+()
 ) %7B%0A    
@@ -1976,15 +1976,8 @@
 ply(
-result.
 path

Remove the unnecessary unbounded thread pool