Search in sources :

Example 1 with ACloseableIterator

use of de.invesdwin.util.collections.iterable.ACloseableIterator in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method readRangeFilesReverse.

protected ICloseableIterable<File> readRangeFilesReverse(final FDate from, final FDate to) {
    final FDate usedFrom;
    if (from == null) {
        final V lastValue = getLastValue();
        if (lastValue == null) {
            return EmptyCloseableIterable.getInstance();
        }
        usedFrom = extractTime.apply(lastValue);
    } else {
        usedFrom = from;
    }
    return new ICloseableIterable<File>() {

        @Override
        public ACloseableIterator<File> iterator() {
            return new ACloseableIterator<File>() {

                // use latest time available even if delegate iterator has no values
                private FDate latestLastTime = fileLookupTable_latestRangeKeyCache.get(usedFrom);

                // add 1 ms to not collide with firstTime
                private final ICloseableIterator<FDate> delegate = getRangeKeysReverse(hashKey, usedFrom.addMilliseconds(-1), to);

                private FDate delegateLastTime = null;

                @Override
                protected boolean innerHasNext() {
                    return latestLastTime != null || delegateLastTime != null || delegate.hasNext();
                }

                private ICloseableIterator<FDate> getRangeKeysReverse(final String hashKey, final FDate from, final FDate to) {
                    final ICloseableIterator<FDate> range = getAllRangeKeysReverse();
                    return new ASkippingIterator<FDate>(range) {

                        @Override
                        protected boolean skip(final FDate element) {
                            if (element.isAfter(from)) {
                                return true;
                            } else if (element.isBefore(to)) {
                                throw new FastNoSuchElementException("getRangeKeysReverse reached end");
                            }
                            return false;
                        }
                    };
                }

                @Override
                protected File innerNext() {
                    final FDate time;
                    if (delegateLastTime != null) {
                        time = delegateLastTime;
                        delegateLastTime = null;
                    } else if (latestLastTime != null) {
                        time = latestLastTime;
                        latestLastTime = null;
                        if (delegate.hasNext()) {
                            // prevent duplicate first times
                            delegateLastTime = delegate.next();
                            if (delegateLastTime.isAfterOrEqualTo(time)) {
                                delegateLastTime = null;
                            }
                        }
                    } else {
                        time = delegate.next();
                    }
                    return newFile(time);
                }

                @Override
                protected void innerClose() {
                    delegate.close();
                }
            };
        }
    };
}
Also used : ACloseableIterator(de.invesdwin.util.collections.iterable.ACloseableIterator) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) File(java.io.File) FDate(de.invesdwin.util.time.fdate.FDate) ASkippingIterator(de.invesdwin.util.collections.iterable.ASkippingIterator)

Example 2 with ACloseableIterator

use of de.invesdwin.util.collections.iterable.ACloseableIterator in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method readRangeFiles.

protected ICloseableIterable<File> readRangeFiles(final FDate from, final FDate to) {
    final FDate usedFrom;
    if (from == null) {
        final V firstValue = getFirstValue();
        if (firstValue == null) {
            return EmptyCloseableIterable.getInstance();
        }
        usedFrom = extractTime.apply(firstValue);
    } else {
        usedFrom = from;
    }
    return new ICloseableIterable<File>() {

        @Override
        public ACloseableIterator<File> iterator() {
            return new ACloseableIterator<File>() {

                // use latest time available even if delegate iterator has no values
                private FDate latestFirstTime = fileLookupTable_latestRangeKeyCache.get(usedFrom);

                // add 1 ms to not collide with firstTime
                private final ICloseableIterator<FDate> delegate = getRangeKeys(hashKey, usedFrom.addMilliseconds(1), to);

                private FDate delegateFirstTime = null;

                @Override
                protected boolean innerHasNext() {
                    return latestFirstTime != null || delegateFirstTime != null || delegate.hasNext();
                }

                private ICloseableIterator<FDate> getRangeKeys(final String hashKey, final FDate from, final FDate to) {
                    final ICloseableIterator<FDate> range = getAllRangeKeys();
                    return new ASkippingIterator<FDate>(range) {

                        @Override
                        protected boolean skip(final FDate element) {
                            if (element.isBefore(from)) {
                                return true;
                            } else if (element.isAfter(to)) {
                                throw new FastNoSuchElementException("getRangeKeys reached end");
                            }
                            return false;
                        }
                    };
                }

                @Override
                protected File innerNext() {
                    final FDate time;
                    if (delegateFirstTime != null) {
                        time = delegateFirstTime;
                        delegateFirstTime = null;
                    } else if (latestFirstTime != null) {
                        time = latestFirstTime;
                        latestFirstTime = null;
                        if (delegate.hasNext()) {
                            // prevent duplicate first times
                            delegateFirstTime = delegate.next();
                            if (delegateFirstTime.isBeforeOrEqualTo(time)) {
                                delegateFirstTime = null;
                            }
                        }
                    } else {
                        time = delegate.next();
                    }
                    return newFile(time);
                }

                @Override
                protected void innerClose() {
                    delegate.close();
                }
            };
        }
    };
}
Also used : ACloseableIterator(de.invesdwin.util.collections.iterable.ACloseableIterator) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) File(java.io.File) FDate(de.invesdwin.util.time.fdate.FDate) ASkippingIterator(de.invesdwin.util.collections.iterable.ASkippingIterator)

Example 3 with ACloseableIterator

use of de.invesdwin.util.collections.iterable.ACloseableIterator in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method readRangeFiles.

protected ICloseableIterable<MemoryFileSummary> readRangeFiles(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 firstValue = getFirstValue();
                if (firstValue == null) {
                    return EmptyCloseableIterator.getInstance();
                }
                usedFrom = extractEndTime.apply(firstValue);
            } else {
                usedFrom = from;
            }
            return new ACloseableIterator<MemoryFileSummary>(new TextDescription("%s[%s]: readRangeFiles(%s, %s)", TimeSeriesStorageCache.class.getSimpleName(), hashKey, from, to)) {

                // use latest time available even if delegate iterator has no values
                private RangeTableRow<String, FDate, MemoryFileSummary> latestFirstTime = fileLookupTable_latestRangeKeyCache.get(usedFrom);

                private final ICloseableIterator<RangeTableRow<String, FDate, MemoryFileSummary>> delegate;

                {
                    if (latestFirstTime == null) {
                        delegate = EmptyCloseableIterator.getInstance();
                    } else {
                        delegate = getRangeKeys(hashKey, latestFirstTime.getRangeKey().addMilliseconds(1), to);
                    }
                }

                @Override
                protected boolean innerHasNext() {
                    return latestFirstTime != null || delegate.hasNext();
                }

                private ICloseableIterator<RangeTableRow<String, FDate, MemoryFileSummary>> getRangeKeys(final String hashKey, final FDate from, final FDate to) {
                    readLock.lock();
                    try {
                        final ICloseableIterator<RangeTableRow<String, FDate, MemoryFileSummary>> range = getAllRangeKeys(readLock).iterator();
                        final GetRangeKeysIterator rangeFiltered = new GetRangeKeysIterator(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 (latestFirstTime != null) {
                        summary = latestFirstTime.getValue();
                        latestFirstTime = 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)

Example 4 with ACloseableIterator

use of de.invesdwin.util.collections.iterable.ACloseableIterator 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

ACloseableIterator (de.invesdwin.util.collections.iterable.ACloseableIterator)4 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)4 ICloseableIterator (de.invesdwin.util.collections.iterable.ICloseableIterator)4 MemoryFileSummary (de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary)2 ASkippingIterator (de.invesdwin.util.collections.iterable.ASkippingIterator)2 ASkippingIterator (de.invesdwin.util.collections.iterable.skip.ASkippingIterator)2 FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)2 TextDescription (de.invesdwin.util.lang.description.TextDescription)2 FDate (de.invesdwin.util.time.date.FDate)2 FDate (de.invesdwin.util.time.fdate.FDate)2 RangeTableRow (ezdb.table.RangeTableRow)2 File (java.io.File)2