Convert SkyQueryEnvironment#buildTransitiveClosure to use batch operations.

--
MOS_MIGRATED_REVID=96433497
diff --git a/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java
index 473c69d..9f4aa40 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java
@@ -17,7 +17,7 @@
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
-import com.google.common.base.Supplier;
+import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -25,7 +25,6 @@
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
-import com.google.common.collect.Multimaps;
 import com.google.devtools.build.lib.cmdline.ResolvedTargets;
 import com.google.devtools.build.lib.cmdline.TargetParsingException;
 import com.google.devtools.build.lib.cmdline.TargetPattern;
@@ -57,7 +56,6 @@
 import com.google.devtools.build.skyframe.WalkableGraph.WalkableGraphFactory;
 
 import java.util.ArrayDeque;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Deque;
 import java.util.HashMap;
@@ -327,13 +325,12 @@
       throws QueryException {
     // Everything has already been loaded, so here we just check for errors so that we can
     // pre-emptively throw/report if needed.
-    for (Target target : targets) {
-      SkyKey targetKey = TransitiveTargetValue.key(target.getLabel());
-      checkExistence(targetKey);
-      Exception exception = graph.getException(targetKey);
-      if (exception != null) {
-        reportBuildFileError(caller, exception.getMessage());
+    for (Map.Entry<SkyKey, Exception> entry :
+      graph.getMissingAndExceptions(makeKeys(targets)).entrySet()) {
+      if (entry.getValue() == null) {
+        throw new QueryException(entry.getKey().argument() + " does not exist in graph");
       }
+      reportBuildFileError(caller, entry.getValue().getMessage());
     }
   }
 
@@ -416,14 +413,7 @@
   }
 
   private Map<SkyKey, Target> makeTargetsWithAssociations(Iterable<SkyKey> keys) {
-    Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = Multimaps.newListMultimap(
-        new HashMap<SkyKey, Collection<SkyKey>>(),
-        new Supplier<List<SkyKey>>() {
-          @Override
-          public List<SkyKey> get() {
-            return new ArrayList<>();
-          }
-        });
+    Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = ArrayListMultimap.create();
     for (SkyKey key : keys) {
       SkyFunctionName functionName = key.functionName();
       if (!functionName.equals(SkyFunctions.TRANSITIVE_TARGET)) {
@@ -437,7 +427,7 @@
       }
     }
     ImmutableMap.Builder<SkyKey, Target> result = ImmutableMap.builder();
-    Map<SkyKey, SkyValue> packageMap = graph.getValuesMaybe(packageKeyToTargetKeyMap.keySet());
+    Map<SkyKey, SkyValue> packageMap = graph.getDoneValues(packageKeyToTargetKeyMap.keySet());
     for (Map.Entry<SkyKey, SkyValue> entry : packageMap.entrySet()) {
       for (SkyKey targetKey : packageKeyToTargetKeyMap.get(entry.getKey())) {
         try {
diff --git a/src/main/java/com/google/devtools/build/skyframe/DelegatingWalkableGraph.java b/src/main/java/com/google/devtools/build/skyframe/DelegatingWalkableGraph.java
index b88b8903..0e4e86b 100644
--- a/src/main/java/com/google/devtools/build/skyframe/DelegatingWalkableGraph.java
+++ b/src/main/java/com/google/devtools/build/skyframe/DelegatingWalkableGraph.java
@@ -19,6 +19,7 @@
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.annotation.Nullable;
@@ -70,11 +71,29 @@
       };
 
   @Override
-  public Map<SkyKey, SkyValue> getValuesMaybe(Iterable<SkyKey> keys) {
+  public Map<SkyKey, SkyValue> getDoneValues(Iterable<SkyKey> keys) {
     return Maps.filterValues(Maps.transformValues(graph.getBatch(keys), GET_SKY_VALUE_FUNCTION),
         Predicates.notNull());
   }
 
+  @Override
+  public Map<SkyKey, Exception> getMissingAndExceptions(Iterable<SkyKey> keys) {
+    Map<SkyKey, Exception> result = new HashMap<>();
+    Map<SkyKey, NodeEntry> graphResult = graph.getBatch(keys);
+    for (SkyKey key : keys) {
+      NodeEntry nodeEntry = graphResult.get(key);
+      if (nodeEntry == null || !nodeEntry.isDone()) {
+        result.put(key, null);
+      } else {
+        ErrorInfo errorInfo = nodeEntry.getErrorInfo();
+        if (errorInfo != null) {
+          result.put(key, errorInfo.getException());
+        }
+      }
+    }
+    return result;
+  }
+
   @Nullable
   @Override
   public Exception getException(SkyKey key) {
diff --git a/src/main/java/com/google/devtools/build/skyframe/WalkableGraph.java b/src/main/java/com/google/devtools/build/skyframe/WalkableGraph.java
index be68248..181b7dc 100644
--- a/src/main/java/com/google/devtools/build/skyframe/WalkableGraph.java
+++ b/src/main/java/com/google/devtools/build/skyframe/WalkableGraph.java
@@ -44,7 +44,16 @@
    * Returns a map giving the values of the given keys for done keys. Keys not present in the graph
    * or whose nodes are not done will not be present in the returned map.
    */
-  Map<SkyKey, SkyValue> getValuesMaybe(Iterable<SkyKey> keys);
+  Map<SkyKey, SkyValue> getDoneValues(Iterable<SkyKey> keys);
+
+  /**
+   * Returns a map giving exceptions associated to the given keys for done keys. Keys not present in
+   * the graph or whose nodes are not done will be present in the returned map, with null value. In
+   * other words, if {@code key} is in {@param keys}, then the returned map will contain an entry
+   * for {@code key} if and only if the node for {@code key} did <i>not</i> evaluate successfully
+   * without error.
+   */
+  Map<SkyKey, Exception> getMissingAndExceptions(Iterable<SkyKey> keys);
 
   /**
    * Returns the exception thrown when computing the node with the given key, if any. If the node