Search in sources :

Example 6 with TimeRange

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

the class ASegmentedTimeSeriesDBWithLimitedCacheTest 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 7 with TimeRange

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

the class LiveSegmentedTimeSeriesStorageCache method putNextLiveValue.

public void putNextLiveValue(final V nextLiveValue) {
    final FDate nextLiveKey = historicalSegmentTable.extractEndTime(nextLiveValue);
    final FDate lastAvailableHistoricalSegmentTo = historicalSegmentTable.getLastAvailableHistoricalSegmentTo(key, nextLiveKey);
    final TimeRange segment = historicalSegmentTable.getSegmentFinder(key).query().getValue(nextLiveKey);
    if (lastAvailableHistoricalSegmentTo.isAfterNotNullSafe(segment.getFrom()) && /*
                 * allow equals since on first value of the next bar we might get an overlap for once when the last
                 * available time was updated beforehand
                 */
    !lastAvailableHistoricalSegmentTo.equalsNotNullSafe(segment.getTo())) {
        throw new IllegalStateException("lastAvailableHistoricalSegmentTo [" + lastAvailableHistoricalSegmentTo + "] should be before or equal to liveSegmentFrom [" + segment.getFrom() + "]");
    }
    if (liveSegment != null && nextLiveKey.isAfter(liveSegment.getSegmentedKey().getSegment().getTo())) {
        if (!lastAvailableHistoricalSegmentTo.isBeforeOrEqualTo(liveSegment.getSegmentedKey().getSegment().getTo())) {
            throw new IllegalStateException("lastAvailableHistoricalSegmentTo [" + lastAvailableHistoricalSegmentTo + "] should be before or equal to liveSegmentTo [" + segment.getTo() + "]");
        }
        liveSegment.convertLiveSegmentToHistorical();
        liveSegment.close();
        liveSegment = null;
    }
    if (liveSegment == null) {
        final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, segment);
        liveSegment = new SwitchingLiveSegment<K, V>(segmentedKey, historicalSegmentTable, batchFlushInterval);
    }
    liveSegment.putNextLiveValue(nextLiveKey, nextLiveValue);
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) SegmentedKey(de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentedKey) FDate(de.invesdwin.util.time.date.FDate)

Example 8 with TimeRange

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

the class PersistentLiveSegment method finish.

public void finish() {
    if (!isEmpty()) {
        final ADelegateRangeTable<String, TimeRange, SegmentStatus> segmentStatusTable = historicalSegmentTable.getStorage().getSegmentStatusTable();
        final SegmentStatus existingStatus = segmentStatusTable.get(hashKey, segmentedKey.getSegment());
        if (existingStatus == SegmentStatus.INITIALIZING) {
            segmentStatusTable.put(hashKey, segmentedKey.getSegment(), SegmentStatus.COMPLETE);
            final ICloseableIterable<V> rangeValues = rangeValues(segmentedKey.getSegment().getFrom(), segmentedKey.getSegment().getTo(), DisabledLock.INSTANCE, null);
            historicalSegmentTable.getLookupTableCache(segmentedKey.getKey()).onSegmentCompleted(segmentedKey, rangeValues);
        }
    }
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) SegmentStatus(de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentStatus)

Example 9 with TimeRange

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

the class PeriodicalSegmentFinder method newCalculationBounds.

private TimeRange newCalculationBounds(final FDate lookupTime) {
    FDate start = lookupTime.truncate(boundsTimeUnit);
    if (period.isExactMultipleOfPeriod(FTimeUnit.WEEKS.durationValue())) {
        // weekly periods should start at the previous monday (even if it is in the previous year)
        start = start.setFWeekday(FWeekday.Monday);
    }
    final FDate end = start.add(boundsTimeUnit, 1).addMilliseconds(-1);
    return new TimeRange(start, end);
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) IEvaluateGenericFDate(de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate) FDate(de.invesdwin.util.time.date.FDate)

Example 10 with TimeRange

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

the class PeriodicalSegmentFinder method getSegment.

public synchronized TimeRange getSegment(final FDate key) {
    if (calculationBounds == null || calculationBounds.getFrom().isAfter(key) || calculationBounds.getTo().isBefore(key)) {
        calculationBounds = newCalculationBounds(key);
        curTimeRange = null;
    }
    if (curTimeRange == null) {
        // init first timerange
        curTimeRange = calculateNextTimeRange(calculationBounds.getFrom());
    }
    if (curTimeRange.getFrom().isAfter(key)) {
        do {
            // go back a few steps
            curTimeRange = calculatePrevTimeRange(curTimeRange.getFrom());
        } while (curTimeRange.getFrom().isAfter(key));
    } else if (curTimeRange.getTo().isBefore(key)) {
        do {
            // go forward a few steps
            curTimeRange = calculateNextTimeRange(curTimeRange.getFrom());
        } while (curTimeRange.getTo().isBefore(key));
    }
    if (calculationBounds.getTo().isBefore(curTimeRange.getTo())) {
        curTimeRange = new TimeRange(curTimeRange.getFrom(), calculationBounds.getTo());
    }
    return curTimeRange;
}
Also used : 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