diff options
author | 2017-08-11 21:31:03 +0200 | |
---|---|---|
committer | 2017-08-14 14:15:31 +0200 | |
commit | 50f72495a5a1bfbb4ef57a42a81872b42829fb9f (patch) | |
tree | 4ab988716f5da073b144c1a7cd1462e8c90cd088 /src/main | |
parent | 9b24f404bcd9080046417610725f1a6e76e4254c (diff) |
Introduce the 'siblings' query function.
RELNOTES[NEW]: There is now a 'siblings' query function. See the query documentation for more details.
PiperOrigin-RevId: 165010653
Diffstat (limited to 'src/main')
4 files changed, 96 insertions, 5 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java index 2781b0fcd7..fe267b94f2 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java @@ -127,6 +127,14 @@ public class BlazeQueryEnvironment extends AbstractBlazeQueryEnvironment<Target> } @Override + public Collection<Target> getSiblingTargetsInPackage(Target target) { + Collection<Target> siblings = target.getPackage().getTargets().values(); + // Ensure that the sibling targets are in the graph being built-up. + siblings.forEach(this::getNode); + return siblings; + } + + @Override public QueryTaskFuture<Void> getTargetsMatchingPattern( QueryExpression owner, String pattern, Callback<Target> callback) { try { 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 84313ae534..a9130f7fdc 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 @@ -691,6 +691,12 @@ public class SkyQueryEnvironment extends AbstractBlazeQueryEnvironment<Target> @ThreadSafe @Override + public Collection<Target> getSiblingTargetsInPackage(Target target) { + return target.getPackage().getTargets().values(); + } + + @ThreadSafe + @Override public QueryTaskFuture<Void> getTargetsMatchingPattern( QueryExpression owner, String pattern, Callback<Target> callback) { TargetPatternKey targetPatternKey; diff --git a/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java index 453a049cb8..1edc6584e6 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java @@ -16,6 +16,7 @@ package com.google.devtools.build.lib.query2.engine; import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; +import java.util.Collection; import java.util.List; import java.util.Set; import java.util.concurrent.Callable; @@ -142,6 +143,9 @@ public interface QueryEnvironment<T> { } } + /** Returns all of the targets in <code>target</code>'s package, in some stable order. */ + Collection<T> getSiblingTargetsInPackage(T target); + /** * Invokes {@code callback} with the set of target nodes in the graph for the specified target * pattern, in 'blaze build' syntax. @@ -545,16 +549,17 @@ public interface QueryEnvironment<T> { ImmutableList<QueryFunction> DEFAULT_QUERY_FUNCTIONS = ImmutableList.of( new AllPathsFunction(), - new BuildFilesFunction(), - new LoadFilesFunction(), new AttrFunction(), + new BuildFilesFunction(), + new DepsFunction(), new FilterFunction(), - new LabelsFunction(), new KindFunction(), + new LabelsFunction(), + new LoadFilesFunction(), + new RdepsFunction(), + new SiblingsFunction(), new SomeFunction(), new SomePathFunction(), new TestsFunction(), - new DepsFunction(), - new RdepsFunction(), new VisibleFunction()); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/engine/SiblingsFunction.java b/src/main/java/com/google/devtools/build/lib/query2/engine/SiblingsFunction.java new file mode 100644 index 0000000000..5d03bfd949 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/query2/engine/SiblingsFunction.java @@ -0,0 +1,72 @@ +// Copyright 2017 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.engine; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Sets; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment.Argument; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment.ArgumentType; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryTaskFuture; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor; +import java.util.List; +import java.util.Set; + +/** + * A "siblings" query expression, which computes all of the targets in all of the packages of all + * the targets to which the argument evaluates. + * + * <pre>expr ::= SIBLINGS '(' expr ')'</pre> + */ +public class SiblingsFunction implements QueryFunction { + @Override + public String getName() { + return "siblings"; + } + + @Override + public int getMandatoryArguments() { + return 1; + } + + @Override + public Iterable<ArgumentType> getArgumentTypes() { + return ImmutableList.of(ArgumentType.EXPRESSION); + } + + @Override + public <T> QueryTaskFuture<Void> eval( + QueryEnvironment<T> env, + VariableContext<T> context, + final QueryExpression expression, + List<Argument> args, + final Callback<T> callback) { + final TargetAccessor<T> targetAccessor = env.getAccessor(); + Set<String> packageNames = Sets.newConcurrentHashSet(); + return env.eval( + args.get(0).getExpression(), + context, + new Callback<T>() { + @Override + public void process(Iterable<T> partialResult) + throws QueryException, InterruptedException { + for (T target : partialResult) { + if (packageNames.add(targetAccessor.getPackage(target))) { + callback.process(env.getSiblingTargetsInPackage(target)); + } + } + } + }); + } +} |