Example usage for com.google.common.collect ImmutableSetMultimap entries

List of usage examples for com.google.common.collect ImmutableSetMultimap entries

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSetMultimap entries.

Prototype

ImmutableSet entries

To view the source code for com.google.common.collect ImmutableSetMultimap entries.

Click Source Link

Usage

From source file:zipkin.storage.cassandra.Indexer.java

@VisibleForTesting
static ImmutableSetMultimap<PartitionKeyToTraceId, Long> entriesThatIncreaseGap(
        ConcurrentMap<PartitionKeyToTraceId, Pair<Long>> sharedState,
        ImmutableSetMultimap<PartitionKeyToTraceId, Long> updates) {
    ImmutableSet.Builder<PartitionKeyToTraceId> toUpdate = ImmutableSet.builder();

    // Enter a loop that affects shared state when an update widens the time interval for a key.
    for (Map.Entry<PartitionKeyToTraceId, Long> input : updates.entries()) {
        PartitionKeyToTraceId key = input.getKey();
        long timestamp = input.getValue();
        for (;;) {
            Pair<Long> oldRange = sharedState.get(key);
            if (oldRange == null) {
                // Initial state is where this key has a single timestamp.
                oldRange = sharedState.putIfAbsent(key, Pair.create(timestamp, timestamp));

                // If there was no previous value, we need to update the index
                if (oldRange == null) {
                    toUpdate.add(key);// w w w.java 2 s  . co  m
                    break;
                }
            }

            long first = timestamp < oldRange._1 ? timestamp : oldRange._1;
            long last = timestamp > oldRange._2 ? timestamp : oldRange._2;

            Pair<Long> newRange = Pair.create(first, last);
            if (oldRange.equals(newRange)) {
                break; // the current timestamp is contained
            } else if (sharedState.replace(key, oldRange, newRange)) {
                toUpdate.add(key); // The range was extended
                break;
            }
        }
    }

    // When the loop completes, we'll know one of our updates widened the interval of a trace, if
    // it is the first or last timestamp. By ignoring those between an existing interval, we can
    // end up with less Cassandra writes.
    Builder<PartitionKeyToTraceId, Long> result = ImmutableSetMultimap.builder();
    for (PartitionKeyToTraceId needsUpdate : toUpdate.build()) {
        Pair<Long> firstLast = sharedState.get(needsUpdate);
        if (updates.containsEntry(needsUpdate, firstLast._1))
            result.put(needsUpdate, firstLast._1);
        if (updates.containsEntry(needsUpdate, firstLast._2))
            result.put(needsUpdate, firstLast._2);
    }
    return result.build();
}

From source file:zipkin2.storage.cassandra.v1.Indexer.java

@VisibleForTesting
static ImmutableSetMultimap<PartitionKeyToTraceId, Long> entriesThatIncreaseGap(
        ConcurrentMap<PartitionKeyToTraceId, Pair> sharedState,
        ImmutableSetMultimap<PartitionKeyToTraceId, Long> updates) {
    ImmutableSet.Builder<PartitionKeyToTraceId> toUpdate = ImmutableSet.builder();

    // Enter a loop that affects shared state when an update widens the time interval for a key.
    for (Map.Entry<PartitionKeyToTraceId, Long> input : updates.entries()) {
        PartitionKeyToTraceId key = input.getKey();
        long timestamp = input.getValue();
        for (;;) {
            Pair oldRange = sharedState.get(key);
            if (oldRange == null) {
                // Initial state is where this key has a single timestamp.
                oldRange = sharedState.putIfAbsent(key, new Pair(timestamp, timestamp));

                // If there was no previous value, we need to update the index
                if (oldRange == null) {
                    toUpdate.add(key);//  w  ww  .  j a va 2  s  .  com
                    break;
                }
            }

            long first = timestamp < oldRange.left ? timestamp : oldRange.left;
            long last = timestamp > oldRange.right ? timestamp : oldRange.right;

            Pair newRange = new Pair(first, last);
            if (oldRange.equals(newRange)) {
                break; // the current timestamp is contained
            } else if (sharedState.replace(key, oldRange, newRange)) {
                toUpdate.add(key); // The range was extended
                break;
            }
        }
    }

    // When the loop completes, we'll know one of our updates widened the interval of a trace, if
    // it is the first or last timestamp. By ignoring those between an existing interval, we can
    // end up with less Cassandra writes.
    Builder<PartitionKeyToTraceId, Long> result = ImmutableSetMultimap.builder();
    for (PartitionKeyToTraceId needsUpdate : toUpdate.build()) {
        Pair firstLast = sharedState.get(needsUpdate);
        if (updates.containsEntry(needsUpdate, firstLast.left))
            result.put(needsUpdate, firstLast.left);
        if (updates.containsEntry(needsUpdate, firstLast.right))
            result.put(needsUpdate, firstLast.right);
    }
    return result.build();
}

From source file:com.facebook.buck.apple.WorkspaceAndProjectGenerator.java

@VisibleForTesting
static void groupSchemeTests(ImmutableSet<TargetNode<AppleTestDescription.Arg>> groupableTests,
        ImmutableSetMultimap<String, TargetNode<AppleTestDescription.Arg>> selectedTests,
        ImmutableMultimap.Builder<AppleTestBundleParamsKey, TargetNode<AppleTestDescription.Arg>> groupedTestsBuilder,
        ImmutableSetMultimap.Builder<String, TargetNode<AppleTestDescription.Arg>> ungroupedTestsBuilder) {
    for (Map.Entry<String, TargetNode<AppleTestDescription.Arg>> testEntry : selectedTests.entries()) {
        String schemeName = testEntry.getKey();
        TargetNode<AppleTestDescription.Arg> test = testEntry.getValue();
        if (groupableTests.contains(test)) {
            Preconditions.checkState(test.getConstructorArg().canGroup(),
                    "Groupable test should actually be groupable.");
            groupedTestsBuilder//from www  .j  a va  2s . co  m
                    .put(AppleTestBundleParamsKey.fromAppleTestDescriptionArg(test.getConstructorArg()), test);
        } else {
            ungroupedTestsBuilder.put(schemeName, test);
        }
    }
}

From source file:com.facebook.buck.features.apple.project.WorkspaceAndProjectGenerator.java

/**
 * Transform a map from scheme name to `TargetNode` to scheme name to the associated `PBXProject`.
 *
 * @param schemeToTargetNodes Map to transform.
 * @return Map of scheme name to associated `PXBProject`s.
 *//*w w  w  .ja  v  a 2s . c  om*/
private static ImmutableSetMultimap<String, PBXTarget> mapFromSchemeToPBXProject(
        ImmutableSetMultimap<String, ? extends TargetNode<?>> schemeToTargetNodes,
        ImmutableMap<BuildTarget, PBXTarget> buildTargetToPBXTarget) {
    ImmutableSetMultimap<String, PBXTarget> schemeToPBXProject = ImmutableSetMultimap
            .copyOf(schemeToTargetNodes.entries().stream()
                    .map(stringTargetNodeEntry -> Maps.immutableEntry(stringTargetNodeEntry.getKey(),
                            buildTargetToPBXTarget.get(stringTargetNodeEntry.getValue().getBuildTarget())))
                    .filter(stringPBXTargetEntry -> {
                        return stringPBXTargetEntry.getValue() != null;
                    }).collect(Collectors.toList()));
    return schemeToPBXProject;
}

From source file:com.facebook.buck.features.apple.project.WorkspaceAndProjectGenerator.java

/**
 * Transform a map from scheme name to `TargetNode` to scheme name to the associated `PBXProject`.
 * This wraps `mapFromSchemeToPBXProject` with added functionality filters out null target nodes.
 *
 * @param schemeToOptionalTargetNodes Map to transform.
 * @return Map of scheme name to associated `PXBProject`s.
 *//*from   w  w  w  . j  av  a  2  s .com*/
private static ImmutableSetMultimap<String, PBXTarget> mapFromOptionalSchemeToPBXProject(
        ImmutableSetMultimap<String, Optional<TargetNode<?>>> schemeToOptionalTargetNodes,
        ImmutableMap<BuildTarget, PBXTarget> buildTargetToPBXTarget) {
    // filter out map entries that are null
    ImmutableSetMultimap<String, TargetNode<?>> schemeToTargetNodes = ImmutableSetMultimap
            .copyOf(schemeToOptionalTargetNodes.entries().stream().filter(stringOptionalEntry -> { // removes scheme mapped to null
                return stringOptionalEntry.getValue().isPresent();
            }).map(stringOptionalEntry -> {
                // force map to non-Optional value since those values are filtered above
                return Maps.immutableEntry(stringOptionalEntry.getKey(), stringOptionalEntry.getValue().get());
            }).collect(Collectors.toList()));

    return mapFromSchemeToPBXProject(schemeToTargetNodes, buildTargetToPBXTarget);
}

From source file:com.facebook.buck.android.AndroidTransitiveDependencyGraph.java

/**
 * @param uberRDotJava that may have produced {@code R.class} files that need to be dexed.
 *//*  ww w.ja  v  a 2 s .  c o  m*/
public AndroidDexTransitiveDependencies findDexDependencies(
        ImmutableList<HasAndroidResourceDeps> androidResourceDeps,
        final ImmutableSet<JavaLibrary> buildRulesToExcludeFromDex, final UberRDotJava uberRDotJava) {
    // These are paths that will be dex'ed. They may be either directories of compiled .class files,
    // or paths to compiled JAR files.
    final ImmutableSet.Builder<Path> pathsToDexBuilder = ImmutableSet.builder();

    // Paths to the classfiles to not dex.
    final ImmutableSet.Builder<Path> noDxPathsBuilder = ImmutableSet.builder();

    // These are paths to third-party jars that may contain resources that must be included in the
    // final APK.
    final ImmutableSet.Builder<Path> pathsToThirdPartyJarsBuilder = ImmutableSet.builder();

    AndroidResourceDetails details = findAndroidResourceDetails(androidResourceDeps);

    // Update pathsToDex.
    final ImmutableSetMultimap<JavaLibrary, Path> classpath = Classpaths
            .getClasspathEntries(rulesToTraverseForTransitiveDeps);
    for (Map.Entry<JavaLibrary, Path> entry : classpath.entries()) {
        if (!buildRulesToExcludeFromDex.contains(entry.getKey())) {
            pathsToDexBuilder.add(entry.getValue());
        } else {
            noDxPathsBuilder.add(entry.getValue());
        }
    }
    // Include the directory of compiled R.java files on the classpath.
    final ImmutableSet<String> rDotJavaPackages = details.rDotJavaPackages;
    if (!rDotJavaPackages.isEmpty()) {
        Path pathToCompiledRDotJavaFiles = uberRDotJava.getPathToCompiledRDotJavaFiles();
        pathsToDexBuilder.add(pathToCompiledRDotJavaFiles);
    }

    ImmutableSet<Path> noDxPaths = noDxPathsBuilder.build();

    // Filter out the classpath entries to exclude from dex'ing, if appropriate
    Set<Path> classpathEntries = Sets.difference(pathsToDexBuilder.build(), noDxPaths);

    Supplier<Map<String, HashCode>> classNamesToHashesSupplier = Suppliers
            .memoize(new Supplier<Map<String, HashCode>>() {
                @Override
                public Map<String, HashCode> get() {
                    ImmutableMap.Builder<String, HashCode> builder = ImmutableMap.builder();
                    for (JavaLibrary javaLibrary : classpath.keySet()) {
                        if (!buildRulesToExcludeFromDex.contains(javaLibrary)) {
                            builder.putAll(javaLibrary.getClassNamesToHashes());
                        }
                    }
                    if (!rDotJavaPackages.isEmpty()) {
                        builder.putAll(uberRDotJava.getClassNamesToHashes());
                    }
                    return builder.build();
                }
            });

    // Visit all of the transitive dependencies to populate the above collections.
    new AbstractDependencyVisitor(rulesToTraverseForTransitiveDeps) {
        @Override
        public ImmutableSet<BuildRule> visit(BuildRule rule) {
            // We need to include the transitive closure of the compiled .class files when dex'ing, as
            // well as the third-party jars that they depend on.
            // Update pathsToThirdPartyJars.
            if (rule.getBuildable() instanceof PrebuiltJar) {
                PrebuiltJar prebuiltJar = (PrebuiltJar) rule.getBuildable();
                pathsToThirdPartyJarsBuilder.add(prebuiltJar.getBinaryJar());
            }
            return maybeVisitAllDeps(rule, rule.getProperties().is(LIBRARY));
        }
    }.start();

    // Classpath entries that should be excluded from dexing should also be excluded from
    // pathsToThirdPartyJars because their resources should not end up in main APK. If they do,
    // the pre-dexed library may try to load a resource from the main APK rather than from within
    // the pre-dexed library (even though the resource is available in both locations). This
    // causes a significant performance regression, as the resource may take more than one second
    // longer to load.
    Set<Path> pathsToThirdPartyJars = Sets.difference(pathsToThirdPartyJarsBuilder.build(), noDxPaths);

    return new AndroidDexTransitiveDependencies(classpathEntries, pathsToThirdPartyJars, noDxPaths,
            classNamesToHashesSupplier);
}

From source file:org.spongepowered.mod.service.world.SpongeChunkLoadService.java

@Override
public ImmutableSetMultimap<Vector3i, LoadingTicket> getForcedChunks(World world) {
    ImmutableSetMultimap<ChunkCoordIntPair, Ticket> forgeForcedChunks = ForgeChunkManager
            .getPersistentChunksFor((net.minecraft.world.World) world);
    ImmutableSetMultimap.Builder<Vector3i, LoadingTicket> spongeForcedChunks = ImmutableSetMultimap.builder();
    for (Map.Entry<ChunkCoordIntPair, Ticket> ticketPair : forgeForcedChunks.entries()) {
        spongeForcedChunks.put(new Vector3i(ticketPair.getKey().chunkXPos, 0, ticketPair.getKey().chunkZPos),
                new SpongeLoadingTicket(ticketPair.getValue()));
    }/*from   ww w  .  ja  v a  2 s  .c  om*/

    return spongeForcedChunks.build();
}

From source file:org.glowroot.local.store.TraceDao.java

@Override
public void store(final Trace trace, CharSource entries, @Nullable CharSource profile) throws Exception {
    long entriesId = cappedDatabase.write(entries);
    Long profileId = null;/*from w  w w.java2  s .  c  om*/
    if (profile != null) {
        profileId = cappedDatabase.write(profile);
    }
    dataSource.update(
            "merge into trace (id, partial, error, start_time, capture_time,"
                    + " duration, transaction_type, transaction_name, headline, user,"
                    + " custom_attributes, custom_detail, error_message, error_throwable, timers,"
                    + " thread_cpu_time, thread_blocked_time, thread_waited_time,"
                    + " thread_allocated_bytes, gc_infos, entry_count, entries_capped_id,"
                    + " profile_sample_count, profile_capped_id) values"
                    + " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            trace.id(), trace.partial(), trace.error(), trace.startTime(), trace.captureTime(),
            trace.duration(), trace.transactionType(), trace.transactionName(), trace.headline(), trace.user(),
            trace.customAttributes(), trace.customDetail(), trace.errorMessage(), trace.errorThrowable(),
            trace.timers(), trace.threadCpuTime(), trace.threadBlockedTime(), trace.threadWaitedTime(),
            trace.threadAllocatedBytes(), trace.gcInfos(), trace.entryCount(), entriesId,
            trace.profileSampleCount(), profileId);
    final ImmutableSetMultimap<String, String> customAttributesForIndexing = trace
            .customAttributesForIndexing();
    if (!customAttributesForIndexing.isEmpty()) {
        dataSource.batchUpdate("insert into trace_custom_attribute (trace_id, name,"
                + " value, capture_time) values (?, ?, ?, ?)", new BatchAdder() {
                    @Override
                    public void addBatches(PreparedStatement preparedStatement) throws SQLException {
                        for (Entry<String, String> entry : customAttributesForIndexing.entries()) {
                            preparedStatement.setString(1, trace.id());
                            preparedStatement.setString(2, entry.getKey());
                            preparedStatement.setString(3, entry.getValue());
                            preparedStatement.setLong(4, trace.captureTime());
                            preparedStatement.addBatch();
                        }
                    }
                });
    }
}

From source file:zipkin2.storage.cassandra.v1.Indexer.java

void index(Span span, List<Call<Void>> calls) {
    // First parse each span into partition keys used to support query requests
    Builder<PartitionKeyToTraceId, Long> parsed = ImmutableSetMultimap.builder();
    long timestamp = span.timestampAsLong();
    if (timestamp == 0L)
        return;/* www . j a v a2s  .c  om*/
    for (String partitionKey : index.partitionKeys(span)) {
        parsed.put(new PartitionKeyToTraceId(index.table(), partitionKey, span.traceId()),
                1000 * (timestamp / 1000)); // index precision is millis
    }

    // The parsed results may include inserts that already occur, or are redundant as they don't
    // impact QueryRequest.endTs or QueryRequest.loopback. For example, a parsed timestamp could
    // be between timestamps of rows that already exist for a particular trace.
    ImmutableSetMultimap<PartitionKeyToTraceId, Long> maybeInsert = parsed.build();
    if (maybeInsert.isEmpty())
        return;

    ImmutableSetMultimap<PartitionKeyToTraceId, Long> toInsert;
    if (sharedState == null) { // special-case when caching is disabled.
        toInsert = maybeInsert;
    } else {
        // Optimized results will be smaller when the input includes traces with local spans, or when
        // other threads indexed the same trace.
        toInsert = entriesThatIncreaseGap(sharedState, maybeInsert);

        if (maybeInsert.size() > toInsert.size() && LOG.isDebugEnabled()) {
            int delta = maybeInsert.size() - toInsert.size();
            LOG.debug("optimized out {}/{} inserts into {}", delta, maybeInsert.size(), index.table());
        }
    }

    // For each entry, insert a new row in the index table asynchronously
    for (Map.Entry<PartitionKeyToTraceId, Long> entry : toInsert.entries()) {
        long traceId = HexCodec.lowerHexToUnsignedLong(entry.getKey().traceId);
        calls.add(new IndexCall(traceId, entry.getValue(), entry.getKey().partitionKey));
    }
}

From source file:zipkin.storage.cassandra.Indexer.java

ImmutableSet<ListenableFuture<?>> index(List<Span> spans) {
    // First parse each span into partition keys used to support query requests
    Builder<PartitionKeyToTraceId, Long> parsed = ImmutableSetMultimap.builder();
    for (Span span : spans) {
        Long timestamp = guessTimestamp(span);
        if (timestamp == null)
            continue;
        for (String partitionKey : index.partitionKeys(span)) {
            parsed.put(new PartitionKeyToTraceId(index.table(), partitionKey, span.traceId),
                    1000 * (timestamp / 1000)); // index precision is millis
        }/*from  w ww .  j a va 2 s  .c  om*/
    }

    // The parsed results may include inserts that already occur, or are redundant as they don't
    // impact QueryRequest.endTs or QueryRequest.loopback. For example, a parsed timestamp could
    // be between timestamps of rows that already exist for a particular trace.
    ImmutableSetMultimap<PartitionKeyToTraceId, Long> maybeInsert = parsed.build();

    ImmutableSetMultimap<PartitionKeyToTraceId, Long> toInsert;
    if (sharedState == null) { // special-case when caching is disabled.
        toInsert = maybeInsert;
    } else {
        // Optimized results will be smaller when the input includes traces with local spans, or when
        // other threads indexed the same trace.
        toInsert = entriesThatIncreaseGap(sharedState, maybeInsert);

        if (maybeInsert.size() > toInsert.size() && LOG.isDebugEnabled()) {
            int delta = maybeInsert.size() - toInsert.size();
            LOG.debug("optimized out {}/{} inserts into {}", delta, maybeInsert.size(), index.table());
        }
    }

    // For each entry, insert a new row in the index table asynchronously
    ImmutableSet.Builder<ListenableFuture<?>> result = ImmutableSet.builder();
    for (Map.Entry<PartitionKeyToTraceId, Long> entry : toInsert.entries()) {
        BoundStatement bound = bindWithName(prepared, boundName).setLong("trace_id", entry.getKey().traceId)
                .setBytesUnsafe("ts", timestampCodec.serialize(entry.getValue()));
        if (indexTtl != null) {
            bound.setInt("ttl_", indexTtl);
        }
        index.bindPartitionKey(bound, entry.getKey().partitionKey);
        result.add(session.executeAsync(bound));
    }
    return result.build();
}