@Override public void put(final Reservation reservation) { log.trace("ReservationManagerImpl.put({})", reservation); checkRunning(); reservationsCacheLock.lock(); try { CacheItem<Reservation> item = new CacheItem<Reservation>(stopwatchProvider.get(), reservation); reservationsBySrk.put(reservation.getSecretReservationKeys(), item); for (NodeUrn nodeUrn : reservation.getNodeUrns()) { List<CacheItem<Reservation>> entry = reservationsByNodeUrn.get(nodeUrn); if (entry == null) { entry = Lists.newArrayList(); reservationsByNodeUrn.put(nodeUrn, entry); } entry.add(item); } } finally { reservationsCacheLock.unlock(); } }
@Override public void remove(Reservation reservation) { log.trace("ReservationCacheImpl.remove({})", reservation); checkRunning(); reservationsCacheLock.lock(); try { int removedNodeCacheEntries = 0; for (Iterator<Map.Entry<NodeUrn, List<CacheItem<Reservation>>>> cacheIterator = reservationsByNodeUrn.entrySet().iterator(); cacheIterator.hasNext(); ) { final Map.Entry<NodeUrn, List<CacheItem<Reservation>>> entry = cacheIterator.next(); for (Iterator<CacheItem<Reservation>> itemIt = entry.getValue().iterator(); itemIt.hasNext(); ) { final CacheItem<Reservation> item = itemIt.next(); final Reservation res = item.get(); if (res == reservation) { itemIt.remove(); removedNodeCacheEntries++; } } if (entry.getValue().isEmpty()) { cacheIterator.remove(); } } log.trace("ReservationCacheImpl removed {} node cache entries", removedNodeCacheEntries); reservationsBySrk.remove(reservation.getSecretReservationKeys()); } finally { reservationsCacheLock.unlock(); } }