List of usage examples for com.google.common.collect Multimap asMap
Map<K, Collection<V>> asMap();
From source file:org.openepics.discs.ccdb.core.auditlog.SlotEntityLogger.java
private List<AuditRecord> createAuditRecords(Slot slot, EntityTypeOperation operation) { final Map<String, String> propertiesMap = new TreeMap<>(); if (slot.getSlotPropertyList() != null) { for (SlotPropertyValue propValue : slot.getSlotPropertyList()) { final String entryValue = propValue.getPropValue() == null ? null : propValue.getPropValue().auditLogString(100, 50); propertiesMap.put(propValue.getProperty().getName(), entryValue); }// w w w . j a va 2 s . c o m } final Map<String, String> artifactsMap = new TreeMap<>(); if (slot.getSlotArtifactList() != null) { for (SlotArtifact artifact : slot.getSlotArtifactList()) { artifactsMap.put(artifact.getName(), artifact.getUri()); } } final Multimap<String, String> childrenMap = ArrayListMultimap.create(); if (slot.getPairsInWhichThisSlotIsAParentList() != null) { for (SlotPair slotPair : slot.getPairsInWhichThisSlotIsAParentList()) { childrenMap.put(slotPair.getChildSlot().getName(), slotPair.getSlotRelation().getName().toString()); } } final Multimap<String, String> parentsMap = ArrayListMultimap.create(); if (slot.getPairsInWhichThisSlotIsAChildList() != null) { for (SlotPair slotPair : slot.getPairsInWhichThisSlotIsAChildList()) { parentsMap.put(slotPair.getParentSlot().getName(), slotPair.getSlotRelation().getIname()); } } final Map<String, String> installationDeviceMap = new TreeMap<>(); InstallationRecord lastInstallationRecord = null; for (InstallationRecord installationRecord : slot.getInstallationRecordList()) { if (lastInstallationRecord == null || installationRecord.getModifiedAt().after(lastInstallationRecord.getModifiedAt())) { lastInstallationRecord = installationRecord; } } if (lastInstallationRecord != null) { final SimpleDateFormat timestampFormat = new SimpleDateFormat(Conversion.DATE_TIME_FORMAT); final String installationDeviceSerial = lastInstallationRecord.getDevice().getSerialNumber(); installationDeviceMap.put("inventoryID", installationDeviceSerial); installationDeviceMap.put("installationDate", timestampFormat.format(lastInstallationRecord.getInstallDate())); if (lastInstallationRecord.getUninstallDate() != null) { installationDeviceMap.put("uninstallationDate", timestampFormat.format(lastInstallationRecord.getUninstallDate())); } } final AuditLogUtil logUtil = new AuditLogUtil(slot) .removeTopProperties( Arrays.asList("id", "modifiedAt", "modifiedBy", "version", "name", "componentType")) .addStringProperty("componentType", slot.getComponentType().getName()) .addArrayOfMappedProperties("slotPropertyList", propertiesMap) .addArrayOfMappedProperties("slotArtifactList", artifactsMap) .addArrayOfMappedProperties("childrenSlots", childrenMap.asMap()) .addArrayOfMappedProperties("parentSlots", parentsMap.asMap()) .addArrayOfMappedProperties("installation", installationDeviceMap) .addArrayOfProperties("tagsList", EntityLoggerUtil.getTagNamesFromTagsSet(slot.getTags())); return ImmutableList.of(logUtil.auditEntry(operation, EntityType.SLOT, slot.getName(), slot.getId())); }
From source file:org.openepics.discs.conf.auditlog.SlotEntityLogger.java
private List<AuditRecord> createAuditRecords(Slot slot, EntityTypeOperation operation) { final Map<String, String> propertiesMap = new TreeMap<>(); if (slot.getSlotPropertyList() != null) { for (SlotPropertyValue propValue : slot.getSlotPropertyList()) { final String entryValue = propValue.getPropValue() == null ? null : propValue.getPropValue().auditLogString(EntityLoggerUtil.AUDIT_LOG_ROWS, EntityLoggerUtil.AUDIT_LOG_COLUMNS); propertiesMap.put(propValue.getProperty().getName(), entryValue); }// w w w. j a va 2s .c om } final Map<String, String> artifactsMap = new TreeMap<>(); if (slot.getSlotArtifactList() != null) { for (SlotArtifact artifact : slot.getSlotArtifactList()) { artifactsMap.put(artifact.getName(), artifact.getUri()); } } final Multimap<String, String> childrenMap = ArrayListMultimap.create(); if (slot.getPairsInWhichThisSlotIsAParentList() != null) { for (SlotPair slotPair : slot.getPairsInWhichThisSlotIsAParentList()) { childrenMap.put(slotPair.getChildSlot().getName(), slotPair.getSlotRelation().getName().toString()); } } final Multimap<String, String> parentsMap = ArrayListMultimap.create(); if (slot.getPairsInWhichThisSlotIsAChildList() != null) { for (SlotPair slotPair : slot.getPairsInWhichThisSlotIsAChildList()) { parentsMap.put(slotPair.getParentSlot().getName(), slotPair.getSlotRelation().getIname()); } } final Map<String, String> installationDeviceMap = new TreeMap<>(); InstallationRecord lastInstallationRecord = null; for (InstallationRecord installationRecord : slot.getInstallationRecordList()) { if (lastInstallationRecord == null || installationRecord.getModifiedAt().after(lastInstallationRecord.getModifiedAt())) { lastInstallationRecord = installationRecord; } } if (lastInstallationRecord != null) { final SimpleDateFormat timestampFormat = new SimpleDateFormat(Conversion.DATE_TIME_FORMAT); final String installationDeviceSerial = lastInstallationRecord.getDevice().getSerialNumber(); installationDeviceMap.put("inventoryID", installationDeviceSerial); installationDeviceMap.put("installationDate", timestampFormat.format(lastInstallationRecord.getInstallDate())); if (lastInstallationRecord.getUninstallDate() != null) { installationDeviceMap.put("uninstallationDate", timestampFormat.format(lastInstallationRecord.getUninstallDate())); } } final AuditLogUtil logUtil = new AuditLogUtil(slot) .removeTopProperties( Arrays.asList("id", "modifiedAt", "modifiedBy", "version", "name", "componentType")) .addStringProperty("componentType", slot.getComponentType().getName()) .addArrayOfMappedProperties("slotPropertyList", propertiesMap) .addArrayOfMappedProperties("slotArtifactList", artifactsMap) .addArrayOfMappedProperties("childrenSlots", childrenMap.asMap()) .addArrayOfMappedProperties("parentSlots", parentsMap.asMap()) .addArrayOfMappedProperties("installation", installationDeviceMap) .addArrayOfProperties("tagsList", EntityLoggerUtil.getTagNamesFromTagsSet(slot.getTags())); return ImmutableList.of(logUtil.auditEntry(operation, EntityType.SLOT, slot.getName(), slot.getId())); }
From source file:org.onosproject.p4runtime.ctl.P4RuntimeClientImpl.java
private Collection<PiActionGroup> doDumpGroups(PiActionProfileId piActionProfileId, PiPipeconf pipeconf) { log.debug("Dumping groups from action profile {} from {} (pipeconf {})...", piActionProfileId.id(), deviceId, pipeconf.id());/*from ww w .ja v a 2s . c o m*/ final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); if (browser == null) { log.warn("Unable to get a P4Info browser for pipeconf {}, aborting dump action profile", pipeconf); return Collections.emptySet(); } final int actionProfileId; try { actionProfileId = browser.actionProfiles().getByName(piActionProfileId.id()).getPreamble().getId(); } catch (P4InfoBrowser.NotFoundException e) { log.warn("Unable to dump groups: {}", e.getMessage()); return Collections.emptySet(); } // Prepare read request to read all groups from the given action profile. final ReadRequest groupRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) .addEntities(Entity.newBuilder() .setActionProfileGroup( ActionProfileGroup.newBuilder().setActionProfileId(actionProfileId).build()) .build()) .build(); // Read groups. final Iterator<ReadResponse> groupResponses; try { groupResponses = blockingStub.read(groupRequestMsg); } catch (StatusRuntimeException e) { log.warn("Unable to dump action profile {} from {}: {}", piActionProfileId, deviceId, e.getMessage()); return Collections.emptySet(); } final List<ActionProfileGroup> groupMsgs = Tools.stream(() -> groupResponses) .map(ReadResponse::getEntitiesList).flatMap(List::stream) .filter(entity -> entity.getEntityCase() == ACTION_PROFILE_GROUP).map(Entity::getActionProfileGroup) .collect(Collectors.toList()); log.debug("Retrieved {} groups from action profile {} on {}...", groupMsgs.size(), piActionProfileId.id(), deviceId); // Returned groups contain only a minimal description of their members. // We need to issue a new request to get the full description of each member. // Keep a map of all member IDs for each group ID, will need it later. final Multimap<Integer, Integer> groupIdToMemberIdsMap = HashMultimap.create(); groupMsgs.forEach(g -> groupIdToMemberIdsMap.putAll(g.getGroupId(), g.getMembersList().stream() .map(ActionProfileGroup.Member::getMemberId).collect(Collectors.toList()))); // Prepare one big read request to read all members in one shot. final Set<Entity> entityMsgs = groupMsgs.stream().flatMap(g -> g.getMembersList().stream()) .map(ActionProfileGroup.Member::getMemberId) // Prevent issuing many read requests for the same member. .distinct() .map(id -> ActionProfileMember.newBuilder().setActionProfileId(actionProfileId).setMemberId(id) .build()) .map(m -> Entity.newBuilder().setActionProfileMember(m).build()).collect(Collectors.toSet()); final ReadRequest memberRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) .addAllEntities(entityMsgs).build(); // Read members. final Iterator<ReadResponse> memberResponses; try { memberResponses = blockingStub.read(memberRequestMsg); } catch (StatusRuntimeException e) { log.warn("Unable to read members of action profile {} from {}: {}", piActionProfileId, deviceId, e.getMessage()); return Collections.emptyList(); } final Multimap<Integer, ActionProfileMember> groupIdToMembersMap = HashMultimap.create(); Tools.stream(() -> memberResponses).map(ReadResponse::getEntitiesList).flatMap(List::stream) .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER).map(Entity::getActionProfileMember) .forEach(member -> groupIdToMemberIdsMap.asMap() // Get all group IDs that contain this member. .entrySet().stream().filter(entry -> entry.getValue().contains(member.getMemberId())) .map(Map.Entry::getKey).forEach(gid -> groupIdToMembersMap.put(gid, member))); log.debug("Retrieved {} group members from action profile {} on {}...", groupIdToMembersMap.size(), piActionProfileId.id(), deviceId); return groupMsgs.stream().map(groupMsg -> { try { return ActionProfileGroupEncoder.decode(groupMsg, groupIdToMembersMap.get(groupMsg.getGroupId()), pipeconf); } catch (P4InfoBrowser.NotFoundException | EncodeException e) { log.warn("Unable to decode group: {}\n {}", e.getMessage(), groupMsg); return null; } }).filter(Objects::nonNull).collect(Collectors.toList()); }
From source file:org.elasticsearch.cassandra.cluster.routing.PrimaryFirstSearchStrategy.java
@Override public AbstractSearchStrategy.Result topology(String ksName, Collection<InetAddress> startedShards) { Predicate<InetAddress> isLocalDC = new Predicate<InetAddress>() { String localDC = DatabaseDescriptor.getEndpointSnitch() .getDatacenter(FBUtilities.getBroadcastAddress()); public boolean apply(InetAddress address) { String remoteDC = DatabaseDescriptor.getEndpointSnitch().getDatacenter(address); return remoteDC.equals(localDC); }//from www . j a va 2 s . c o m }; Set<InetAddress> localLiveNodes = Sets .newHashSet(Collections2.filter(Gossiper.instance.getLiveTokenOwners(), isLocalDC)); Set<InetAddress> localUnreachableNodes = Sets .newHashSet(Collections2.filter(Gossiper.instance.getUnreachableTokenOwners(), isLocalDC)); Map<Range<Token>, List<InetAddress>> allRanges = StorageService.instance .getRangeToAddressMapInLocalDC(ksName); Multimap<InetAddress, Range<Token>> topo = ArrayListMultimap.create(); Set<Range<Token>> orphanRanges = new HashSet<Range<Token>>(); boolean consistent = true; // get live primary token ranges for (InetAddress node : localLiveNodes) { if (startedShards.contains(node)) { topo.putAll(node, StorageService.instance.getPrimaryRangeForEndpointWithinDC(ksName, node)); } else { localUnreachableNodes.add(node); } } if (logger.isDebugEnabled()) { logger.debug("keyspace={} live nodes={}, primary ranges map = {}", ksName, localLiveNodes, topo); } // pickup random live replica for primary range owned by unreachable or not started nodes. if (localUnreachableNodes.size() > 0) { if (logger.isDebugEnabled()) { logger.debug("unreachableNodes = {} ", localUnreachableNodes); } Random rnd = new Random(); for (InetAddress node : localUnreachableNodes) { for (Range<Token> orphanRange : StorageService.instance.getPrimaryRangeForEndpointWithinDC(ksName, node)) { Set<InetAddress> replicaEndPoints = new HashSet<InetAddress>(); for (Range range : allRanges.keySet()) { if (range.contains(orphanRange)) { replicaEndPoints.addAll(allRanges.get(range)); } } replicaEndPoints.removeAll(localUnreachableNodes); if (replicaEndPoints.size() == 0) { consistent = false; orphanRanges.add(orphanRange); logger.warn("Inconsistent search for keyspace {}, no alive node having range {}", ksName, orphanRange); } else { InetAddress[] replicas = replicaEndPoints.toArray(new InetAddress[replicaEndPoints.size()]); InetAddress liveReplica = replicas[rnd.nextInt(replicas.length)]; topo.put(liveReplica, orphanRange); logger.debug("orphanRanges {} available on = {} ", orphanRanges, liveReplica); } } } } if (logger.isDebugEnabled()) { logger.debug("topology for keyspace {} = {}, consistent={} unreachableNodes={} orphanRanges={}", ksName, topo.asMap(), consistent, localUnreachableNodes, orphanRanges); } return new AbstractSearchStrategy.Result(topo.asMap(), orphanRanges, localUnreachableNodes, allRanges); }
From source file:com.bigdata.dastor.service.StorageService.java
/** * Called when an endPoint is removed from the ring without proper * STATE_LEAVING -> STATE_LEFT sequence. This function checks * whether this node becomes responsible for new ranges as a * consequence and streams data if needed. * * This is rather ineffective, but it does not matter so much * since this is called very seldom/*from w ww . j ava2 s. c o m*/ * * @param endPoint node that has left */ private void restoreReplicaCount(InetAddress endPoint) { InetAddress myAddress = FBUtilities.getLocalAddress(); for (String table : DatabaseDescriptor.getNonSystemTables()) { // get all ranges that change ownership (that is, a node needs // to take responsibility for new range) Multimap<Range, InetAddress> changedRanges = getChangedRangesForLeaving(table, endPoint); // check if any of these ranges are coming our way Set<Range> myNewRanges = new HashSet<Range>(); for (Map.Entry<Range, InetAddress> entry : changedRanges.entries()) { if (entry.getValue().equals(myAddress)) myNewRanges.add(entry.getKey()); } if (!myNewRanges.isEmpty()) { if (logger_.isDebugEnabled()) logger_.debug( endPoint + " was removed, my added ranges: " + StringUtils.join(myNewRanges, ", ")); Multimap<Range, InetAddress> rangeAddresses = getReplicationStrategy(table) .getRangeAddresses(tokenMetadata_, table); Multimap<InetAddress, Range> sourceRanges = HashMultimap.create(); IFailureDetector failureDetector = FailureDetector.instance; // find alive sources for our new ranges for (Range myNewRange : myNewRanges) { List<InetAddress> sources = DatabaseDescriptor.getEndPointSnitch(table) .getSortedListByProximity(myAddress, rangeAddresses.get(myNewRange)); assert (!sources.contains(myAddress)); for (InetAddress source : sources) { if (source.equals(endPoint)) continue; if (failureDetector.isAlive(source)) { sourceRanges.put(source, myNewRange); break; } } } // Finally we have a list of addresses and ranges to // stream. Proceed to stream for (Map.Entry<InetAddress, Collection<Range>> entry : sourceRanges.asMap().entrySet()) { if (logger_.isDebugEnabled()) logger_.debug("Requesting from " + entry.getKey() + " ranges " + StringUtils.join(entry.getValue(), ", ")); StreamIn.requestRanges(entry.getKey(), table, entry.getValue()); } } } }
From source file:com.cloudant.sync.datastore.DatastoreImpl.java
/** * Returns the subset of given the document id/revisions that are not stored in the database. * * The input revisions is a map, whose key is document id, and value is a list of revisions. * An example input could be (in json format): * * { "03ee06461a12f3c288bb865b22000170": * [/*from w w w .ja va 2s .co m*/ * "1-b2e54331db828310f3c772d6e042ac9c", * "2-3a24009a9525bde9e4bfa8a99046b00d" * ], * "82e04f650661c9bdb88c57e044000a4b": * [ * "3-bb39f8c740c6ffb8614c7031b46ac162" * ] * } * * The output is in same format. * * @see <a href="http://wiki.apache.org/couchdb/HttpPostRevsDiff">HttpPostRevsDiff documentation</a> * @param revisions a Multimap of document id revision id * @return a Map of document id collection of revision id: the subset of given the document * id/revisions that are not stored in the database */ public Map<String, Collection<String>> revsDiff(final Multimap<String, String> revisions) { Preconditions.checkState(this.isOpen(), "Database is closed"); Preconditions.checkNotNull(revisions, "Input revisions must not be null"); try { return queue.submit(new SQLQueueCallable<Map<String, Collection<String>>>() { @Override public Map<String, Collection<String>> call(SQLDatabase db) throws Exception { Multimap<String, String> missingRevs = ArrayListMultimap.create(); // Break the potentially big multimap into small ones so for each map, // a single query can be use to check if the <id, revision> pairs in sqlDb or not List<Multimap<String, String>> batches = multiMapPartitions(revisions, SQLITE_QUERY_PLACEHOLDERS_LIMIT); for (Multimap<String, String> batch : batches) { revsDiffBatch(db, batch); missingRevs.putAll(batch); } return missingRevs.asMap(); } }).get(); } catch (InterruptedException e) { logger.log(Level.SEVERE, "Failed to do revsdiff", e); } catch (ExecutionException e) { logger.log(Level.SEVERE, "Failed to do revsdiff", e); } return null; }
From source file:org.dllearner.reasoning.SPARQLReasoner.java
public Set<OWLProperty> getProperties(boolean inferType) { Set<OWLProperty> properties = new TreeSet<>(); String query = "SELECT DISTINCT ?p ?type WHERE {?s ?p ?o. OPTIONAL{?p a ?type.}}"; ResultSet rs = executeSelectQuery(query); Multimap<String, String> uri2Types = HashMultimap.create(); QuerySolution qs;/*from www . ja v a 2 s. c o m*/ while (rs.hasNext()) { qs = rs.next(); String uri = qs.getResource("p").getURI(); String type = ""; if (qs.getResource("type") != null) { type = qs.getResource("type").getURI(); } uri2Types.put(uri, type); } for (Entry<String, Collection<String>> entry : uri2Types.asMap().entrySet()) { String uri = entry.getKey(); Collection<String> types = entry.getValue(); if (types.contains(OWL.ObjectProperty.getURI()) && !types.contains(OWL.DatatypeProperty.getURI())) { properties.add(df.getOWLObjectProperty(IRI.create(uri))); } else if (!types.contains(OWL.ObjectProperty.getURI()) && types.contains(OWL.DatatypeProperty.getURI())) { properties.add(df.getOWLDataProperty(IRI.create(uri))); } else if (inferType) { //infer the type by values query = "SELECT ?o WHERE {?s <" + uri + "> ?o. } LIMIT 100"; rs = executeSelectQuery(query); boolean op = true; boolean dp = true; RDFNode node; while (rs.hasNext()) { node = rs.next().get("o"); op = node.isResource(); dp = node.isLiteral(); } if (op && !dp) { properties.add(df.getOWLObjectProperty(IRI.create(uri))); } else if (!op && dp) { properties.add(df.getOWLDataProperty(IRI.create(uri))); } else { //not possible to decide } } } return properties; }
From source file:org.dllearner.reasoning.SPARQLReasoner.java
public Set<OWLProperty> getProperties(boolean inferType, String namespace) { Set<OWLProperty> properties = new HashSet<>(); String query = "SELECT DISTINCT ?p ?type WHERE {?s ?p ?o." + (namespace != null ? ("FILTER(REGEX(?p,'^" + namespace + "'))") : "") + "OPTIONAL{?p a ?type.}}"; ResultSet rs = executeSelectQuery(query); Multimap<String, String> uri2Types = HashMultimap.create(); QuerySolution qs;/*from w w w.ja va2 s .co m*/ while (rs.hasNext()) { qs = rs.next(); String uri = qs.getResource("p").getURI(); String type = ""; if (qs.getResource("type") != null) { type = qs.getResource("type").getURI(); } uri2Types.put(uri, type); } for (Entry<String, Collection<String>> entry : uri2Types.asMap().entrySet()) { String uri = entry.getKey(); Collection<String> types = entry.getValue(); if (types.contains(OWL.ObjectProperty.getURI()) && !types.contains(OWL.DatatypeProperty.getURI())) { properties.add(df.getOWLObjectProperty(IRI.create(uri))); } else if (!types.contains(OWL.ObjectProperty.getURI()) && types.contains(OWL.DatatypeProperty.getURI())) { properties.add(df.getOWLDataProperty(IRI.create(uri))); } else if (inferType) { //infer the type by values query = "SELECT ?o WHERE {?s <" + uri + "> ?o. } LIMIT 100"; rs = executeSelectQuery(query); boolean op = true; boolean dp = true; RDFNode node; while (rs.hasNext()) { node = rs.next().get("o"); op = node.isResource(); dp = node.isLiteral(); } if (op && !dp) { properties.add(df.getOWLObjectProperty(IRI.create(uri))); } else if (!op && dp) { properties.add(df.getOWLDataProperty(IRI.create(uri))); } else { //not possible to decide } } } return properties; }
From source file:de.hzi.helmholtz.Compare.PathwayComparisonUsingModules.java
public String getmax(Collection<Multimap<Double, String>> collection, Multimap<Integer, Multimap<Double, String>> reverse) { String geneToBeCompared = ""; String maxScoringGene = ""; // can be one gene or combination of many genes double max = 0; boolean isGenePresent = true; if (collection.size() > 0) { while (isGenePresent) { for (Multimap<Double, String> gene : collection) { try { max = gene.keySet().iterator().next(); finalscore = max;/*w w w. j a va 2s . c o m*/ for (String geneCombinationWithBestScore : gene.get(max)) { if (geneCombinationWithBestScore.contains("+")) { String[] individualGenes = geneCombinationWithBestScore.split("\\+"); for (String individualGene : individualGenes) { if (reverse.containsKey(Integer.parseInt(individualGene))) { if (geneToBeCompared.equals("")) { geneToBeCompared = individualGene; } else { geneToBeCompared += "+" + individualGene; } } } } else { if (reverse.containsKey(Integer.parseInt(geneCombinationWithBestScore))) { if (geneToBeCompared.equals("")) { geneToBeCompared = geneCombinationWithBestScore; } else { geneToBeCompared += ";" + geneCombinationWithBestScore; } } } } if (geneToBeCompared.trim().equals("")) { gene.asMap().remove(max); } } catch (Exception ds) { ds.printStackTrace(); } } if (!geneToBeCompared.trim().equals("")) { isGenePresent = false; } double roundOff = (double) Math.round(max * 100) / 100; // set score precision to two decimal places maxScoringGene = roundOff + "=" + geneToBeCompared; } } return maxScoringGene; }