diff options
author | 2015-11-06 19:05:13 +0000 | |
---|---|---|
committer | 2015-11-06 22:53:28 +0000 | |
commit | 42984f371c18033f3f162965b8f288ececbdbfd8 (patch) | |
tree | 3057903ff47fe12daf65f87bda0ec7321076adad /src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java | |
parent | 297f1202500e199fec28acdc070336507a8e2876 (diff) |
Transform Blaze query to be able to work in streamed mode.
--
MOS_MIGRATED_REVID=107249788
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java b/src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java index f844af97d8..ed49ef4f93 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java +++ b/src/main/java/com/google/devtools/build/lib/query2/engine/AllRdepsFunction.java @@ -22,10 +22,7 @@ import com.google.devtools.build.lib.query2.engine.QueryEnvironment.ArgumentType import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction; import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; /** * An "allrdeps" query expression, which computes the reverse dependencies of the argument within @@ -58,39 +55,41 @@ public class AllRdepsFunction implements QueryFunction { * Breadth-first search from the argument while sticking to nodes satisfying the {@code universe} * predicate. */ - protected <T> Set<T> eval(QueryEnvironment<T> env, List<Argument> args, Predicate<T> universe) + protected static <T> void eval(final QueryEnvironment<T> env, final List<Argument> args, + final Callback<T> callback, final Predicate<T> universe) throws QueryException, InterruptedException { - Set<T> argumentValue = args.get(0).getExpression().eval(env); - int depthBound = args.size() > 1 ? args.get(1).getInteger() : Integer.MAX_VALUE; - Set<T> visited = new LinkedHashSet<>(); - Collection<T> current = argumentValue; + final Uniquifier<T> uniquifier = env.createUniquifier(); + final int depthBound = args.size() > 1 ? args.get(1).getInteger() : Integer.MAX_VALUE; + env.eval(args.get(0).getExpression(), new Callback<T>() { + @Override + public void process(Iterable<T> partialResult) throws QueryException, InterruptedException { + Iterable<T> current = partialResult; + // We need to iterate depthBound + 1 times. + for (int i = 0; i <= depthBound; i++) { + List<T> next = new ArrayList<>(); + // Restrict to nodes satisfying the universe predicate. + Iterable<T> currentInUniverse = Iterables.filter(current, universe); + // Filter already visited nodes: if we see a node in a later round, then we don't need to + // visit it again, because the depth at which we see it must be greater than or equal to + // the last visit. + next.addAll(env.getReverseDeps(uniquifier.unique(currentInUniverse))); + callback.process(currentInUniverse); + if (next.isEmpty()) { + // Exit when there are no more nodes to visit. + break; + } + current = next; + } - // We need to iterate depthBound + 1 times. - for (int i = 0; i <= depthBound; i++) { - List<T> next = new ArrayList<>(); - // Restrict to nodes satisfying the universe predicate. - Iterable<T> currentInUniverse = Iterables.filter(current, universe); - // Filter already visited nodes: if we see a node in a later round, then we don't need to - // visit it again, because the depth at which we see it must be greater than or equal to - // the last visit. - next.addAll(env.getReverseDeps(Iterables.filter(currentInUniverse, - Predicates.not(Predicates.in(visited))))); - Iterables.addAll(visited, currentInUniverse); - if (next.isEmpty()) { - // Exit when there are no more nodes to visit. - break; } - current = next; - } - - return visited; + }); } /** Breadth-first search from the argument. */ @Override - public <T> Set<T> eval(QueryEnvironment<T> env, QueryExpression expression, List<Argument> args) - throws QueryException, InterruptedException { - return eval(env, args, Predicates.<T>alwaysTrue()); + public <T> void eval(QueryEnvironment<T> env, QueryExpression expression, List<Argument> args, + Callback<T> callback) throws QueryException, InterruptedException { + eval(env, args, callback, Predicates.<T>alwaysTrue()); } } |