Search in sources :

Example 11 with MemoryFileSummary

use of de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary 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();
                }
            };
        }
    };
}
Also used : MemoryFileSummary(de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary) ACloseableIterator(de.invesdwin.util.collections.iterable.ACloseableIterator) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) TextDescription(de.invesdwin.util.lang.description.TextDescription) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) RangeTableRow(ezdb.table.RangeTableRow) FDate(de.invesdwin.util.time.date.FDate) ASkippingIterator(de.invesdwin.util.collections.iterable.skip.ASkippingIterator)

Aggregations

MemoryFileSummary (de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary)11 FDate (de.invesdwin.util.time.date.FDate)7 ISkipFileFunction (de.invesdwin.context.persistence.timeseriesdb.storage.ISkipFileFunction)3 SingleValue (de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue)3 MutableReference (de.invesdwin.util.concurrent.reference.MutableReference)3 FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)3 NoSuchElementException (java.util.NoSuchElementException)3 MutableInt (org.apache.commons.lang3.mutable.MutableInt)3 ACloseableIterator (de.invesdwin.util.collections.iterable.ACloseableIterator)2 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)2 ICloseableIterator (de.invesdwin.util.collections.iterable.ICloseableIterator)2 ASkippingIterator (de.invesdwin.util.collections.iterable.skip.ASkippingIterator)2 TextDescription (de.invesdwin.util.lang.description.TextDescription)2 RangeTableRow (ezdb.table.RangeTableRow)2 MemoryMappedFile (de.invesdwin.util.streams.buffer.MemoryMappedFile)1 TimeRange (de.invesdwin.util.time.range.TimeRange)1 File (java.io.File)1 SerializationException (org.apache.commons.lang3.SerializationException)1