Search in sources :

Example 1 with BufferingIterator

use of de.invesdwin.util.collections.iterable.buffer.BufferingIterator 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 2 with BufferingIterator

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

ATransformingIterable (de.invesdwin.util.collections.iterable.ATransformingIterable)2 BufferingIterator (de.invesdwin.util.collections.iterable.buffer.BufferingIterator)2 IBufferingIterator (de.invesdwin.util.collections.iterable.buffer.IBufferingIterator)2 FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)2 Entry (java.util.Map.Entry)2