Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1.     private void refreshLiveEtaDataCache() {
  2.         Map<Date, List<JourneyLiveEta>> oldDateJourneyLiveEtaMap = cacheInMemoryHolder.getCachedJourneyLiveEtas();
  3.         Map<Date, List<JourneyLiveEta>> dateJourneyLiveEtaMap = null;
  4.  
  5.         // declare as null - will be set later.
  6.         List<LiveEta> liveEtas = null;
  7.  
  8.         // Get all dates from the database.
  9.         List<Date> dates = journeyLiveEtaManager.findAllDates();
  10.         if (dates.size() > 0) {
  11.  
  12.             // Define what dates are needed.
  13.             Date startDate = dates.get(Math.max(0, dates.size() - numberOfLastJourneyDatesToReload));
  14.             Date endDate = dates.get(dates.size() - 1);
  15.  
  16.             LOG.info("Loading liveEtas...");
  17.             liveEtas = loadLiveEtasBasedOnDateConditions(startDate, endDate);
  18.             LOG.info("Loading liveEtas... finished. liveEtas.size={}", liveEtas.size());
  19.  
  20.             // filter present.
  21.             if (dataAlreadyCached(oldDateJourneyLiveEtaMap)) {
  22.                 dateJourneyLiveEtaMap = oldDateJourneyLiveEtaMap.entrySet()
  23.                         .stream()
  24.                         .filter(cacheDateKeyIsBefore(startDate))       
  25.                         .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
  26.  
  27.                 // update old map data with reloaded journeys
  28.                 dateJourneyLiveEtaMap.putAll(JourneyLiveEtaUtils.groupLiveEtaByJourneyAndDate(liveEtas));
  29.             } else {
  30.                 // initial caching
  31.                 dateJourneyLiveEtaMap = JourneyLiveEtaUtils.groupLiveEtaByJourneyAndDate(liveEtas);
  32.             }
  33.         } else {
  34.             dateJourneyLiveEtaMap = new HashMap<>();
  35.         }
  36.  
  37.         // map liveEta's to their bus stops.
  38.         Map<String, List<LiveEta>> tmpCacheLiveEtasByBusStops = mapLiveEtasToBusStopIds(dateJourneyLiveEtaMap);
  39.         LOG.info("cacheLiveEtasByBusStops.size={}", tmpCacheLiveEtasByBusStops.size());
  40.  
  41.         // cache/re-cache
  42.         cacheInMemoryHolder.setCachedLiveEtasByBusStops(tmpCacheLiveEtasByBusStops);
  43.         cacheInMemoryHolder.setCachedJourneyLiveEtas(dateJourneyLiveEtaMap);
  44.     }
  45.  
  46.     /**
  47.      * Load liveEtas checking what the start / end dates. If the start date =
  48.      * end date then it will findByDate enabled index lookup on date.
  49.      *
  50.      * @param startDate
  51.      * @param endDate
  52.      * @return
  53.      */
  54.     private List<LiveEta> loadLiveEtasBasedOnDateConditions(final Date startDate, final Date endDate) {
  55.         List<LiveEta> liveEtas;
  56.         if (startDate.equals(endDate)) {
  57.             // load by single date to enable index lookup.
  58.             liveEtas = liveEtaManager.findByDate(startDate);
  59.         } else {
  60.             liveEtas = liveEtaManager.findByDateRange(startDate, endDate);
  61.         }
  62.         return liveEtas;
  63.     }
  64.  
  65.     /**
  66.      * @param dateJourneyLiveEtaMap
  67.      * @return
  68.      */
  69.     private Map<String, List<LiveEta>>
  70.             mapLiveEtasToBusStopIds(final Map<Date, List<JourneyLiveEta>> dateJourneyLiveEtaMap) {
  71.         return dateJourneyLiveEtaMap.values()
  72.                 .stream()
  73.                 .flatMap(ds -> ds.stream())
  74.                 .flatMap(jle -> jle.getStopLiveEta()
  75.                         .stream())
  76.                 .collect(Collectors.groupingBy(le -> le.getBusStopId()));
  77.     }
  78.  
  79.     /**
  80.      * Predicate to check if dateKeyIsBefore startKey. To filter from cache.
  81.      *
  82.      * @param startDate
  83.      * @return
  84.      */
  85.     private Predicate<? super Entry<Date, List<JourneyLiveEta>>> cacheDateKeyIsBefore(final Date startDate) {
  86.         return e -> e.getKey()
  87.                 .before(startDate);
  88.     }
  89.  
  90.     /**
  91.      * @param oldDateJourneyLiveEtaMap
  92.      * @return
  93.      */
  94.     private boolean dataAlreadyCached(final Map<Date, List<JourneyLiveEta>> oldDateJourneyLiveEtaMap) {
  95.         return oldDateJourneyLiveEtaMap != null && oldDateJourneyLiveEtaMap.size() > 0;
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement