Search in sources :

Example 1 with DigraphQueryEvalResult

use of com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult in project bazel by bazelbuild.

the class BlazeQueryEnvironment method evaluateQuery.

@Override
public DigraphQueryEvalResult<Target> evaluateQuery(QueryExpression expr, ThreadSafeOutputFormatterCallback<Target> callback) throws QueryException, InterruptedException, IOException {
    eventHandler.resetErrors();
    resolvedTargetPatterns.clear();
    QueryEvalResult queryEvalResult = super.evaluateQuery(expr, callback);
    return new DigraphQueryEvalResult<>(queryEvalResult.getSuccess(), queryEvalResult.isEmpty(), graph);
}
Also used : DigraphQueryEvalResult(com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult) QueryEvalResult(com.google.devtools.build.lib.query2.engine.QueryEvalResult) DigraphQueryEvalResult(com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult)

Example 2 with DigraphQueryEvalResult

use of com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult in project bazel by bazelbuild.

the class QueryOutputUtils method output.

public static void output(QueryOptions queryOptions, QueryEvalResult result, Set<Target> targetsResult, OutputFormatter formatter, OutputStream outputStream, AspectResolver aspectResolver) throws IOException, InterruptedException {
    /*
     * This is not really streaming, but we are using the streaming interface for writing into the
     * output everything in one batch. This happens when the QueryEnvironment does not
     * support streaming but we don't care about ordered results.
     */
    boolean orderedResults = !shouldStreamResults(queryOptions, formatter);
    if (orderedResults) {
        formatter.output(queryOptions, ((DigraphQueryEvalResult<Target>) result).getGraph().extractSubgraph(targetsResult), outputStream, aspectResolver);
    } else {
        StreamedFormatter streamedFormatter = (StreamedFormatter) formatter;
        streamedFormatter.setOptions(queryOptions, aspectResolver);
        OutputFormatterCallback.processAllTargets(streamedFormatter.createPostFactoStreamCallback(outputStream, queryOptions), targetsResult);
    }
}
Also used : StreamedFormatter(com.google.devtools.build.lib.query2.output.OutputFormatter.StreamedFormatter) DigraphQueryEvalResult(com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult)

Example 3 with DigraphQueryEvalResult

use of com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult in project bazel by bazelbuild.

the class GenQuery method doQuery.

@SuppressWarnings("unchecked")
@Nullable
private byte[] doQuery(QueryOptions queryOptions, PackageProvider packageProvider, Predicate<Label> labelFilter, TargetPatternEvaluator evaluator, String query, RuleContext ruleContext) throws InterruptedException {
    DigraphQueryEvalResult<Target> queryResult;
    OutputFormatter formatter;
    AggregateAllOutputFormatterCallback<Target> targets = QueryUtil.newOrderedAggregateAllOutputFormatterCallback();
    try {
        Set<Setting> settings = queryOptions.toSettings();
        // Turns out, if we have two targets with a cycle of length 2 were one of
        // the edges is of type NODEP_LABEL type, the targets both show up in
        // each other's result for deps(X) when the query is executed using
        // 'blaze query'. This obviously does not fly when doing the query as a
        // part of the build, thus, there is a slight discrepancy between the
        // behavior of the query engine in these two use cases.
        settings.add(Setting.NO_NODEP_DEPS);
        ImmutableList<OutputFormatter> outputFormatters = QUERY_OUTPUT_FORMATTERS.get(ruleContext.getAnalysisEnvironment().getSkyframeEnv());
        // This is a precomputed value so it should have been injected by the rules module by the
        // time we get there.
        formatter = OutputFormatter.getFormatter(Preconditions.checkNotNull(outputFormatters), queryOptions.outputFormat);
        // All the packages are already loaded at this point, so there is no need
        // to start up many threads. 4 are started up to make good use of multiple
        // cores.
        BlazeQueryEnvironment queryEnvironment = (BlazeQueryEnvironment) QUERY_ENVIRONMENT_FACTORY.create(/*transitivePackageLoader=*/
        null, /*graph=*/
        null, packageProvider, evaluator, /*keepGoing=*/
        false, ruleContext.attributes().get("strict", Type.BOOLEAN), /*orderedResults=*/
        !QueryOutputUtils.shouldStreamResults(queryOptions, formatter), /*universeScope=*/
        ImmutableList.<String>of(), /*loadingPhaseThreads=*/
        4, labelFilter, getEventHandler(ruleContext), settings, ImmutableList.<QueryFunction>of(), /*packagePath=*/
        null, /*blockUniverseEvaluationErrors=*/
        false);
        queryResult = (DigraphQueryEvalResult<Target>) queryEnvironment.evaluateQuery(query, targets);
    } catch (SkyframeRestartQueryException e) {
        // inconsistent from run to run, and make detecting legitimate errors more difficult.
        return null;
    } catch (QueryException e) {
        ruleContext.ruleError("query failed: " + e.getMessage());
        return null;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        QueryOutputUtils.output(queryOptions, queryResult, targets.getResult(), formatter, outputStream, queryOptions.aspectDeps.createResolver(packageProvider, getEventHandler(ruleContext)));
    } catch (ClosedByInterruptException e) {
        throw new InterruptedException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return outputStream.toByteArray();
}
Also used : Setting(com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputFormatter(com.google.devtools.build.lib.query2.output.OutputFormatter) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) SkyframeRestartQueryException(com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException) BlazeQueryEnvironment(com.google.devtools.build.lib.query2.BlazeQueryEnvironment) QueryFunction(com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction) SkyframeRestartQueryException(com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException) Nullable(javax.annotation.Nullable)

Aggregations

DigraphQueryEvalResult (com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 Target (com.google.devtools.build.lib.packages.Target)1 BlazeQueryEnvironment (com.google.devtools.build.lib.query2.BlazeQueryEnvironment)1 QueryFunction (com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction)1 Setting (com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting)1 QueryEvalResult (com.google.devtools.build.lib.query2.engine.QueryEvalResult)1 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)1 SkyframeRestartQueryException (com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException)1 OutputFormatter (com.google.devtools.build.lib.query2.output.OutputFormatter)1 StreamedFormatter (com.google.devtools.build.lib.query2.output.OutputFormatter.StreamedFormatter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 Nullable (javax.annotation.Nullable)1