Example usage for com.google.common.collect ListMultimap get

List of usage examples for com.google.common.collect ListMultimap get

Introduction

In this page you can find the example usage for com.google.common.collect ListMultimap get.

Prototype

@Override
List<V> get(@Nullable K key);

Source Link

Document

Because the values for a given key may have duplicates and follow the insertion ordering, this method returns a List , instead of the java.util.Collection specified in the Multimap interface.

Usage

From source file:org.kairosdb.core.datastore.KairosDatastore.java

protected List<DataPointGroup> groupByTypeAndTag(String metricName, List<DataPointRow> rows,
        TagGroupBy tagGroupBy, Order order) {
    List<DataPointGroup> ret = new ArrayList<DataPointGroup>();
    MemoryMonitor mm = new MemoryMonitor(20);

    if (rows.isEmpty()) {
        ret.add(new SortingDataPointGroup(metricName, order));
    } else {/*from  www. j ava 2  s.co  m*/
        ListMultimap<String, DataPointGroup> typeGroups = ArrayListMultimap.create();

        //Go through each row grouping them by type
        for (DataPointRow row : rows) {
            String groupType = m_dataPointFactory.getGroupType(row.getDatastoreType());

            typeGroups.put(groupType, new DataPointGroupRowWrapper(row));
            mm.checkMemoryAndThrowException();
        }

        //Sort the types for predictable results
        TreeSet<String> sortedTypes = new TreeSet<String>(typeGroups.keySet());

        //Now go through each type group and group by tag if needed.
        for (String type : sortedTypes) {
            if (tagGroupBy != null) {
                ListMultimap<String, DataPointGroup> groups = ArrayListMultimap.create();
                Map<String, TagGroupByResult> groupByResults = new HashMap<String, TagGroupByResult>();

                for (DataPointGroup dataPointGroup : typeGroups.get(type)) {
                    //Todo: Add code to datastore implementations to filter by the group by tag

                    LinkedHashMap<String, String> matchingTags = getMatchingTags(dataPointGroup,
                            tagGroupBy.getTagNames());
                    String tagsKey = getTagsKey(matchingTags);
                    groups.put(tagsKey, dataPointGroup);
                    groupByResults.put(tagsKey, new TagGroupByResult(tagGroupBy, matchingTags));
                    mm.checkMemoryAndThrowException();
                }

                //Sort groups by tags
                TreeSet<String> sortedGroups = new TreeSet<String>(groups.keySet());

                for (String key : sortedGroups) {
                    SortingDataPointGroup sdpGroup = new SortingDataPointGroup(groups.get(key),
                            groupByResults.get(key), order);
                    sdpGroup.addGroupByResult(new TypeGroupByResult(type));
                    ret.add(sdpGroup);
                }
            } else {
                ret.add(new SortingDataPointGroup(typeGroups.get(type), new TypeGroupByResult(type), order));
            }
        }
    }

    return ret;
}

From source file:org.wso2.carbon.analytics.datasource.hbase.HBaseTimestampIterator.java

private void fetchRecords() throws AnalyticsTableNotAvailableException {
    if (this.fullyFetched) {
        return;//from w ww. j  av a 2  s .  c  o  m
    }
    ListMultimap<String, Long> batchedResults = this.populateNextRecordBatch();
    if (batchedResults.size() == 0) {
        return;
    }
    Set<String> colSet = null;
    List<Record> fetchedRecords = new ArrayList<>();
    List<Get> gets = new ArrayList<>();

    for (String currentId : batchedResults.keySet()) {
        Get get = new Get(Bytes.toBytes(currentId));
        get.addFamily(HBaseAnalyticsDSConstants.ANALYTICS_DATA_COLUMN_FAMILY_NAME);
        gets.add(get);
    }

    try {
        /* If the list of columns to be retrieved is null, retrieve ALL columns. */
        if (this.columns != null && this.columns.size() > 0) {
            colSet = new HashSet<>(this.columns);

        }
        Result[] results = this.table.get(gets);
        for (Result currentResult : results) {
            if (!currentResult.isEmpty()) {
                Record record = HBaseUtils.constructRecord(currentResult, tableName, colSet);
                if (record != null) {
                    byte[] currentRecordId = currentResult.getRow();
                    List<Long> indexEntries = batchedResults.get(Bytes.toString(currentRecordId));
                    long originalTimestamp = record.getTimestamp();
                    if (indexEntries.contains(originalTimestamp)) {
                        fetchedRecords.add(record);
                        indexEntries.remove(originalTimestamp);
                    }
                    List<Delete> obsoleteEntries = indexEntries.stream()
                            .map(timestamp -> new Delete(Bytes.toBytes(timestamp)).addColumn(
                                    HBaseAnalyticsDSConstants.ANALYTICS_INDEX_COLUMN_FAMILY_NAME,
                                    currentResult.getRow()))
                            .collect(Collectors.toList());
                    this.deleteObsoleteEntries(obsoleteEntries);
                }
            } /*else {
              byte[] currentRecordId = currentResult.getRow();    //this is null :(
              List<Long> indexEntries = batchedResults.get(Bytes.toString(currentRecordId));
              List<Delete> obsoleteEntries = new ArrayList<>();
              for (Long timestamp : indexEntries) {
                  obsoleteEntries.add(new Delete(Bytes.toBytes(timestamp)).addColumn(
                          HBaseAnalyticsDSConstants.ANALYTICS_INDEX_COLUMN_FAMILY_NAME, currentResult.getRow()));
              }
              this.deleteObsoleteEntries(obsoleteEntries);
              }*/
        }
        this.subIterator = fetchedRecords.iterator();
    } catch (Exception e) {
        if (e instanceof RetriesExhaustedException) {
            throw new AnalyticsTableNotAvailableException(tableName);
        }
        this.cleanup();
        throw new HBaseRuntimeException("Error reading data from table [" + this.tableName + "]", e);
    }
}

From source file:eu.crydee.alignment.aligner.cr.BritannicaCR.java

@Override
public void getNext(JCas jcas) throws IOException, CollectionException {
    JCas eleV, briV;// www.  j av a 2 s . c o m
    try {
        eleV = ViewCreatorAnnotator.createViewSafely(jcas, eleName);
        briV = ViewCreatorAnnotator.createViewSafely(jcas, normalName);
    } catch (AnalysisEngineProcessException ex) {
        throw new CollectionException(ex);
    }
    jcas.setDocumentLanguage("en");
    eleV.setDocumentLanguage("en");
    briV.setDocumentLanguage("en");
    String eleFilepath = filesNames[currentIndex], normalFilepath = eleFilepath.replace("-ele.ada", "-bri.ada"),
            annName = eleFilepath.replace("-ele.ada", "-hum.txt"),
            name = StringUtils.capitalize(eleFilepath.replace("-ele.ada", ""));
    File ele = new File(corpus, eleFilepath), bri = new File(corpus, normalFilepath),
            ann = new File(anns, annName);
    ListMultimap<Integer, Integer> eleBriGold = ArrayListMultimap.create(),
            briEleGold = ArrayListMultimap.create();
    try (BufferedReader br = new BufferedReader(new FileReader(ann))) {
        String line;
        int k = 0;
        int i = -1;
        while ((line = br.readLine()) != null) {
            switch (k % 3) {
            case 0:
                i = Integer.parseInt(line.split(" ")[0]);
                break;
            case 1:
                int j = Integer.parseInt(line.split(" ")[0]);
                eleBriGold.put(i, j);
                briEleGold.put(j, i);
                break;
            case 2:
                break;
            }
            ++k;
        }
    }
    StringBuilder eleSb = new StringBuilder(), normalSb = new StringBuilder();
    List<Sentence> eleSents = new ArrayList<>(), briSents = new ArrayList<>();
    handleAda(ele, eleSb, eleSents, eleV);
    handleAda(bri, normalSb, briSents, briV);
    for (Integer eleIndex : eleBriGold.keySet()) {
        Sentence eleSent = eleSents.get(eleIndex - 1);
        List<Integer> briIndeces = eleBriGold.get(eleIndex);
        eleSent.setGoldSimilarities(new FSArray(eleV, briIndeces.size()));
        for (int i = 0, l = briIndeces.size(); i < l; ++i) {
            Sentence briSent = briSents.get(briIndeces.get(i) - 1);
            eleSent.setGoldSimilarities(i, briSent);
        }
    }
    for (Integer briIndex : briEleGold.keySet()) {
        Sentence briSent = briSents.get(briIndex - 1);
        List<Integer> eleIndeces = briEleGold.get(briIndex);
        briSent.setGoldSimilarities(new FSArray(briV, eleIndeces.size()));
        for (int i = 0, l = eleIndeces.size(); i < l; ++i) {
            Sentence eleSent = eleSents.get(eleIndeces.get(i) - 1);
            briSent.setGoldSimilarities(i, eleSent);
        }
    }
    eleV.setDocumentText(eleSb.toString());
    briV.setDocumentText(normalSb.toString());
    jcas.setDocumentText(FileUtils.readFileToString(ann));
    for (JCas j : new JCas[] { eleV, briV, jcas }) {
        Document document = new Document(j, 0, j.getDocumentText().length() - 1);
        document.setName(name);
        document.addToIndexes();
    }
    ++currentIndex;
}

From source file:org.sonar.plugins.core.timemachine.NewViolationsDecorator.java

private void computeNewViolationsPerRule(DecoratorContext context) {
    for (RulePriority severity : RulePriority.values()) {
        Metric metric = severityToMetric(severity);
        ListMultimap<Rule, Measure> childMeasuresPerRule = ArrayListMultimap.create();
        ListMultimap<Rule, Violation> violationsPerRule = ArrayListMultimap.create();
        Set<Rule> rules = Sets.newHashSet();

        Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rules(metric));
        for (Measure child : children) {
            RuleMeasure childRuleMeasure = (RuleMeasure) child;
            Rule rule = childRuleMeasure.getRule();
            if (rule != null) {
                childMeasuresPerRule.put(rule, childRuleMeasure);
                rules.add(rule);//  www  . j ava 2 s.c o  m
            }
        }

        for (Violation violation : context.getViolations()) {
            if (violation.getSeverity().equals(severity)) {
                rules.add(violation.getRule());
                violationsPerRule.put(violation.getRule(), violation);
            }
        }

        for (Rule rule : rules) {
            RuleMeasure measure = RuleMeasure.createForRule(metric, rule, null);
            measure.setSeverity(severity);
            for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
                int variationIndex = pastSnapshot.getIndex();
                int count = countViolations(violationsPerRule.get(rule), pastSnapshot.getTargetDate());
                double sum = MeasureUtils.sumOnVariation(true, variationIndex, childMeasuresPerRule.get(rule))
                        + count;
                measure.setVariation(variationIndex, sum);
            }
            context.saveMeasure(measure);
        }
    }
}

From source file:com.google.gerrit.server.plugins.PluginGuiceEnvironment.java

private void reattachSet(ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> oldHandles,
        Map<TypeLiteral<?>, DynamicSet<?>> sets, @Nullable Injector src, Plugin newPlugin) {
    if (src == null || sets == null || sets.isEmpty()) {
        return;//w w  w.ja va2s . c  o  m
    }

    for (Map.Entry<TypeLiteral<?>, DynamicSet<?>> e : sets.entrySet()) {
        @SuppressWarnings("unchecked")
        TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();

        @SuppressWarnings("unchecked")
        DynamicSet<Object> set = (DynamicSet<Object>) e.getValue();

        // Index all old handles that match this DynamicSet<T> keyed by
        // annotations. Ignore the unique annotations, thereby favoring
        // the @Named annotations or some other non-unique naming.
        Map<Annotation, ReloadableRegistrationHandle<?>> am = Maps.newHashMap();
        List<ReloadableRegistrationHandle<?>> old = oldHandles.get(type);
        Iterator<ReloadableRegistrationHandle<?>> oi = old.iterator();
        while (oi.hasNext()) {
            ReloadableRegistrationHandle<?> h = oi.next();
            Annotation a = h.getKey().getAnnotation();
            if (a != null && !UNIQUE_ANNOTATION.isInstance(a)) {
                am.put(a, h);
                oi.remove();
            }
        }

        // Replace old handles with new bindings, favoring cases where there
        // is an exact match on an @Named annotation. If there is no match
        // pick any handle and replace it. We generally expect only one
        // handle of each DynamicSet type when using unique annotations, but
        // possibly multiple ones if @Named was used. Plugin authors that want
        // atomic replacement across reloads should use @Named annotations with
        // stable names that do not change across plugin versions to ensure the
        // handles are swapped correctly.
        oi = old.iterator();
        for (Binding<?> binding : bindings(src, type)) {
            @SuppressWarnings("unchecked")
            Binding<Object> b = (Binding<Object>) binding;
            Key<Object> key = b.getKey();
            if (key.getAnnotation() == null) {
                continue;
            }

            @SuppressWarnings("unchecked")
            ReloadableRegistrationHandle<Object> h1 = (ReloadableRegistrationHandle<Object>) am
                    .remove(key.getAnnotation());
            if (h1 != null) {
                replace(newPlugin, h1, b);
            } else if (oi.hasNext()) {
                @SuppressWarnings("unchecked")
                ReloadableRegistrationHandle<Object> h2 = (ReloadableRegistrationHandle<Object>) oi.next();
                oi.remove();
                replace(newPlugin, h2, b);
            } else {
                newPlugin.add(set.add(b.getKey(), b.getProvider()));
            }
        }
    }
}

From source file:com.jivesoftware.os.miru.cluster.MiruReplicaSetDirector.java

private Set<MiruHost> electToReplicaSetForTenantPartition(MiruTenantId tenantId, MiruPartitionId partitionId,
        MiruReplicaSet replicaSet, long eligibleAfter) throws Exception {

    LinkedHashSet<HostHeartbeat> hostHeartbeats = clusterRegistry.getAllHosts();

    Set<MiruHost> hostsWithReplica = Sets.newHashSet(replicaSet.getHostsWithReplica());
    int hashCode = Objects.hash(tenantId, partitionId);

    Map<MiruHost, String> hostToRack = Maps.newHashMap();
    hostDescriptorProvider.stream((datacenter, rack, host) -> {
        hostToRack.put(host, rackAwareElections ? rack : "");
        return true;
    });//from   www.j a v  a 2 s .  c  o m

    ListMultimap<String, MiruHost> perRackCurrent = ArrayListMultimap.create();
    for (MiruHost host : hostsWithReplica) {
        String rack = hostToRack.getOrDefault(host, "");
        perRackCurrent.put(rack, host);
    }

    Map<String, List<MiruHost>> perRackHosts = new HashMap<>();
    for (HostHeartbeat hostHeartbeat : hostHeartbeats) {
        if (hostHeartbeat.heartbeat > eligibleAfter) {
            String rack = hostToRack.get(hostHeartbeat.host);
            if (rack != null) {
                perRackHosts.computeIfAbsent(rack, (key) -> new ArrayList<>()).add(hostHeartbeat.host);
            } else {
                LOG.warn("Ignored host with missing rack {}", hostHeartbeat.host);
            }
        }
    }

    Random random = new Random(new Random(hashCode).nextLong());
    for (List<MiruHost> rackMembers : perRackHosts.values()) {
        Collections.shuffle(rackMembers, random);
    }

    List<String> racks = new ArrayList<>(perRackHosts.keySet());
    int desiredNumberOfReplicas = replicaSet.getDesiredNumberOfReplicas();

    while (perRackCurrent.size() < desiredNumberOfReplicas) {
        Collections.sort(racks,
                (o1, o2) -> Integer.compare(perRackCurrent.get(o1).size(), perRackCurrent.get(o2).size()));
        boolean advanced = false;
        for (String cycleRack : racks) {
            List<MiruHost> rackHosts = perRackHosts.get(cycleRack);
            if (!rackHosts.isEmpty()) {
                perRackCurrent.put(cycleRack, rackHosts.remove(rackHosts.size() - 1));
                advanced = true;
                break;
            }
        }
        if (!advanced) {
            break;
        }
    }

    if (perRackCurrent.size() < desiredNumberOfReplicas) {
        LOG.error("Tenant {} partition {} required {} replicas but only {} hosts are available", tenantId,
                partitionId, desiredNumberOfReplicas, perRackCurrent.size());
    }

    for (MiruHost hostToElect : perRackCurrent.values()) {
        if (hostsWithReplica.add(hostToElect)) {
            elect(hostToElect, tenantId, partitionId, Long.MAX_VALUE - orderIdProvider.nextId());
        }
        LOG.debug("Elected {} to {} on {}", hostToElect, tenantId, partitionId);
    }
    return hostsWithReplica;
}

From source file:de.bund.bfr.knime.openkrise.MyKrisenInterfacesNodeModel.java

private BufferedDataTable getDeliveryConnectionsTable(List<Delivery> deliveries,
         BufferedDataTable deliveryTable, ExecutionContext exec) throws CanceledExecutionException {
     BufferedDataContainer container = exec.createDataContainer(
             new DataTableSpec(new DataColumnSpecCreator(TracingColumns.ID, StringCell.TYPE).createSpec(),
                     new DataColumnSpecCreator(TracingColumns.NEXT, StringCell.TYPE).createSpec()));
     int index = 0;

     if (!set.isLotBased()) {
         for (Delivery delivery : deliveries) {
             for (String next : delivery.getAllNextIds()) {
                 container.addRowToTable(
                         new DefaultRow(index++ + "", createCell(delivery.getId()), createCell(next)));
                 exec.checkCanceled();//  ww  w.  ja  v  a2 s.c  om
             }
         }
     } else {
         ListMultimap<String, String> incoming = ArrayListMultimap.create();
         ListMultimap<String, String> outgoing = ArrayListMultimap.create();

         for (DataRow row : deliveryTable) {
             incoming.put(IO.getString(row.getCell(deliveryTable.getSpec().findColumnIndex(TracingColumns.TO))),
                     IO.getString(row.getCell(deliveryTable.getSpec().findColumnIndex(TracingColumns.ID))));
             outgoing.put(
                     IO.getString(row.getCell(deliveryTable.getSpec().findColumnIndex(TracingColumns.FROM))),
                     IO.getString(row.getCell(deliveryTable.getSpec().findColumnIndex(TracingColumns.ID))));
         }

         for (String lot : Sets.intersection(incoming.keySet(), outgoing.keySet())) {
             for (String in : incoming.get(lot)) {
                 for (String out : outgoing.get(lot)) {
                     container.addRowToTable(new DefaultRow(index++ + "", createCell(in), createCell(out)));
                     exec.checkCanceled();
                 }
             }
         }
     }

     container.close();

     return container.getTable();
 }

From source file:es.ehu.si.ixa.pipe.convert.Convert.java

/**
 * Aggregates {@code POSDictionary} from a list of words and its postag.
 * /*from www.j  a va 2s  .  c o  m*/
 * @param inputLines
 *          the list of words and postag per line
 * @param tagDict
 *          the POSDictionary to which the lemma dictionary will be added
 */
private void addPOSTaggerDict(List<String> inputLines, POSDictionary tagDict) {
    ListMultimap<String, String> dictMultiMap = ArrayListMultimap.create();
    for (String line : inputLines) {
        String[] lineArray = line.split(" ");
        if (lineArray.length == 2) {
            dictMultiMap.put(lineArray[0], lineArray[1]);
        }
    }
    for (String token : dictMultiMap.keySet()) {
        List<String> tags = dictMultiMap.get(token);
        if (tags.size() == 1) {
            tagDict.put(token, tags.toArray(new String[tags.size()]));
        }
    }
}

From source file:com.google.gerrit.server.change.WalkSorter.java

private List<PatchSetData> sortProject(Project.NameKey project, Collection<ChangeData> in)
        throws OrmException, IOException {
    try (Repository repo = repoManager.openRepository(project); RevWalk rw = new RevWalk(repo)) {
        rw.setRetainBody(retainBody);/* ww w . j  a v a  2  s.c  o  m*/
        ListMultimap<RevCommit, PatchSetData> byCommit = byCommit(rw, in);
        if (byCommit.isEmpty()) {
            return ImmutableList.of();
        } else if (byCommit.size() == 1) {
            return ImmutableList.of(byCommit.values().iterator().next());
        }

        // Walk from all patch set SHA-1s, and terminate as soon as we've found
        // everything we're looking for. This is equivalent to just sorting the
        // list of commits by the RevWalk's configured order.
        //
        // Partially topo sort the list, ensuring no parent is emitted before a
        // direct child that is also in the input set. This preserves the stable,
        // expected sort in the case where many commits share the same timestamp,
        // e.g. a quick rebase. It also avoids JGit's topo sort, which slurps all
        // interesting commits at the beginning, which is a problem since we don't
        // know which commits to mark as uninteresting. Finding a reasonable set
        // of commits to mark uninteresting (the "rootmost" set) is at least as
        // difficult as just implementing this partial topo sort ourselves.
        //
        // (This is slightly less efficient than JGit's topo sort, which uses a
        // private in-degree field in RevCommit rather than multimaps. We assume
        // the input size is small enough that this is not an issue.)

        Set<RevCommit> commits = byCommit.keySet();
        ListMultimap<RevCommit, RevCommit> children = collectChildren(commits);
        ListMultimap<RevCommit, RevCommit> pending = MultimapBuilder.hashKeys().arrayListValues().build();
        Deque<RevCommit> todo = new ArrayDeque<>();

        RevFlag done = rw.newFlag("done");
        markStart(rw, commits);
        int expected = commits.size();
        int found = 0;
        RevCommit c;
        List<PatchSetData> result = new ArrayList<>(expected);
        while (found < expected && (c = rw.next()) != null) {
            if (!commits.contains(c)) {
                continue;
            }
            todo.clear();
            todo.add(c);
            int i = 0;
            while (!todo.isEmpty()) {
                // Sanity check: we can't pop more than N pending commits, otherwise
                // we have an infinite loop due to programmer error or something.
                checkState(++i <= commits.size(), "Too many pending steps while sorting %s", commits);
                RevCommit t = todo.removeFirst();
                if (t.has(done)) {
                    continue;
                }
                boolean ready = true;
                for (RevCommit child : children.get(t)) {
                    if (!child.has(done)) {
                        pending.put(child, t);
                        ready = false;
                    }
                }
                if (ready) {
                    found += emit(t, byCommit, result, done);
                    todo.addAll(pending.get(t));
                }
            }
        }
        return result;
    }
}

From source file:eu.stratosphere.sopremo.serialization.SopremoRecordPostPass.java

/**
 * @param node//from   w w  w. j  a  v a  2s .  c  o m
 * @return
 */
private void checkNode(PlanNode node) {
    if (node instanceof WorksetIterationPlanNode)
        node = addDummyNode(node, node.getOutgoingChannels(), node.getOutgoingChannels(),
                ((SopremoRecordSerializerFactory) ((WorksetIterationPlanNode) node).getSolutionSetSerializer())
                        .getLayout());

    List<Channel> outgoingChannels = node.getOutgoingChannels();
    if (outgoingChannels.size() > 1) {
        ListMultimap<SopremoRecordLayout, Channel> layouts = ArrayListMultimap.create();
        for (Channel channel : outgoingChannels)
            layouts.put(((SopremoRecordSerializerFactory) channel.getSerializer()).getLayout(), channel);

        // we need indeed different layouts; create a dummy map node for each layout
        if (layouts.keySet().size() > 1) {
            // layout to dummy node is empty, so we can ignore all nodes that also require empty layout
            layouts.removeAll(SopremoRecordLayout.EMPTY);
            for (SopremoRecordLayout layout : layouts.keySet()) {
                List<Channel> channelsToBeChanged = layouts.get(layout);
                addDummyNode(node, outgoingChannels, channelsToBeChanged, SopremoRecordLayout.EMPTY);
            }
        }
        fixInputs(node);
    }

}