Search in sources :

Example 1 with SingleValue

use of de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesStorageCache method getNextValue.

public V getNextValue(final FDate date, final int shiftForwardUnits) {
    assertShiftUnitsPositiveNonZero(shiftForwardUnits);
    final V lastValue = getLastValue();
    final FDate lastTime = segmentedTable.extractEndTime(lastValue);
    if (date.isAfterOrEqualTo(lastTime)) {
        return lastValue;
    } else {
        final SingleValue value = storage.getOrLoad_nextValueLookupTable(hashKey, date, shiftForwardUnits, () -> {
            final MutableReference<V> nextValue = new MutableReference<>();
            final MutableInt shiftForwardRemaining = new MutableInt(shiftForwardUnits);
            try (ICloseableIterator<V> rangeValues = readRangeValues(date, null, DisabledLock.INSTANCE, new ISkipFileFunction() {

                @Override
                public boolean skipFile(final MemoryFileSummary file) {
                    final boolean skip = nextValue.get() != null && file.getValueCount() < shiftForwardRemaining.intValue();
                    if (skip) {
                        shiftForwardRemaining.subtract(file.getValueCount());
                    }
                    return skip;
                }
            }).iterator()) {
                while (shiftForwardRemaining.intValue() >= 0) {
                    nextValue.set(rangeValues.next());
                    shiftForwardRemaining.decrement();
                }
            } catch (final NoSuchElementException e) {
            // ignore
            }
            return new SingleValue(valueSerde, nextValue.get());
        });
        return value.getValue(valueSerde);
    }
}
Also used : MemoryFileSummary(de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary) SingleValue(de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MutableReference(de.invesdwin.util.concurrent.reference.MutableReference) ISkipFileFunction(de.invesdwin.context.persistence.timeseriesdb.storage.ISkipFileFunction) FDate(de.invesdwin.util.time.date.FDate) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with SingleValue

use of de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method getNextValue.

public V getNextValue(final FDate date, final int shiftForwardUnits) {
    assertShiftUnitsPositiveNonZero(shiftForwardUnits);
    final V lastValue = getLastValue();
    if (lastValue == null) {
        return null;
    }
    final FDate lastTime = extractEndTime.apply(lastValue);
    if (date.isAfterOrEqualTo(lastTime)) {
        return lastValue;
    } else {
        final SingleValue value = storage.getOrLoad_nextValueLookupTable(hashKey, date, shiftForwardUnits, () -> {
            final MutableReference<V> nextValue = new MutableReference<>();
            final MutableInt shiftForwardRemaining = new MutableInt(shiftForwardUnits);
            try (ICloseableIterator<V> rangeValues = readRangeValues(date, null, DisabledLock.INSTANCE, new ISkipFileFunction() {

                @Override
                public boolean skipFile(final MemoryFileSummary file) {
                    final boolean skip = nextValue.get() != null && file.getValueCount() < shiftForwardRemaining.intValue();
                    if (skip) {
                        shiftForwardRemaining.subtract(file.getValueCount());
                    }
                    return skip;
                }
            })) {
                while (shiftForwardRemaining.intValue() >= 0) {
                    nextValue.set(rangeValues.next());
                    shiftForwardRemaining.decrement();
                }
            } catch (final NoSuchElementException e) {
            // ignore
            }
            return new SingleValue(valueSerde, nextValue.get());
        });
        return value.getValue(valueSerde);
    }
}
Also used : MemoryFileSummary(de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary) SingleValue(de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MutableReference(de.invesdwin.util.concurrent.reference.MutableReference) ISkipFileFunction(de.invesdwin.context.persistence.timeseriesdb.storage.ISkipFileFunction) FDate(de.invesdwin.util.time.date.FDate) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with SingleValue

use of de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesStorageCache method getPreviousValue.

public V getPreviousValue(final FDate date, final int shiftBackUnits) {
    assertShiftUnitsPositiveNonZero(shiftBackUnits);
    final V firstValue = getFirstValue();
    final FDate firstTime = segmentedTable.extractEndTime(firstValue);
    if (date.isBeforeOrEqualTo(firstTime)) {
        return firstValue;
    } else {
        final SingleValue value = storage.getOrLoad_previousValueLookupTable(hashKey, date, shiftBackUnits, () -> {
            final MutableReference<V> previousValue = new MutableReference<>();
            final MutableInt shiftBackRemaining = new MutableInt(shiftBackUnits);
            try (ICloseableIterator<V> rangeValuesReverse = readRangeValuesReverse(date, null, DisabledLock.INSTANCE, new ISkipFileFunction() {

                @Override
                public boolean skipFile(final MemoryFileSummary file) {
                    final boolean skip = previousValue.get() != null && file.getValueCount() < shiftBackRemaining.intValue();
                    if (skip) {
                        shiftBackRemaining.add(file.getValueCount());
                    }
                    return skip;
                }
            }).iterator()) {
                while (shiftBackRemaining.intValue() >= 0) {
                    previousValue.set(rangeValuesReverse.next());
                    shiftBackRemaining.decrement();
                }
            } catch (final NoSuchElementException e) {
            // ignore
            }
            return new SingleValue(valueSerde, previousValue.get());
        });
        return value.getValue(valueSerde);
    }
}
Also used : MemoryFileSummary(de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary) SingleValue(de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MutableReference(de.invesdwin.util.concurrent.reference.MutableReference) ISkipFileFunction(de.invesdwin.context.persistence.timeseriesdb.storage.ISkipFileFunction) FDate(de.invesdwin.util.time.date.FDate) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with SingleValue

use of de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue 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 5 with SingleValue

use of de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method getPreviousValue.

public V getPreviousValue(final FDate date, final int shiftBackUnits) {
    assertShiftUnitsPositiveNonZero(shiftBackUnits);
    final V firstValue = getFirstValue();
    if (firstValue == null) {
        return null;
    }
    final FDate firstTime = extractEndTime.apply(firstValue);
    if (date.isBeforeOrEqualTo(firstTime)) {
        return firstValue;
    } else {
        final SingleValue value = storage.getOrLoad_previousValueLookupTable(hashKey, date, shiftBackUnits, () -> {
            final MutableReference<V> previousValue = new MutableReference<>();
            final MutableInt shiftBackRemaining = new MutableInt(shiftBackUnits);
            try (ICloseableIterator<V> rangeValuesReverse = readRangeValuesReverse(date, null, DisabledLock.INSTANCE, file -> {
                final boolean skip = previousValue.get() != null && file.getValueCount() < shiftBackRemaining.intValue();
                if (skip) {
                    shiftBackRemaining.subtract(file.getValueCount());
                }
                return skip;
            })) {
                while (shiftBackRemaining.intValue() >= 0) {
                    previousValue.set(rangeValuesReverse.next());
                    shiftBackRemaining.decrement();
                }
            } catch (final NoSuchElementException e) {
            // ignore
            }
            return new SingleValue(valueSerde, previousValue.get());
        });
        return value.getValue(valueSerde);
    }
}
Also used : SingleValue(de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MutableReference(de.invesdwin.util.concurrent.reference.MutableReference) FDate(de.invesdwin.util.time.date.FDate) FastNoSuchElementException(de.invesdwin.util.error.FastNoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

SingleValue (de.invesdwin.context.persistence.timeseriesdb.storage.SingleValue)5 FDate (de.invesdwin.util.time.date.FDate)5 MutableReference (de.invesdwin.util.concurrent.reference.MutableReference)4 FastNoSuchElementException (de.invesdwin.util.error.FastNoSuchElementException)4 NoSuchElementException (java.util.NoSuchElementException)4 MutableInt (org.apache.commons.lang3.mutable.MutableInt)4 ISkipFileFunction (de.invesdwin.context.persistence.timeseriesdb.storage.ISkipFileFunction)3 MemoryFileSummary (de.invesdwin.context.persistence.timeseriesdb.storage.MemoryFileSummary)3 TimeRange (de.invesdwin.util.time.range.TimeRange)1