Example usage for com.google.common.collect Multimap keySet

List of usage examples for com.google.common.collect Multimap keySet

Introduction

In this page you can find the example usage for com.google.common.collect Multimap keySet.

Prototype

Set<K> keySet();

Source Link

Document

Returns a view collection of all distinct keys contained in this multimap.

Usage

From source file:eumetsat.pn.elasticsearch.webapp.ElasticsearchApp.java

@Override
protected Map<String, Object> search(String searchTerms, String filterString, int from, int size) {
    Map<String, Object> data = new HashMap<>();
    // put "session" parameters here rightaway so it can be used in template even when empty result
    data.put("search_terms", searchTerms);
    data.put("filter_terms", filterString);

    URL url;//from w  w  w. j  av a2  s.  c o m
    try {
        url = new URL(searchEndpointUrlString);
    } catch (MalformedURLException e) {
        log.error("Search enpoint URL malformed: {}", e.getMessage());
        addMessage(data, MessageLevel.danger, "Search endpoint URL is malformed: " + e.getMessage());
        return data;
    }

    List<Map<String, String>> resHits = new ArrayList<>();
    Map<String, String> resHit = null;

    Multimap<String, String> filterTermsMap = parseFiltersTerms(filterString);
    Set<String> hiddenFacets = new HashSet<>(); // to create the list of filters to hide

    String filterConstruct = "";
    if (filterTermsMap.size() > 0) {
        int i = 0;
        String filterTerms = "";
        for (String key : filterTermsMap.keySet()) {
            for (String term : filterTermsMap.get(key)) {
                if (i == 0) {
                    filterTerms += "{ \"term\" : { \"" + FACETS2HIERACHYNAMES.get(key) + "\":\"" + term
                            + "\"}}";
                } else {
                    filterTerms += ",{ \"term\" : { \"" + FACETS2HIERACHYNAMES.get(key) + "\":\"" + term
                            + "\"}}";
                }

                // hide the facets that are used for filtering
                hiddenFacets.add(key + ":" + term);

                i++;
            }
        }

        filterConstruct = " \"bool\" : { \"must\" : [" + filterTerms + "] }";
    }

    int lengthOfTitle = 300;
    int lengthOfAbstract = 5000;
    int boostFactorTitle = 10;

    String body = "{ " + // pagination information
            "\"from\" : " + from + ", \"size\" : " + size + "," + // request highlighted info
            "\"highlight\" : { \"pre_tags\" : [\"<em><strong>\"], \"post_tags\" : [\"</strong></em>\"], "
            + "                  \"fields\" : { \"identificationInfo.title\": {\"fragment_size\" : "
            + lengthOfTitle + ", \"number_of_fragments\" : 1}, "
            + "                                 \"identificationInfo.abstract\": {\"fragment_size\" : "
            + lengthOfAbstract + ", \"number_of_fragments\" : 1} } } , " + // request facets to refine search (here the maximum number of facets can be configured)
            " \"facets\" :   { \"satellites\": { \"terms\" : { \"field\" : \"hierarchyNames.satellite\", \"size\":5 } }, "
            + "                  \"instruments\": { \"terms\" : { \"field\" : \"hierarchyNames.instrument\", \"size\":5  } }, "
            + "                  \"categories\": { \"terms\" : { \"field\" : \"hierarchyNames.category\", \"size\": 5 } }, "
            + "                  \"societal Benefit Area\": { \"terms\" : { \"field\" : \"hierarchyNames.societalBenefitArea\", \"size\":5 } }, "
            + "                  \"distribution\": { \"terms\" : { \"field\" : \"hierarchyNames.distribution\", \"size\":5 } } "
            + "                }," + // add query info
            "\"query\" : { \"filtered\": { \"query\": "
            + "              { \"simple_query_string\" : { \"fields\" : [\"identificationInfo.title^"
            + boostFactorTitle + "\", \"identificationInfo.abstract\"], " + "\"query\" : \"" + searchTerms
            + "\" } " + "}" + ",\"filter\": {" + filterConstruct + "}" + " }}}";

    log.trace("elastic-search request: {}", body);

    //measure elapsed time
    Stopwatch stopwatch = Stopwatch.createStarted();

    WebResponse response = rClient.doGetRequest(url, new HashMap<String, String>(),
            new HashMap<String, String>(), body, log.isDebugEnabled());

    if (response == null) {
        log.error("Response from {} is null!", this.name);
        data.put("total_hits", 0);
        data = addMessage(data, MessageLevel.danger, "Response is null from " + this.name);
    } else {
        log.trace("Got response: {}", response);

        if (response.status == 200) {
            JSONParser parser = new JSONParser();
            JSONObject jsObj;
            try {
                jsObj = (JSONObject) parser.parse(response.body);
            } catch (ParseException e) {
                log.error("Could not parse search server response: {}", e);
                addMessage(data, MessageLevel.danger, "Could not parse server response: " + e.getMessage());
                return data;
            }

            Long hitcount = (Long) ((Map<?, ?>) jsObj.get("hits")).get("total");
            data.put("total_hits", hitcount);
            if (hitcount < 1)
                addMessage(data, MessageLevel.info, "No results found!");

            // compute the pagination information to create the pagination bar
            Map<String, Object> pagination = computePaginationParams(hitcount.intValue(), from);
            data.put("pagination", pagination);

            @SuppressWarnings("unchecked")
            List<Map<String, Object>> hits = (List<Map<String, Object>>) ((Map<?, ?>) jsObj.get("hits"))
                    .get("hits");

            //to get the highlight
            Map<?, ?> highlight = null;
            for (Map<String, Object> hit : hits) {
                resHit = new HashMap<>();

                resHit.put("id", (String) hit.get("_id"));
                resHit.put("score", String.format("%.4g%n", ((Double) hit.get("_score"))));

                // can have or not title or abstract
                // strategy. If it doesn't have an abstract or a title match then take it from the _source
                highlight = (Map<?, ?>) hit.get("highlight");

                if (highlight.containsKey("identificationInfo.title")) {
                    resHit.put("title",
                            (String) ((JSONArray) highlight.get("identificationInfo.title")).get(0));
                } else {
                    resHit.put("title", ((String) (((Map<?, ?>) (((Map<?, ?>) hit.get("_source"))
                            .get("identificationInfo"))).get("title"))));
                }

                if (highlight.containsKey("identificationInfo.abstract")) {
                    resHit.put("abstract",
                            (String) ((JSONArray) highlight.get("identificationInfo.abstract")).get(0));
                } else {
                    resHit.put("abstract", ((String) (((Map<?, ?>) (((Map<?, ?>) hit.get("_source"))
                            .get("identificationInfo"))).get("abstract"))));
                }

                JSONObject info = (JSONObject) ((JSONObject) hit.get("_source")).get("identificationInfo");
                JSONArray keywords = (JSONArray) info.get("keywords");
                resHit.put("keywords", Joiner.on(", ").join(keywords.iterator()));

                resHit.put("thumbnail", info.get("thumbnail").toString());
                resHit.put("status", info.get("status").toString());

                resHits.add(resHit);
            }

            data.put("hits", resHits);

            data.put("facets", jsObj.get("facets"));

            data.put("tohide", hiddenFacets);

        } else { // non-200 resonse
            log.error("Received non-200 response: {}", response);
            data = addMessage(data, MessageLevel.danger, "Non 200 response: " + response.toString());
        }
    }

    stopwatch.stop();

    data.put("elapsed", (double) (stopwatch.elapsed(TimeUnit.MILLISECONDS)) / (double) 1000);

    return data;
}

From source file:org.nuunframework.kernel.internal.scanner.ClasspathScannerInternal.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override// ww  w .j a va2  s. c  om
public void scanClasspathForSpecification(final Specification<Class<?>> specification,
        final Callback callback) {
    //        Reflections reflections = new Reflections(configurationBuilder().addUrls(computeUrls()).setScanners(new TypesScanner()));
    queue(new ScannerCommand() {
        @Override
        public void execute(Reflections reflections) {
            Store store = reflections.getStore();
            Multimap<String, String> multimap = store.get(TypesScanner.class);
            Collection<String> collectionOfString = multimap.keySet();

            Collection<Class<?>> types = null;
            Collection<Class<?>> filteredTypes = new HashSet<Class<?>>();

            // Convert String to classes
            if (collectionOfString.size() > 0) {
                types = toClasses(collectionOfString);
            } else {
                types = Collections.emptySet();
            }

            // Filter via specification
            for (Class<?> candidate : types) {
                if (specification.isSatisfiedBy(candidate)) {
                    filteredTypes.add(candidate);
                }
            }

            callback.callback((Collection) postTreatment((Collection) filteredTypes));

        }

        @Override
        public Scanner scanner() {
            return new TypesScanner();
        }

    });

}

From source file:com.b2international.snowowl.core.internal.validation.ValidationRepositoryContext.java

void commit() {
    if (!newObjects.isEmpty() || !objectsToDelete.isEmpty()) {
        final Set<String> ruleIdsAffectedByDeletion = Sets.newHashSet();
        service(ValidationRepository.class).write(writer -> {
            writer.putAll(newObjects);/*from   ww  w .j  a v a2  s. co m*/

            final Multimap<String, ComponentIdentifier> addToWhitelist = HashMultimap.create();
            newObjects.values().stream().filter(ValidationWhiteList.class::isInstance)
                    .map(ValidationWhiteList.class::cast).forEach(whitelist -> addToWhitelist
                            .put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));

            if (!addToWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : addToWhitelist.keySet()) {
                    filter.should(Expressions.builder()
                            .filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId))
                            .filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID,
                                    addToWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId)
                                            .collect(Collectors.toSet())))
                            .build());
                }
                writer.bulkUpdate(
                        new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Fields.ID,
                                ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", true)));
            }

            final Multimap<String, ComponentIdentifier> removeFromWhitelist = HashMultimap.create();
            ValidationRequests.whiteList().prepareSearch().all()
                    .filterByIds(ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class))).build()
                    .execute(this).forEach(whitelist -> removeFromWhitelist.put(whitelist.getRuleId(),
                            whitelist.getComponentIdentifier()));

            if (!removeFromWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : removeFromWhitelist.keySet()) {
                    ruleIdsAffectedByDeletion.add(ruleId);
                    filter.should(Expressions.builder()
                            .filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId))
                            .filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID,
                                    removeFromWhitelist.get(ruleId).stream()
                                            .map(ComponentIdentifier::getComponentId)
                                            .collect(Collectors.toSet())))
                            .build());
                }
                writer.bulkUpdate(
                        new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Fields.ID,
                                ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", false)));
            }

            final Map<Class<?>, Set<String>> docsToDelete = newHashMap();
            objectsToDelete.asMap().forEach((type, ids) -> docsToDelete.put(type, ImmutableSet.copyOf(ids)));
            writer.removeAll(docsToDelete);

            writer.commit();
            return null;
        });

        if (!newObjects.isEmpty()) {
            final Set<String> addedWhiteLists = newHashSet();
            final Set<String> affectedRuleIds = newHashSet();
            newObjects.forEach((id, doc) -> {
                if (doc instanceof ValidationWhiteList) {
                    ValidationWhiteList whitelistedDoc = (ValidationWhiteList) doc;
                    affectedRuleIds.add(whitelistedDoc.getRuleId());
                    addedWhiteLists.add(id);
                }
            });
            if (!addedWhiteLists.isEmpty()) {
                WhiteListNotification.added(addedWhiteLists, affectedRuleIds).publish(service(IEventBus.class));
            }
        }

        if (!objectsToDelete.isEmpty()) {
            final Set<String> removedWhiteLists = ImmutableSet
                    .copyOf(objectsToDelete.get(ValidationWhiteList.class));
            if (!removedWhiteLists.isEmpty()) {
                WhiteListNotification.removed(removedWhiteLists, ruleIdsAffectedByDeletion)
                        .publish(service(IEventBus.class));
            }
        }
    }
}

From source file:org.jenkinsci.plugins.all_changes.AllChangesWorkflowAction.java

/**
 * Returns all changes which contribute to a build.
 *
 * @param build//ww w . j  av  a 2  s.  co  m
 * @return
 */
public Multimap<ChangeLogSet.Entry, WorkflowRun> getAllChanges(WorkflowRun build) {
    Set<WorkflowRun> builds = getContributingBuilds(build);
    Multimap<String, ChangeLogSet.Entry> changes = ArrayListMultimap.create();
    for (WorkflowRun changedBuild : builds) {
        for (ChangeLogSet changeLogSet : changedBuild.getChangeSets()) {
            ChangeLogSet<ChangeLogSet.Entry> changeSet = (ChangeLogSet<ChangeLogSet.Entry>) changeLogSet;
            for (ChangeLogSet.Entry entry : changeSet) {
                changes.put(entry.getCommitId() + entry.getMsgAnnotated() + entry.getTimestamp(), entry);
            }
        }
    }
    Multimap<ChangeLogSet.Entry, WorkflowRun> change2Build = HashMultimap.create();
    for (String changeKey : changes.keySet()) {
        ChangeLogSet.Entry change = changes.get(changeKey).iterator().next();
        for (ChangeLogSet.Entry entry : changes.get(changeKey)) {
            change2Build.put(change, (WorkflowRun) entry.getParent().getRun());
        }
    }
    return change2Build;
}

From source file:com.facebook.presto.accumulo.tools.RewriteIndex.java

private void setRowIdStatuses(Connector connector, AccumuloTable table, long timestamp,
        Multimap<ByteBuffer, Mutation> queryIndexEntries, Map<ByteBuffer, RowStatus> rowIdStatuses)
        throws TableNotFoundException {
    // Set ranges to all row IDs that we have no status for
    List<Range> queryRanges = queryIndexEntries.keySet().stream().filter(x -> !rowIdStatuses.containsKey(x))
            .map(x -> new Range(new Text(x.array()))).collect(Collectors.toList());

    if (queryRanges.size() == 0) {
        return;/*from   w  ww .j  ava2s.  c om*/
    }

    BatchScanner scanner = connector.createBatchScanner(table.getFullTableName(), auths, 10);
    scanner.setRanges(queryRanges);

    IteratorSetting iteratorSetting = new IteratorSetting(Integer.MAX_VALUE, TimestampFilter.class);
    TimestampFilter.setEnd(iteratorSetting, timestamp, true);
    scanner.addScanIterator(iteratorSetting);

    scanner.addScanIterator(new IteratorSetting(1, FirstEntryInRowIterator.class));

    // Make a copy of all the row IDs we are querying on to back-fill collection
    Set<ByteBuffer> allRowIDs = new HashSet<>(queryIndexEntries.keySet());

    // Scan the data table, removing all known row IDs and setting their status to present
    Text text = new Text();
    for (Entry<Key, Value> entry : scanner) {
        ByteBuffer rowID = ByteBuffer.wrap(entry.getKey().getRow(text).copyBytes());
        allRowIDs.remove(rowID);

        // Assert that this entry is new
        if (rowIdStatuses.put(rowID, RowStatus.PRESENT) != null) {
            throw new RuntimeException(
                    format("Internal error, row %s already has status", new String(rowID.array(), UTF_8)));
        }
    }
    scanner.close();

    AtomicLong newlyAbsent = new AtomicLong(0);
    // Back-fill the absent map -- rows may already be flagged as absent
    allRowIDs.forEach(rowID -> {
        RowStatus existingStatus = rowIdStatuses.get(rowID);
        if (existingStatus == null) {
            newlyAbsent.incrementAndGet();
            rowIdStatuses.put(rowID, RowStatus.ABSENT);
        } else if (existingStatus == RowStatus.PRESENT) {
            throw new RuntimeException(format("Internal error, row %s already has PRESENT status",
                    new String(rowID.array(), UTF_8)));
        }
    });
}

From source file:org.splevo.vpm.analyzer.DefaultVPMAnalyzerService.java

/**
 * Check if the same vp is contained in several buckets which would indicate an error.
 *
 * @param buckets/*from  w  w w.  j  av a2  s. c om*/
 *            The buckets to check.
 */
private void checkDistinctBuckets(Multimap<VariationPoint, VariationPoint> buckets) {

    Map<VariationPoint, VariationPoint> vpHits = Maps.newLinkedHashMap();
    for (VariationPoint key : buckets.keySet()) {
        for (VariationPoint vp : buckets.get(key)) {
            if (vpHits.containsKey(vp)) {
                logger.error("VP contained in more than one merge bucket");
            }
            vpHits.put(vp, key);
        }
    }
}

From source file:uk.ac.ebi.apps.benchmark.ChemicalNameSearch.java

private int getRealScore(Multimap<String, Set<String>> results, FingerprintEncoder encoder, File file) {

    int hitCount = 0;

    FileWriter writer = null;/* w  w w .  j  a v a2 s .co m*/

    try {
        if (file != null)
            writer = new FileWriter(file);
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }

    QUERY: for (String name : results.keySet()) {

        String normName = encoder.encode(name);

        StringBuffer buffer = new StringBuffer();

        for (Set<String> hitNames : results.get(name)) {

            for (String hit : hitNames) {

                String normHit = encoder.encode(hit);

                buffer.append("\t").append(hit);
                buffer.append("\t").append(normHit);
                buffer.append("\t").append(StringUtils.getLevenshteinDistance(normName, normHit));
                buffer.append("\n");

                if (normName.equals(normHit)) {
                    hitCount++;
                    continue QUERY;
                }

            }

        }

        try {
            if (writer != null) {
                writer.write(name + "\n");
                writer.write(buffer.toString() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }

    }

    try {
        if (writer != null)
            writer.close();
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    System.out.println("Written unmatched results to :" + file);

    return hitCount;

}

From source file:org.sosy_lab.cpachecker.cpa.value.refiner.ValueAnalysisConcreteErrorPathAllocator.java

private Map<LeftHandSide, Address> generateVariableAddresses(
        Multimap<IDExpression, MemoryLocation> pMemoryLocationsInPath) {

    Map<LeftHandSide, Address> result = Maps.newHashMapWithExpectedSize(pMemoryLocationsInPath.size());

    // Start with Address 0
    Address nextAddressToBeAssigned = Address.valueOf(BigInteger.ZERO);

    for (IDExpression variable : pMemoryLocationsInPath.keySet()) {
        result.put(variable, nextAddressToBeAssigned);

        // leave enough space for values between addresses
        nextAddressToBeAssigned = generateNextAddresses(pMemoryLocationsInPath.get(variable),
                nextAddressToBeAssigned);

    }/*w ww .  ja v a 2 s .  c o  m*/

    return result;
}

From source file:io.nuun.kernel.core.internal.scanner.disk.ClasspathScannerDisk.java

@SuppressWarnings({})
@Override//  www . j a v  a  2  s  . c om
public void scanClasspathForSubTypeRegex(final String subTypeName, final Callback callback) {

    //        Reflections reflections = new Reflections(configurationBuilder().addUrls(computeUrls()).setScanners(new TypesScanner()));

    queue(new ScannerCommand() {
        @Override
        public void execute(Reflections reflections) {
            // empty just add
        }

        @Override
        public Scanner scanner() {
            return new SubTypesScanner();
        }
    });
    queue(new ScannerCommand() {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public void execute(Reflections reflections) {
            Store store = reflections.getStore();
            Multimap<String, String> multimap = store.get(TypesScanner.class);

            Collection<String> collectionOfString = new HashSet<String>();

            for (String loopKey : multimap.keySet()) {

                if (loopKey.matches(subTypeName)) {
                    collectionOfString.add(loopKey);
                }
            }

            Collection<Class<?>> types = null;

            if (collectionOfString.size() > 0) {
                types = toClasses(collectionOfString);
            } else {
                types = Collections.emptySet();
            }

            // Then find subclasses of types
            Collection<Class<?>> finalClasses = new HashSet<Class<?>>();
            for (Class<?> subType : types) {
                // Collection<Class<?>> scanClasspathForSubTypeClass =
                // scanClasspathForSubTypeClass(class1);
                // ///////////////////////////////
                Collection<?> typesAnnotatedWith = reflections.getSubTypesOf(subType);

                if (typesAnnotatedWith == null) {
                    typesAnnotatedWith = Collections.emptySet();
                }
                // ///////////////////////////////
                finalClasses.addAll(postTreatment((Collection) typesAnnotatedWith));
            }

            // removed ignored already done
            callback.callback(finalClasses);
            // return (Collection) removeIgnore((Collection)types);

        }

        @Override
        public Scanner scanner() {
            return new TypesScanner();

        }
    });

}