Example usage for com.google.common.collect Multimaps index

List of usage examples for com.google.common.collect Multimaps index

Introduction

In this page you can find the example usage for com.google.common.collect Multimaps index.

Prototype

public static <K, V> ImmutableListMultimap<K, V> index(Iterator<V> values, Function<? super V, K> keyFunction) 

Source Link

Document

Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in an Iterator of values.

Usage

From source file:org.apache.hadoop.hive.ql.stats.BasicStatsNoJobTask.java

private int updatePartitions(Hive db, List<FooterStatCollector> scs, Table table)
        throws InvalidOperationException, HiveException {

    String tableFullName = table.getFullyQualifiedName();

    if (scs.isEmpty()) {
        return 0;
    }//from   ww w  .  j  av  a2  s.co  m
    if (work.isStatsReliable()) {
        for (FooterStatCollector statsCollection : scs) {
            if (statsCollection.result == null) {
                LOG.debug("Stats requested to be reliable. Empty stats found: {}",
                        statsCollection.partish.getSimpleName());
                return -1;
            }
        }
    }
    List<FooterStatCollector> validColectors = Lists.newArrayList();
    for (FooterStatCollector statsCollection : scs) {
        if (statsCollection.isValid()) {
            validColectors.add(statsCollection);
        }
    }

    EnvironmentContext environmentContext = new EnvironmentContext();
    environmentContext.putToProperties(StatsSetupConst.DO_NOT_UPDATE_STATS, StatsSetupConst.TRUE);

    ImmutableListMultimap<String, FooterStatCollector> collectorsByTable = Multimaps.index(validColectors,
            FooterStatCollector.SIMPLE_NAME_FUNCTION);

    LOG.debug("Collectors.size(): {}", collectorsByTable.keySet());

    if (collectorsByTable.keySet().size() < 1) {
        LOG.warn("Collectors are empty! ; {}", tableFullName);
    }

    // for now this should be true...
    assert (collectorsByTable.keySet().size() <= 1);

    LOG.debug("Updating stats for: {}", tableFullName);

    for (String partName : collectorsByTable.keySet()) {
        ImmutableList<FooterStatCollector> values = collectorsByTable.get(partName);

        if (values == null) {
            throw new RuntimeException("very intresting");
        }

        if (values.get(0).result instanceof Table) {
            db.alterTable(tableFullName, (Table) values.get(0).result, environmentContext, true);
            LOG.debug("Updated stats for {}.", tableFullName);
        } else {
            if (values.get(0).result instanceof Partition) {
                List<Partition> results = Lists.transform(values, FooterStatCollector.EXTRACT_RESULT_FUNCTION);
                db.alterPartitions(tableFullName, results, environmentContext, true);
                LOG.debug("Bulk updated {} partitions of {}.", results.size(), tableFullName);
            } else {
                throw new RuntimeException("inconsistent");
            }
        }
    }
    LOG.debug("Updated stats for: {}", tableFullName);
    return 0;
}

From source file:org.nla.tarotdroid.biz.computers.GuavaGameSetStatisticsComputer.java

/**
 * Returns for each bet the number of games this king has been called.   
 * @return for each bet the number of games this king has been called as a Map<KingType, Double>.
 *//*from ww  w  .  ja va  2s.  c  o  m*/
public Map<KingType, Integer> getKingCount() {

    // transform StandardTarot5Game into BetType
    Function<StandardTarot5Game, KingType> gameToBetTypeFunction = new Function<StandardTarot5Game, KingType>() {

        @Override
        public KingType apply(final StandardTarot5Game stdBaseGame) {
            return stdBaseGame.getCalledKing().getKingType();
        }
    };

    // group standard games by bet type 
    ImmutableListMultimap<KingType, StandardTarot5Game> kingTypeMultimap = Multimaps
            .index(this.gameSet.getStandard5Games(), gameToBetTypeFunction);

    // build return object
    Map<KingType, Integer> toReturn = newHashMap();
    for (KingType kingType : kingTypeMultimap.keys()) {
        toReturn.put(kingType, kingTypeMultimap.get(kingType).size());
    }
    this.kingSeriesCount = toReturn.keySet().size();

    return toReturn;
}

From source file:com.b2international.snowowl.snomed.datastore.converter.SnomedConceptConverter.java

private void expandRelationships(List<SnomedConcept> results, final Set<String> conceptIds) {
    if (!expand().containsKey(SnomedConcept.Expand.RELATIONSHIPS)) {
        return;// w w w . j av  a  2 s . c o  m
    }

    final Options expandOptions = expand().get(SnomedConcept.Expand.RELATIONSHIPS, Options.class);
    final SnomedRelationships relationships = SnomedRequests.prepareSearchRelationship().all()
            .filterByActive(expandOptions.containsKey("active") ? expandOptions.getBoolean("active") : null)
            .filterByCharacteristicType(expandOptions.containsKey("characteristicType")
                    ? expandOptions.getString("characteristicType")
                    : null)
            .filterByType(
                    expandOptions.containsKey("typeId") ? expandOptions.getCollection("typeId", String.class)
                            : null)
            .filterByDestination(expandOptions.containsKey("destinationId")
                    ? expandOptions.getCollection("destinationId", String.class)
                    : null)
            .filterBySource(conceptIds).setExpand(expandOptions.get("expand", Options.class))
            .setLocales(locales()).build().execute(context());

    final ListMultimap<String, SnomedRelationship> relationshipsByConceptId = Multimaps.index(relationships,
            relationship -> relationship.getSourceId());

    for (SnomedConcept concept : results) {
        final List<SnomedRelationship> conceptRelationships = relationshipsByConceptId.get(concept.getId());
        concept.setRelationships(new SnomedRelationships(conceptRelationships, null, null,
                conceptRelationships.size(), conceptRelationships.size()));
    }
}

From source file:org.robotframework.ide.eclipse.main.plugin.project.build.causes.ProblemCategory.java

public static Map<ProblemCategoryType, Collection<ProblemCategory>> getAllCategories() {
    final List<ProblemCategory> categories = Arrays.asList(ProblemCategory.values());
    final Multimap<ProblemCategoryType, ProblemCategory> groupedCategories = Multimaps.index(categories,
            new Function<ProblemCategory, ProblemCategoryType>() {

                @Override//from ww  w .j  ava2s  . c o m
                public ProblemCategoryType apply(final ProblemCategory category) {
                    return category.type;
                }
            });
    return TreeMultimap.create(groupedCategories).asMap();
}

From source file:cc.arduino.contributions.packages.ContributionsIndexer.java

public Set<ContributedTool> getInstalledTools() {
    Set<ContributedTool> tools = new HashSet<>();
    if (index == null) {
        return tools;
    }//from   www.j  a  v  a  2 s . com
    for (ContributedPackage pack : index.getPackages()) {
        Collection<ContributedPlatform> platforms = pack.getPlatforms().stream()
                .filter(new InstalledPredicate()).collect(Collectors.toList());
        ImmutableListMultimap<String, ContributedPlatform> platformsByName = Multimaps.index(platforms,
                ContributedPlatform::getName);

        for (Map.Entry<String, Collection<ContributedPlatform>> entry : platformsByName.asMap().entrySet()) {
            Collection<ContributedPlatform> platformsWithName = entry.getValue();
            if (platformsWithName.size() > 1) {
                platformsWithName = platformsWithName.stream().filter(new BuiltInPredicate().negate())
                        .collect(Collectors.toList());
            }
            for (ContributedPlatform platform : platformsWithName) {
                tools.addAll(platform.getResolvedTools());
            }
        }
    }
    return tools;
}

From source file:com.b2international.snowowl.snomed.datastore.converter.SnomedConceptConverter.java

private void expandInboundRelationships(List<SnomedConcept> results, final Set<String> conceptIds) {
    if (!expand().containsKey(SnomedConcept.Expand.INBOUND_RELATIONSHIPS)) {
        return;// w  w  w  .j  a  v a 2 s .c  o m
    }

    final Options expandOptions = expand().get(SnomedConcept.Expand.INBOUND_RELATIONSHIPS, Options.class);

    final int relationshipSearchLimit = getLimit(expandOptions);

    final SnomedRelationships inboundRelationships = SnomedRequests.prepareSearchRelationship()
            .setLimit(relationshipSearchLimit)
            .filterByType(
                    expandOptions.containsKey("typeId") ? expandOptions.getCollection("typeId", String.class)
                            : null)
            .filterBySource(expandOptions.containsKey("sourceId")
                    ? expandOptions.getCollection("sourceId", String.class)
                    : null)
            .filterByActive(expandOptions.containsKey("active") ? expandOptions.getBoolean("active") : null)
            .filterByCharacteristicType(expandOptions.containsKey("characteristicType")
                    ? expandOptions.getString("characteristicType")
                    : null)
            .filterByDestination(conceptIds).setExpand(expandOptions.get("expand", Options.class))
            .setLocales(locales()).build().execute(context());

    final ListMultimap<String, SnomedRelationship> inboundRelationshipsByConceptId = Multimaps
            .index(inboundRelationships, inboundRelationship -> inboundRelationship.getDestinationId());

    for (SnomedConcept concept : results) {
        final List<SnomedRelationship> conceptInboundRelationships = inboundRelationshipsByConceptId
                .get(concept.getId());
        concept.setInboundRelationships(new SnomedRelationships(conceptInboundRelationships, null, null,
                conceptInboundRelationships.size(), conceptInboundRelationships.size()));
    }
}

From source file:com.mrd.bitlib.StandardTransactionBuilder.java

@VisibleForTesting
Address extractRichest(Collection<UnspentTransactionOutput> unspent, final NetworkParameters network) {
    Preconditions.checkArgument(!unspent.isEmpty());
    Function<UnspentTransactionOutput, Address> txout2Address = new Function<UnspentTransactionOutput, Address>() {
        @Override//w  ww.j a v  a 2  s  . c o  m
        public Address apply(UnspentTransactionOutput input) {
            return input.script.getAddress(network);
        }
    };
    Multimap<Address, UnspentTransactionOutput> index = Multimaps.index(unspent, txout2Address);
    Address ret = extractRichest(index);
    return Preconditions.checkNotNull(ret);
}

From source file:com.palantir.atlasdb.cleaner.Scrubber.java

void scrubImmediately(final TransactionManager txManager,
        final Multimap<String, Cell> tableNameToCell, final long scrubTimestamp, final long commitTimestamp) {
    if (log.isInfoEnabled()) {
        log.info("Scrubbing a total of " + tableNameToCell.size() + " cells immediately.");
    }//from  ww  w .ja  v a  2  s .c  o  m

    // Note that if the background scrub thread is also running at the same time, it will try to scrub
    // the same cells as the current thread (since these cells were queued for scrubbing right before
    // the hard delete transaction committed; while this is unfortunate (because it means we will be
    // doing more work than necessary), the behavior is still correct
    long nextImmutableTimestamp;
    while ((nextImmutableTimestamp = immutableTimestampSupplier.get()) < commitTimestamp) {
        try {
            if (log.isInfoEnabled()) {
                log.info(String.format(
                        "Sleeping because immutable timestamp %d has not advanced to at least commit timestamp %d",
                        nextImmutableTimestamp, commitTimestamp));
            }
            Thread.sleep(AtlasDbConstants.SCRUBBER_RETRY_DELAY_MILLIS);
        } catch (InterruptedException e) {
            log.error("Interrupted while waiting for immutableTimestamp to advance past commitTimestamp", e);
        }
    }

    List<Future<Void>> scrubFutures = Lists.newArrayList();
    for (List<Entry<String, Cell>> batch : Iterables.partition(tableNameToCell.entries(),
            batchSizeSupplier.get())) {
        final Multimap<String, Cell> batchMultimap = HashMultimap.create();
        for (Entry<String, Cell> e : batch) {
            batchMultimap.put(e.getKey(), e.getValue());
        }

        final Callable<Void> c = new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                if (log.isInfoEnabled()) {
                    log.info("Scrubbing " + batchMultimap.size() + " cells immediately.");
                }

                // Here we don't need to check scrub timestamps because we guarantee that scrubImmediately is called
                // AFTER the transaction commits
                scrubCells(txManager, batchMultimap, scrubTimestamp, TransactionType.AGGRESSIVE_HARD_DELETE);

                Multimap<Cell, Long> cellToScrubTimestamp = HashMultimap.create();

                cellToScrubTimestamp = Multimaps.invertFrom(
                        Multimaps.index(batchMultimap.values(), Functions.constant(scrubTimestamp)),
                        cellToScrubTimestamp);

                scrubberStore.markCellsAsScrubbed(cellToScrubTimestamp, batchSizeSupplier.get());

                if (log.isInfoEnabled()) {
                    log.info("Completed scrub immediately.");
                }
                return null;
            }
        };
        if (!inScrubThread.get()) {
            scrubFutures.add(exec.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    inScrubThread.set(true);
                    c.call();
                    return null;
                }
            }));
        } else {
            try {
                c.call();
            } catch (Exception e) {
                throw Throwables.throwUncheckedException(e);
            }
        }
    }

    for (Future<Void> future : scrubFutures) {
        try {
            future.get();
        } catch (InterruptedException e) {
            throw Throwables.throwUncheckedException(e);
        } catch (ExecutionException e) {
            throw Throwables.rewrapAndThrowUncheckedException(e);
        }
    }
}

From source file:org.opentestsystem.authoring.testauth.publish.BasePublisherHelper.java

protected List<PoolProperty> buildPoolPropertyListFromItemList(final Map<String, TestItem> testItemMap,
        final List<Item> itemList) {
    final List<PoolProperty> combinedPoolPropertyList = Lists.newArrayList();
    final List<PoolProperty> metadataPoolPropertyList = Lists.newArrayList();
    String assessmentId = null;/*from w ww.  j ava  2s  . c om*/

    for (final Item item : itemList) {
        assessmentId = item.getAssessmentId();
        if (testItemMap.get(item.getId()) != null) {
            metadataPoolPropertyList.addAll(testItemMap.get(item.getId()).getPoolPropertyList());
        }
    }
    ItemMetadataConfig itemMetadataConfig = null;
    if (StringUtils.isNotBlank(assessmentId)) {
        itemMetadataConfig = this.itemMetadataConfigService.getItemMetadataConfigByAssessment(assessmentId);
    }
    if (itemMetadataConfig != null) {
        final Map<String, Collection<PoolProperty>> poolPropertyMultiMap = Maps
                .newHashMap(
                        Multimaps
                                .index(metadataPoolPropertyList,
                                        POOLPROPERTY_TO_POOLPROPERTY_TYPE_TRANSFORMER.getInstance(Lists
                                                .newArrayList(itemMetadataConfig.getItemMetadataReckonSet())))
                                .asMap());
        poolPropertyMultiMap.remove(PublisherUtil.EMPTY_PROPERTY);
        for (final Entry<String, Collection<PoolProperty>> poolPropertyEntry : poolPropertyMultiMap
                .entrySet()) {
            final Map<String, Collection<PoolProperty>> poolPropertyValueMultiMap = Multimaps
                    .index(poolPropertyEntry.getValue(), POOLPROPERTY_TO_POOLPROPERTY_VALUE_TRANSFORMER)
                    .asMap();
            for (final Entry<String, Collection<PoolProperty>> poolPropertyValueEntry : poolPropertyValueMultiMap
                    .entrySet()) {
                combinedPoolPropertyList.add(new PoolProperty(poolPropertyEntry.getKey(),
                        poolPropertyValueEntry.getKey(), poolPropertyValueEntry.getKey(),
                        String.valueOf(poolPropertyValueEntry.getValue().size())));
            }
        }
    }

    return combinedPoolPropertyList;
}

From source file:org.apache.usergrid.apm.service.StatelessComplexEventProcessingService.java

public void calculateSummarySessionMetrics2(Long appId, String fullAppName,
        List<ClientSessionMetrics> clientSessionMetrics, List<SummarySessionMetrics> dirtySummarySessionMetrics,
        Set<Long> modifiedMinutes) {

    // Empty metrics checker = should not be happening in production
    if (clientSessionMetrics.size() == 0) {
        log.error(// w w  w .  j a v  a 2 s. c o m
                "Something is very odd. Skipping Summary Session Metrics calculation since there is no clientSessionMetrics for app : "
                        + fullAppName);
        return;
    }

    /*For the purpose of backwards compatibility, if sessionId is null, then setting the session id to something meaningful
            
    for(ClientSessionMetrics csm: clientSessionMetrics)
    {
       if(csm.getSessionId() == null && csm.getDeviceId() != null)
       {
    log.warn("Encountered metrics data for appId " +  appId + " where sessionId was not populated. This indicates potentially usage of an old client : " + csm.getDeviceId() );
    String fakeSessionId = csm.getDeviceId().toString() + "_" + csm.getEndHour().toString();
    csm.setSessionId(fakeSessionId);
       }
    } */

    // Group Client Session metrics by sessionId since there could be different messgaes from same device for the same session
    Function<ClientSessionMetrics, String> sessionFunction = new Function<ClientSessionMetrics, String>() {
        @Override
        public String apply(@Nullable ClientSessionMetrics arg0) {
            return arg0.getSessionId();
        }

    };

    ImmutableListMultimap<String, ClientSessionMetrics> csmBySessionId = Multimaps.index(clientSessionMetrics,
            sessionFunction);

    // 2. For each CSM grouping, find CSM with latest timestamp.
    Comparator<ClientSessionMetrics> endMinuteComparator = new Comparator<ClientSessionMetrics>() {
        @Override
        public int compare(ClientSessionMetrics csm0, ClientSessionMetrics csm1) {
            return csm0.getEndMinute().compareTo(csm1.getEndMinute());

        }
    };

    Map<String, ClientSessionMetrics> latestCSM = new HashMap<String, ClientSessionMetrics>();

    for (String sessionId : csmBySessionId.keySet()) {
        ClientSessionMetrics latestClientSessionMetric = Collections.max(csmBySessionId.get(sessionId),
                endMinuteComparator);
        latestCSM.put(sessionId, latestClientSessionMetric);
    }

    //3. Query DB for matching Sessions. 
    //TODO: This potentially could be slow for a high volume app. Need to investigate better approach
    log.info("Querying summary session table for appId " + fullAppName + " for "
            + csmBySessionId.keySet().size() + " sessions");
    List<SummarySessionMetrics> matchingSSM = sessionDBService.getSummarySessionsBySessionId(appId,
            csmBySessionId.keySet());
    log.info("Found " + matchingSSM.size() + " pre-existing sessions in database for app " + fullAppName);

    //      4a. If no match exists, create a new summary session
    //      4b.   If match exists, but match's end time >= to the end time of the CSM being ingested, do nothing
    //      4c.   If the match exists, and the match's end time < to the end time of the CSM being ingested, extend the session.

    // Group Summary Session metrics by sessionId
    //Todo : since matchingSSM has distinct summarySessions, following function may not be needed..confirm
    Function<SummarySessionMetrics, String> sessionFunction2 = new Function<SummarySessionMetrics, String>() {
        @Override
        public String apply(@Nullable SummarySessionMetrics arg0) {
            return arg0.getSessionId();
        }

    };

    ImmutableListMultimap<String, SummarySessionMetrics> ssmBySessionId = Multimaps.index(matchingSSM,
            sessionFunction2);

    //ssmBySessionId.keySet();

    //Find new Sessions
    Set<String> newSessions = Sets.difference(latestCSM.keySet(), ssmBySessionId.keySet());
    log.info("Found " + newSessions.size() + " new sessions for app " + fullAppName);
    //Create new SSM

    for (String sessionId : newSessions) {
        SummarySessionMetrics ssm = createSummarySessionMetrics(latestCSM.get(sessionId));
        modifiedMinutes.add(ssm.getEndMinute());
        dirtySummarySessionMetrics.add(ssm);
    }

    //Update SSM
    for (SummarySessionMetrics ssm : matchingSSM) {
        if (latestCSM.containsKey(ssm.getSessionId())) {
            if (latestCSM.get(ssm.getSessionId()).getEndMinute() > ssm.getEndMinute()) {
                ssm.setPrevSessionEndTime(ssm.getSessionEndTime());
                ssm.setSessionEndTime(latestCSM.get(ssm.getSessionId()).getTimeStamp());
                ssm.setEndBatteryLevel(latestCSM.get(ssm.getSessionId()).getBatteryLevel());
                addModifiedMinutes(modifiedMinutes, ssm);
                dirtySummarySessionMetrics.add(ssm);
            } else {
                log.warn("Got a message that was out of order or the data"
                        + "has been within the same minute for app " + ssm.getFullAppName() + " and SessiondId "
                        + ssm.getSessionId());
            }
        } else {
            log.warn("Matching Summary Session Metrics didn't actually find a match for app "
                    + ssm.getFullAppName() + " SessionId :" + ssm.getSessionId());
        }
    }

}