Search in sources :

Example 1 with FastNoSuchElementException

use of de.invesdwin.util.error.FastNoSuchElementException 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 FastNoSuchElementException

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

the class TimeSeriesStorageCache method readRangeValuesReverse.

protected ICloseableIterator<V> readRangeValuesReverse(final FDate from, final FDate to) {
    final ICloseableIterator<File> fileIterator = readRangeFilesReverse(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 IReverseCloseableIterable<V> serializingCollection = newSerializingCollection(value);
            if (first) {
                first = false;
                if (hasNext()) {
                    return new ASkippingIterator<V>(serializingCollection.reverseIterator()) {

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

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

                    @Override
                    protected boolean skip(final V element) {
                        final FDate time = extractTime.apply(element);
                        if (time.isBefore(to)) {
                            throw new FastNoSuchElementException("getRangeValues reached end");
                        }
                        return false;
                    }
                };
            } else {
                return serializingCollection.reverseIterator();
            }
        }
    };
    // 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 3 with FastNoSuchElementException

use of de.invesdwin.util.error.FastNoSuchElementException 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 4 with FastNoSuchElementException

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

the class HeapLiveSegment method rangeReverseValues.

@Override
public ICloseableIterable<V> rangeReverseValues(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
    // we expect the read lock to be already locked from the outside
    if (values == null || from != null && to != null && from.isBeforeNotNullSafe(to)) {
        return EmptyCloseableIterable.getInstance();
    }
    if (from != null && !firstValue.isEmpty() && from.isBeforeOrEqualToNotNullSafe(firstValueKey)) {
        if (from.isBeforeNotNullSafe(firstValueKey)) {
            return EmptyCloseableIterable.getInstance();
        } else {
            return firstValue.snapshot();
        }
    }
    if (to != null && !lastValue.isEmpty() && to.isAfterOrEqualToNotNullSafe(lastValueKey)) {
        if (to.isAfterNotNullSafe(lastValueKey)) {
            return EmptyCloseableIterable.getInstance();
        } else {
            return lastValue.snapshot();
        }
    }
    final SortedMap<Long, V> headMap;
    if (from == null) {
        headMap = values.descendingMap();
    } else {
        headMap = values.descendingMap().tailMap(from.millisValue(), true);
    }
    final ICloseableIterable<Entry<Long, V>> tail = WrapperCloseableIterable.maybeWrap(headMap.entrySet());
    final ICloseableIterable<Entry<Long, V>> skipping;
    if (to == null) {
        skipping = tail;
    } else {
        skipping = new ASkippingIterable<Entry<Long, V>>(tail) {

            @Override
            protected boolean skip(final Entry<Long, V> element) {
                if (element.getKey() < to.millisValue()) {
                    throw new FastNoSuchElementException("LiveSegment rangeReverseValues end reached");
                }
                return false;
            }
        };
    }
    final ATransformingIterable<Entry<Long, V>, V> transforming = new ATransformingIterable<Entry<Long, V>, V>(skipping) {

        @Override
        protected V transform(final Entry<Long, V> value) {
            return value.getValue();
        }
    };
    if (readLock == DisabledLock.INSTANCE) {
        return transforming;
    } else {
        // we expect the read lock to be already locked from the outside
        return new BufferingIterator<>(transforming);
    }
}
Also used : ATransformingIterable(de.invesdwin.util.collections.iterable.ATransformingIterable) Entry(java.util.Map.Entry) IBufferingIterator(de.invesdwin.util.collections.iterable.buffer.IBufferingIterator) BufferingIterator(de.invesdwin.util.collections.iterable.buffer.BufferingIterator) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException)

Example 5 with FastNoSuchElementException

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

the class HeapLiveSegment method rangeValues.

@Override
public ICloseableIterable<V> rangeValues(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
    // we expect the read lock to be already locked from the outside
    if (values == null || from != null && to != null && from.isAfterNotNullSafe(to)) {
        return EmptyCloseableIterable.getInstance();
    }
    if (from != null && !lastValue.isEmpty() && from.isAfterOrEqualToNotNullSafe(lastValueKey)) {
        if (from.isAfterNotNullSafe(lastValueKey)) {
            return EmptyCloseableIterable.getInstance();
        } else {
            return lastValue.snapshot();
        }
    }
    if (to != null && !firstValue.isEmpty() && to.isBeforeOrEqualToNotNullSafe(firstValueKey)) {
        if (to.isBeforeNotNullSafe(firstValueKey)) {
            return EmptyCloseableIterable.getInstance();
        } else {
            return firstValue.snapshot();
        }
    }
    final SortedMap<Long, V> tailMap;
    if (from == null) {
        tailMap = values;
    } else {
        tailMap = values.tailMap(from.millisValue(), true);
    }
    final ICloseableIterable<Entry<Long, V>> tail = WrapperCloseableIterable.maybeWrap(tailMap.entrySet());
    final ICloseableIterable<Entry<Long, V>> skipping;
    if (to == null) {
        skipping = tail;
    } else {
        skipping = new ASkippingIterable<Entry<Long, V>>(tail) {

            @Override
            protected boolean skip(final Entry<Long, V> element) {
                if (element.getKey() > to.millisValue()) {
                    throw new FastNoSuchElementException("LiveSegment rangeValues end reached");
                }
                return false;
            }
        };
    }
    final ATransformingIterable<Entry<Long, V>, V> transforming = new ATransformingIterable<Entry<Long, V>, V>(skipping) {

        @Override
        protected V transform(final Entry<Long, V> value) {
            return value.getValue();
        }
    };
    if (readLock == DisabledLock.INSTANCE) {
        return transforming;
    } else {
        // we expect the read lock to be already locked from the outside
        return new BufferingIterator<>(transforming);
    }
}
Also used : ATransformingIterable(de.invesdwin.util.collections.iterable.ATransformingIterable) Entry(java.util.Map.Entry) IBufferingIterator(de.invesdwin.util.collections.iterable.buffer.IBufferingIterator) BufferingIterator(de.invesdwin.util.collections.iterable.buffer.BufferingIterator) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException)

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