Search in sources :

Example 26 with TimeRange

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

the class PeriodicalSegmentFinder method calculatePrevTimeRange.

private TimeRange calculatePrevTimeRange(final FDate curTimeRangeFrom) {
    final FDate prevTimeRangeStart = decrementFunction.apply(curTimeRangeFrom);
    final FDate prevTimeRangeEnd = curTimeRangeFrom.addMilliseconds(-1);
    return new TimeRange(prevTimeRangeStart, prevTimeRangeEnd);
}
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 27 with TimeRange

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

Example 28 with TimeRange

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

the class ALiveSegmentedTimeSeriesDBWithoutShiftKeysAndQueryInterceptorTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    final AHistoricalCache<TimeRange> segmentFinder = PeriodicalSegmentFinder.newCache(new Duration(2, FTimeUnit.YEARS), false);
    table = new ALiveSegmentedTimeSeriesDB<String, FDate>(getClass().getSimpleName()) {

        private FDate curTime = null;

        @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() || curTime == null) {
                return null;
            }
            final FDate firstTime = FDates.min(curTime, entities.get(0));
            final TimeRange firstSegment = segmentFinder.query().getValue(firstTime);
            if (firstSegment.getTo().isBeforeOrEqualTo(curTime)) {
                return firstSegment.getFrom();
            } else {
                return segmentFinder.query().getValue(firstSegment.getFrom().addMilliseconds(-1)).getFrom();
            }
        }

        @Override
        public FDate getLastAvailableHistoricalSegmentTo(final String key, final FDate updateTo) {
            if (entities.isEmpty() || curTime == null) {
                return null;
            }
            final TimeRange lastSegment = segmentFinder.query().getValue(curTime);
            if (lastSegment.getTo().isBeforeOrEqualTo(curTime)) {
                return lastSegment.getTo();
            } else {
                return segmentFinder.query().getValue(lastSegment.getFrom().addMilliseconds(-1)).getTo();
            }
        }

        @Override
        public void putNextLiveValue(final String key, final FDate nextLiveValue) {
            curTime = nextLiveValue;
            super.putNextLiveValue(key, nextLiveValue);
        }

        @Override
        protected String getElementsName() {
            return "values";
        }
    };
    for (final FDate entity : entities) {
        table.putNextLiveValue(KEY, entity);
    }
}
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 29 with TimeRange

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

the class HeapLiveSegmentTest method testInverseOrder.

@Test
public void testInverseOrder() {
    final Map<Integer, FDate> extractTime = new HashMap<>();
    final SegmentedKey<FDate> segmentedKey = new SegmentedKey<FDate>(FDate.MIN_DATE, new TimeRange(FDate.MIN_DATE, FDate.MAX_DATE));
    final ALiveSegmentedTimeSeriesDB<FDate, Integer> timeSeriesDB = new ALiveSegmentedTimeSeriesDB<FDate, Integer>("testInverseOrder") {

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

        @Override
        protected ICloseableIterable<? extends Integer> downloadSegmentElements(final SegmentedKey<FDate> segmentedKey) {
            throw new UnsupportedOperationException();
        }

        @Override
        public AHistoricalCache<TimeRange> getSegmentFinder(final FDate key) {
            throw new UnsupportedOperationException();
        }

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

        @Override
        protected ISerde<Integer> newValueSerde() {
            return IntegerSerde.GET;
        }

        @Override
        protected FDate extractEndTime(final Integer value) {
            return extractTime.get(value);
        }

        @Override
        protected String innerHashKeyToString(final FDate key) {
            throw new UnsupportedOperationException();
        }

        @Override
        public FDate getFirstAvailableHistoricalSegmentFrom(final FDate key) {
            throw new UnsupportedOperationException();
        }

        @Override
        public FDate getLastAvailableHistoricalSegmentTo(final FDate key, final FDate updateTo) {
            throw new UnsupportedOperationException();
        }

        @Override
        protected String getElementsName() {
            throw new UnsupportedOperationException();
        }
    };
    @SuppressWarnings("unchecked") final ALiveSegmentedTimeSeriesDB<FDate, Integer>.HistoricalSegmentTable historicalSegmentTable = Reflections.field("historicalSegmentTable").ofType(HistoricalSegmentTable.class).in(timeSeriesDB).get();
    final HeapLiveSegment<FDate, Integer> rangeTable = new HeapLiveSegment<FDate, Integer>(segmentedKey, historicalSegmentTable);
    final FDate now = FDateBuilder.newDate(2000);
    final FDate oneDate = now.addDays(1);
    final FDate twoDate = now.addDays(2);
    final FDate threeDate = now.addDays(3);
    rangeTable.putNextLiveValue(oneDate, 1);
    extractTime.put(1, oneDate);
    rangeTable.putNextLiveValue(twoDate, 2);
    extractTime.put(2, twoDate);
    rangeTable.putNextLiveValue(threeDate, 3);
    extractTime.put(3, threeDate);
    final ICloseableIterator<Integer> range3 = rangeTable.rangeValues(now, null, DisabledLock.INSTANCE, null).iterator();
    Assertions.assertThat(range3.next()).isEqualTo(1);
    Assertions.assertThat(range3.next()).isEqualTo(2);
    Assertions.assertThat(range3.next()).isEqualTo(3);
    Assertions.assertThat(range3.hasNext()).isFalse();
    try {
        range3.next();
        Fail.fail("Exception expected!");
    } catch (final NoSuchElementException e) {
        Assertions.assertThat(e).isNotNull();
    }
    // should already be closed but should not cause an error when calling again
    range3.close();
    final ICloseableIterator<Integer> rangeNone = rangeTable.rangeValues(null, null, DisabledLock.INSTANCE, null).iterator();
    Assertions.assertThat(rangeNone.next()).isEqualTo(1);
    Assertions.assertThat(rangeNone.next()).isEqualTo(2);
    Assertions.assertThat(rangeNone.next()).isEqualTo(3);
    Assertions.assertThat(rangeNone.hasNext()).isFalse();
    try {
        rangeNone.next();
        Fail.fail("Exception expected!");
    } catch (final NoSuchElementException e) {
        Assertions.assertThat(e).isNotNull();
    }
    final ICloseableIterator<Integer> rangeMin = rangeTable.rangeValues(FDate.MIN_DATE, null, DisabledLock.INSTANCE, null).iterator();
    Assertions.assertThat(rangeMin.next()).isEqualTo(1);
    Assertions.assertThat(rangeMin.next()).isEqualTo(2);
    Assertions.assertThat(rangeMin.next()).isEqualTo(3);
    Assertions.assertThat(rangeMin.hasNext()).isFalse();
    try {
        rangeMin.next();
        Fail.fail("Exception expected!");
    } catch (final NoSuchElementException e) {
        Assertions.assertThat(e).isNotNull();
    }
    final ICloseableIterator<Integer> rangeMax = rangeTable.rangeValues(FDate.MAX_DATE, null, DisabledLock.INSTANCE, null).iterator();
    Assertions.assertThat(rangeMax.hasNext()).isFalse();
    try {
        rangeMax.next();
        Fail.fail("Exception expected!");
    } catch (final NoSuchElementException e) {
        Assertions.assertThat(e).isNotNull();
    }
    final ICloseableIterator<Integer> range2 = rangeTable.rangeValues(twoDate, null, DisabledLock.INSTANCE, null).iterator();
    Assertions.assertThat(range2.next()).isEqualTo(2);
    Assertions.assertThat(range2.next()).isEqualTo(3);
    Assertions.assertThat(range2.hasNext()).isFalse();
    try {
        range2.next();
        Fail.fail("Exception expected!");
    } catch (final NoSuchElementException e) {
        Assertions.assertThat(e).isNotNull();
    }
    testReverse(rangeTable, oneDate, twoDate, threeDate);
    testGetLatestForRange(rangeTable, oneDate, twoDate, threeDate);
    rangeTable.close();
}
Also used : ALiveSegmentedTimeSeriesDB(de.invesdwin.context.persistence.timeseriesdb.segmented.live.ALiveSegmentedTimeSeriesDB) HashMap(java.util.HashMap) FDate(de.invesdwin.util.time.date.FDate) TimeRange(de.invesdwin.util.time.range.TimeRange) SegmentedKey(de.invesdwin.context.persistence.timeseriesdb.segmented.SegmentedKey) NoSuchElementException(java.util.NoSuchElementException) ATest(de.invesdwin.context.test.ATest) Test(org.junit.jupiter.api.Test)

Example 30 with TimeRange

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

the class ALiveSegmentedTimeSeriesDBWithLimitedCacheTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    final AHistoricalCache<TimeRange> segmentFinder = PeriodicalSegmentFinder.newCache(new Duration(2, FTimeUnit.YEARS), false);
    table = new ALiveSegmentedTimeSeriesDB<String, FDate>(getClass().getSimpleName()) {

        private FDate curTime = null;

        @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() || curTime == null) {
                return null;
            }
            final FDate firstTime = FDates.min(curTime, entities.get(0));
            final TimeRange firstSegment = segmentFinder.query().getValue(firstTime);
            if (firstSegment.getTo().isBeforeOrEqualTo(curTime)) {
                return firstSegment.getFrom();
            } else {
                return segmentFinder.query().getValue(firstSegment.getFrom().addMilliseconds(-1)).getFrom();
            }
        }

        @Override
        public FDate getLastAvailableHistoricalSegmentTo(final String key, final FDate updateTo) {
            if (entities.isEmpty() || curTime == null) {
                return null;
            }
            final TimeRange lastSegment = segmentFinder.query().getValue(curTime);
            if (lastSegment.getTo().isBeforeOrEqualTo(curTime)) {
                return lastSegment.getTo();
            } else {
                return segmentFinder.query().getValue(lastSegment.getFrom().addMilliseconds(-1)).getTo();
            }
        }

        @Override
        public void putNextLiveValue(final String key, final FDate nextLiveValue) {
            curTime = nextLiveValue;
            super.putNextLiveValue(key, nextLiveValue);
        }

        @Override
        protected String getElementsName() {
            return "values";
        }
    };
    for (final FDate entity : entities) {
        table.putNextLiveValue(KEY, entity);
    }
}
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)

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