Search in sources :

Example 16 with ICloseableIterable

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

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

Example 18 with ICloseableIterable

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

Example 19 with ICloseableIterable

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

the class FileBufferCache method resultCache_load.

private static ArrayFileBufferCacheResult resultCache_load(final ResultCacheKey key) throws Exception {
    final ICloseableIterable values = key.getSource().getSource();
    key.setSource(null);
    final ArrayList list = LIST_POOL.borrowObject();
    try (ICloseableIterator it = values.iterator()) {
        while (true) {
            list.add(it.next());
        }
    } catch (final NoSuchElementException e) {
    // end reached
    }
    return new ArrayFileBufferCacheResult(list);
}
Also used : ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) ArrayList(java.util.ArrayList) ICloseableIterator(de.invesdwin.util.collections.iterable.ICloseableIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 20 with ICloseableIterable

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

the class ASegmentedTimeSeriesDBWithNoCacheAndNoQueryCacheTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    final AHistoricalCache<TimeRange> segmentFinder = new AHistoricalCache<TimeRange>() {

        private final PeriodicalSegmentFinder calculation = PeriodicalSegmentFinder.newInstance(new Duration(2, FTimeUnit.YEARS));

        @Override
        protected Integer getInitialMaximumSize() {
            return 1000;
        }

        @Override
        protected FDate innerExtractKey(final TimeRange value) {
            return value.getFrom();
        }

        @Override
        protected IEvaluateGenericFDate<TimeRange> newLoadValue() {
            return this::loadValue;
        }

        private synchronized TimeRange loadValue(final IFDateProvider pKey) {
            final FDate key = pKey.asFDate();
            final TimeRange value = calculation.getSegment(key);
            final TimeRange upperTimeRange = new TimeRange(value.getFrom().addYears(1), value.getTo().addYears(1));
            if (upperTimeRange.containsInclusive(key)) {
                return upperTimeRange;
            } else {
                return new TimeRange(value.getFrom().addYears(-1), value.getTo().addYears(-1));
            }
        }

        @Override
        protected FDate innerCalculateNextKey(final FDate key) {
            return query().getValue(key).getTo().addMilliseconds(1);
        }

        @Override
        protected FDate innerCalculatePreviousKey(final FDate key) {
            return query().getValue(key).getFrom().addMilliseconds(-1);
        }

        @Override
        public void preloadData(final ExecutorService executor) {
        // noop
        }
    };
    table = new ASegmentedTimeSeriesDB<String, FDate>(getClass().getSimpleName()) {

        @Override
        public AHistoricalCache<TimeRange> getSegmentFinder(final String key) {
            return segmentFinder;
        }

        @Override
        protected ISerde<FDate> newValueSerde() {
            return new TypeDelegateSerde<FDate>(FDate.class);
        }

        @Override
        protected Integer newValueFixedLength() {
            return null;
        }

        @Override
        protected String innerHashKeyToString(final String key) {
            return key;
        }

        @Override
        protected File getBaseDirectory() {
            return ContextProperties.TEMP_DIRECTORY;
        }

        @Override
        protected ICloseableIterable<? extends FDate> downloadSegmentElements(final SegmentedKey<String> segmentedKey) {
            return new ASkippingIterable<FDate>(WrapperCloseableIterable.maybeWrap(entities)) {

                private final FDate from = segmentedKey.getSegment().getFrom();

                private final FDate to = segmentedKey.getSegment().getTo();

                @Override
                protected boolean skip(final FDate element) {
                    return element.isBefore(from) || element.isAfter(to);
                }
            };
        }

        @Override
        protected FDate extractEndTime(final FDate value) {
            return value;
        }

        @Override
        public FDate getFirstAvailableHistoricalSegmentFrom(final String key) {
            if (entities.isEmpty()) {
                return null;
            }
            return segmentFinder.query().getValue(entities.get(0)).getFrom();
        }

        @Override
        public FDate getLastAvailableHistoricalSegmentTo(final String key, final FDate updateTo) {
            if (entities.isEmpty()) {
                return null;
            }
            return segmentFinder.query().getValue(entities.get(entities.size() - 1)).getTo();
        }

        @Override
        protected String getElementsName() {
            return "values";
        }
    };
}
Also used : ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) Duration(de.invesdwin.util.time.duration.Duration) ISerde(de.invesdwin.util.marshallers.serde.ISerde) AHistoricalCache(de.invesdwin.util.collections.loadingcache.historical.AHistoricalCache) IEvaluateGenericFDate(de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate) FDate(de.invesdwin.util.time.date.FDate) TimeRange(de.invesdwin.util.time.range.TimeRange) IFDateProvider(de.invesdwin.util.time.date.IFDateProvider) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Aggregations

ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)23 FDate (de.invesdwin.util.time.date.FDate)20 TimeRange (de.invesdwin.util.time.range.TimeRange)16 File (java.io.File)14 AHistoricalCache (de.invesdwin.util.collections.loadingcache.historical.AHistoricalCache)12 ISerde (de.invesdwin.util.marshallers.serde.ISerde)12 Duration (de.invesdwin.util.time.duration.Duration)12 ICloseableIterator (de.invesdwin.util.collections.iterable.ICloseableIterator)7 ACloseableIterator (de.invesdwin.util.collections.iterable.ACloseableIterator)4 FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)4 ArrayList (java.util.ArrayList)3 MemoryFileSummary (de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary)2 ATimeSeriesUpdater (de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater)2 ATest (de.invesdwin.context.test.ATest)2 ASkippingIterator (de.invesdwin.util.collections.iterable.ASkippingIterator)2 ATransformingIterable (de.invesdwin.util.collections.iterable.ATransformingIterable)2 FlatteningIterable (de.invesdwin.util.collections.iterable.FlatteningIterable)2 ASkippingIterable (de.invesdwin.util.collections.iterable.skip.ASkippingIterable)2 ASkippingIterator (de.invesdwin.util.collections.iterable.skip.ASkippingIterator)2 ILock (de.invesdwin.util.concurrent.lock.ILock)2