List of usage examples for com.google.common.collect SetMultimap keys
Multiset<K> keys();
From source file:org.xbib.tools.merge.zdb.licenseinfo.timeline.WithHoldingsAndLicensesInTimelinePipeline.java
private void indexManifestation(Manifestation m, Set<String> visited) throws IOException { String id = m.externalID();// w ww. ja v a2 s .c o m // protection against recursion (should not happen) if (visited.contains(id)) { return; } visited.add(id); // make sure at other threads that we do never index a manifestation twice if (service.indexed().contains(id)) { return; } service.indexed().add(id); // index this manifestation String tag = service.settings().get("tag"); XContentBuilder builder = jsonBuilder(); String docid = m.build(builder, tag, null); if (docid != null) { service.ingest().index(manifestationsIndex, manifestationsIndexType, docid, builder.string()); } // volumes by date and the services for them Integer volumeHoldingsCount = 0; if (!m.getVolumesByDate().isEmpty()) { SetMultimap<Integer, Holding> volumesByDate; synchronized (m.getVolumesByDate()) { volumesByDate = ImmutableSetMultimap.copyOf(m.getVolumesByDate()); } for (Integer date : volumesByDate.keySet()) { String identifier = (tag != null ? tag + "." : "") + m.externalID() + (date != -1 ? "." + date : ""); Set<Holding> holdings = volumesByDate.get(date); if (holdings != null && !holdings.isEmpty()) { builder = jsonBuilder(); docid = m.buildHoldingsByDate(builder, tag, m.externalID(), date, holdings); if (docid != null) { service.ingest().index(volumesIndex, volumesIndexType, identifier, builder.string()); serviceMetric.mark(1); if (logger.isDebugEnabled()) { logger.debug("indexed volume identifier {}, date {}", docid, date); } volumeHoldingsCount++; } } } } // holdings (list of institutions) if (!m.getVolumesByHolder().isEmpty()) { SetMultimap<String, Holding> holdings; synchronized (m.getVolumesByHolder()) { holdings = ImmutableSetMultimap.copyOf(m.getVolumesByHolder()); } builder = jsonBuilder(); builder.startObject().startArray("holdings"); for (String holder : holdings.keySet()) { docid = m.buildHoldingsByISIL(builder, tag, m.externalID(), holder, holdings.get(holder)); } builder.endArray().endObject(); if (docid != null) { service.ingest().index(holdingsIndex, holdingsIndexType, docid, builder.string()); } volumeHoldingsCount++; serviceMetric.mark(holdings.size()); if (logger.isDebugEnabled()) { logger.debug("indexed {} holdings for {}", holdings.size(), docid); } if (volumeHoldingsCount == 0) { logger.warn("no volumes/holdings indexed for {}", m.externalID()); } } // index related manifestations if (!m.getRelatedManifestations().isEmpty()) { SetMultimap<String, Manifestation> rels; synchronized (m.getRelatedManifestations()) { rels = ImmutableSetMultimap.copyOf(m.getRelatedManifestations()); } for (String rel : rels.keys()) { for (Manifestation mm : rels.get(rel)) { indexManifestation(mm, visited); } } } }
From source file:org.xbib.tools.merge.zdb.licenseinfo.WithHoldingsAndLicensesPipeline.java
private void indexManifestation(Manifestation m, Set<String> visited) throws IOException { String id = m.externalID();//from w ww .ja v a2 s. co m // protection against recursion (should not happen) if (visited.contains(id)) { return; } visited.add(id); // make sure at other threads that we do never index a manifestation twice if (service.indexed().contains(id)) { return; } service.indexed().add(id); String tag = service.settings().get("tag"); // first, index related volumes (conference/proceedings/abstracts/...) List<String> vids = newArrayList(); if (!m.getVolumes().isEmpty()) { final ImmutableList<Volume> volumes; synchronized (m.getVolumes()) { volumes = ImmutableList.copyOf(m.getVolumes()); } for (Volume volume : volumes) { XContentBuilder builder = jsonBuilder(); String vid = volume.build(builder, tag, null); service.ingest().index(manifestationsIndex, manifestationsIndexType, vid, builder.string()); vids.add(vid); for (VolumeHolding volumeHolding : volume.getHoldings()) { builder = jsonBuilder(); vid = volumeHolding.build(builder, tag); // by holding service.ingest().index(holdingsIndex, holdingsIndexType, vid, builder.string()); // extra entry by date service.ingest().index(dateHoldingsIndex, dateHoldingsIndexType, vid + "." + volumeHolding.dates().get(0), builder.string()); } int n = 1 + 2 * volume.getHoldings().size(); service.indexMetric().mark(n); } int n = m.getVolumes().size(); service.indexMetric().mark(n); } m.addVolumeIDs(vids); // index this manifestation XContentBuilder builder = jsonBuilder(); String docid = m.build(builder, tag, null); service.ingest().index(manifestationsIndex, manifestationsIndexType, docid, builder.string()); service.indexMetric().mark(1); // holdings by date and the services for them if (!m.getVolumesByDate().isEmpty()) { SetMultimap<Integer, Holding> volumesByDate; synchronized (m.getVolumesByDate()) { volumesByDate = ImmutableSetMultimap.copyOf(m.getVolumesByDate()); } for (Integer date : volumesByDate.keySet()) { String identifier = (tag != null ? tag + "." : "") + m.externalID() + (date != -1 ? "." + date : ""); Set<Holding> holdings = volumesByDate.get(date); if (holdings != null && !holdings.isEmpty()) { builder = jsonBuilder(); docid = m.buildHoldingsByDate(builder, tag, m.externalID(), date, holdings); service.ingest().index(dateHoldingsIndex, dateHoldingsIndexType, identifier, builder.string()); service.indexMetric().mark(1); logger.debug("indexed volume {} date {}", docid, date); } } } // holdings (list of institutions) if (!m.getVolumesByHolder().isEmpty()) { final SetMultimap<String, Holding> holdings; synchronized (m.getVolumesByHolder()) { holdings = ImmutableSetMultimap.copyOf(m.getVolumesByHolder()); } builder = jsonBuilder(); builder.startObject().startArray("holdings"); for (String holder : holdings.keySet()) { docid = m.buildHoldingsByISIL(builder, tag, m.externalID(), holder, holdings.get(holder)); } builder.endArray().endObject(); service.ingest().index(holdingsIndex, holdingsIndexType, docid, builder.string()); service.indexMetric().mark(1); logger.debug("indexed {} holdings for {}", holdings.size(), docid); } // index related manifestations if (!m.getRelatedManifestations().isEmpty()) { SetMultimap<String, Manifestation> rels; synchronized (m.getRelatedManifestations()) { rels = ImmutableSetMultimap.copyOf(m.getRelatedManifestations()); } for (String rel : rels.keys()) { for (Manifestation mm : rels.get(rel)) { indexManifestation(mm, visited); } } } }
From source file:org.apache.brooklyn.feed.jmx.JmxFeed.java
@Override protected void preStart() { /*//from ww w.ja v a 2s . c om * All actions on the JmxHelper are done async (through the poller's threading) so we don't * block on start/rebind if the entity is unreachable * (without this we get a 120s pause in JmxHelper.connect restarting) */ final SetMultimap<NotificationFilter, JmxNotificationSubscriptionConfig<?>> notificationSubscriptions = getConfig( NOTIFICATION_SUBSCRIPTIONS); final SetMultimap<List<?>, JmxOperationPollConfig<?>> operationPolls = getConfig(OPERATION_POLLS); final SetMultimap<String, JmxAttributePollConfig<?>> attributePolls = getConfig(ATTRIBUTE_POLLS); getPoller().submit(new Callable<Void>() { public Void call() { getHelper().connect(getConfig(JMX_CONNECTION_TIMEOUT)); return null; } @Override public String toString() { return "Connect JMX " + getHelper().getUrl(); } }); for (final NotificationFilter filter : notificationSubscriptions.keySet()) { getPoller().submit(new Callable<Void>() { public Void call() { // TODO Could config.getObjectName have wildcards? Is this code safe? Set<JmxNotificationSubscriptionConfig<?>> configs = notificationSubscriptions.get(filter); NotificationListener listener = registerNotificationListener(configs); ObjectName objectName = Iterables.get(configs, 0).getObjectName(); notificationListeners.put(objectName, listener); return null; } @Override public String toString() { return "Register JMX notifications: " + notificationSubscriptions.get(filter); } }); } // Setup polling of sensors for (final String jmxAttributeName : attributePolls.keys()) { registerAttributePoller(attributePolls.get(jmxAttributeName)); } // Setup polling of operations for (final List<?> operationIdentifier : operationPolls.keys()) { registerOperationPoller(operationPolls.get(operationIdentifier)); } }
From source file:brooklyn.event.feed.jmx.JmxFeed.java
@Override protected void preStart() { /*/*from w w w . j a v a2s . com*/ * All actions on the JmxHelper are done async (through the poller's threading) so we don't * block on start for a long time (e.g. if the entity is not contactable and doing a rebind * on restart of brooklyn). Without that, one gets a 120 second pause with it stuck in a * stack trace like: * * at brooklyn.event.feed.jmx.JmxHelper.sleep(JmxHelper.java:640) * at brooklyn.event.feed.jmx.JmxHelper.connect(JmxHelper.java:320) * at brooklyn.event.feed.jmx.JmxFeed.preStart(JmxFeed.java:172) * at brooklyn.event.feed.AbstractFeed.start(AbstractFeed.java:68) * at brooklyn.event.feed.jmx.JmxFeed$Builder.build(JmxFeed.java:119) * at brooklyn.entity.java.JavaAppUtils.connectMXBeanSensors(JavaAppUtils.java:109) * at brooklyn.entity.java.VanillaJavaApp.connectSensors(VanillaJavaApp.java:97) * at brooklyn.entity.basic.SoftwareProcessImpl.callRebindHooks(SoftwareProcessImpl.java:189) * at brooklyn.entity.basic.SoftwareProcessImpl.rebind(SoftwareProcessImpl.java:235) * ... * at brooklyn.entity.rebind.RebindManagerImpl.rebind(RebindManagerImpl.java:184) */ final SetMultimap<NotificationFilter, JmxNotificationSubscriptionConfig<?>> notificationSubscriptions = getConfig( NOTIFICATION_SUBSCRIPTIONS); final SetMultimap<List<?>, JmxOperationPollConfig<?>> operationPolls = getConfig(OPERATION_POLLS); final SetMultimap<String, JmxAttributePollConfig<?>> attributePolls = getConfig(ATTRIBUTE_POLLS); getPoller().submit(new Callable<Void>() { public Void call() { getHelper().connect(getConfig(JMX_CONNECTION_TIMEOUT)); return null; } @Override public String toString() { return "Connect JMX " + getHelper().getUrl(); } }); for (final NotificationFilter filter : notificationSubscriptions.keySet()) { getPoller().submit(new Callable<Void>() { public Void call() { // TODO Could config.getObjectName have wildcards? Is this code safe? Set<JmxNotificationSubscriptionConfig<?>> configs = notificationSubscriptions.get(filter); NotificationListener listener = registerNotificationListener(configs); ObjectName objectName = Iterables.get(configs, 0).getObjectName(); notificationListeners.put(objectName, listener); return null; } @Override public String toString() { return "Register JMX notifications: " + notificationSubscriptions.get(filter); } }); } // Setup polling of sensors for (final String jmxAttributeName : attributePolls.keys()) { registerAttributePoller(attributePolls.get(jmxAttributeName)); } // Setup polling of operations for (final List<?> operationIdentifier : operationPolls.keys()) { registerOperationPoller(operationPolls.get(operationIdentifier)); } }
From source file:org.eclipse.gef4.mvc.policies.CreationPolicy.java
/** * Creates an {@link IContentPart} for the given content {@link Object} and * establishes parent and anchored relationships for the newly created part. * Besides, operations are created for the establishment of the parent and * anchored relationships within the content model. These operations are * part of the operation returned by {@link #commit()}. * * @param content//w w w. jav a 2s . co m * The content {@link Object} to be created. * @param parent * The {@link IContentPart} where the <i>content</i> is added as * a child. * @param index * The index for the new element. * @param anchoreds * The {@link IContentPart} whose content should be attached to * the new content under the given roles. * @return The {@link IContentPart} controlling the newly created content. */ @SuppressWarnings("serial") public IContentPart<VR, ? extends VR> create(Object content, IContentPart<VR, ? extends VR> parent, int index, SetMultimap<IContentPart<VR, ? extends VR>, String> anchoreds) { checkInitialized(); if (content == null) { throw new IllegalArgumentException("The given content may not be null."); } if (parent == null) { throw new IllegalArgumentException("The given parent may not be null."); } if (anchoreds == null) { throw new IllegalArgumentException("The given anchored parts may not be null"); } // create content part beforehand IContentPart<VR, ? extends VR> contentPart = contentPartFactory.createContentPart(content, null, null); // establish relationships to parent and anchored parts contentPart.setContent(content); parent.addChild(contentPart, index); for (Entry<IContentPart<VR, ? extends VR>, String> anchored : anchoreds.entries()) { anchored.getKey().attachToAnchorage(contentPart, anchored.getValue()); } // register the content part, so that the ContentBehavior // synchronization reuses it (when committing the create operation) contentPartPool.add(contentPart); // add to parent via content policy ContentPolicy<VR> parentContentPolicy = parent.getAdapter(new TypeToken<ContentPolicy<VR>>() { }.where(new TypeParameter<VR>() { }, Types.<VR>argumentOf(getHost().getRoot().getViewer().getClass()))); if (parentContentPolicy == null) { throw new IllegalStateException("No ContentPolicy registered for <" + parent + ">."); } parentContentPolicy.init(); parentContentPolicy.addContentChild(content, index); ITransactionalOperation addToParentOperation = parentContentPolicy.commit(); if (addToParentOperation != null) { getCompositeOperation().add(addToParentOperation); } // add anchoreds via content policy for (IContentPart<VR, ? extends VR> anchored : anchoreds.keys()) { ContentPolicy<VR> anchoredPolicy = anchored.getAdapter(new TypeToken<ContentPolicy<VR>>() { }.where(new TypeParameter<VR>() { }, Types.<VR>argumentOf(getHost().getRoot().getViewer().getClass()))); if (anchoredPolicy == null) { throw new IllegalStateException("No ContentPolicy registered for <" + anchored + ">."); } anchoredPolicy.init(); for (String role : anchoreds.get(anchored)) { anchoredPolicy.attachToContentAnchorage(content, role); } ITransactionalOperation attachToAnchorageOperation = anchoredPolicy.commit(); if (attachToAnchorageOperation != null) { getCompositeOperation().add(attachToAnchorageOperation); } } // set as focus part ITransactionalOperation focusOperation = createFocusOperation(contentPart); if (focusOperation != null) { getCompositeOperation().add(focusOperation); } // select the newly created part ITransactionalOperation selectOperation = createSelectOperation(contentPart); if (selectOperation != null) { getCompositeOperation().add(selectOperation); } locallyExecuteOperation(); return contentPart; }
From source file:it.sayservice.platform.smartplanner.cache.annotated.AnnotatedReader.java
public void buildTimetable(String router, String agencyId, boolean setTripsIds, List<AnnotatedTimetable> timetables) throws Exception { AgencyCacheIndex aci = new AgencyCacheIndex(agencyId); aci.setVersion(1);// ww w . j a va 2 s. c o m ObjectMapper mapper = new ObjectMapper(); String cacheDir = System.getenv("OTP_HOME") + System.getProperty("file.separator") + router + System.getProperty("file.separator") + "cache" + System.getProperty("file.separator") + "client"; String agencyDir = cacheDir + System.getProperty("file.separator") + agencyId; File dir = new File(agencyDir); if (!dir.exists()) { dir.mkdir(); } else { for (File f : dir.listFiles()) { f.delete(); } } Map<String, WeekdayFilter> weekdayFilter = handler.readAgencyWeekDay(router, agencyId); Map<String, WeekdayException> weekdayException = handler.readAgencyWeekDayExceptions(router, agencyId); Multimap<String, ExtendedAnnotatedColumn> extendedAnnotatedColumn = createExtendedAnnotatedColumns( timetables); SetMultimap<String, String> tripsSymbolicRouteId = TreeMultimap.create(); List<SymbolicRouteDayInfoHashCalendar> agencySrdihs = Lists.newArrayList(); for (String rId : extendedAnnotatedColumn.keySet()) { System.out.println("Generating " + rId); Map<String, AnnotatedColumn> tripsColumns = Maps.newTreeMap(); Map<String, AnnotatedTimetable> tripsTable = Maps.newTreeMap(); Map<String, AnnotatedTimetable> serviceTable = Maps.newTreeMap(); AnnotatedTimetable baseTable = null; Multimap<String, String> daysTrips = ArrayListMultimap.create(); SetMultimap<String, String> daysServices = TreeMultimap.create(); Map<String, AnnotatedTimetable> daysTable = Maps.newTreeMap(); Map<String, String> daysHashes = Maps.newTreeMap(); Map<String, String> tripsRoutes = Maps.newTreeMap(); for (ExtendedAnnotatedColumn ac : extendedAnnotatedColumn.get(rId)) { baseTable = ac.getSource(); String tripId = agencyId + "_" + ac.getTripId(); tripsSymbolicRouteId.put(tripId, rId); tripsRoutes.put(ac.getTripId(), ac.getRouteId()); tripsColumns.put(tripId, ac); tripsTable.put(tripId, ac.getSource()); String serviceId = agencyId + "_" + ac.getServiceId(); serviceTable.put(serviceId, ac.getSource()); WeekdayFilter filter = weekdayFilter.get(serviceId); if (filter == null) { System.out.println("ServiceId not found: " + serviceId); } Set<String> days = Sets.newHashSet(); DateFormat df = new SimpleDateFormat("yyyyMMdd"); String from = filter.getFromDate(); String to = filter.getToDate(); Calendar fromDate = new GregorianCalendar(); Calendar toDate = new GregorianCalendar(); fromDate.setTime(df.parse(from)); toDate.setTime(df.parse(to)); Calendar date = new GregorianCalendar(); date.setTime(fromDate.getTime()); String prevDay = null; while (df.format(date.getTime()).compareTo(to) <= 0) { String day = df.format(date.getTime()); boolean sameDay = day.equals(prevDay); if (!sameDay) { int dotw = convertDayOfTheWeek(date.get(Calendar.DAY_OF_WEEK)); if (filter.getDays()[dotw]) { days.add(day); } } prevDay = day; date.setTime(new Date(date.getTime().getTime())); date.add(Calendar.DAY_OF_YEAR, 1); } WeekdayException ex = weekdayException.get(serviceId); if (ex != null) { for (String toAdd : ex.getAdded()) { days.add(toAdd); } for (String toRemove : ex.getRemoved()) { days.remove(toRemove); } } ac.setDays(Lists.newArrayList(days)); for (String day : days) { daysTable.put(day, baseTable); daysTrips.put(day, tripId); daysServices.put(day, serviceId); } } Map<String, CacheTable> hashTable = Maps.newTreeMap(); SymbolicRouteDayInfoHashCalendar srdihs = new SymbolicRouteDayInfoHashCalendar(); srdihs.setAgencyId(agencyId); agencySrdihs.add(srdihs); Map<String, String> srdihsCalendar = Maps.newTreeMap(); Map<String, SymbolicRouteDayInfo> srdihsValues = Maps.newTreeMap(); srdihs.setCalendar(srdihsCalendar); srdihs.setValues(srdihsValues); srdihs.setRouteId(rId); for (String day : daysTrips.keySet()) { List<String> dayTrips = Lists.newArrayList(daysTrips.get(day)); List<String> dayServices = Lists.newArrayList(daysServices.get(day)); String hash = getEqString(dayServices, agencyId); if (daysHashes.containsKey(day)) { continue; } daysHashes.put(day, hash); Set<String> dayServicesSet = daysServices.get(day); AnnotatedTimetable reducedTable = reduceAnnotatedTimetable(daysTable.get(day), dayServicesSet, agencyId); CacheTable ct = new CacheTable(); List<String> tripIds = Lists.newArrayList(); List<List<String>> compressedTimes = Lists.newArrayList(); ct.setTimes(compressedTimes); for (String trip : dayTrips) { int index = trip.indexOf("_"); tripIds.add(trip.substring(index + 1)); compressedTimes.add(tripsColumns.get(trip).getTimes()); } if (setTripsIds) { ct.setTripIds(tripIds); } List<String> routesIds = Lists.newArrayList(); for (String ti : tripIds) { routesIds.add(tripsRoutes.get(ti)); } ct.setStops(reducedTable.getStopNames()); ct.setStopsId(reducedTable.getStopIds()); ct.setInvisibles(reducedTable.getInvisibles()); ct.setFrequency(reducedTable.getFrequency()); ct.setLine(reducedTable.getLine()); ct.setRoutesIds(routesIds); ct.setShortDescription(reducedTable.getShortDescription()); ct.setLongDescription(reducedTable.getLongDescription()); ct.setValidity(reducedTable.getValidity()); ct.setSchedule(reducedTable.getSchedule()); ct.compress(); hashTable.put(hash, ct); if (!srdihsValues.containsKey(hash)) { SymbolicRouteDayInfo srdi = new SymbolicRouteDayInfo(reducedTable); StopNames sn = new StopNames(); sn.setRouteId(rId); sn.setIds(reducedTable.getStopIds()); sn.setNames(reducedTable.getStopNames()); srdi.setStopNames(sn); srdi.setTripIds(tripIds); srdihsValues.put(hash, srdi); } srdihsCalendar.put(day, hash); } System.out.println("Writing " + rId); String calendar = agencyDir + System.getProperty("file.separator") + "calendar_" + rId + ".js"; mapper.writeValue(new File(calendar), daysHashes); for (String hash : hashTable.keySet()) { String hashFile = agencyDir + System.getProperty("file.separator") + rId + "_" + hash + ".js"; mapper.writeValue(new File(hashFile), hashTable.get(hash)); CacheIndexEntry cie = new CacheIndexEntry(); cie.setId(rId + "_" + hash); cie.setVersion(1); cie.setStatus(CacheEntryStatus.ADDED); aci.getEntries().put(rId + "_" + hash, cie); } } String indexFile = cacheDir + System.getProperty("file.separator") + agencyId + "_index.txt"; File aciFile = new File(indexFile); if (aciFile.exists()) { AgencyCacheIndex oldAci = mapper.readValue(aciFile, AgencyCacheIndex.class); aci.setVersion(oldAci.getVersion() + 1); } mapper.writeValue(new File(indexFile), aci); String auxDir = cacheDir + System.getProperty("file.separator") + Constants.AUXILIARY_CACHE_DIR; String infoFile = auxDir + System.getProperty("file.separator") + agencyId + "_info.txt"; mapper.writeValue(new File(infoFile), agencySrdihs); String symbolicFile = auxDir + System.getProperty("file.separator") + agencyId + "_symbolic_trips.txt"; Map<String, Collection<String>> map = Maps.newTreeMap(); for (String key : tripsSymbolicRouteId.keys()) { List<String> rids = Lists.newArrayList(); rids.addAll(tripsSymbolicRouteId.get(key)); map.put(key, rids); } mapper.writeValue(new File(symbolicFile), map); }