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

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

Introduction

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

Prototype

public static <K, V> Builder<K, V> builder() 

Source Link

Document

Returns a new builder.

Usage

From source file:com.facebook.buck.android.dalvik.DalvikAwareZipSplitter.java

@Override
public ImmutableMultimap<APKModule, Path> execute() throws IOException {
    ClasspathTraverser classpathTraverser = new DefaultClasspathTraverser();
    Set<String> secondaryTail = new HashSet<String>();

    // Start out by writing the primary zip and recording which entries were added to it.
    primaryOut = newZipOutput(outPrimary);
    secondaryDexWriter.reset();//from  ww w  .  j  a  v a  2 s  .co m

    ImmutableMap.Builder<String, FileLike> entriesBuilder = ImmutableMap.builder();
    List<String> additionalDexStoreEntries = new ArrayList<>();

    // Iterate over all of the inFiles and add all entries that match the requiredInPrimaryZip
    // predicate.
    LOG.verbose("Traversing classpath for primary zip");

    classpathTraverser.traverse(new ClasspathTraversal(inFiles, filesystem) {
        @Override
        public void visit(FileLike entry) throws IOException {
            LOG.verbose("Visiting " + entry.getRelativePath());

            String relativePath = entry.getRelativePath();
            if (!relativePath.endsWith(".class")) {
                // We don't need resources in dex jars, so just drop them.
                return;
            }
            String classPath = relativePath.replaceAll("\\.class$", "");

            Objects.requireNonNull(primaryOut);
            Objects.requireNonNull(classPathToDexStore);

            if (requiredInPrimaryZip.test(relativePath)) {
                primaryOut.putEntry(entry);
            } else if (wantedInPrimaryZip.contains(relativePath)
                    || (secondaryHeadSet != null && secondaryHeadSet.contains(relativePath))) {
                entriesBuilder.put(relativePath, new BufferedFileLike(entry));
            } else if (secondaryTailSet != null && secondaryTailSet.contains(relativePath)) {
                entriesBuilder.put(relativePath, new BufferedFileLike(entry));
                secondaryTail.add(relativePath);
            } else {
                ImmutableCollection<APKModule> containingModule = classPathToDexStore.get(classPath);
                if (!containingModule.isEmpty()) {
                    if (containingModule.size() > 1) {
                        throw new IllegalStateException(
                                String.format("classpath %s is contained in multiple dex stores: %s", classPath,
                                        classPathToDexStore.get(classPath).asList().toString()));
                    }
                    APKModule dexStore = containingModule.iterator().next();
                    if (!dexStore.equals(rootModule)) {
                        MySecondaryDexHelper dexHelper = additionalDexWriters.get(dexStore);
                        Objects.requireNonNull(dexHelper);
                        dexHelper.getOutputToWriteTo(entry).putEntry(entry);
                        additionalDexStoreEntries.add(relativePath);
                    }
                }
            }
        }
    });

    // Put as many of the items wanted in the primary dex as we can into the primary dex.
    ImmutableMap<String, FileLike> entries = entriesBuilder.build();
    for (String wanted : wantedInPrimaryZip) {
        FileLike entry = entries.get(wanted);
        if ((entry != null) && !primaryOut.containsEntry(entry) && primaryOut.canPutEntry(entry)) {
            primaryOut.putEntry(entry);
        }
    }

    if (secondaryHeadSet != null) {
        for (String head : secondaryHeadSet) {
            FileLike headEntry = entries.get(head);
            if ((headEntry != null) && !primaryOut.containsEntry(headEntry)) {
                secondaryDexWriter.getOutputToWriteTo(headEntry).putEntry(headEntry);
            }
        }
    }

    LOG.verbose("Traversing classpath for secondary zip");

    // Now that all of the required entries have been added to the primary zip, fill the rest of
    // the zip up with the remaining entries.
    classpathTraverser.traverse(new ClasspathTraversal(inFiles, filesystem) {
        @Override
        public void visit(FileLike entry) throws IOException {
            Objects.requireNonNull(primaryOut);
            String relativePath = entry.getRelativePath();

            // skip if it is the primary dex, is part of a modular dex store, or is not a class file
            if (primaryOut.containsEntry(entry) || additionalDexStoreEntries.contains(relativePath)) {
                return;
            }

            LOG.verbose("Visiting " + entry.getRelativePath());

            // Even if we have started writing a secondary dex, we still check if there is any
            // leftover
            // room in the primary dex for the current entry in the traversal.
            if (dexSplitStrategy == DexSplitStrategy.MAXIMIZE_PRIMARY_DEX_SIZE
                    && primaryOut.canPutEntry(entry)) {
                primaryOut.putEntry(entry);
            } else {
                if (secondaryHeadSet != null && secondaryHeadSet.contains(relativePath)) {
                    return;
                }
                if (secondaryTail.contains(relativePath)) {
                    return;
                }
                secondaryDexWriter.getOutputToWriteTo(entry).putEntry(entry);
            }
        }
    });
    if (secondaryTailSet != null) {
        for (String tail : secondaryTailSet) {
            FileLike tailEntry = entries.get(tail);
            if ((tailEntry != null) && !primaryOut.containsEntry(tailEntry) && secondaryTail.contains(tail)) {
                secondaryDexWriter.getOutputToWriteTo(tailEntry).putEntry(tailEntry);
            }
        }
    }
    primaryOut.close();
    secondaryDexWriter.close();

    ImmutableMultimap.Builder<APKModule, Path> outputFilesBuilder = ImmutableMultimap.builder();
    APKModule secondaryDexStore = rootModule;
    outputFilesBuilder.putAll(secondaryDexStore, secondaryDexWriter.getFiles());
    for (Map.Entry<APKModule, MySecondaryDexHelper> entry : additionalDexWriters.entrySet()) {
        if (!entry.getKey().equals(secondaryDexStore)) {
            entry.getValue().close();
            outputFilesBuilder.putAll(entry.getKey(), entry.getValue().getFiles());
        }
    }
    return outputFilesBuilder.build();
}

From source file:io.prestosql.plugin.accumulo.index.IndexLookup.java

private boolean getRangesWithMetrics(ConnectorSession session, String schema, String table,
        Multimap<AccumuloColumnConstraint, Range> constraintRanges, Collection<Range> rowIdRanges,
        List<TabletSplitMetadata> tabletSplits, Authorizations auths) throws Exception {
    String metricsTable = getMetricsTableName(schema, table);
    long numRows = getNumRowsInTable(metricsTable, auths);

    // Get the cardinalities from the metrics table
    Multimap<Long, AccumuloColumnConstraint> cardinalities;
    if (isIndexShortCircuitEnabled(session)) {
        cardinalities = cardinalityCache.getCardinalities(schema, table, auths, constraintRanges,
                (long) (numRows * getIndexSmallCardThreshold(session)),
                getIndexCardinalityCachePollingDuration(session));
    } else {//from  ww w .j  a  va2s  .  co m
        // disable short circuit using 0
        cardinalities = cardinalityCache.getCardinalities(schema, table, auths, constraintRanges, 0,
                new Duration(0, TimeUnit.MILLISECONDS));
    }

    Optional<Entry<Long, AccumuloColumnConstraint>> entry = cardinalities.entries().stream().findFirst();
    if (!entry.isPresent()) {
        return false;
    }

    Entry<Long, AccumuloColumnConstraint> lowestCardinality = entry.get();
    String indexTable = getIndexTableName(schema, table);
    double threshold = getIndexThreshold(session);
    List<Range> indexRanges;

    // If the smallest cardinality in our list is above the lowest cardinality threshold,
    // we should look at intersecting the row ID ranges to try and get under the threshold.
    if (smallestCardAboveThreshold(session, numRows, lowestCardinality.getKey())) {
        // If we only have one column, we can skip the intersection process and just check the index threshold
        if (cardinalities.size() == 1) {
            long numEntries = lowestCardinality.getKey();
            double ratio = ((double) numEntries / (double) numRows);
            LOG.debug(
                    "Use of index would scan %s of %s rows, ratio %s. Threshold %2f, Using for index table? %s",
                    numEntries, numRows, ratio, threshold, ratio < threshold);
            if (ratio >= threshold) {
                return false;
            }
        }

        // Else, get the intersection of all row IDs for all column constraints
        LOG.debug("%d indexed columns, intersecting ranges", constraintRanges.size());
        indexRanges = getIndexRanges(indexTable, constraintRanges, rowIdRanges, auths);
        LOG.debug("Intersection results in %d ranges from secondary index", indexRanges.size());
    } else {
        // Else, we don't need to intersect the columns and we can just use the column with the lowest cardinality,
        // so get all those row IDs in a set of ranges.
        LOG.debug("Not intersecting columns, using column with lowest cardinality ");
        ImmutableMultimap.Builder<AccumuloColumnConstraint, Range> lcBldr = ImmutableMultimap.builder();
        lcBldr.putAll(lowestCardinality.getValue(), constraintRanges.get(lowestCardinality.getValue()));
        indexRanges = getIndexRanges(indexTable, lcBldr.build(), rowIdRanges, auths);
    }

    if (indexRanges.isEmpty()) {
        LOG.debug("Query would return no results, returning empty list of splits");
        return true;
    }

    // Okay, we now check how many rows we would scan by using the index vs. the overall number
    // of rows
    long numEntries = indexRanges.size();
    double ratio = (double) numEntries / (double) numRows;
    LOG.debug("Use of index would scan %d of %d rows, ratio %s. Threshold %2f, Using for table? %b", numEntries,
            numRows, ratio, threshold, ratio < threshold, table);

    // If the percentage of scanned rows, the ratio, less than the configured threshold
    if (ratio < threshold) {
        // Bin the ranges into TabletMetadataSplits and return true to use the tablet splits
        binRanges(getNumIndexRowsPerSplit(session), indexRanges, tabletSplits);
        LOG.debug("Number of splits for %s.%s is %d with %d ranges", schema, table, tabletSplits.size(),
                indexRanges.size());
        return true;
    } else {
        // We are going to do too much work to use the secondary index, so return false
        return false;
    }
}

From source file:org.apache.druid.sql.calcite.schema.DruidSchema.java

@Override
protected Multimap<String, org.apache.calcite.schema.Function> getFunctionMultimap() {
    final ImmutableMultimap.Builder<String, org.apache.calcite.schema.Function> builder = ImmutableMultimap
            .builder();/*w w w . j  av a2s.  c om*/
    for (Map.Entry<String, DruidViewMacro> entry : viewManager.getViews().entrySet()) {
        builder.put(entry);
    }
    return builder.build();
}

From source file:com.facebook.buck.android.exopackage.ExopackageInstaller.java

/**
 * Parses a text file which is supposed to be in the following format: "file_path_without_spaces
 * file_hash ...." i.e. it parses the first two columns of each line and ignores the rest of it.
 *
 * @return A multi map from the file hash to its path, which equals the raw path resolved against
 *     {@code resolvePathAgainst}.//from  www . j av  a2  s  .c  o  m
 */
@VisibleForTesting
public static ImmutableMultimap<String, Path> parseExopackageInfoMetadata(Path metadataTxt,
        Path resolvePathAgainst, ProjectFilesystem filesystem) throws IOException {
    ImmutableMultimap.Builder<String, Path> builder = ImmutableMultimap.builder();
    for (String line : filesystem.readLines(metadataTxt)) {
        // ignore lines that start with '.'
        if (line.startsWith(".")) {
            continue;
        }
        List<String> parts = Splitter.on(' ').splitToList(line);
        if (parts.size() < 2) {
            throw new RuntimeException("Illegal line in metadata file: " + line);
        }
        builder.put(parts.get(1), resolvePathAgainst.resolve(parts.get(0)));
    }
    return builder.build();
}

From source file:com.opengamma.strata.loader.csv.RatesCurvesCsvLoader.java

private static Multimap<LocalDate, Curve> buildCurves(Map<CurveName, LoadedCurveSettings> settingsMap,
        Map<LoadedCurveKey, List<LoadedCurveNode>> allNodes) {

    ImmutableMultimap.Builder<LocalDate, Curve> results = ImmutableMultimap.builder();

    for (Map.Entry<LoadedCurveKey, List<LoadedCurveNode>> entry : allNodes.entrySet()) {
        LoadedCurveKey key = entry.getKey();
        LoadedCurveSettings settings = settingsMap.get(key.getCurveName());

        if (settings == null) {
            throw new IllegalArgumentException(Messages.format("Missing settings for curve: {}", key));
        }/*from w w w  .  j a  v  a 2 s .  c o m*/
        results.put(key.getCurveDate(), settings.createCurve(key.getCurveDate(), entry.getValue()));
    }
    return results.build();
}

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 ww w  .ja  v  a  2s  . co  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.apple.project_generator.WorkspaceAndProjectGenerator.java

private void generateProject(final Map<Path, ProjectGenerator> projectGenerators,
        ListeningExecutorService listeningExecutorService, WorkspaceGenerator workspaceGenerator,
        ImmutableSet<BuildTarget> targetsInRequiredProjects,
        ImmutableMultimap.Builder<BuildTarget, PBXTarget> buildTargetToPbxTargetMapBuilder,
        ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder,
        final Optional<BuildTarget> targetToBuildWithBuck) throws IOException, InterruptedException {
    ImmutableMultimap.Builder<Cell, BuildTarget> projectCellToBuildTargetsBuilder = ImmutableMultimap.builder();
    for (TargetNode<?, ?> targetNode : projectGraph.getNodes()) {
        BuildTarget buildTarget = targetNode.getBuildTarget();
        projectCellToBuildTargetsBuilder.put(rootCell.getCell(buildTarget), buildTarget);
    }//from w  w w. ja v a2 s .  c  om
    ImmutableMultimap<Cell, BuildTarget> projectCellToBuildTargets = projectCellToBuildTargetsBuilder.build();
    List<ListenableFuture<GenerationResult>> projectGeneratorFutures = new ArrayList<>();
    for (final Cell projectCell : projectCellToBuildTargets.keySet()) {
        ImmutableMultimap.Builder<Path, BuildTarget> projectDirectoryToBuildTargetsBuilder = ImmutableMultimap
                .builder();
        final ImmutableSet<BuildTarget> cellRules = ImmutableSet
                .copyOf(projectCellToBuildTargets.get(projectCell));
        for (BuildTarget buildTarget : cellRules) {
            projectDirectoryToBuildTargetsBuilder.put(buildTarget.getBasePath(), buildTarget);
        }
        ImmutableMultimap<Path, BuildTarget> projectDirectoryToBuildTargets = projectDirectoryToBuildTargetsBuilder
                .build();
        final Path relativeTargetCell = rootCell.getRoot().relativize(projectCell.getRoot());
        for (final Path projectDirectory : projectDirectoryToBuildTargets.keySet()) {
            final ImmutableSet<BuildTarget> rules = filterRulesForProjectDirectory(projectGraph,
                    ImmutableSet.copyOf(projectDirectoryToBuildTargets.get(projectDirectory)));
            if (Sets.intersection(targetsInRequiredProjects, rules).isEmpty()) {
                continue;
            }

            projectGeneratorFutures.add(listeningExecutorService.submit(() -> {
                GenerationResult result = generateProjectForDirectory(projectGenerators, targetToBuildWithBuck,
                        projectCell, projectDirectory, rules);
                // convert the projectPath to relative to the target cell here
                result = GenerationResult.of(relativeTargetCell.resolve(result.getProjectPath()),
                        result.isProjectGenerated(), result.getRequiredBuildTargets(),
                        result.getBuildTargetToGeneratedTargetMap());
                return result;
            }));
        }
    }

    List<GenerationResult> generationResults;
    try {
        generationResults = Futures.allAsList(projectGeneratorFutures).get();
    } catch (ExecutionException e) {
        Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
        Throwables.propagateIfPossible(e.getCause());
        throw new IllegalStateException("Unexpected exception: ", e);
    }
    for (GenerationResult result : generationResults) {
        if (!result.isProjectGenerated()) {
            continue;
        }
        workspaceGenerator.addFilePath(result.getProjectPath());
        processGenerationResult(buildTargetToPbxTargetMapBuilder, targetToProjectPathMapBuilder, result);
    }
}

From source file:com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.java

/**
 * Computes the set of {@link Label}s corresponding to a set of Skylark {@link LoadStatement}s.
 *
 * @param imports a collection of Skylark {@link LoadStatement}s
 * @param containingFileLabel the {@link Label} of the file containing the load statements
 * @return an {@link ImmutableMap} which maps a {@link String} used in the load statement to its
 *     corresponding {@Label}. Returns {@code null} if any Skyframe dependencies are unavailable.
 * @throws SkylarkImportFailedException if no package can be found that contains the loaded file
 *//*  www . ja  v a  2  s.co m*/
@Nullable
static ImmutableMap<String, Label> findLabelsForLoadStatements(ImmutableCollection<SkylarkImport> imports,
        Label containingFileLabel, Environment env) throws SkylarkImportFailedException, InterruptedException {
    Preconditions.checkArgument(!containingFileLabel.getPackageIdentifier().getRepository().isDefault());
    Map<String, Label> outputMap = Maps.newHashMapWithExpectedSize(imports.size());

    // Filter relative vs. absolute paths.
    ImmutableSet.Builder<PathFragment> absoluteImportsToLookup = new ImmutableSet.Builder<>();
    // We maintain a multimap from path fragments to their correspond import strings, to cover the
    // (unlikely) case where two distinct import strings generate the same path fragment.
    ImmutableMultimap.Builder<PathFragment, String> pathToImports = new ImmutableMultimap.Builder<>();
    for (SkylarkImport imp : imports) {
        if (imp.hasAbsolutePath()) {
            absoluteImportsToLookup.add(imp.getAbsolutePath());
            pathToImports.put(imp.getAbsolutePath(), imp.getImportString());
        } else {
            outputMap.put(imp.getImportString(), imp.getLabel(containingFileLabel));
        }
    }

    // Look up labels for absolute paths.
    ImmutableMap<PathFragment, Label> absoluteLabels = labelsForAbsoluteImports(absoluteImportsToLookup.build(),
            env);
    if (absoluteLabels == null) {
        return null;
    }
    for (Entry<PathFragment, Label> entry : absoluteLabels.entrySet()) {
        PathFragment currPath = entry.getKey();
        Label currLabel = entry.getValue();
        for (String importString : pathToImports.build().get(currPath)) {
            outputMap.put(importString, currLabel);
        }
    }

    ImmutableMap<String, Label> immutableOutputMap = ImmutableMap.copyOf(outputMap);
    return immutableOutputMap;
}

From source file:org.jclouds.ec2.compute.EC2ComputeService.java

/**
 * Cleans implicit keypairs and security groups.
 *//* w  ww . ja v  a  2s  .c  om*/
@Override
protected void cleanUpIncidentalResourcesOfDeadNodes(Set<? extends NodeMetadata> deadNodes) {
    Builder<String, String> regionGroups = ImmutableMultimap.builder();
    for (NodeMetadata nodeMetadata : deadNodes) {
        if (nodeMetadata.getGroup() != null)
            regionGroups.put(AWSUtils.parseHandle(nodeMetadata.getId())[0], nodeMetadata.getGroup());
    }
    for (Entry<String, String> regionGroup : regionGroups.build().entries()) {
        cleanUpIncidentalResources(regionGroup.getKey(), regionGroup.getValue());
    }
}

From source file:org.pentaho.di.trans.dataservice.DataServiceExecutor.java

protected void prepareExecution() throws KettleException {
    // Setup executor with default execution plan
    ImmutableMultimap.Builder<ExecutionPoint, Runnable> builder = ImmutableMultimap.builder();
    builder.putAll(ExecutionPoint.PREPARE, new CopyParameters(parameters, serviceTrans),
            new PrepareExecution(genTrans), new PrepareExecution(serviceTrans));

    builder.putAll(ExecutionPoint.READY, new DefaultTransWiring(this));

    builder.putAll(ExecutionPoint.START, new TransStarter(genTrans), new TransStarter(serviceTrans));

    listenerMap.putAll(builder.build());
}