Lift individual OutputFormatters and helper classes into their own files
OutputFormatter's actual interface is rather simple, but it's difficult to find
with all of its subclasses and helpers that we've accumulated over the years. This
should make it easier to work with, as well as simplify unit testing individual
formatters.
RELNOTES: None
PiperOrigin-RevId: 263837939
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java
new file mode 100644
index 0000000..b8f63a1
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java
@@ -0,0 +1,61 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.common.collect.Iterables;
+import com.google.devtools.build.lib.graph.Digraph;
+import com.google.devtools.build.lib.graph.Node;
+import com.google.devtools.build.lib.packages.DependencyFilter;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.CommonQueryOptions;
+import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
+import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
+import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
+import java.io.IOException;
+import java.io.OutputStream;
+
+abstract class AbstractUnorderedFormatter extends OutputFormatter implements StreamedFormatter {
+ protected CommonQueryOptions options;
+ protected AspectResolver aspectResolver;
+ protected DependencyFilter dependencyFilter;
+
+ @Override
+ public void setOptions(CommonQueryOptions options, AspectResolver aspectResolver) {
+ this.options = options;
+ this.aspectResolver = aspectResolver;
+ this.dependencyFilter = OutputFormatter.getDependencyFilter(options);
+ }
+
+ @Override
+ public void output(
+ QueryOptions options,
+ Digraph<Target> result,
+ OutputStream out,
+ AspectResolver aspectResolver,
+ ConditionalEdges conditionalEdges)
+ throws IOException, InterruptedException {
+ setOptions(options, aspectResolver);
+ OutputFormatterCallback.processAllTargets(
+ createPostFactoStreamCallback(out, options), getOrderedTargets(result, options));
+ }
+
+ protected Iterable<Target> getOrderedTargets(Digraph<Target> result, QueryOptions options) {
+ Iterable<Node<Target>> orderedResult =
+ options.orderOutput == OrderOutput.DEPS
+ ? result.getTopologicalOrder()
+ : result.getTopologicalOrder(new TargetOrdering());
+ return Iterables.transform(orderedResult, EXTRACT_NODE_LABEL);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java
index 1c9b928..86137b9 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java
@@ -29,8 +29,8 @@
import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
import com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback;
import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
-import com.google.devtools.build.lib.query2.query.output.OutputFormatter.AbstractUnorderedFormatter;
import com.google.devtools.build.lib.syntax.EvalUtils;
+import com.google.devtools.build.lib.syntax.Printer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
@@ -209,4 +209,17 @@
public String getName() {
return "build";
}
+
+ /** Prints labels in their canonical form. */
+ private static class LabelPrinter extends Printer.BasePrinter {
+ @Override
+ public LabelPrinter repr(Object o) {
+ if (o instanceof Label) {
+ writeString(((Label) o).getCanonicalForm());
+ } else {
+ super.repr(o);
+ }
+ return this;
+ }
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java
new file mode 100644
index 0000000..fb173d7
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java
@@ -0,0 +1,68 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
+import com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * An output formatter that prints the labels of the resulting target set in
+ * topological order, optionally with the target's kind.
+ */
+class LabelOutputFormatter extends AbstractUnorderedFormatter {
+
+ private final boolean showKind;
+
+ LabelOutputFormatter(boolean showKind) {
+ this.showKind = showKind;
+ }
+
+ @Override
+ public String getName() {
+ return showKind ? "label_kind" : "label";
+ }
+
+ @Override
+ public OutputFormatterCallback<Target> createPostFactoStreamCallback(
+ OutputStream out, final QueryOptions options) {
+ return new TextOutputFormatterCallback<Target>(out) {
+ @Override
+ public void processOutput(Iterable<Target> partialResult) throws IOException {
+ String lineTerm = options.getLineTerminator();
+ for (Target target : partialResult) {
+ if (showKind) {
+ writer.append(target.getTargetKind());
+ writer.append(' ');
+ }
+ Label label = target.getLabel();
+ writer.append(label.getDefaultCanonicalForm()).append(lineTerm);
+ }
+ }
+ };
+ }
+
+ @Override
+ public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
+ OutputStream out, QueryOptions options, QueryEnvironment<?> env) {
+ return new SynchronizedDelegatingOutputFormatterCallback<>(
+ createPostFactoStreamCallback(out, options));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java
new file mode 100644
index 0000000..f81d585
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java
@@ -0,0 +1,89 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.events.Location;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.AbstractBlazeQueryEnvironment;
+import com.google.devtools.build.lib.query2.engine.AggregatingQueryExpressionVisitor.ContainsFunctionQueryExpressionVisitor;
+import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
+import com.google.devtools.build.lib.query2.engine.QueryException;
+import com.google.devtools.build.lib.query2.engine.QueryExpression;
+import com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * An output formatter that prints the labels of the targets, preceded by
+ * their locations and kinds, in topological order. For output files, the
+ * location of the generating rule is given; for input files, the location of
+ * line 1 is given.
+ */
+class LocationOutputFormatter extends AbstractUnorderedFormatter {
+
+ @Override
+ public String getName() {
+ return "location";
+ }
+
+ @Override
+ public void verifyCompatible(QueryEnvironment<?> env, QueryExpression expr)
+ throws QueryException {
+ if (!(env instanceof AbstractBlazeQueryEnvironment)) {
+ return;
+ }
+
+ ContainsFunctionQueryExpressionVisitor noteBuildFilesAndLoadLilesVisitor =
+ new ContainsFunctionQueryExpressionVisitor(ImmutableList.of("loadfiles", "buildfiles"));
+
+ if (expr.accept(noteBuildFilesAndLoadLilesVisitor)) {
+ throw new QueryException(
+ "Query expressions involving 'buildfiles' or 'loadfiles' cannot be used with "
+ + "--output=location");
+ }
+ }
+
+ @Override
+ public OutputFormatterCallback<Target> createPostFactoStreamCallback(
+ OutputStream out, final QueryOptions options) {
+ return new TextOutputFormatterCallback<Target>(out) {
+
+ @Override
+ public void processOutput(Iterable<Target> partialResult) throws IOException {
+ final String lineTerm = options.getLineTerminator();
+ for (Target target : partialResult) {
+ Location location = target.getLocation();
+ writer
+ .append(location.print())
+ .append(": ")
+ .append(target.getTargetKind())
+ .append(" ")
+ .append(target.getLabel().getDefaultCanonicalForm())
+ .append(lineTerm);
+ }
+ }
+ };
+ }
+
+ @Override
+ public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
+ OutputStream out, QueryOptions options, QueryEnvironment<?> env) {
+ return new SynchronizedDelegatingOutputFormatterCallback<>(
+ createPostFactoStreamCallback(out, options));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java
new file mode 100644
index 0000000..ba2581a
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java
@@ -0,0 +1,112 @@
+// Copyright 2014 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.query2.query.output;
+
+import static java.util.Comparator.comparingInt;
+
+import com.google.devtools.build.lib.graph.Digraph;
+import com.google.devtools.build.lib.graph.Node;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
+import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * An output formatter that prints the labels in maximum rank order, preceded
+ * by their rank number. "Roots" have rank 0, all other nodes have a rank
+ * which is one greater than the maximum rank of each of their predecessors.
+ * All nodes in a cycle are considered of equal rank. MAXRANK shows the
+ * highest rank for a given node, i.e. the length of the longest non-cyclic
+ * path from a zero-rank node to it.
+ *
+ * <p>If the result came from a <code>deps(x)</code> query, then the MAXRANKs
+ * correspond to the longest path from x to each of its prerequisites.
+ */
+class MaxrankOutputFormatter extends OutputFormatter {
+
+ @Override
+ public String getName() {
+ return "maxrank";
+ }
+
+ @Override
+ public void output(
+ QueryOptions options,
+ Digraph<Target> result,
+ OutputStream out,
+ AspectResolver aspectResolver,
+ ConditionalEdges conditionalEdges)
+ throws IOException {
+ // In order to handle cycles correctly, we need work on the strong
+ // component graph, as cycles should be treated a "clump" of nodes all on
+ // the same rank. Graphs may contain cycles because there are errors in BUILD files.
+
+ // Dynamic programming algorithm:
+ // rank(x) = max(rank(p)) + 1 foreach p in preds(x)
+ // TODO(bazel-team): Move to Digraph.
+ class DP {
+ final Map<Node<Set<Node<Target>>>, Integer> ranks = new HashMap<>();
+
+ int rank(Node<Set<Node<Target>>> node) {
+ Integer rank = ranks.get(node);
+ if (rank == null) {
+ int maxPredRank = -1;
+ for (Node<Set<Node<Target>>> p : node.getPredecessors()) {
+ maxPredRank = Math.max(maxPredRank, rank(p));
+ }
+ rank = maxPredRank + 1;
+ ranks.put(node, rank);
+ }
+ return rank;
+ }
+ }
+ DP dp = new DP();
+
+ // Now sort by rank...
+ List<RankAndLabel> output = new ArrayList<>();
+ for (Node<Set<Node<Target>>> x : result.getStrongComponentGraph().getNodes()) {
+ int rank = dp.rank(x);
+ for (Node<Target> y : x.getLabel()) {
+ output.add(new RankAndLabel(rank, y.getLabel().getLabel()));
+ }
+ }
+ if (options.orderOutput == OrderOutput.FULL) {
+ // Use the natural order for RankAndLabels, which breaks ties alphabetically.
+ Collections.sort(output);
+ } else {
+ Collections.sort(output, comparingInt(RankAndLabel::getRank));
+ }
+ final String lineTerm = options.getLineTerminator();
+ PrintStream printStream = new PrintStream(out);
+ for (RankAndLabel item : output) {
+ printStream.print(item + lineTerm);
+ }
+ flushAndCheckError(printStream);
+ }
+
+ private static void flushAndCheckError(PrintStream printStream) throws IOException {
+ if (printStream.checkError()) {
+ throw new IOException("PrintStream encountered an error");
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java
new file mode 100644
index 0000000..7323e8f
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java
@@ -0,0 +1,120 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.graph.Digraph;
+import com.google.devtools.build.lib.graph.Node;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
+import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.Nullable;
+
+/**
+ * An output formatter that prints the labels in minimum rank order, preceded by
+ * their rank number. "Roots" have rank 0, their direct prerequisites have
+ * rank 1, etc. All nodes in a cycle are considered of equal rank. MINRANK
+ * shows the lowest rank for a given node, i.e. the length of the shortest
+ * path from a zero-rank node to it.
+ *
+ * <p>If the result came from a <code>deps(x)</code> query, then the MINRANKs
+ * correspond to the shortest path from x to each of its prerequisites.
+ */
+class MinrankOutputFormatter extends OutputFormatter {
+
+ @Override
+ public String getName() {
+ return "minrank";
+ }
+
+ private static void outputToStreamOrSave(
+ int rank,
+ Label label,
+ PrintStream out,
+ @Nullable List<RankAndLabel> toSave,
+ final String lineTerminator) {
+ if (toSave != null) {
+ toSave.add(new RankAndLabel(rank, label));
+ } else {
+ out.print(rank + " " + label.getDefaultCanonicalForm() + lineTerminator);
+ }
+ }
+
+ @Override
+ public void output(
+ QueryOptions options,
+ Digraph<Target> result,
+ OutputStream out,
+ AspectResolver aspectResolver,
+ ConditionalEdges conditionalEdges)
+ throws IOException {
+ PrintStream printStream = new PrintStream(out);
+ // getRoots() isn't defined for cyclic graphs, so in order to handle
+ // cycles correctly, we need work on the strong component graph, as
+ // cycles should be treated a "clump" of nodes all on the same rank.
+ // Graphs may contain cycles because there are errors in BUILD files.
+
+ List<RankAndLabel> outputToOrder =
+ options.orderOutput == OrderOutput.FULL ? new ArrayList<>() : null;
+ Digraph<Set<Node<Target>>> scGraph = result.getStrongComponentGraph();
+ Set<Node<Set<Node<Target>>>> rankNodes = scGraph.getRoots();
+ Set<Node<Set<Node<Target>>>> seen = new HashSet<>();
+ seen.addAll(rankNodes);
+ final String lineTerm = options.getLineTerminator();
+ for (int rank = 0; !rankNodes.isEmpty(); rank++) {
+ // Print out this rank:
+ for (Node<Set<Node<Target>>> xScc : rankNodes) {
+ for (Node<Target> x : xScc.getLabel()) {
+ outputToStreamOrSave(
+ rank, x.getLabel().getLabel(), printStream, outputToOrder, lineTerm);
+ }
+ }
+
+ // Find the next rank:
+ Set<Node<Set<Node<Target>>>> nextRankNodes = new LinkedHashSet<>();
+ for (Node<Set<Node<Target>>> x : rankNodes) {
+ for (Node<Set<Node<Target>>> y : x.getSuccessors()) {
+ if (seen.add(y)) {
+ nextRankNodes.add(y);
+ }
+ }
+ }
+ rankNodes = nextRankNodes;
+ }
+ if (outputToOrder != null) {
+ Collections.sort(outputToOrder);
+ for (RankAndLabel item : outputToOrder) {
+ printStream.print(item + lineTerm);
+ }
+ }
+
+ flushAndCheckError(printStream);
+ }
+
+ private static void flushAndCheckError(PrintStream printStream) throws IOException {
+ if (printStream.checkError()) {
+ throw new IOException("PrintStream encountered an error");
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java
index 3799c91..06e305b 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java
@@ -13,18 +13,13 @@
// limitations under the License.
package com.google.devtools.build.lib.query2.query.output;
-import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.joining;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
-import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.graph.Digraph;
import com.google.devtools.build.lib.graph.Node;
@@ -34,36 +29,16 @@
import com.google.devtools.build.lib.packages.DependencyFilter;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Target;
-import com.google.devtools.build.lib.query2.AbstractBlazeQueryEnvironment;
import com.google.devtools.build.lib.query2.CommonQueryOptions;
-import com.google.devtools.build.lib.query2.engine.AggregatingQueryExpressionVisitor.ContainsFunctionQueryExpressionVisitor;
-import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
import com.google.devtools.build.lib.query2.engine.QueryException;
import com.google.devtools.build.lib.query2.engine.QueryExpression;
-import com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback;
-import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
-import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
-import com.google.devtools.build.lib.syntax.Printer;
-import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintStream;
import java.io.Serializable;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.Comparator;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import javax.annotation.Nullable;
/**
* Interface for classes which order, format and print the result of a Blaze
@@ -72,13 +47,13 @@
public abstract class OutputFormatter implements Serializable {
/** Where the value of an attribute comes from */
- public enum AttributeValueSource {
+ enum AttributeValueSource {
RULE, // Explicitly specified on the rule
PACKAGE, // Package default
DEFAULT // Rule class default
}
- public static final Function<Node<Target>, Target> EXTRACT_NODE_LABEL = Node::getLabel;
+ static final Function<Node<Target>, Target> EXTRACT_NODE_LABEL = Node::getLabel;
public static ImmutableList<OutputFormatter> getDefaultFormatters() {
return ImmutableList.of(
@@ -106,11 +81,8 @@
.collect(joining(", "));
}
- /**
- * Returns the output formatter for the specified command-line options.
- */
- public static OutputFormatter getFormatter(
- Iterable<OutputFormatter> formatters, String type) {
+ /** Returns the output formatter for the specified command-line options. */
+ public static OutputFormatter getFormatter(Iterable<OutputFormatter> formatters, String type) {
for (OutputFormatter formatter : formatters) {
if (formatter.getName().equals(type)) {
return formatter;
@@ -121,11 +93,10 @@
}
/**
- * Given a set of query options, returns a BinaryPredicate suitable for
- * passing to {@link Rule#getLabels()}, {@link XmlOutputFormatter}, etc.
+ * Given a set of query options, returns a BinaryPredicate suitable for passing to {@link
+ * Rule#getLabels()}, {@link XmlOutputFormatter}, etc.
*/
- public static DependencyFilter getDependencyFilter(
- CommonQueryOptions queryOptions) {
+ static DependencyFilter getDependencyFilter(CommonQueryOptions queryOptions) {
// TODO(bazel-team): Optimize: and(ALL_DEPS, x) -> x, etc.
return DependencyFilter.and(
queryOptions.includeHostDeps ? DependencyFilter.ALL_DEPS : DependencyFilter.NO_HOST_DEPS,
@@ -134,6 +105,9 @@
: DependencyFilter.NO_IMPLICIT_DEPS);
}
+ /** Returns the user-visible name of the output formatter. */
+ public abstract String getName();
+
/**
* Workaround for a bug in {@link java.nio.channels.Channels#newChannel(OutputStream)}, which
* attempts to close the output stream on interrupt, which can cause a deadlock if there is an
@@ -144,9 +118,12 @@
return true;
}
+ /**
+ * Verifies that the environment and expression are compatible with this formatter, throws a
+ * {@link QueryException} if not.
+ */
public void verifyCompatible(QueryEnvironment<?> env, QueryExpression expr)
- throws QueryException {
- }
+ throws QueryException {}
/**
* Format the result (a set of target nodes implicitly ordered according to the graph maintained
@@ -160,441 +137,14 @@
ConditionalEdges conditionalEdges)
throws IOException, InterruptedException;
- /**
- * Unordered streamed output formatter (wrt. dependency ordering).
- *
- * <p>Formatters that support streamed output may be used when only the set of query results is
- * requested but their ordering is irrelevant.
- *
- * <p>The benefit of using a streamed formatter is that we can save the potentially expensive
- * subgraph extraction step before presenting the query results and that depending on the query
- * environment used, it can be more memory performant, as it does not aggregate all the data
- * before writing in the output.
- */
- public interface StreamedFormatter {
- /** Specifies options to be used by subsequent calls to {@link #createStreamCallback}. */
- void setOptions(CommonQueryOptions options, AspectResolver aspectResolver);
-
- /**
- * Returns a {@link ThreadSafeOutputFormatterCallback} whose
- * {@link OutputFormatterCallback#process} outputs formatted {@link Target}s to the given
- * {@code out}.
- *
- * <p>Takes any options specified via the most recent call to {@link #setOptions} into
- * consideration.
- *
- * <p>Intended to be use for streaming out during evaluation of a query.
- */
- ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
- OutputStream out, QueryOptions options, QueryEnvironment<?> env);
-
- /**
- * Same as {@link #createStreamCallback}, but intended to be used for outputting the
- * already-computed result of a query.
- */
- OutputFormatterCallback<Target> createPostFactoStreamCallback(
- OutputStream out, QueryOptions options);
- }
-
- /**
- * Returns the user-visible name of the output formatter.
- */
- public abstract String getName();
-
- abstract static class AbstractUnorderedFormatter extends OutputFormatter
- implements StreamedFormatter {
- protected CommonQueryOptions options;
- protected AspectResolver aspectResolver;
- protected DependencyFilter dependencyFilter;
-
- @Override
- public void setOptions(CommonQueryOptions options, AspectResolver aspectResolver) {
- this.options = options;
- this.aspectResolver = aspectResolver;
- this.dependencyFilter = OutputFormatter.getDependencyFilter(options);
- }
-
- @Override
- public void output(
- QueryOptions options,
- Digraph<Target> result,
- OutputStream out,
- AspectResolver aspectResolver,
- ConditionalEdges conditionalEdges)
- throws IOException, InterruptedException {
- setOptions(options, aspectResolver);
- OutputFormatterCallback.processAllTargets(
- createPostFactoStreamCallback(out, options), getOrderedTargets(result, options));
- }
-
- protected Iterable<Target> getOrderedTargets(Digraph<Target> result, QueryOptions options) {
- Iterable<Node<Target>> orderedResult =
- options.orderOutput == OrderOutput.DEPS
- ? result.getTopologicalOrder()
- : result.getTopologicalOrder(new TargetOrdering());
- return Iterables.transform(orderedResult, EXTRACT_NODE_LABEL);
- }
- }
-
- /** Abstract class supplying a {@link PrintStream} to implementations, flushing it on close. */
- abstract static class TextOutputFormatterCallback<T> extends OutputFormatterCallback<T> {
- protected Writer writer;
-
- @SuppressWarnings("DefaultCharset")
- TextOutputFormatterCallback(OutputStream out) {
- // This code intentionally uses the platform default encoding.
- this.writer = new BufferedWriter(new OutputStreamWriter(out));
- }
-
- @Override
- public void close(boolean failFast) throws IOException {
- writer.flush();
- }
- }
-
- /**
- * An output formatter that prints the labels of the resulting target set in
- * topological order, optionally with the target's kind.
- */
- private static class LabelOutputFormatter extends AbstractUnorderedFormatter {
-
- private final boolean showKind;
-
- private LabelOutputFormatter(boolean showKind) {
- this.showKind = showKind;
- }
-
- @Override
- public String getName() {
- return showKind ? "label_kind" : "label";
- }
-
- @Override
- public OutputFormatterCallback<Target> createPostFactoStreamCallback(
- OutputStream out, final QueryOptions options) {
- return new TextOutputFormatterCallback<Target>(out) {
- @Override
- public void processOutput(Iterable<Target> partialResult) throws IOException {
- String lineTerm = options.getLineTerminator();
- for (Target target : partialResult) {
- if (showKind) {
- writer.append(target.getTargetKind());
- writer.append(' ');
- }
- Label label = target.getLabel();
- writer.append(label.getDefaultCanonicalForm()).append(lineTerm);
- }
- }
- };
- }
-
- @Override
- public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
- OutputStream out, QueryOptions options, QueryEnvironment<?> env) {
- return new SynchronizedDelegatingOutputFormatterCallback<>(
- createPostFactoStreamCallback(out, options));
- }
- }
-
- /**
- * An ordering of Targets based on the ordering of their labels.
- */
- @VisibleForTesting
- public static class TargetOrdering implements Comparator<Target> {
+ /** An ordering of Targets based on the ordering of their labels. */
+ static class TargetOrdering implements Comparator<Target> {
@Override
public int compare(Target o1, Target o2) {
return o1.getLabel().compareTo(o2.getLabel());
}
}
- /**
- * An output formatter that prints the names of the packages of the target
- * set, in lexicographical order without duplicates.
- */
- private static class PackageOutputFormatter extends AbstractUnorderedFormatter {
-
-
- @Override
- public String getName() {
- return "package";
- }
-
- @Override
- public OutputFormatterCallback<Target> createPostFactoStreamCallback(
- OutputStream out, final QueryOptions options) {
- return new TextOutputFormatterCallback<Target>(out) {
- private final Set<String> packageNames = Sets.newTreeSet();
-
- @Override
- public void processOutput(Iterable<Target> partialResult) {
-
- for (Target target : partialResult) {
- packageNames.add(target.getLabel().getPackageIdentifier().toString());
- }
- }
-
- @Override
- public void close(boolean failFast) throws IOException {
- if (!failFast) {
- final String lineTerm = options.getLineTerminator();
- for (String packageName : packageNames) {
- writer.append(packageName).append(lineTerm);
- }
- }
- super.close(failFast);
- }
- };
- }
-
- @Override
- public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
- OutputStream out, QueryOptions options, QueryEnvironment<?> env) {
- return new SynchronizedDelegatingOutputFormatterCallback<>(
- createPostFactoStreamCallback(out, options));
- }
- }
-
- /**
- * An output formatter that prints the labels of the targets, preceded by
- * their locations and kinds, in topological order. For output files, the
- * location of the generating rule is given; for input files, the location of
- * line 1 is given.
- */
- private static class LocationOutputFormatter extends AbstractUnorderedFormatter {
-
- @Override
- public String getName() {
- return "location";
- }
-
- @Override
- public void verifyCompatible(QueryEnvironment<?> env, QueryExpression expr)
- throws QueryException {
- if (!(env instanceof AbstractBlazeQueryEnvironment)) {
- return;
- }
-
- ContainsFunctionQueryExpressionVisitor noteBuildFilesAndLoadLilesVisitor =
- new ContainsFunctionQueryExpressionVisitor(ImmutableList.of("loadfiles", "buildfiles"));
-
- if (expr.accept(noteBuildFilesAndLoadLilesVisitor)) {
- throw new QueryException(
- "Query expressions involving 'buildfiles' or 'loadfiles' cannot be used with "
- + "--output=location");
- }
- }
-
- @Override
- public OutputFormatterCallback<Target> createPostFactoStreamCallback(
- OutputStream out, final QueryOptions options) {
- return new TextOutputFormatterCallback<Target>(out) {
-
- @Override
- public void processOutput(Iterable<Target> partialResult) throws IOException {
- final String lineTerm = options.getLineTerminator();
- for (Target target : partialResult) {
- Location location = target.getLocation();
- writer
- .append(location.print())
- .append(": ")
- .append(target.getTargetKind())
- .append(" ")
- .append(target.getLabel().getDefaultCanonicalForm())
- .append(lineTerm);
- }
- }
- };
- }
-
- @Override
- public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
- OutputStream out, QueryOptions options, QueryEnvironment<?> env) {
- return new SynchronizedDelegatingOutputFormatterCallback<>(
- createPostFactoStreamCallback(out, options));
- }
- }
-
-
-
- private static class RankAndLabel implements Comparable<RankAndLabel> {
- private final int rank;
- private final Label label;
-
- private RankAndLabel(int rank, Label label) {
- this.rank = rank;
- this.label = label;
- }
-
- @Override
- public int compareTo(RankAndLabel o) {
- if (this.rank != o.rank) {
- return this.rank - o.rank;
- }
- return this.label.compareTo(o.label);
- }
-
- @Override
- public String toString() {
- return rank + " " + label.getDefaultCanonicalForm();
- }
- }
-
- /**
- * An output formatter that prints the labels in minimum rank order, preceded by
- * their rank number. "Roots" have rank 0, their direct prerequisites have
- * rank 1, etc. All nodes in a cycle are considered of equal rank. MINRANK
- * shows the lowest rank for a given node, i.e. the length of the shortest
- * path from a zero-rank node to it.
- *
- * <p>If the result came from a <code>deps(x)</code> query, then the MINRANKs
- * correspond to the shortest path from x to each of its prerequisites.
- */
- private static class MinrankOutputFormatter extends OutputFormatter {
-
- @Override
- public String getName() {
- return "minrank";
- }
-
- private static void outputToStreamOrSave(
- int rank,
- Label label,
- PrintStream out,
- @Nullable List<RankAndLabel> toSave,
- final String lineTerminator) {
- if (toSave != null) {
- toSave.add(new RankAndLabel(rank, label));
- } else {
- out.print(rank + " " + label.getDefaultCanonicalForm() + lineTerminator);
- }
- }
-
- @Override
- public void output(
- QueryOptions options,
- Digraph<Target> result,
- OutputStream out,
- AspectResolver aspectResolver,
- ConditionalEdges conditionalEdges)
- throws IOException {
- PrintStream printStream = new PrintStream(out);
- // getRoots() isn't defined for cyclic graphs, so in order to handle
- // cycles correctly, we need work on the strong component graph, as
- // cycles should be treated a "clump" of nodes all on the same rank.
- // Graphs may contain cycles because there are errors in BUILD files.
-
- List<RankAndLabel> outputToOrder =
- options.orderOutput == OrderOutput.FULL ? new ArrayList<>() : null;
- Digraph<Set<Node<Target>>> scGraph = result.getStrongComponentGraph();
- Set<Node<Set<Node<Target>>>> rankNodes = scGraph.getRoots();
- Set<Node<Set<Node<Target>>>> seen = new HashSet<>();
- seen.addAll(rankNodes);
- final String lineTerm = options.getLineTerminator();
- for (int rank = 0; !rankNodes.isEmpty(); rank++) {
- // Print out this rank:
- for (Node<Set<Node<Target>>> xScc : rankNodes) {
- for (Node<Target> x : xScc.getLabel()) {
- outputToStreamOrSave(
- rank, x.getLabel().getLabel(), printStream, outputToOrder, lineTerm);
- }
- }
-
- // Find the next rank:
- Set<Node<Set<Node<Target>>>> nextRankNodes = new LinkedHashSet<>();
- for (Node<Set<Node<Target>>> x : rankNodes) {
- for (Node<Set<Node<Target>>> y : x.getSuccessors()) {
- if (seen.add(y)) {
- nextRankNodes.add(y);
- }
- }
- }
- rankNodes = nextRankNodes;
- }
- if (outputToOrder != null) {
- Collections.sort(outputToOrder);
- for (RankAndLabel item : outputToOrder) {
- printStream.print(item + lineTerm);
- }
- }
-
- flushAndCheckError(printStream);
- }
- }
-
- /**
- * An output formatter that prints the labels in maximum rank order, preceded
- * by their rank number. "Roots" have rank 0, all other nodes have a rank
- * which is one greater than the maximum rank of each of their predecessors.
- * All nodes in a cycle are considered of equal rank. MAXRANK shows the
- * highest rank for a given node, i.e. the length of the longest non-cyclic
- * path from a zero-rank node to it.
- *
- * <p>If the result came from a <code>deps(x)</code> query, then the MAXRANKs
- * correspond to the longest path from x to each of its prerequisites.
- */
- private static class MaxrankOutputFormatter extends OutputFormatter {
-
- @Override
- public String getName() {
- return "maxrank";
- }
-
- @Override
- public void output(
- QueryOptions options,
- Digraph<Target> result,
- OutputStream out,
- AspectResolver aspectResolver,
- ConditionalEdges conditionalEdges)
- throws IOException {
- // In order to handle cycles correctly, we need work on the strong
- // component graph, as cycles should be treated a "clump" of nodes all on
- // the same rank. Graphs may contain cycles because there are errors in BUILD files.
-
- // Dynamic programming algorithm:
- // rank(x) = max(rank(p)) + 1 foreach p in preds(x)
- // TODO(bazel-team): Move to Digraph.
- class DP {
- final Map<Node<Set<Node<Target>>>, Integer> ranks = new HashMap<>();
-
- int rank(Node<Set<Node<Target>>> node) {
- Integer rank = ranks.get(node);
- if (rank == null) {
- int maxPredRank = -1;
- for (Node<Set<Node<Target>>> p : node.getPredecessors()) {
- maxPredRank = Math.max(maxPredRank, rank(p));
- }
- rank = maxPredRank + 1;
- ranks.put(node, rank);
- }
- return rank;
- }
- }
- DP dp = new DP();
-
- // Now sort by rank...
- List<RankAndLabel> output = new ArrayList<>();
- for (Node<Set<Node<Target>>> x : result.getStrongComponentGraph().getNodes()) {
- int rank = dp.rank(x);
- for (Node<Target> y : x.getLabel()) {
- output.add(new RankAndLabel(rank, y.getLabel().getLabel()));
- }
- }
- if (options.orderOutput == OrderOutput.FULL) {
- // Use the natural order for RankAndLabels, which breaks ties alphabetically.
- Collections.sort(output);
- } else {
- Collections.sort(output, comparingInt(arg -> arg.rank));
- }
- final String lineTerm = options.getLineTerminator();
- PrintStream printStream = new PrintStream(out);
- for (RankAndLabel item : output) {
- printStream.print(item + lineTerm);
- }
- flushAndCheckError(printStream);
- }
- }
-
/** Helper class for {@link #getPossibleAttributeValues}. */
public static class PossibleAttributeValues implements Iterable<Object> {
final Iterable<Object> values;
@@ -653,7 +203,7 @@
* operations generally don't care about specific attribute values - they just care which labels
* are possible.
*/
- protected static PossibleAttributeValues getPossibleAttributeValues(Rule rule, Attribute attr) {
+ static PossibleAttributeValues getPossibleAttributeValues(Rule rule, Attribute attr) {
AttributeValueSource source = getAttributeSource(rule, attr);
AggregatingAttributeMapper attributeMap = AggregatingAttributeMapper.of(rule);
Iterable<?> list;
@@ -679,12 +229,6 @@
}
}
- private static void flushAndCheckError(PrintStream printStream) throws IOException {
- if (printStream.checkError()) {
- throw new IOException("PrintStream encountered an error");
- }
- }
-
/**
* Returns the target location, eventually stripping out the workspace path to obtain a relative
* target (stable across machines / workspaces).
@@ -693,24 +237,11 @@
* @param relative Whether to return a relative path or not.
* @return the target location
*/
- protected static String getLocation(Target target, boolean relative) {
+ static String getLocation(Target target, boolean relative) {
Location location = target.getLocation();
return relative
? location.print(target.getPackage().getPackageDirectory().asFragment(),
target.getPackage().getNameFragment())
: location.print();
}
-
- /** Prints labels in their canonical form. */
- public static class LabelPrinter extends Printer.BasePrinter {
- @Override
- public LabelPrinter repr(Object o) {
- if (o instanceof Label) {
- writeString(((Label) o).getCanonicalForm());
- } else {
- super.repr(o);
- }
- return this;
- }
- }
}
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java
new file mode 100644
index 0000000..12170d2
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java
@@ -0,0 +1,71 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.common.collect.Sets;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
+import com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Set;
+
+/**
+ * An output formatter that prints the names of the packages of the target
+ * set, in lexicographical order without duplicates.
+ */
+class PackageOutputFormatter extends AbstractUnorderedFormatter {
+
+ @Override
+ public String getName() {
+ return "package";
+ }
+
+ @Override
+ public OutputFormatterCallback<Target> createPostFactoStreamCallback(
+ OutputStream out, final QueryOptions options) {
+ return new TextOutputFormatterCallback<Target>(out) {
+ private final Set<String> packageNames = Sets.newTreeSet();
+
+ @Override
+ public void processOutput(Iterable<Target> partialResult) {
+
+ for (Target target : partialResult) {
+ packageNames.add(target.getLabel().getPackageIdentifier().toString());
+ }
+ }
+
+ @Override
+ public void close(boolean failFast) throws IOException {
+ if (!failFast) {
+ final String lineTerm = options.getLineTerminator();
+ for (String packageName : packageNames) {
+ writer.append(packageName).append(lineTerm);
+ }
+ }
+ super.close(failFast);
+ }
+ };
+ }
+
+ @Override
+ public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
+ OutputStream out, QueryOptions options, QueryEnvironment<?> env) {
+ return new SynchronizedDelegatingOutputFormatterCallback<>(
+ createPostFactoStreamCallback(out, options));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java
index a206804..7cf24ed 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java
@@ -52,7 +52,6 @@
import com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult;
import com.google.devtools.build.lib.query2.proto.proto2api.Build.SourceFile;
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
-import com.google.devtools.build.lib.query2.query.output.OutputFormatter.AbstractUnorderedFormatter;
import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
import com.google.devtools.build.lib.syntax.Type;
import com.google.protobuf.CodedOutputStream;
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java
index cba93a7..2cc908c 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java
@@ -21,7 +21,6 @@
import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
import com.google.devtools.build.lib.query2.engine.QueryEvalResult;
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
-import com.google.devtools.build.lib.query2.query.output.OutputFormatter.StreamedFormatter;
import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
import java.io.IOException;
import java.io.OutputStream;
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java
new file mode 100644
index 0000000..0200fab
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java
@@ -0,0 +1,44 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.devtools.build.lib.cmdline.Label;
+
+class RankAndLabel implements Comparable<RankAndLabel> {
+ private final int rank;
+ private final Label label;
+
+ RankAndLabel(int rank, Label label) {
+ this.rank = rank;
+ this.label = label;
+ }
+
+ int getRank() {
+ return rank;
+ }
+
+ @Override
+ public int compareTo(RankAndLabel o) {
+ if (this.rank != o.rank) {
+ return this.rank - o.rank;
+ }
+ return this.label.compareTo(o.label);
+ }
+
+ @Override
+ public String toString() {
+ return rank + " " + label.getDefaultCanonicalForm();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java
new file mode 100644
index 0000000..b390ee5
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java
@@ -0,0 +1,59 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.query2.CommonQueryOptions;
+import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
+import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
+import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
+import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
+import java.io.OutputStream;
+
+/**
+ * Unordered streamed output formatter (wrt. dependency ordering).
+ *
+ * <p>Formatters that support streamed output may be used when only the set of query results is
+ * requested but their ordering is irrelevant.
+ *
+ * <p>The benefit of using a streamed formatter is that we can save the potentially expensive
+ * subgraph extraction step before presenting the query results and that depending on the query
+ * environment used, it can be more memory performant, as it does not aggregate all the data
+ * before writing in the output.
+ */
+public interface StreamedFormatter {
+ /** Specifies options to be used by subsequent calls to {@link #createStreamCallback}. */
+ void setOptions(CommonQueryOptions options, AspectResolver aspectResolver);
+
+ /**
+ * Returns a {@link ThreadSafeOutputFormatterCallback} whose
+ * {@link OutputFormatterCallback#process} outputs formatted {@link Target}s to the given
+ * {@code out}.
+ *
+ * <p>Takes any options specified via the most recent call to {@link #setOptions} into
+ * consideration.
+ *
+ * <p>Intended to be use for streaming out during evaluation of a query.
+ */
+ ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
+ OutputStream out, QueryOptions options, QueryEnvironment<?> env);
+
+ /**
+ * Same as {@link #createStreamCallback}, but intended to be used for outputting the
+ * already-computed result of a query.
+ */
+ OutputFormatterCallback<Target> createPostFactoStreamCallback(
+ OutputStream out, QueryOptions options);
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/TextOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/TextOutputFormatterCallback.java
new file mode 100644
index 0000000..5f62822
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/TextOutputFormatterCallback.java
@@ -0,0 +1,39 @@
+// Copyright 2014 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.query2.query.output;
+
+import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintStream;
+import java.io.Writer;
+
+/** Abstract class supplying a {@link PrintStream} to implementations, flushing it on close. */
+abstract class TextOutputFormatterCallback<T> extends OutputFormatterCallback<T> {
+ protected Writer writer;
+
+ @SuppressWarnings("DefaultCharset")
+ TextOutputFormatterCallback(OutputStream out) {
+ // This code intentionally uses the platform default encoding.
+ this.writer = new BufferedWriter(new OutputStreamWriter(out));
+ }
+
+ @Override
+ public void close(boolean failFast) throws IOException {
+ writer.flush();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java
index 80603a3..0fcebaa 100644
--- a/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java
+++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java
@@ -34,7 +34,6 @@
import com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback;
import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
-import com.google.devtools.build.lib.query2.query.output.OutputFormatter.AbstractUnorderedFormatter;
import com.google.devtools.build.lib.syntax.Type;
import java.io.IOException;
import java.io.OutputStream;
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java
index 3d0c1fc..3523539 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java
@@ -29,9 +29,9 @@
import com.google.devtools.build.lib.query2.engine.QueryUtil.AggregateAllOutputFormatterCallback;
import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
import com.google.devtools.build.lib.query2.query.output.OutputFormatter;
-import com.google.devtools.build.lib.query2.query.output.OutputFormatter.StreamedFormatter;
import com.google.devtools.build.lib.query2.query.output.QueryOptions;
import com.google.devtools.build.lib.query2.query.output.QueryOutputUtils;
+import com.google.devtools.build.lib.query2.query.output.StreamedFormatter;
import com.google.devtools.build.lib.runtime.BlazeCommandResult;
import com.google.devtools.build.lib.runtime.Command;
import com.google.devtools.build.lib.runtime.CommandEnvironment;