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