Search in sources :

Example 21 with TimeRange

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

the class ASegmentedTimeSeriesStorageCache method getLatestValue.

public V getLatestValue(final FDate pDate) {
    final FDate date = FDates.min(pDate, getLastAvailableSegmentTo(key, pDate));
    final SingleValue value = storage.getOrLoad_latestValueLookupTable(hashKey, date, () -> {
        final FDate firstAvailableSegmentFrom = getFirstAvailableSegmentFrom(key);
        // already adjusted on the outside
        final FDate adjFrom = date;
        final FDate adjTo = firstAvailableSegmentFrom;
        final FDate lastAvailableSegmentTo = getLastAvailableSegmentTo(key, adjFrom);
        final ICloseableIterable<TimeRange> segmentsReverse = getSegmentsReverse(adjFrom, adjTo, lastAvailableSegmentTo);
        try (ICloseableIterator<TimeRange> it = segmentsReverse.iterator()) {
            V latestValue = null;
            while (it.hasNext()) {
                final TimeRange segment = it.next();
                final SegmentedKey<K> segmentedKey = new SegmentedKey<K>(key, segment);
                maybeInitSegment(segmentedKey);
                final V newValue = segmentedTable.getLatestValue(segmentedKey, date);
                if (newValue != null) {
                    final FDate newValueTime = segmentedTable.extractEndTime(newValue);
                    if (newValueTime.isBeforeOrEqualTo(date)) {
                        /*
                             * even if we got the first value in this segment and it is after the desired key we just
                             * continue to the beginning to search for an earlier value until we reach the overall
                             * firstValue
                             */
                        latestValue = newValue;
                        break;
                    }
                }
            }
            if (latestValue == null) {
                latestValue = getFirstValue();
            }
            if (latestValue == null) {
                return null;
            }
            return new SingleValue(valueSerde, latestValue);
        }
    });
    if (value == null) {
        return null;
    }
    return value.getValue(valueSerde);
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) SingleValue(de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue) FDate(de.invesdwin.util.time.date.FDate)

Example 22 with TimeRange

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

the class ASegmentedTimeSeriesStorageCache method initSegment.

private void initSegment(final SegmentedKey<K> segmentedKey, final Function<SegmentedKey<K>, ICloseableIterable<? extends V>> source) {
    try {
        final ITimeSeriesUpdater<SegmentedKey<K>, V> updater = newSegmentUpdater(segmentedKey, source);
        final Callable<Void> task = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                // write lock is reentrant
                updater.update();
                return null;
            }
        };
        final String taskName = "Loading " + getElementsName() + " for " + hashKey;
        final Callable<Percent> progress = new Callable<Percent>() {

            @Override
            public Percent call() throws Exception {
                return updater.getProgress();
            }
        };
        TaskInfoCallable.of(taskName, task, progress).call();
        final FDate minTime = updater.getMinTime();
        if (minTime != null) {
            final FDate segmentFrom = segmentedKey.getSegment().getFrom();
            final TimeRange prevSegment = getSegmentFinder(segmentedKey.getKey()).query().getValue(segmentFrom.addMilliseconds(-1));
            if (prevSegment.getTo().equalsNotNullSafe(segmentFrom) && minTime.isBeforeOrEqualTo(segmentFrom)) {
                throw new IllegalStateException(segmentedKey + ": minTime [" + minTime + "] should not be before or equal to segmentFrom [" + segmentFrom + "] when overlapping segments are used");
            } else if (minTime.isBefore(segmentFrom)) {
                throw new IllegalStateException(segmentedKey + ": minTime [" + minTime + "] should not be before segmentFrom [" + segmentFrom + "] when non overlapping segments are used");
            }
            final FDate maxTime = updater.getMaxTime();
            final FDate segmentTo = segmentedKey.getSegment().getTo();
            if (maxTime.isAfter(segmentTo)) {
                throw new IllegalStateException(segmentedKey + ": maxTime [" + maxTime + "] should not be after segmentTo [" + segmentTo + "]");
            }
        }
    } catch (final Throwable t) {
        if (Throwables.isCausedByType(t, IncompleteUpdateFoundException.class)) {
            segmentedTable.deleteRange(new SegmentedKey<K>(segmentedKey.getKey(), segmentedKey.getSegment()));
            throw new RetryLaterRuntimeException(t);
        } else {
            throw Throwables.propagate(t);
        }
    }
}
Also used : Percent(de.invesdwin.util.math.decimal.scaled.Percent) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) Callable(java.util.concurrent.Callable) ARetryCallable(de.invesdwin.context.integration.retry.task.ARetryCallable) TaskInfoCallable(de.invesdwin.util.concurrent.taskinfo.provider.TaskInfoCallable) FDate(de.invesdwin.util.time.date.FDate) TimeRange(de.invesdwin.util.time.range.TimeRange) IncompleteUpdateFoundException(de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)

Example 23 with TimeRange

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

the class ASegmentedTimeSeriesStorageCache method deleteAll.

public synchronized void deleteAll() {
    final ADelegateRangeTable<String, TimeRange, SegmentStatus> segmentStatusTable = storage.getSegmentStatusTable();
    final List<TimeRange> rangeKeys;
    try (ICloseableIterator<TimeRange> rangeKeysIterator = new ATransformingIterator<RangeTableRow<String, TimeRange, SegmentStatus>, TimeRange>(segmentStatusTable.range(hashKey)) {

        @Override
        protected TimeRange transform(final RangeTableRow<String, TimeRange, SegmentStatus> value) {
            return value.getRangeKey();
        }
    }) {
        rangeKeys = Lists.toListWithoutHasNext(rangeKeysIterator);
    }
    for (int i = 0; i < rangeKeys.size(); i++) {
        final TimeRange rangeKey = rangeKeys.get(i);
        segmentedTable.deleteRange(new SegmentedKey<K>(key, rangeKey));
    }
    segmentStatusTable.deleteRange(hashKey);
    storage.deleteRange_latestValueLookupTable(hashKey);
    storage.deleteRange_nextValueLookupTable(hashKey);
    storage.deleteRange_previousValueLookupTable(hashKey);
    clearCaches();
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) ATransformingIterator(de.invesdwin.util.collections.iterable.ATransformingIterator) RangeTableRow(ezdb.table.RangeTableRow)

Example 24 with TimeRange

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

the class PersistentLiveSegment method putNextLiveValues.

public void putNextLiveValues(final ICloseableIterable<V> memoryValues) {
    final ADelegateRangeTable<String, TimeRange, SegmentStatus> segmentStatusTable = historicalSegmentTable.getStorage().getSegmentStatusTable();
    final SegmentStatus existingStatus = segmentStatusTable.get(hashKey, segmentedKey.getSegment());
    if (existingStatus == null) {
        segmentStatusTable.put(hashKey, segmentedKey.getSegment(), SegmentStatus.INITIALIZING);
    } else if (existingStatus != SegmentStatus.INITIALIZING) {
        throw UnknownArgumentException.newInstance(SegmentStatus.class, existingStatus);
    }
    final ATimeSeriesUpdater<SegmentedKey<K>, V> updater = new ATimeSeriesUpdater<SegmentedKey<K>, V>(segmentedKey, table) {

        @Override
        protected ICloseableIterable<? extends V> getSource(final FDate updateFrom) {
            return memoryValues;
        }

        @Override
        protected void onUpdateFinished(final Instant updateStart) {
        }

        @Override
        protected void onUpdateStart() {
        }

        @Override
        protected FDate extractEndTime(final V element) {
            return historicalSegmentTable.extractEndTime(element);
        }

        @Override
        protected void onFlush(final int flushIndex, final ATimeSeriesUpdater<SegmentedKey<K>, V>.UpdateProgress updateProgress) {
        }

        @Override
        protected boolean shouldRedoLastFile() {
            return false;
        }

        @Override
        public Percent getProgress() {
            return null;
        }
    };
    try {
        Assertions.checkTrue(updater.update());
    } catch (final IncompleteUpdateFoundException e) {
        throw new RuntimeException(e);
    }
    empty = false;
}
Also used : SegmentStatus(de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentStatus) Instant(de.invesdwin.util.time.Instant) FDate(de.invesdwin.util.time.date.FDate) TimeRange(de.invesdwin.util.time.range.TimeRange) ATimeSeriesUpdater(de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater) SegmentedKey(de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentedKey) IncompleteUpdateFoundException(de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)

Example 25 with TimeRange

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

the class PeriodicalSegmentFinder method calculateNextTimeRange.

private TimeRange calculateNextTimeRange(final FDate curTimeRangeFrom) {
    final FDate nextTimeRangeStart = incrementFunction.apply(curTimeRangeFrom);
    final FDate nextTimeRangeEnd = incrementFunction.apply(nextTimeRangeStart).addMilliseconds(-1);
    return new TimeRange(nextTimeRangeStart, nextTimeRangeEnd);
}
Also used : TimeRange(de.invesdwin.util.time.range.TimeRange) IEvaluateGenericFDate(de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate) FDate(de.invesdwin.util.time.date.FDate)

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