Example usage for com.google.common.collect ImmutableMultimap of

List of usage examples for com.google.common.collect ImmutableMultimap of

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMultimap of.

Prototype

public static <K, V> ImmutableMultimap<K, V> of() 

Source Link

Document

Returns an empty multimap.

Usage

From source file:org.javersion.store.jdbc.AbstractVersionStoreJdbc.java

protected Multimap<Id, Revision> doPublish() {
    Map<Revision, Id> uncommittedRevisions = getUnpublishedRevisionsForUpdate();
    long lastOrdinal = getMaxOrdinal();

    log.debug("publish({})", uncommittedRevisions.size());
    if (uncommittedRevisions.isEmpty()) {
        return ImmutableMultimap.of();
    }//from w w  w. j  ava 2 s  .  c o  m

    Multimap<Id, Revision> publishedDocs = ArrayListMultimap.create();

    SQLUpdateClause versionUpdateBatch = options.queryFactory.update(options.version);

    for (Map.Entry<Revision, Id> entry : uncommittedRevisions.entrySet()) {
        Revision revision = entry.getKey();
        Id docId = entry.getValue();
        publishedDocs.put(docId, revision);
        setOrdinal(versionUpdateBatch, ++lastOrdinal).where(options.version.revision.eq(revision)).addBatch();
    }

    versionUpdateBatch.execute();

    afterPublish(publishedDocs);
    return publishedDocs;
}

From source file:io.prestosql.execution.scheduler.SourcePartitionedScheduler.java

@Override
public synchronized ScheduleResult schedule() {
    dropListenersFromWhenFinishedOrNewLifespansAdded();

    int overallSplitAssignmentCount = 0;
    ImmutableSet.Builder<RemoteTask> overallNewTasks = ImmutableSet.builder();
    List<ListenableFuture<?>> overallBlockedFutures = new ArrayList<>();
    boolean anyBlockedOnPlacements = false;
    boolean anyBlockedOnNextSplitBatch = false;
    boolean anyNotBlocked = false;

    for (Entry<Lifespan, ScheduleGroup> entry : scheduleGroups.entrySet()) {
        Lifespan lifespan = entry.getKey();
        ScheduleGroup scheduleGroup = entry.getValue();
        Set<Split> pendingSplits = scheduleGroup.pendingSplits;

        if (scheduleGroup.state == ScheduleGroupState.NO_MORE_SPLITS
                || scheduleGroup.state == ScheduleGroupState.DONE) {
            verify(scheduleGroup.nextSplitBatchFuture == null);
        } else if (pendingSplits.isEmpty()) {
            // try to get the next batch
            if (scheduleGroup.nextSplitBatchFuture == null) {
                scheduleGroup.nextSplitBatchFuture = splitSource.getNextBatch(scheduleGroup.partitionHandle,
                        lifespan, splitBatchSize - pendingSplits.size());

                long start = System.nanoTime();
                addSuccessCallback(scheduleGroup.nextSplitBatchFuture, () -> stage.recordGetSplitTime(start));
            }//  w w w . j a  va  2  s  .  com

            if (scheduleGroup.nextSplitBatchFuture.isDone()) {
                SplitBatch nextSplits = getFutureValue(scheduleGroup.nextSplitBatchFuture);
                scheduleGroup.nextSplitBatchFuture = null;
                pendingSplits.addAll(nextSplits.getSplits());
                if (nextSplits.isLastBatch()) {
                    if (scheduleGroup.state == ScheduleGroupState.INITIALIZED && pendingSplits.isEmpty()) {
                        // Add an empty split in case no splits have been produced for the source.
                        // For source operators, they never take input, but they may produce output.
                        // This is well handled by Presto execution engine.
                        // However, there are certain non-source operators that may produce output without any input,
                        // for example, 1) an AggregationOperator, 2) a HashAggregationOperator where one of the grouping sets is ().
                        // Scheduling an empty split kicks off necessary driver instantiation to make this work.
                        pendingSplits
                                .add(new Split(splitSource.getConnectorId(), splitSource.getTransactionHandle(),
                                        new EmptySplit(splitSource.getConnectorId()), lifespan));
                    }
                    scheduleGroup.state = ScheduleGroupState.NO_MORE_SPLITS;
                }
            } else {
                overallBlockedFutures.add(scheduleGroup.nextSplitBatchFuture);
                anyBlockedOnNextSplitBatch = true;
                continue;
            }
        }

        Multimap<Node, Split> splitAssignment = ImmutableMultimap.of();
        if (!pendingSplits.isEmpty()) {
            if (!scheduleGroup.placementFuture.isDone()) {
                anyBlockedOnPlacements = true;
                continue;
            }

            if (scheduleGroup.state == ScheduleGroupState.INITIALIZED) {
                scheduleGroup.state = ScheduleGroupState.SPLITS_ADDED;
            }
            if (state == State.INITIALIZED) {
                state = State.SPLITS_ADDED;
            }

            // calculate placements for splits
            SplitPlacementResult splitPlacementResult = splitPlacementPolicy.computeAssignments(pendingSplits);
            splitAssignment = splitPlacementResult.getAssignments();

            // remove splits with successful placements
            splitAssignment.values().forEach(pendingSplits::remove); // AbstractSet.removeAll performs terribly here.
            overallSplitAssignmentCount += splitAssignment.size();

            // if not completed placed, mark scheduleGroup as blocked on placement
            if (!pendingSplits.isEmpty()) {
                scheduleGroup.placementFuture = splitPlacementResult.getBlocked();
                overallBlockedFutures.add(scheduleGroup.placementFuture);
                anyBlockedOnPlacements = true;
            }
        }

        // if no new splits will be assigned, update state and attach completion event
        Multimap<Node, Lifespan> noMoreSplitsNotification = ImmutableMultimap.of();
        if (pendingSplits.isEmpty() && scheduleGroup.state == ScheduleGroupState.NO_MORE_SPLITS) {
            scheduleGroup.state = ScheduleGroupState.DONE;
            if (!lifespan.isTaskWide()) {
                Node node = ((BucketedSplitPlacementPolicy) splitPlacementPolicy)
                        .getNodeForBucket(lifespan.getId());
                noMoreSplitsNotification = ImmutableMultimap.of(node, lifespan);
            }
        }

        // assign the splits with successful placements
        overallNewTasks.addAll(assignSplits(splitAssignment, noMoreSplitsNotification));

        // Assert that "placement future is not done" implies "pendingSplits is not empty".
        // The other way around is not true. One obvious reason is (un)lucky timing, where the placement is unblocked between `computeAssignments` and this line.
        // However, there are other reasons that could lead to this.
        // Note that `computeAssignments` is quite broken:
        // 1. It always returns a completed future when there are no tasks, regardless of whether all nodes are blocked.
        // 2. The returned future will only be completed when a node with an assigned task becomes unblocked. Other nodes don't trigger future completion.
        // As a result, to avoid busy loops caused by 1, we check pendingSplits.isEmpty() instead of placementFuture.isDone() here.
        if (scheduleGroup.nextSplitBatchFuture == null && scheduleGroup.pendingSplits.isEmpty()
                && scheduleGroup.state != ScheduleGroupState.DONE) {
            anyNotBlocked = true;
        }
    }

    // * `splitSource.isFinished` invocation may fail after `splitSource.close` has been invoked.
    //   If state is NO_MORE_SPLITS/FINISHED, splitSource.isFinished has previously returned true, and splitSource is closed now.
    // * Even if `splitSource.isFinished()` return true, it is not necessarily safe to tear down the split source.
    //   * If anyBlockedOnNextSplitBatch is true, it means we have not checked out the recently completed nextSplitBatch futures,
    //     which may contain recently published splits. We must not ignore those.
    //   * If any scheduleGroup is still in DISCOVERING_SPLITS state, it means it hasn't realized that there will be no more splits.
    //     Next time it invokes getNextBatch, it will realize that. However, the invocation will fail we tear down splitSource now.
    if ((state == State.NO_MORE_SPLITS || state == State.FINISHED)
            || (noMoreScheduleGroups && scheduleGroups.isEmpty() && splitSource.isFinished())) {
        switch (state) {
        case INITIALIZED:
            // We have not scheduled a single split so far.
            // But this shouldn't be possible. See usage of EmptySplit in this method.
            throw new IllegalStateException("At least 1 split should have been scheduled for this plan node");
        case SPLITS_ADDED:
            state = State.NO_MORE_SPLITS;
            splitSource.close();
            // fall through
        case NO_MORE_SPLITS:
            state = State.FINISHED;
            whenFinishedOrNewLifespanAdded.set(null);
            // fall through
        case FINISHED:
            return new ScheduleResult(true, overallNewTasks.build(), overallSplitAssignmentCount);
        default:
            throw new IllegalStateException("Unknown state");
        }
    }

    if (anyNotBlocked) {
        return new ScheduleResult(false, overallNewTasks.build(), overallSplitAssignmentCount);
    }

    if (anyBlockedOnPlacements || groupedExecution) {
        // In a broadcast join, output buffers of the tasks in build source stage have to
        // hold onto all data produced before probe side task scheduling finishes,
        // even if the data is acknowledged by all known consumers. This is because
        // new consumers may be added until the probe side task scheduling finishes.
        //
        // As a result, the following line is necessary to prevent deadlock
        // due to neither build nor probe can make any progress.
        // The build side blocks due to a full output buffer.
        // In the meantime the probe side split cannot be consumed since
        // builder side hash table construction has not finished.
        overallNewTasks.addAll(finalizeTaskCreationIfNecessary());
    }

    ScheduleResult.BlockedReason blockedReason;
    if (anyBlockedOnNextSplitBatch) {
        blockedReason = anyBlockedOnPlacements ? MIXED_SPLIT_QUEUES_FULL_AND_WAITING_FOR_SOURCE
                : WAITING_FOR_SOURCE;
    } else {
        blockedReason = anyBlockedOnPlacements ? SPLIT_QUEUES_FULL : NO_ACTIVE_DRIVER_GROUP;
    }

    overallBlockedFutures.add(whenFinishedOrNewLifespanAdded);
    return new ScheduleResult(false, overallNewTasks.build(),
            nonCancellationPropagating(whenAnyComplete(overallBlockedFutures)), blockedReason,
            overallSplitAssignmentCount);
}

From source file:com.facebook.buck.features.python.PythonBinaryDescription.java

@Override
public PythonBinary createBuildRule(BuildRuleCreationContextWithTargetGraph context, BuildTarget buildTarget,
        BuildRuleParams params, PythonBinaryDescriptionArg args) {
    if (args.getMain().isPresent() == args.getMainModule().isPresent()) {
        throw new HumanReadableException("%s: must set exactly one of `main_module` and `main`", buildTarget);
    }/* w w  w. ja v  a 2 s  .  com*/
    Path baseModule = PythonUtil.getBasePath(buildTarget, args.getBaseModule());

    String mainModule;
    ImmutableMap.Builder<Path, SourcePath> modules = ImmutableMap.builder();
    ActionGraphBuilder graphBuilder = context.getActionGraphBuilder();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(graphBuilder);
    SourcePathResolver pathResolver = DefaultSourcePathResolver.from(ruleFinder);

    // If `main` is set, add it to the map of modules for this binary and also set it as the
    // `mainModule`, otherwise, use the explicitly set main module.
    if (args.getMain().isPresent()) {
        LOG.warn("%s: parameter `main` is deprecated, please use `main_module` instead.", buildTarget);
        String mainName = pathResolver.getSourcePathName(buildTarget, args.getMain().get());
        Path main = baseModule.resolve(mainName);
        modules.put(baseModule.resolve(mainName), args.getMain().get());
        mainModule = PythonUtil.toModuleName(buildTarget, main.toString());
    } else {
        mainModule = args.getMainModule().get();
    }
    // Build up the list of all components going into the python binary.
    PythonPackageComponents binaryPackageComponents = PythonPackageComponents.of(modules.build(),
            /* resources */ ImmutableMap.of(), /* nativeLibraries */ ImmutableMap.of(),
            /* moduleDirs */ ImmutableMultimap.of(), /* zipSafe */ args.getZipSafe());

    FlavorDomain<PythonPlatform> pythonPlatforms = toolchainProvider
            .getByName(PythonPlatformsProvider.DEFAULT_NAME, PythonPlatformsProvider.class)
            .getPythonPlatforms();

    // Extract the platforms from the flavor, falling back to the default platforms if none are
    // found.
    PythonPlatform pythonPlatform = pythonPlatforms.getValue(buildTarget)
            .orElse(pythonPlatforms.getValue(args.getPlatform().<Flavor>map(InternalFlavor::of)
                    .orElse(pythonPlatforms.getFlavors().iterator().next())));
    CxxPlatform cxxPlatform = getCxxPlatform(buildTarget, args).resolve(graphBuilder);
    CellPathResolver cellRoots = context.getCellPathResolver();
    ProjectFilesystem projectFilesystem = context.getProjectFilesystem();
    StringWithMacrosConverter macrosConverter = StringWithMacrosConverter.builder().setBuildTarget(buildTarget)
            .setCellPathResolver(cellRoots).setExpanders(PythonUtil.MACRO_EXPANDERS).build();
    PythonPackageComponents allPackageComponents = PythonUtil.getAllComponents(cellRoots, buildTarget,
            projectFilesystem, params, graphBuilder, ruleFinder,
            PythonUtil.getDeps(pythonPlatform, cxxPlatform, args.getDeps(), args.getPlatformDeps()).stream()
                    .map(graphBuilder::getRule).collect(ImmutableList.toImmutableList()),
            binaryPackageComponents, pythonPlatform, cxxBuckConfig, cxxPlatform,
            args.getLinkerFlags().stream().map(x -> macrosConverter.convert(x, graphBuilder))
                    .collect(ImmutableList.toImmutableList()),
            pythonBuckConfig.getNativeLinkStrategy(), args.getPreloadDeps());
    return createPackageRule(buildTarget, projectFilesystem, params, graphBuilder, ruleFinder, pythonPlatform,
            cxxPlatform, mainModule, args.getExtension(), allPackageComponents, args.getBuildArgs(),
            args.getPackageStyle().orElse(pythonBuckConfig.getPackageStyle()),
            PythonUtil.getPreloadNames(graphBuilder, cxxPlatform, args.getPreloadDeps()));
}

From source file:com.google.devtools.build.lib.rules.cpp.FdoSupport.java

/**
 * Extracts the FDO zip file and collects data from it that's needed during analysis.
 *
 * <p>When an {@code --fdo_optimize} compile is requested, unpacks the given
 * FDO gcda zip file into a clean working directory under execRoot.
 *
 * @throws FdoException if the FDO ZIP contains a file of unknown type
 *///from w w  w  . j ava2 s .  c  o m
private static FdoZipContents extractFdoZip(FdoMode fdoMode, LipoMode lipoMode, Path execRoot, Path fdoProfile,
        PathFragment fdoRootExecPath, String productName) throws IOException, FdoException {
    // The execRoot != null case is only there for testing. We cannot provide a real ZIP file in
    // tests because ZipFileSystem does not work with a ZIP on an in-memory file system.
    // IMPORTANT: Keep in sync with #declareSkyframeDependencies to avoid incrementality issues.
    ImmutableSet<PathFragment> gcdaFiles = ImmutableSet.of();
    ImmutableMultimap<PathFragment, PathFragment> imports = ImmutableMultimap.of();

    if (fdoProfile != null && execRoot != null) {
        Path fdoDirPath = execRoot.getRelative(fdoRootExecPath);

        FileSystemUtils.deleteTreesBelow(fdoDirPath);
        FileSystemUtils.createDirectoryAndParents(fdoDirPath);

        if (fdoMode == FdoMode.AUTO_FDO) {
            if (lipoMode != LipoMode.OFF) {
                imports = readAutoFdoImports(getAutoFdoImportsPath(fdoProfile));
            }
            FileSystemUtils.ensureSymbolicLink(
                    execRoot.getRelative(getAutoProfilePath(fdoProfile, fdoRootExecPath)), fdoProfile);
        } else if (fdoMode == FdoMode.LLVM_FDO) {
            FileSystemUtils.ensureSymbolicLink(
                    execRoot.getRelative(getLLVMProfilePath(fdoProfile, fdoRootExecPath)), fdoProfile);
        } else {
            Path zipFilePath = new ZipFileSystem(fdoProfile).getRootDirectory();
            String outputSymlinkName = productName + "-out";
            if (!zipFilePath.getRelative(outputSymlinkName).isDirectory()) {
                throw new ZipException("FDO zip files must be zipped directly above '" + outputSymlinkName
                        + "' for the compiler to find the profile");
            }
            ImmutableSet.Builder<PathFragment> gcdaFilesBuilder = ImmutableSet.builder();
            ImmutableMultimap.Builder<PathFragment, PathFragment> importsBuilder = ImmutableMultimap.builder();
            extractFdoZipDirectory(zipFilePath, fdoDirPath, gcdaFilesBuilder, importsBuilder);
            gcdaFiles = gcdaFilesBuilder.build();
            imports = importsBuilder.build();
        }
    }

    return new FdoZipContents(gcdaFiles, imports);
}

From source file:com.facebook.buck.features.python.PythonTestDescription.java

@Override
public PythonTest createBuildRule(BuildRuleCreationContextWithTargetGraph context, BuildTarget buildTarget,
        BuildRuleParams params, PythonTestDescriptionArg args) {

    FlavorDomain<PythonPlatform> pythonPlatforms = toolchainProvider
            .getByName(PythonPlatformsProvider.DEFAULT_NAME, PythonPlatformsProvider.class)
            .getPythonPlatforms();/*from   w  ww  .  jav a  2  s.c om*/

    ActionGraphBuilder graphBuilder = context.getActionGraphBuilder();
    PythonPlatform pythonPlatform = pythonPlatforms.getValue(buildTarget)
            .orElse(pythonPlatforms.getValue(args.getPlatform().<Flavor>map(InternalFlavor::of)
                    .orElse(pythonPlatforms.getFlavors().iterator().next())));
    CxxPlatform cxxPlatform = getCxxPlatform(buildTarget, args).resolve(graphBuilder);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(graphBuilder);
    SourcePathResolver pathResolver = DefaultSourcePathResolver.from(ruleFinder);
    Path baseModule = PythonUtil.getBasePath(buildTarget, args.getBaseModule());
    Optional<ImmutableMap<BuildTarget, Version>> selectedVersions = context.getTargetGraph().get(buildTarget)
            .getSelectedVersions();

    ImmutableMap<Path, SourcePath> srcs = PythonUtil.getModules(buildTarget, graphBuilder, ruleFinder,
            pathResolver, pythonPlatform, cxxPlatform, "srcs", baseModule, args.getSrcs(),
            args.getPlatformSrcs(), args.getVersionedSrcs(), selectedVersions);

    ImmutableMap<Path, SourcePath> resources = PythonUtil.getModules(buildTarget, graphBuilder, ruleFinder,
            pathResolver, pythonPlatform, cxxPlatform, "resources", baseModule, args.getResources(),
            args.getPlatformResources(), args.getVersionedResources(), selectedVersions);

    // Convert the passed in module paths into test module names.
    ImmutableSet.Builder<String> testModulesBuilder = ImmutableSet.builder();
    for (Path name : srcs.keySet()) {
        testModulesBuilder.add(PythonUtil.toModuleName(buildTarget, name.toString()));
    }
    ImmutableSet<String> testModules = testModulesBuilder.build();

    ProjectFilesystem projectFilesystem = context.getProjectFilesystem();

    // Construct a build rule to generate the test modules list source file and
    // add it to the build.
    BuildRule testModulesBuildRule = createTestModulesSourceBuildRule(buildTarget, projectFilesystem,
            getTestModulesListPath(buildTarget, projectFilesystem), testModules);
    graphBuilder.addToIndex(testModulesBuildRule);

    String mainModule;
    if (args.getMainModule().isPresent()) {
        mainModule = args.getMainModule().get();
    } else {
        mainModule = PythonUtil.toModuleName(buildTarget, getTestMainName().toString());
    }

    // Build up the list of everything going into the python test.
    PythonPackageComponents testComponents = PythonPackageComponents.of(
            ImmutableMap.<Path, SourcePath>builder()
                    .put(getTestModulesListName(), testModulesBuildRule.getSourcePathToOutput())
                    .put(getTestMainName(), requireTestMain(buildTarget, projectFilesystem, graphBuilder))
                    .putAll(srcs).build(),
            resources, ImmutableMap.of(), ImmutableMultimap.of(), args.getZipSafe());
    ImmutableList<BuildRule> deps = RichStream
            .from(PythonUtil.getDeps(pythonPlatform, cxxPlatform, args.getDeps(), args.getPlatformDeps()))
            .concat(args.getNeededCoverage().stream().map(NeededCoverageSpec::getBuildTarget))
            .map(graphBuilder::getRule).collect(ImmutableList.toImmutableList());

    CellPathResolver cellRoots = context.getCellPathResolver();
    StringWithMacrosConverter macrosConverter = StringWithMacrosConverter.builder().setBuildTarget(buildTarget)
            .setCellPathResolver(cellRoots).setExpanders(PythonUtil.MACRO_EXPANDERS).build();
    PythonPackageComponents allComponents = PythonUtil.getAllComponents(cellRoots, buildTarget,
            projectFilesystem, params, graphBuilder, ruleFinder, deps, testComponents, pythonPlatform,
            cxxBuckConfig, cxxPlatform,
            args.getLinkerFlags().stream().map(x -> macrosConverter.convert(x, graphBuilder))
                    .collect(ImmutableList.toImmutableList()),
            pythonBuckConfig.getNativeLinkStrategy(), args.getPreloadDeps());

    // Build the PEX using a python binary rule with the minimum dependencies.
    buildTarget.assertUnflavored();
    PythonBinary binary = binaryDescription.createPackageRule(buildTarget.withAppendedFlavors(BINARY_FLAVOR),
            projectFilesystem, params, graphBuilder, ruleFinder, pythonPlatform, cxxPlatform, mainModule,
            args.getExtension(), allComponents, args.getBuildArgs(),
            args.getPackageStyle().orElse(pythonBuckConfig.getPackageStyle()),
            PythonUtil.getPreloadNames(graphBuilder, cxxPlatform, args.getPreloadDeps()));
    graphBuilder.addToIndex(binary);

    ImmutableList.Builder<Pair<Float, ImmutableSet<Path>>> neededCoverageBuilder = ImmutableList.builder();
    for (NeededCoverageSpec coverageSpec : args.getNeededCoverage()) {
        BuildRule buildRule = graphBuilder.getRule(coverageSpec.getBuildTarget());
        if (deps.contains(buildRule) && buildRule instanceof PythonLibrary) {
            PythonLibrary pythonLibrary = (PythonLibrary) buildRule;
            ImmutableSortedSet<Path> paths;
            if (coverageSpec.getPathName().isPresent()) {
                Path path = coverageSpec.getBuildTarget().getBasePath()
                        .resolve(coverageSpec.getPathName().get());
                if (!pythonLibrary.getPythonPackageComponents(pythonPlatform, cxxPlatform, graphBuilder)
                        .getModules().keySet().contains(path)) {
                    throw new HumanReadableException(
                            "%s: path %s specified in needed_coverage not found in target %s", buildTarget,
                            path, buildRule.getBuildTarget());
                }
                paths = ImmutableSortedSet.of(path);
            } else {
                paths = ImmutableSortedSet.copyOf(
                        pythonLibrary.getPythonPackageComponents(pythonPlatform, cxxPlatform, graphBuilder)
                                .getModules().keySet());
            }
            neededCoverageBuilder
                    .add(new Pair<>(coverageSpec.getNeededCoverageRatioPercentage() / 100.f, paths));
        } else {
            throw new HumanReadableException(
                    "%s: needed_coverage requires a python library dependency. Found %s instead", buildTarget,
                    buildRule);
        }
    }

    Function<BuildRuleResolver, ImmutableMap<String, Arg>> testEnv = (ruleResolverInner) -> ImmutableMap
            .copyOf(Maps.transformValues(args.getEnv(), x -> macrosConverter.convert(x, graphBuilder)));

    // Additional CXX Targets used to generate CXX coverage.
    ImmutableSet<UnflavoredBuildTarget> additionalCoverageTargets = RichStream
            .from(args.getAdditionalCoverageTargets()).map(target -> target.getUnflavoredBuildTarget())
            .collect(ImmutableSet.toImmutableSet());
    ImmutableSortedSet<SourcePath> additionalCoverageSourcePaths = additionalCoverageTargets.isEmpty()
            ? ImmutableSortedSet.of()
            : binary.getRuntimeDeps(ruleFinder)
                    .filter(target -> additionalCoverageTargets.contains(target.getUnflavoredBuildTarget()))
                    .map(target -> DefaultBuildTargetSourcePath.of(target))
                    .collect(ImmutableSortedSet.toImmutableSortedSet(Ordering.natural()));

    // Generate and return the python test rule, which depends on the python binary rule above.
    return PythonTest.from(buildTarget, projectFilesystem, params, graphBuilder, testEnv, binary,
            args.getLabels(), neededCoverageBuilder.build(), additionalCoverageSourcePaths,
            args.getTestRuleTimeoutMs().map(Optional::of).orElse(
                    cxxBuckConfig.getDelegate().getView(TestBuckConfig.class).getDefaultTestRuleTimeoutMs()),
            args.getContacts());
}

From source file:com.palantir.atlasdb.keyvalue.jdbc.JdbcKeyValueService.java

@Override
public Multimap<Cell, Long> getAllTimestamps(final String tableName, final Set<Cell> cells,
        final long timestamp) throws InsufficientConsistencyException {
    if (cells.isEmpty()) {
        return ImmutableMultimap.of();
    }//w w w  .jav  a2 s  .  com
    return run(new Function<DSLContext, Multimap<Cell, Long>>() {
        @Override
        public Multimap<Cell, Long> apply(DSLContext ctx) {
            Result<? extends Record> records = ctx.select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP)
                    .from(atlasTable(tableName).as(ATLAS_TABLE))
                    .join(values(ctx, toRows(cells), TEMP_TABLE_1, ROW_NAME, COL_NAME))
                    .on(A_ROW_NAME.eq(T1_ROW_NAME).and(A_COL_NAME.eq(T1_COL_NAME)))
                    .where(A_TIMESTAMP.lessThan(timestamp)).fetch();
            Multimap<Cell, Long> results = ArrayListMultimap.create(records.size() / 4, 4);
            for (Record record : records) {
                results.put(Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)),
                        record.getValue(A_TIMESTAMP));
            }
            return results;
        }
    });
}

From source file:io.prestosql.execution.SqlStageExecution.java

public synchronized Optional<RemoteTask> scheduleTask(Node node, int partition, OptionalInt totalPartitions) {
    requireNonNull(node, "node is null");

    if (stateMachine.getState().isDone()) {
        return Optional.empty();
    }/* ww w  .java 2 s . c  om*/
    checkState(!splitsScheduled.get(), "scheduleTask can not be called once splits have been scheduled");
    return Optional.of(scheduleTask(node, new TaskId(stateMachine.getStageId(), partition),
            ImmutableMultimap.of(), totalPartitions));
}

From source file:org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.java

/**
 * tries to execute current {@link ModelProcessingPhase} of source parsing
 *
 * @param phase//from www.j a  v  a  2s. c  om
 *            to be executed (completed)
 * @return if phase was successfully completed
 * @throws SourceException
 *             when an error occured in source parsing
 */
boolean tryToCompletePhase(final ModelProcessingPhase phase) {

    boolean finished = true;
    final Collection<ContextMutation> openMutations = phaseMutation.get(phase);
    if (!openMutations.isEmpty()) {
        final Iterator<ContextMutation> it = openMutations.iterator();
        while (it.hasNext()) {
            final ContextMutation current = it.next();
            if (current.isFinished()) {
                it.remove();
            } else {
                finished = false;
            }
        }

        if (openMutations.isEmpty()) {
            phaseMutation.removeAll(phase);
            if (phaseMutation.isEmpty()) {
                phaseMutation = ImmutableMultimap.of();
            }
        }
    }

    for (final StatementContextBase<?, ?, ?> child : substatements.values()) {
        finished &= child.tryToCompletePhase(phase);
    }
    for (final StatementContextBase<?, ?, ?> child : effective) {
        finished &= child.tryToCompletePhase(phase);
    }

    if (finished) {
        onPhaseCompleted(phase);
        return true;
    }
    return false;
}

From source file:org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.java

/**
 * Occurs on end of {@link ModelProcessingPhase} of source parsing
 *
 * @param phase// w ww  .ja v a2 s.  co m
 *            that was to be completed (finished)
 * @throws SourceException
 *             when an error occurred in source parsing
 */
private void onPhaseCompleted(final ModelProcessingPhase phase) {
    completedPhase = phase;

    final Collection<OnPhaseFinished> listeners = phaseListeners.get(phase);
    if (listeners.isEmpty()) {
        return;
    }

    final Iterator<OnPhaseFinished> listener = listeners.iterator();
    while (listener.hasNext()) {
        final OnPhaseFinished next = listener.next();
        if (next.phaseFinished(this, phase)) {
            listener.remove();
        }
    }

    if (listeners.isEmpty()) {
        phaseListeners.removeAll(phase);
        if (phaseListeners.isEmpty()) {
            phaseListeners = ImmutableMultimap.of();
        }
    }
}

From source file:com.facebook.buck.apple.toolchain.impl.AppleCxxPlatforms.java

@VisibleForTesting
public static AppleCxxPlatform buildWithXcodeToolFinder(ProjectFilesystem filesystem, AppleSdk targetSdk,
        String minVersion, String targetArchitecture, AppleSdkPaths sdkPaths, BuckConfig buckConfig,
        XcodeToolFinder xcodeToolFinder, XcodeBuildVersionCache xcodeBuildVersionCache) {
    AppleCxxPlatform.Builder platformBuilder = AppleCxxPlatform.builder();

    ImmutableList.Builder<Path> toolSearchPathsBuilder = ImmutableList.builder();
    // Search for tools from most specific to least specific.
    toolSearchPathsBuilder.add(sdkPaths.getSdkPath().resolve(USR_BIN))
            .add(sdkPaths.getSdkPath().resolve("Developer").resolve(USR_BIN))
            .add(sdkPaths.getPlatformPath().resolve("Developer").resolve(USR_BIN));
    for (Path toolchainPath : sdkPaths.getToolchainPaths()) {
        toolSearchPathsBuilder.add(toolchainPath.resolve(USR_BIN));
    }//w  w w .  j a  v a2 s  .  c om
    if (sdkPaths.getDeveloperPath().isPresent()) {
        toolSearchPathsBuilder.add(sdkPaths.getDeveloperPath().get().resolve(USR_BIN));
        toolSearchPathsBuilder.add(sdkPaths.getDeveloperPath().get().resolve("Tools"));
    }

    // TODO(beng): Add more and better cflags.
    ImmutableList.Builder<String> cflagsBuilder = ImmutableList.builder();
    cflagsBuilder.add("-isysroot", sdkPaths.getSdkPath().toString());
    cflagsBuilder.add("-arch", targetArchitecture);
    cflagsBuilder.add(targetSdk.getApplePlatform().getMinVersionFlagPrefix() + minVersion);

    if (targetSdk.getApplePlatform().equals(ApplePlatform.WATCHOS)) {
        cflagsBuilder.add("-fembed-bitcode");
    }

    AppleConfig appleConfig = buckConfig.getView(AppleConfig.class);

    ImmutableList.Builder<String> ldflagsBuilder = ImmutableList.builder();
    ldflagsBuilder.addAll(Linkers.iXlinker("-sdk_version", targetSdk.getVersion()));
    if (appleConfig.linkAllObjC()) {
        ldflagsBuilder.addAll(Linkers.iXlinker("-ObjC"));
    }
    if (targetSdk.getApplePlatform().equals(ApplePlatform.WATCHOS)) {
        ldflagsBuilder.addAll(Linkers.iXlinker("-bitcode_verify"));
    }

    // Populate Xcode version keys from Xcode's own Info.plist if available.
    Optional<String> xcodeBuildVersion = Optional.empty();
    Optional<Path> developerPath = sdkPaths.getDeveloperPath();
    if (developerPath.isPresent()) {
        Path xcodeBundlePath = developerPath.get().getParent();
        if (xcodeBundlePath != null) {
            Path xcodeInfoPlistPath = xcodeBundlePath.resolve("Info.plist");
            try (InputStream stream = Files.newInputStream(xcodeInfoPlistPath)) {
                NSDictionary parsedXcodeInfoPlist = (NSDictionary) PropertyListParser.parse(stream);

                NSObject xcodeVersionObject = parsedXcodeInfoPlist.objectForKey("DTXcode");
                if (xcodeVersionObject != null) {
                    Optional<String> xcodeVersion = Optional.of(xcodeVersionObject.toString());
                    platformBuilder.setXcodeVersion(xcodeVersion);
                }
            } catch (IOException e) {
                LOG.warn("Error reading Xcode's info plist %s; ignoring Xcode versions", xcodeInfoPlistPath);
            } catch (PropertyListFormatException | ParseException | ParserConfigurationException
                    | SAXException e) {
                LOG.warn("Error in parsing %s; ignoring Xcode versions", xcodeInfoPlistPath);
            }
        }

        xcodeBuildVersion = xcodeBuildVersionCache.lookup(developerPath.get());
        platformBuilder.setXcodeBuildVersion(xcodeBuildVersion);
        LOG.debug("Xcode build version is: " + xcodeBuildVersion.orElse("<absent>"));
    }

    ImmutableList.Builder<String> versions = ImmutableList.builder();
    versions.add(targetSdk.getVersion());

    ImmutableList<String> toolchainVersions = targetSdk.getToolchains().stream().map(AppleToolchain::getVersion)
            .flatMap(Optionals::toStream).collect(ImmutableList.toImmutableList());
    if (toolchainVersions.isEmpty()) {
        if (!xcodeBuildVersion.isPresent()) {
            throw new HumanReadableException("Failed to read toolchain versions and Xcode version.");
        }
        versions.add(xcodeBuildVersion.get());
    } else {
        versions.addAll(toolchainVersions);
    }

    String version = Joiner.on(':').join(versions.build());

    ImmutableList<Path> toolSearchPaths = toolSearchPathsBuilder.build();

    Tool clangPath = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "clang", version);

    Tool clangXxPath = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "clang++",
            version);

    Tool ar = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "ar", version);

    Tool ranlib = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "ranlib", version);

    Tool strip = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "strip", version);

    Tool nm = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "nm", version);

    Tool actool = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "actool", version);

    Tool ibtool = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "ibtool", version);

    Tool momc = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "momc", version);

    Tool xctest = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "xctest", version);

    Tool dsymutil = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "dsymutil",
            version);

    Tool lipo = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "lipo", version);

    Tool lldb = getXcodeTool(filesystem, toolSearchPaths, xcodeToolFinder, appleConfig, "lldb", version);

    Optional<Path> stubBinaryPath = targetSdk.getApplePlatform().getStubBinaryPath()
            .map(input -> sdkPaths.getSdkPath().resolve(input));

    UserFlavor targetFlavor = UserFlavor.of(
            Flavor.replaceInvalidCharacters(targetSdk.getName() + "-" + targetArchitecture),
            String.format("SDK: %s, architecture: %s", targetSdk.getName(), targetArchitecture));
    CxxBuckConfig config = appleConfig.useFlavoredCxxSections() ? new CxxBuckConfig(buckConfig, targetFlavor)
            : new CxxBuckConfig(buckConfig);

    ImmutableBiMap.Builder<Path, String> sanitizerPaths = ImmutableBiMap.builder();
    sanitizerPaths.put(sdkPaths.getSdkPath(), "APPLE_SDKROOT");
    sanitizerPaths.put(sdkPaths.getPlatformPath(), "APPLE_PLATFORM_DIR");
    if (sdkPaths.getDeveloperPath().isPresent()) {
        sanitizerPaths.put(sdkPaths.getDeveloperPath().get(), "APPLE_DEVELOPER_DIR");
    }

    // https://github.com/facebook/buck/pull/1168: add the root cell's absolute path to the quote
    // include path, and also force it to be sanitized by all user rule keys.
    sanitizerPaths.put(filesystem.getRootPath(), ".");
    cflagsBuilder.add("-iquote", filesystem.getRootPath().toString());

    DebugPathSanitizer compilerDebugPathSanitizer = new PrefixMapDebugPathSanitizer(
            DebugPathSanitizer.getPaddedDir(".", config.getDebugPathSanitizerLimit(), File.separatorChar),
            sanitizerPaths.build());
    DebugPathSanitizer assemblerDebugPathSanitizer = new MungingDebugPathSanitizer(
            config.getDebugPathSanitizerLimit(), File.separatorChar, Paths.get("."), sanitizerPaths.build());

    ImmutableList<String> cflags = cflagsBuilder.build();

    ImmutableMap.Builder<String, String> macrosBuilder = ImmutableMap.builder();
    macrosBuilder.put("SDKROOT", sdkPaths.getSdkPath().toString());
    macrosBuilder.put("PLATFORM_DIR", sdkPaths.getPlatformPath().toString());
    macrosBuilder.put("CURRENT_ARCH", targetArchitecture);
    if (sdkPaths.getDeveloperPath().isPresent()) {
        macrosBuilder.put("DEVELOPER_DIR", sdkPaths.getDeveloperPath().get().toString());
    }
    ImmutableMap<String, String> macros = macrosBuilder.build();

    Optional<String> buildVersion = Optional.empty();
    Path platformVersionPlistPath = sdkPaths.getPlatformPath().resolve("version.plist");
    try (InputStream versionPlist = Files.newInputStream(platformVersionPlistPath)) {
        NSDictionary versionInfo = (NSDictionary) PropertyListParser.parse(versionPlist);
        if (versionInfo != null) {
            NSObject productBuildVersion = versionInfo.objectForKey("ProductBuildVersion");
            if (productBuildVersion != null) {
                buildVersion = Optional.of(productBuildVersion.toString());
            } else {
                LOG.warn("In %s, missing ProductBuildVersion. Build version will be unset for this platform.",
                        platformVersionPlistPath);
            }
        } else {
            LOG.warn("Empty version plist in %s. Build version will be unset for this platform.",
                    platformVersionPlistPath);
        }
    } catch (NoSuchFileException e) {
        LOG.warn("%s does not exist. Build version will be unset for this platform.", platformVersionPlistPath);
    } catch (PropertyListFormatException | SAXException | ParserConfigurationException | ParseException
            | IOException e) {
        // Some other error occurred, print the exception since it may contain error details.
        LOG.warn(e, "Failed to parse %s. Build version will be unset for this platform.",
                platformVersionPlistPath);
    }

    PreprocessorProvider aspp = new PreprocessorProvider(new ConstantToolProvider(clangPath),
            CxxToolProvider.Type.CLANG);
    CompilerProvider as = new CompilerProvider(new ConstantToolProvider(clangPath), CxxToolProvider.Type.CLANG,
            config.getUseDetailedUntrackedHeaderMessages());
    PreprocessorProvider cpp = new PreprocessorProvider(new ConstantToolProvider(clangPath),
            CxxToolProvider.Type.CLANG);
    CompilerProvider cc = new CompilerProvider(new ConstantToolProvider(clangPath), CxxToolProvider.Type.CLANG,
            config.getUseDetailedUntrackedHeaderMessages());
    PreprocessorProvider cxxpp = new PreprocessorProvider(new ConstantToolProvider(clangXxPath),
            CxxToolProvider.Type.CLANG);
    CompilerProvider cxx = new CompilerProvider(new ConstantToolProvider(clangXxPath),
            CxxToolProvider.Type.CLANG, config.getUseDetailedUntrackedHeaderMessages());
    ImmutableList.Builder<String> whitelistBuilder = ImmutableList.builder();
    whitelistBuilder.add("^" + Pattern.quote(sdkPaths.getSdkPath().toString()) + "\\/.*");
    whitelistBuilder
            .add("^" + Pattern.quote(sdkPaths.getPlatformPath() + "/Developer/Library/Frameworks") + "\\/.*");
    for (Path toolchainPath : sdkPaths.getToolchainPaths()) {
        LOG.debug("Apple toolchain path: %s", toolchainPath);
        try {
            whitelistBuilder.add("^" + Pattern.quote(toolchainPath.toRealPath().toString()) + "\\/.*");
        } catch (IOException e) {
            LOG.warn(e, "Apple toolchain path could not be resolved: %s", toolchainPath);
        }
    }
    HeaderVerification headerVerification = config.getHeaderVerificationOrIgnore()
            .withPlatformWhitelist(whitelistBuilder.build());
    LOG.debug("Headers verification platform whitelist: %s", headerVerification.getPlatformWhitelist());

    CxxPlatform cxxPlatform = CxxPlatforms.build(targetFlavor, Platform.MACOS, config, as, aspp, cc, cxx, cpp,
            cxxpp,
            new DefaultLinkerProvider(LinkerProvider.Type.DARWIN, new ConstantToolProvider(clangXxPath),
                    config.shouldCacheLinks()),
            ImmutableList.<String>builder().addAll(cflags).addAll(ldflagsBuilder.build()).build(),
            ImmutableMultimap.of(), strip, ArchiverProvider.from(new BsdArchiver(ar)), ArchiveContents.NORMAL,
            Optional.of(new ConstantToolProvider(ranlib)), new PosixNmSymbolNameTool(nm), cflagsBuilder.build(),
            ImmutableList.of(), cflags, ImmutableList.of(), cflags, ImmutableList.of(), "dylib", "%s.dylib",
            "a", "o", compilerDebugPathSanitizer, assemblerDebugPathSanitizer, macros, Optional.empty(),
            headerVerification, PicType.PIC);

    ApplePlatform applePlatform = targetSdk.getApplePlatform();
    ImmutableList.Builder<Path> swiftOverrideSearchPathBuilder = ImmutableList.builder();
    AppleSdkPaths.Builder swiftSdkPathsBuilder = AppleSdkPaths.builder().from(sdkPaths);
    Optional<SwiftPlatform> swiftPlatform = getSwiftPlatform(applePlatform.getName(),
            targetArchitecture + "-apple-" + applePlatform.getSwiftName().orElse(applePlatform.getName())
                    + minVersion,
            version, swiftSdkPathsBuilder.build(),
            swiftOverrideSearchPathBuilder.addAll(toolSearchPaths).build(), xcodeToolFinder, filesystem);

    platformBuilder.setCxxPlatform(cxxPlatform).setSwiftPlatform(swiftPlatform).setAppleSdk(targetSdk)
            .setAppleSdkPaths(sdkPaths).setMinVersion(minVersion).setBuildVersion(buildVersion)
            .setActool(actool).setIbtool(ibtool).setMomc(momc)
            .setCopySceneKitAssets(getOptionalTool("copySceneKitAssets", toolSearchPaths, xcodeToolFinder,
                    version, filesystem))
            .setXctest(xctest).setDsymutil(dsymutil).setLipo(lipo).setStubBinary(stubBinaryPath).setLldb(lldb)
            .setCodesignAllocate(
                    getOptionalTool("codesign_allocate", toolSearchPaths, xcodeToolFinder, version, filesystem))
            .setCodesignProvider(appleConfig.getCodesignProvider());

    return platformBuilder.build();
}