Search in sources :

Example 1 with ATransformingIterable

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

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

Example 3 with ATransformingIterable

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

the class MphTablePerformanceTest method testMphTablePerformance.

@Test
public void testMphTablePerformance() throws IOException {
    final File directory = new File(ContextProperties.getCacheDirectory(), MphTablePerformanceTest.class.getSimpleName());
    Files.deleteNative(directory);
    Files.forceMkdir(directory);
    final File file = new File(directory, "testMphTablePerformance");
    final Instant writesStart = new Instant();
    final TableConfig<Long, Long> config = new TableConfig<Long, Long>().withKeySerializer(new SmartLongSerializer()).withValueSerializer(new SmartVLongSerializer());
    final ATransformingIterable<FDate, com.indeed.util.core.Pair<Long, Long>> entries = new ATransformingIterable<FDate, com.indeed.util.core.Pair<Long, Long>>(newValues()) {

        @Override
        protected com.indeed.util.core.Pair<Long, Long> transform(final FDate value) {
            return com.indeed.util.core.Pair.of(value.millisValue(), value.millisValue());
        }
    };
    // can not append to an existing table
    TableWriter.write(file, config, entries);
    printProgress("WritesFinished", writesStart, VALUES, VALUES);
    readIterator(file);
    readGet(file);
}
Also used : ATransformingIterable(de.invesdwin.util.collections.iterable.ATransformingIterable) Instant(de.invesdwin.util.time.Instant) FDate(de.invesdwin.util.time.date.FDate) SmartLongSerializer(com.indeed.mph.serializers.SmartLongSerializer) SmartVLongSerializer(com.indeed.mph.serializers.SmartVLongSerializer) TableConfig(com.indeed.mph.TableConfig) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with ATransformingIterable

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

the class ASegmentedTimeSeriesStorageCache method readRangeValues.

public ICloseableIterable<V> readRangeValues(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
    final FDate firstAvailableSegmentFrom = getFirstAvailableSegmentFrom(key);
    if (firstAvailableSegmentFrom == null) {
        return EmptyCloseableIterable.getInstance();
    }
    final FDate lastAvailableSegmentTo = getLastAvailableSegmentTo(key, to);
    if (lastAvailableSegmentTo == null) {
        return EmptyCloseableIterable.getInstance();
    }
    // adjust dates directly to prevent unnecessary segment calculations
    final FDate adjFrom = FDates.max(from, firstAvailableSegmentFrom);
    final FDate adjTo = FDates.min(to, lastAvailableSegmentTo);
    final ICloseableIterable<TimeRange> segments = getSegments(adjFrom, adjTo, lastAvailableSegmentTo);
    final ATransformingIterable<TimeRange, ICloseableIterable<V>> segmentQueries = new ATransformingIterable<TimeRange, ICloseableIterable<V>>(segments) {

        @Override
        protected ICloseableIterable<V> transform(final TimeRange value) {
            return new ICloseableIterable<V>() {

                @Override
                public ICloseableIterator<V> iterator() {
                    final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, value);
                    maybeInitSegment(segmentedKey);
                    final FDate segmentAdjFrom = FDates.max(adjFrom, value.getFrom());
                    final FDate segmentAdjTo = FDates.min(adjTo, value.getTo());
                    final Lock compositeReadLock = Locks.newCompositeLock(readLock, segmentedTable.getTableLock(segmentedKey).readLock());
                    return segmentedTable.getLookupTableCache(segmentedKey).readRangeValues(segmentAdjFrom, segmentAdjTo, compositeReadLock, skipFileFunction);
                }
            };
        }
    };
    final ICloseableIterable<V> rangeValues = new FlatteningIterable<V>(segmentQueries);
    return rangeValues;
}
Also used : ATransformingIterable(de.invesdwin.util.collections.iterable.ATransformingIterable) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) FDate(de.invesdwin.util.time.date.FDate) ILock(de.invesdwin.util.concurrent.lock.ILock) DisabledLock(de.invesdwin.util.concurrent.lock.disabled.DisabledLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IReadWriteLock(de.invesdwin.util.concurrent.lock.readwrite.IReadWriteLock) Lock(java.util.concurrent.locks.Lock) FlatteningIterable(de.invesdwin.util.collections.iterable.FlatteningIterable) TimeRange(de.invesdwin.util.time.range.TimeRange)

Example 5 with ATransformingIterable

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

the class ASegmentedTimeSeriesStorageCache method readRangeValuesReverse.

public ICloseableIterable<V> readRangeValuesReverse(final FDate from, final FDate to, final Lock readLock, final ISkipFileFunction skipFileFunction) {
    final FDate firstAvailableSegmentFrom = getFirstAvailableSegmentFrom(key);
    final FDate lastAvailableSegmentTo = getLastAvailableSegmentTo(key, to);
    // adjust dates directly to prevent unnecessary segment calculations
    final FDate adjFrom = FDates.min(from, lastAvailableSegmentTo);
    final FDate adjTo = FDates.max(to, firstAvailableSegmentFrom);
    final ICloseableIterable<TimeRange> filteredSegments = getSegmentsReverse(adjFrom, adjTo, lastAvailableSegmentTo);
    final ATransformingIterable<TimeRange, ICloseableIterable<V>> segmentQueries = new ATransformingIterable<TimeRange, ICloseableIterable<V>>(filteredSegments) {

        @Override
        protected ICloseableIterable<V> transform(final TimeRange value) {
            return new ICloseableIterable<V>() {

                @Override
                public ICloseableIterator<V> iterator() {
                    final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, value);
                    maybeInitSegment(segmentedKey);
                    final FDate segmentAdjFrom = FDates.min(adjFrom, value.getTo());
                    final FDate segmentAdjTo = FDates.max(adjTo, value.getFrom());
                    final Lock compositeReadLock = Locks.newCompositeLock(readLock, segmentedTable.getTableLock(segmentedKey).readLock());
                    return segmentedTable.getLookupTableCache(segmentedKey).readRangeValuesReverse(segmentAdjFrom, segmentAdjTo, compositeReadLock, skipFileFunction);
                }
            };
        }
    };
    final ICloseableIterable<V> rangeValues = new FlatteningIterable<V>(segmentQueries);
    return rangeValues;
}
Also used : ATransformingIterable(de.invesdwin.util.collections.iterable.ATransformingIterable) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) FDate(de.invesdwin.util.time.date.FDate) ILock(de.invesdwin.util.concurrent.lock.ILock) DisabledLock(de.invesdwin.util.concurrent.lock.disabled.DisabledLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IReadWriteLock(de.invesdwin.util.concurrent.lock.readwrite.IReadWriteLock) Lock(java.util.concurrent.locks.Lock) FlatteningIterable(de.invesdwin.util.collections.iterable.FlatteningIterable) TimeRange(de.invesdwin.util.time.range.TimeRange)

Aggregations

ATransformingIterable (de.invesdwin.util.collections.iterable.ATransformingIterable)5 FDate (de.invesdwin.util.time.date.FDate)3 FlatteningIterable (de.invesdwin.util.collections.iterable.FlatteningIterable)2 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)2 BufferingIterator (de.invesdwin.util.collections.iterable.buffer.BufferingIterator)2 IBufferingIterator (de.invesdwin.util.collections.iterable.buffer.IBufferingIterator)2 ILock (de.invesdwin.util.concurrent.lock.ILock)2 DisabledLock (de.invesdwin.util.concurrent.lock.disabled.DisabledLock)2 IReadWriteLock (de.invesdwin.util.concurrent.lock.readwrite.IReadWriteLock)2 FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)2 TimeRange (de.invesdwin.util.time.range.TimeRange)2 Entry (java.util.Map.Entry)2 Lock (java.util.concurrent.locks.Lock)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 TableConfig (com.indeed.mph.TableConfig)1 SmartLongSerializer (com.indeed.mph.serializers.SmartLongSerializer)1 SmartVLongSerializer (com.indeed.mph.serializers.SmartVLongSerializer)1 Instant (de.invesdwin.util.time.Instant)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1