use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class ASegmentedTimeSeriesStorageCache method readRangeValues.
public ICloseableIterable<V> readRangeValues(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
final FDate firstAvailableSegmentFrom = getFirstAvailableSegmentFrom(key);
if (firstAvailableSegmentFrom == null) {
return EmptyCloseableIterable.getInstance();
}
final FDate lastAvailableSegmentTo = getLastAvailableSegmentTo(key, to);
if (lastAvailableSegmentTo == null) {
return EmptyCloseableIterable.getInstance();
}
// adjust dates directly to prevent unnecessary segment calculations
final FDate adjFrom = FDates.max(from, firstAvailableSegmentFrom);
final FDate adjTo = FDates.min(to, lastAvailableSegmentTo);
final ICloseableIterable<TimeRange> segments = getSegments(adjFrom, adjTo, lastAvailableSegmentTo);
final ATransformingIterable<TimeRange, ICloseableIterable<V>> segmentQueries = new ATransformingIterable<TimeRange, ICloseableIterable<V>>(segments) {
@Override
protected ICloseableIterable<V> transform(final TimeRange value) {
return new ICloseableIterable<V>() {
@Override
public ICloseableIterator<V> iterator() {
final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, value);
maybeInitSegment(segmentedKey);
final FDate segmentAdjFrom = FDates.max(adjFrom, value.getFrom());
final FDate segmentAdjTo = FDates.min(adjTo, value.getTo());
final Lock compositeReadLock = Locks.newCompositeLock(readLock, segmentedTable.getTableLock(segmentedKey).readLock());
return segmentedTable.getLookupTableCache(segmentedKey).readRangeValues(segmentAdjFrom, segmentAdjTo, compositeReadLock, skipFileFunction);
}
};
}
};
final ICloseableIterable<V> rangeValues = new FlatteningIterable<V>(segmentQueries);
return rangeValues;
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class ASegmentedTimeSeriesStorageCache method readRangeValuesReverse.
public ICloseableIterable<V> readRangeValuesReverse(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
final FDate firstAvailableSegmentFrom = getFirstAvailableSegmentFrom(key);
final FDate lastAvailableSegmentTo = getLastAvailableSegmentTo(key, to);
// adjust dates directly to prevent unnecessary segment calculations
final FDate adjFrom = FDates.min(from, lastAvailableSegmentTo);
final FDate adjTo = FDates.max(to, firstAvailableSegmentFrom);
final ICloseableIterable<TimeRange> filteredSegments = getSegmentsReverse(adjFrom, adjTo, lastAvailableSegmentTo);
final ATransformingIterable<TimeRange, ICloseableIterable<V>> segmentQueries = new ATransformingIterable<TimeRange, ICloseableIterable<V>>(filteredSegments) {
@Override
protected ICloseableIterable<V> transform(final TimeRange value) {
return new ICloseableIterable<V>() {
@Override
public ICloseableIterator<V> iterator() {
final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, value);
maybeInitSegment(segmentedKey);
final FDate segmentAdjFrom = FDates.min(adjFrom, value.getTo());
final FDate segmentAdjTo = FDates.max(adjTo, value.getFrom());
final Lock compositeReadLock = Locks.newCompositeLock(readLock, segmentedTable.getTableLock(segmentedKey).readLock());
return segmentedTable.getLookupTableCache(segmentedKey).readRangeValuesReverse(segmentAdjFrom, segmentAdjTo, compositeReadLock, skipFileFunction);
}
};
}
};
final ICloseableIterable<V> rangeValues = new FlatteningIterable<V>(segmentQueries);
return rangeValues;
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class TimeSeriesStorageCache method readRangeFilesReverse.
protected ICloseableIterable<MemoryFileSummary> readRangeFilesReverse(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
return new ICloseableIterable<MemoryFileSummary>() {
@Override
public ICloseableIterator<MemoryFileSummary> iterator() {
final FDate usedFrom;
if (from == null) {
final V lastValue = getLastValue();
if (lastValue == null) {
return EmptyCloseableIterator.getInstance();
}
usedFrom = extractEndTime.apply(lastValue);
} else {
usedFrom = from;
}
return new ACloseableIterator<MemoryFileSummary>(new TextDescription("%s[%s]: readRangeFilesReverse(%s, %s)", TimeSeriesStorageCache.class.getSimpleName(), hashKey, from, to)) {
// use latest time available even if delegate iterator has no values
private RangeTableRow<String, FDate, MemoryFileSummary> latestLastTime = fileLookupTable_latestRangeKeyCache.get(usedFrom);
// add 1 ms to not collide with firstTime
private final ICloseableIterator<RangeTableRow<String, FDate, MemoryFileSummary>> delegate;
{
if (latestLastTime == null) {
delegate = EmptyCloseableIterator.getInstance();
} else {
delegate = getRangeKeysReverse(hashKey, latestLastTime.getRangeKey().addMilliseconds(-1), to);
}
}
@Override
protected boolean innerHasNext() {
return latestLastTime != null || delegate.hasNext();
}
private ICloseableIterator<RangeTableRow<String, FDate, MemoryFileSummary>> getRangeKeysReverse(final String hashKey, final FDate from, final FDate to) {
readLock.lock();
try {
final ICloseableIterator<RangeTableRow<String, FDate, MemoryFileSummary>> range = getAllRangeKeys(readLock).reverseIterator();
final GetRangeKeysReverseIterator rangeFiltered = new GetRangeKeysReverseIterator(range, from, to);
if (skipFileFunction != null) {
return new ASkippingIterator<RangeTableRow<String, FDate, MemoryFileSummary>>(rangeFiltered) {
@Override
protected boolean skip(final RangeTableRow<String, FDate, MemoryFileSummary> element) {
if (!rangeFiltered.hasNext()) {
/*
* cannot optimize this further for multiple segments because we don't know
* if a segment further back might be empty or not and thus the last segment
* of interest might have been the previous one from which we skipped the
* last file falsely
*/
return false;
}
return skipFileFunction.skipFile(element.getValue());
}
};
} else {
return rangeFiltered;
}
} finally {
readLock.unlock();
}
}
@Override
protected MemoryFileSummary innerNext() {
final MemoryFileSummary summary;
if (latestLastTime != null) {
summary = latestLastTime.getValue();
latestLastTime = null;
} else {
summary = delegate.next().getValue();
}
return summary;
}
@Override
protected void innerClose() {
delegate.close();
}
};
}
};
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class FileBufferCache method resultCache_load.
private static ArrayFileBufferCacheResult resultCache_load(final ResultCacheKey key) throws Exception {
final ICloseableIterable values = key.getSource().getSource();
key.setSource(null);
final ArrayList list = LIST_POOL.borrowObject();
try (ICloseableIterator it = values.iterator()) {
while (true) {
list.add(it.next());
}
} catch (final NoSuchElementException e) {
// end reached
}
return new ArrayFileBufferCacheResult(list);
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class ASegmentedTimeSeriesDBWithNoCacheAndNoQueryCacheTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
final AHistoricalCache<TimeRange> segmentFinder = new AHistoricalCache<TimeRange>() {
private final PeriodicalSegmentFinder calculation = PeriodicalSegmentFinder.newInstance(new Duration(2, FTimeUnit.YEARS));
@Override
protected Integer getInitialMaximumSize() {
return 1000;
}
@Override
protected FDate innerExtractKey(final TimeRange value) {
return value.getFrom();
}
@Override
protected IEvaluateGenericFDate<TimeRange> newLoadValue() {
return this::loadValue;
}
private synchronized TimeRange loadValue(final IFDateProvider pKey) {
final FDate key = pKey.asFDate();
final TimeRange value = calculation.getSegment(key);
final TimeRange upperTimeRange = new TimeRange(value.getFrom().addYears(1), value.getTo().addYears(1));
if (upperTimeRange.containsInclusive(key)) {
return upperTimeRange;
} else {
return new TimeRange(value.getFrom().addYears(-1), value.getTo().addYears(-1));
}
}
@Override
protected FDate innerCalculateNextKey(final FDate key) {
return query().getValue(key).getTo().addMilliseconds(1);
}
@Override
protected FDate innerCalculatePreviousKey(final FDate key) {
return query().getValue(key).getFrom().addMilliseconds(-1);
}
@Override
public void preloadData(final ExecutorService executor) {
// noop
}
};
table = new ASegmentedTimeSeriesDB<String, FDate>(getClass().getSimpleName()) {
@Override
public AHistoricalCache<TimeRange> getSegmentFinder(final String key) {
return segmentFinder;
}
@Override
protected ISerde<FDate> newValueSerde() {
return new TypeDelegateSerde<FDate>(FDate.class);
}
@Override
protected Integer newValueFixedLength() {
return null;
}
@Override
protected String innerHashKeyToString(final String key) {
return key;
}
@Override
protected File getBaseDirectory() {
return ContextProperties.TEMP_DIRECTORY;
}
@Override
protected ICloseableIterable<? extends FDate> downloadSegmentElements(final SegmentedKey<String> segmentedKey) {
return new ASkippingIterable<FDate>(WrapperCloseableIterable.maybeWrap(entities)) {
private final FDate from = segmentedKey.getSegment().getFrom();
private final FDate to = segmentedKey.getSegment().getTo();
@Override
protected boolean skip(final FDate element) {
return element.isBefore(from) || element.isAfter(to);
}
};
}
@Override
protected FDate extractEndTime(final FDate value) {
return value;
}
@Override
public FDate getFirstAvailableHistoricalSegmentFrom(final String key) {
if (entities.isEmpty()) {
return null;
}
return segmentFinder.query().getValue(entities.get(0)).getFrom();
}
@Override
public FDate getLastAvailableHistoricalSegmentTo(final String key, final FDate updateTo) {
if (entities.isEmpty()) {
return null;
}
return segmentFinder.query().getValue(entities.get(entities.size() - 1)).getTo();
}
@Override
protected String getElementsName() {
return "values";
}
};
}
Aggregations