Search in sources :

Example 16 with TimeRange

use of de.invesdwin.util.time.range.TimeRange in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesDBWithoutShiftKeysAndQueryInterceptorTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    final AHistoricalCache<TimeRange> segmentFinder = PeriodicalSegmentFinder.newCache(new Duration(2, FTimeUnit.YEARS), false);
    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) FDate(de.invesdwin.util.time.date.FDate) TimeRange(de.invesdwin.util.time.range.TimeRange) File(java.io.File)

Example 17 with TimeRange

use of de.invesdwin.util.time.range.TimeRange 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)

Example 18 with TimeRange

use of de.invesdwin.util.time.range.TimeRange 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 19 with TimeRange

use of de.invesdwin.util.time.range.TimeRange in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesStorageCache method getFirstValue.

public V getFirstValue() {
    if (cachedFirstValue != null) {
        maybePrepareForUpdate(null);
    }
    if (cachedFirstValue == null) {
        final FDate firstAvailableSegmentFrom = getFirstAvailableSegmentFrom(key);
        if (firstAvailableSegmentFrom == null) {
            cachedFirstValue = Optional.empty();
        } else {
            FDate lastAvailableSegmentTo = getLastAvailableSegmentTo(key, null);
            final TimeRange nextSegment = getSegmentFinder(key).query().getValue(lastAvailableSegmentTo.addMilliseconds(1));
            if (nextSegment.getFrom().equals(lastAvailableSegmentTo)) {
                // adjust for overlapping segments
                lastAvailableSegmentTo = lastAvailableSegmentTo.addMilliseconds(-1);
            }
            final IHistoricalCacheQuery<TimeRange> segmentFinderQuery = getSegmentFinder(key).query();
            final TimeRange lastSegment = segmentFinderQuery.getValue(lastAvailableSegmentTo);
            TimeRange segment = segmentFinderQuery.getValue(firstAvailableSegmentFrom);
            if (!segment.getFrom().equalsNotNullSafe(firstAvailableSegmentFrom)) {
                throw new IllegalStateException("segment.from [" + segment.getFrom() + "] should be equal to firstAvailableSegmentFrom [" + firstAvailableSegmentFrom + "]");
            }
            while (cachedFirstValue == null && segment.getFrom().isBeforeOrEqualTo(lastSegment.getFrom())) {
                final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, segment);
                maybeInitSegment(segmentedKey);
                final String segmentedHashKey = segmentedTable.hashKeyToString(segmentedKey);
                final MemoryFileSummary latestValue = storage.getFileLookupTable().getLatestValue(segmentedHashKey, FDate.MIN_DATE);
                final V firstValue;
                if (latestValue == null) {
                    segment = segmentFinderQuery.getValue(segment.getTo().addMilliseconds(1));
                } else {
                    firstValue = latestValue.getFirstValue(valueSerde);
                    cachedFirstValue = Optional.of(firstValue);
                }
            }
            if (cachedFirstValue == null) {
                cachedFirstValue = Optional.empty();
            }
        }
    }
    return cachedFirstValue.orElse(null);
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) MemoryFileSummary(de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary) FDate(de.invesdwin.util.time.date.FDate)

Example 20 with TimeRange

use of de.invesdwin.util.time.range.TimeRange 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

TimeRange (de.invesdwin.util.time.range.TimeRange)31 FDate (de.invesdwin.util.time.date.FDate)27 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)16 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 File (java.io.File)12 SegmentedKey (de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentedKey)5 IEvaluateGenericFDate (de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate)5 ATest (de.invesdwin.context.test.ATest)4 Test (org.junit.jupiter.api.Test)4 ALiveSegmentedTimeSeriesDB (de.invesdwin.context.persistence.timeseriesdb.segmented.live.ALiveSegmentedTimeSeriesDB)3 HashMap (java.util.HashMap)3 NoSuchElementException (java.util.NoSuchElementException)3 IncompleteUpdateFoundException (de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)2 SegmentStatus (de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentStatus)2 ATransformingIterable (de.invesdwin.util.collections.iterable.ATransformingIterable)2 FlatteningIterable (de.invesdwin.util.collections.iterable.FlatteningIterable)2 ICloseableIterator (de.invesdwin.util.collections.iterable.ICloseableIterator)2 ASkippingIterable (de.invesdwin.util.collections.iterable.skip.ASkippingIterable)2