Search in sources :

Example 6 with FastNoSuchElementException

use of de.invesdwin.util.error.FastNoSuchElementException in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesStorageCache method getSegments.

private ICloseableIterable<TimeRange> getSegments(final FDate from, final FDate to, final FDate lastAvailableSegmentTo) {
    if (from == null || to == null) {
        return EmptyCloseableIterable.getInstance();
    }
    final TimeRange nextSegment = getSegmentFinder(key).query().getValue(to.addMilliseconds(1));
    final FDate adjTo;
    if (to.equalsNotNullSafe(lastAvailableSegmentTo) && nextSegment.getFrom().equalsNotNullSafe(to)) {
        // adjust for overlapping segments
        adjTo = to.addMilliseconds(-1);
    } else {
        adjTo = to;
    }
    final FDate adjFrom = from;
    final ICloseableIterable<TimeRange> segments = new ICloseableIterable<TimeRange>() {

        @Override
        public ICloseableIterator<TimeRange> iterator() {
            return new ICloseableIterator<TimeRange>() {

                private TimeRange nextSegment = getSegmentFinder(key).query().getValue(adjFrom);

                @Override
                public boolean hasNext() {
                    return nextSegment != null && nextSegment.getFrom().isBeforeOrEqualTo(adjTo);
                }

                @Override
                public TimeRange next() {
                    if (nextSegment == null) {
                        throw new FastNoSuchElementException("ASegmentedTimeSeriesStorageCache getSegments nextSegment is null");
                    }
                    final TimeRange curSegment = nextSegment;
                    // get one segment later
                    nextSegment = determineNextSegment(curSegment);
                    return curSegment;
                }

                private TimeRange determineNextSegment(final TimeRange curSegment) {
                    final FDate nextSegmentStart = nextSegment.getTo().addMilliseconds(1);
                    final TimeRange nextSegment = getSegmentFinder(key).query().getValue(nextSegmentStart);
                    if (!curSegment.getTo().equalsNotNullSafe(nextSegment.getFrom()) && !nextSegmentStart.equals(nextSegment.getFrom())) {
                        // allow overlapping segments
                        throw new IllegalStateException("Segment start expected [" + curSegment.getTo() + " or " + nextSegmentStart + "] != found [" + nextSegment.getFrom() + "]");
                    }
                    return nextSegment;
                }

                @Override
                public void close() {
                    nextSegment = null;
                }
            };
        }
    };
    final ASkippingIterable<TimeRange> filteredSegments = new ASkippingIterable<TimeRange>(segments) {

        @Override
        protected boolean skip(final TimeRange element) {
            // though additionally skip ranges that exceed the available dates
            final FDate segmentTo = element.getTo();
            if (segmentTo.isBefore(adjFrom)) {
                throw new IllegalStateException("segmentTo [" + segmentTo + "] should not be before adjFrom [" + adjFrom + "]");
            }
            final FDate segmentFrom = element.getFrom();
            if (segmentFrom.isAfter(adjTo)) {
                // no need to continue going higher
                throw new FastNoSuchElementException("ASegmentedTimeSeriesStorageCache getSegments end reached");
            }
            return false;
        }
    };
    return filteredSegments;
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) ASkippingIterable(de.invesdwin.util.collections.iterable.skip.ASkippingIterable) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) FDate(de.invesdwin.util.time.date.FDate)

Example 7 with FastNoSuchElementException

use of de.invesdwin.util.error.FastNoSuchElementException in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method readRangeValues.

protected ICloseableIterator<V> readRangeValues(final FDate from, final FDate to) {
    final ICloseableIterator<File> fileIterator = readRangeFiles(from, to).iterator();
    final ICloseableIterator<ICloseableIterator<V>> chunkIterator = new ATransformingCloseableIterator<File, ICloseableIterator<V>>(fileIterator) {

        private boolean first = true;

        @Override
        protected ICloseableIterator<V> transform(final File value) {
            final ICloseableIterable<V> serializingCollection = newSerializingCollection(value);
            if (first) {
                first = false;
                if (hasNext()) {
                    return new ASkippingIterator<V>(serializingCollection.iterator()) {

                        @Override
                        protected boolean skip(final V element) {
                            final FDate time = extractTime.apply(element);
                            return time.isBefore(from);
                        }
                    };
                // first and last
                } else {
                    return new ASkippingIterator<V>(serializingCollection.iterator()) {

                        @Override
                        protected boolean skip(final V element) {
                            final FDate time = extractTime.apply(element);
                            if (time.isBefore(from)) {
                                return true;
                            } else if (time.isAfter(to)) {
                                throw new FastNoSuchElementException("getRangeValues reached end");
                            }
                            return false;
                        }
                    };
                }
            // last
            } else if (!hasNext()) {
                return new ASkippingIterator<V>(serializingCollection.iterator()) {

                    @Override
                    protected boolean skip(final V element) {
                        final FDate time = extractTime.apply(element);
                        if (time.isAfter(to)) {
                            throw new FastNoSuchElementException("getRangeValues reached end");
                        }
                        return false;
                    }
                };
            } else {
                return serializingCollection.iterator();
            }
        }
    };
    // final ATransformingCloseableIterator<ICloseableIterator<V>, ICloseableIterator<V>> transformer = new ATransformingCloseableIterator<ICloseableIterator<V>, ICloseableIterator<V>>(
    // chunkIterator) {
    // @Override
    // protected ICloseableIterator<V> transform(final ICloseableIterator<V> value) {
    // //keep file open as shortly as possible to fix too many open files exception
    // return new BufferingIterator<V>(value);
    // }
    // };
    // single threaded is 20% better than with producerqueue
    final FlatteningIterator<V> flatteningIterator = new FlatteningIterator<V>(chunkIterator);
    return flatteningIterator;
}
Also used : ATransformingCloseableIterator(de.invesdwin.util.collections.iterable.ATransformingCloseableIterator) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) FDate(de.invesdwin.util.time.fdate.FDate) FlatteningIterator(de.invesdwin.util.collections.iterable.FlatteningIterator) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) File(java.io.File) ASkippingIterator(de.invesdwin.util.collections.iterable.ASkippingIterator)

Example 8 with FastNoSuchElementException

use of de.invesdwin.util.error.FastNoSuchElementException in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesStorageCache method getSegmentsReverse.

private ICloseableIterable<TimeRange> getSegmentsReverse(final FDate from, final FDate to, final FDate lastAvailableSegmentTo) {
    if (from == null || to == null) {
        return EmptyCloseableIterable.getInstance();
    }
    final TimeRange nextSegment = getSegmentFinder(key).query().getValue(from.addMilliseconds(1));
    final FDate adjFrom;
    if (from.equalsNotNullSafe(lastAvailableSegmentTo) && nextSegment.getFrom().equalsNotNullSafe(from)) {
        // adjust for overlapping segments
        adjFrom = from.addMilliseconds(-1);
    } else {
        adjFrom = from;
    }
    final FDate adjTo = to;
    final ICloseableIterable<TimeRange> segments = new ICloseableIterable<TimeRange>() {

        @Override
        public ICloseableIterator<TimeRange> iterator() {
            return new ICloseableIterator<TimeRange>() {

                private TimeRange nextSegment = getSegmentFinder(key).query().getValue(adjFrom);

                @Override
                public boolean hasNext() {
                    return nextSegment != null && nextSegment.getTo().isAfter(adjTo);
                }

                @Override
                public TimeRange next() {
                    final TimeRange curSegment = nextSegment;
                    if (curSegment == null) {
                        throw new FastNoSuchElementException("ASegmentedTimeSeriesStorageCache getSegments end reached null");
                    }
                    // get one segment earlier
                    nextSegment = getSegmentFinder(key).query().setFutureNullEnabled().getValue(nextSegment.getFrom().addMilliseconds(-1));
                    return curSegment;
                }

                @Override
                public void close() {
                    nextSegment = null;
                }
            };
        }
    };
    final ASkippingIterable<TimeRange> filteredSegments = new ASkippingIterable<TimeRange>(segments) {

        @Override
        protected boolean skip(final TimeRange element) {
            // though additionally skip ranges that exceed the available dates
            final FDate segmentTo = element.getTo();
            if (segmentTo.isBefore(adjTo)) {
                // no need to continue going lower
                throw new FastNoSuchElementException("ASegmentedTimeSeriesStorageCache getSegments end reached adjTo");
            }
            // skip last value and continue with earlier ones
            final FDate segmentFrom = element.getFrom();
            return segmentFrom.isAfter(adjFrom);
        }
    };
    return filteredSegments;
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) ASkippingIterable(de.invesdwin.util.collections.iterable.skip.ASkippingIterable) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) FDate(de.invesdwin.util.time.date.FDate)

Aggregations

FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)8 ICloseableIterator (de.invesdwin.util.collections.iterable.ICloseableIterator)6 ASkippingIterator (de.invesdwin.util.collections.iterable.ASkippingIterator)4 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)4 FDate (de.invesdwin.util.time.fdate.FDate)4 File (java.io.File)4 ACloseableIterator (de.invesdwin.util.collections.iterable.ACloseableIterator)2 ATransformingCloseableIterator (de.invesdwin.util.collections.iterable.ATransformingCloseableIterator)2 ATransformingIterable (de.invesdwin.util.collections.iterable.ATransformingIterable)2 FlatteningIterator (de.invesdwin.util.collections.iterable.FlatteningIterator)2 BufferingIterator (de.invesdwin.util.collections.iterable.buffer.BufferingIterator)2 IBufferingIterator (de.invesdwin.util.collections.iterable.buffer.IBufferingIterator)2 ASkippingIterable (de.invesdwin.util.collections.iterable.skip.ASkippingIterable)2 FDate (de.invesdwin.util.time.date.FDate)2 TimeRange (de.invesdwin.util.time.range.TimeRange)2 Entry (java.util.Map.Entry)2