/** * Writes the oldest data to the storage. */ private void writeOldest() { IAggregatedData<E> oldest = queue.poll(); E data = oldest.getData(); map.remove(getCacheHash(data, data.getTimeStamp().getTime())); elementCount.decrementAndGet(); passToStorageWriter(data); }
/** * {@inheritDoc} */ @Override public Collection<Future<Void>> flush() { Collection<Future<Void>> futures = new ArrayList<>(); IAggregatedData<E> oldest = queue.poll(); while (null != oldest) { E data = oldest.getData(); map.remove(getCacheHash(data, data.getTimeStamp().getTime())); elementCount.decrementAndGet(); Future<Void> future = passToStorageWriter(data); CollectionUtils.addIgnoreNull(futures, future); oldest = queue.poll(); } return futures; }
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") protected Collection<Future<Void>> processData(DefaultData defaultData) { E timerData = (E) defaultData; long alteredTimestamp = getAlteredTimestamp(timerData); int cacheHash = getCacheHash(timerData, alteredTimestamp); IAggregatedData<E> aggData = map.get(cacheHash); if (null == aggData) { aggData = clone(timerData, alteredTimestamp); IAggregatedData<E> insertedData = map.putIfAbsent(cacheHash, aggData); // if put happened null will be returned if (null == insertedData) { queue.add(aggData); int count = elementCount.incrementAndGet(); if (maxElements < count) { this.writeOldest(); } } else { aggData = insertedData; } } dataAggregator.aggregate(aggData, timerData); return Collections.emptyList(); }