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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
}
Aggregations