Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void refreshLiveEtaDataCache() {
- Map<Date, List<JourneyLiveEta>> oldDateJourneyLiveEtaMap = cacheInMemoryHolder.getCachedJourneyLiveEtas();
- Map<Date, List<JourneyLiveEta>> dateJourneyLiveEtaMap = null;
- // declare as null - will be set later.
- List<LiveEta> liveEtas = null;
- // Get all dates from the database.
- List<Date> dates = journeyLiveEtaManager.findAllDates();
- if (dates.size() > 0) {
- // Define what dates are needed.
- Date startDate = dates.get(Math.max(0, dates.size() - numberOfLastJourneyDatesToReload));
- Date endDate = dates.get(dates.size() - 1);
- LOG.info("Loading liveEtas...");
- liveEtas = loadLiveEtasBasedOnDateConditions(startDate, endDate);
- LOG.info("Loading liveEtas... finished. liveEtas.size={}", liveEtas.size());
- // filter present.
- if (dataAlreadyCached(oldDateJourneyLiveEtaMap)) {
- dateJourneyLiveEtaMap = oldDateJourneyLiveEtaMap.entrySet()
- .stream()
- .filter(cacheDateKeyIsBefore(startDate))
- .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
- // update old map data with reloaded journeys
- dateJourneyLiveEtaMap.putAll(JourneyLiveEtaUtils.groupLiveEtaByJourneyAndDate(liveEtas));
- } else {
- // initial caching
- dateJourneyLiveEtaMap = JourneyLiveEtaUtils.groupLiveEtaByJourneyAndDate(liveEtas);
- }
- } else {
- dateJourneyLiveEtaMap = new HashMap<>();
- }
- // map liveEta's to their bus stops.
- Map<String, List<LiveEta>> tmpCacheLiveEtasByBusStops = mapLiveEtasToBusStopIds(dateJourneyLiveEtaMap);
- LOG.info("cacheLiveEtasByBusStops.size={}", tmpCacheLiveEtasByBusStops.size());
- // cache/re-cache
- cacheInMemoryHolder.setCachedLiveEtasByBusStops(tmpCacheLiveEtasByBusStops);
- cacheInMemoryHolder.setCachedJourneyLiveEtas(dateJourneyLiveEtaMap);
- }
- /**
- * Load liveEtas checking what the start / end dates. If the start date =
- * end date then it will findByDate enabled index lookup on date.
- *
- * @param startDate
- * @param endDate
- * @return
- */
- private List<LiveEta> loadLiveEtasBasedOnDateConditions(final Date startDate, final Date endDate) {
- List<LiveEta> liveEtas;
- if (startDate.equals(endDate)) {
- // load by single date to enable index lookup.
- liveEtas = liveEtaManager.findByDate(startDate);
- } else {
- liveEtas = liveEtaManager.findByDateRange(startDate, endDate);
- }
- return liveEtas;
- }
- /**
- * @param dateJourneyLiveEtaMap
- * @return
- */
- private Map<String, List<LiveEta>>
- mapLiveEtasToBusStopIds(final Map<Date, List<JourneyLiveEta>> dateJourneyLiveEtaMap) {
- return dateJourneyLiveEtaMap.values()
- .stream()
- .flatMap(ds -> ds.stream())
- .flatMap(jle -> jle.getStopLiveEta()
- .stream())
- .collect(Collectors.groupingBy(le -> le.getBusStopId()));
- }
- /**
- * Predicate to check if dateKeyIsBefore startKey. To filter from cache.
- *
- * @param startDate
- * @return
- */
- private Predicate<? super Entry<Date, List<JourneyLiveEta>>> cacheDateKeyIsBefore(final Date startDate) {
- return e -> e.getKey()
- .before(startDate);
- }
- /**
- * @param oldDateJourneyLiveEtaMap
- * @return
- */
- private boolean dataAlreadyCached(final Map<Date, List<JourneyLiveEta>> oldDateJourneyLiveEtaMap) {
- return oldDateJourneyLiveEtaMap != null && oldDateJourneyLiveEtaMap.size() > 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement