use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class TimeSeriesStorageCache method readRangeFilesReverse.
protected ICloseableIterable<File> readRangeFilesReverse(final FDate from, final FDate to) {
final FDate usedFrom;
if (from == null) {
final V lastValue = getLastValue();
if (lastValue == null) {
return EmptyCloseableIterable.getInstance();
}
usedFrom = extractTime.apply(lastValue);
} else {
usedFrom = from;
}
return new ICloseableIterable<File>() {
@Override
public ACloseableIterator<File> iterator() {
return new ACloseableIterator<File>() {
// use latest time available even if delegate iterator has no values
private FDate latestLastTime = fileLookupTable_latestRangeKeyCache.get(usedFrom);
// add 1 ms to not collide with firstTime
private final ICloseableIterator<FDate> delegate = getRangeKeysReverse(hashKey, usedFrom.addMilliseconds(-1), to);
private FDate delegateLastTime = null;
@Override
protected boolean innerHasNext() {
return latestLastTime != null || delegateLastTime != null || delegate.hasNext();
}
private ICloseableIterator<FDate> getRangeKeysReverse(final String hashKey, final FDate from, final FDate to) {
final ICloseableIterator<FDate> range = getAllRangeKeysReverse();
return new ASkippingIterator<FDate>(range) {
@Override
protected boolean skip(final FDate element) {
if (element.isAfter(from)) {
return true;
} else if (element.isBefore(to)) {
throw new FastNoSuchElementException("getRangeKeysReverse reached end");
}
return false;
}
};
}
@Override
protected File innerNext() {
final FDate time;
if (delegateLastTime != null) {
time = delegateLastTime;
delegateLastTime = null;
} else if (latestLastTime != null) {
time = latestLastTime;
latestLastTime = null;
if (delegate.hasNext()) {
// prevent duplicate first times
delegateLastTime = delegate.next();
if (delegateLastTime.isAfterOrEqualTo(time)) {
delegateLastTime = null;
}
}
} else {
time = delegate.next();
}
return newFile(time);
}
@Override
protected void innerClose() {
delegate.close();
}
};
}
};
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class TimeSeriesStorageCache method readRangeFiles.
protected ICloseableIterable<File> readRangeFiles(final FDate from, final FDate to) {
final FDate usedFrom;
if (from == null) {
final V firstValue = getFirstValue();
if (firstValue == null) {
return EmptyCloseableIterable.getInstance();
}
usedFrom = extractTime.apply(firstValue);
} else {
usedFrom = from;
}
return new ICloseableIterable<File>() {
@Override
public ACloseableIterator<File> iterator() {
return new ACloseableIterator<File>() {
// use latest time available even if delegate iterator has no values
private FDate latestFirstTime = fileLookupTable_latestRangeKeyCache.get(usedFrom);
// add 1 ms to not collide with firstTime
private final ICloseableIterator<FDate> delegate = getRangeKeys(hashKey, usedFrom.addMilliseconds(1), to);
private FDate delegateFirstTime = null;
@Override
protected boolean innerHasNext() {
return latestFirstTime != null || delegateFirstTime != null || delegate.hasNext();
}
private ICloseableIterator<FDate> getRangeKeys(final String hashKey, final FDate from, final FDate to) {
final ICloseableIterator<FDate> range = getAllRangeKeys();
return new ASkippingIterator<FDate>(range) {
@Override
protected boolean skip(final FDate element) {
if (element.isBefore(from)) {
return true;
} else if (element.isAfter(to)) {
throw new FastNoSuchElementException("getRangeKeys reached end");
}
return false;
}
};
}
@Override
protected File innerNext() {
final FDate time;
if (delegateFirstTime != null) {
time = delegateFirstTime;
delegateFirstTime = null;
} else if (latestFirstTime != null) {
time = latestFirstTime;
latestFirstTime = null;
if (delegate.hasNext()) {
// prevent duplicate first times
delegateFirstTime = delegate.next();
if (delegateFirstTime.isBeforeOrEqualTo(time)) {
delegateFirstTime = null;
}
}
} else {
time = delegate.next();
}
return newFile(time);
}
@Override
protected void innerClose() {
delegate.close();
}
};
}
};
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class ALiveSegmentedTimeSeriesDBWithNoCacheTest 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);
}
}
use of de.invesdwin.util.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class ASegmentedTimeSeriesDBWithCacheTest 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.collections.iterable.ICloseableIterable in project invesdwin-context-persistence by subes.
the class ATimeSeriesDBTest method testGetPreviousAndNext.
// CHECKSTYLE:ON
@Test
public void testGetPreviousAndNext() throws IncompleteUpdateFoundException {
final String key = "asdf";
final ATimeSeriesDB<String, FDate> table = new ATimeSeriesDB<String, FDate>("testGetPrevious") {
@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 FDate extractEndTime(final FDate value) {
return value;
}
@Override
protected File getBaseDirectory() {
return ContextProperties.TEMP_DIRECTORY;
}
};
final List<FDate> dates = new ArrayList<>();
for (int i = 2000; i <= 2010; i++) {
dates.add(FDateBuilder.newDate(i));
}
new ATimeSeriesUpdater<String, FDate>(key, table) {
@Override
protected ICloseableIterable<? extends FDate> getSource(final FDate updateFrom) {
return WrapperCloseableIterable.maybeWrap(dates);
}
@Override
protected void onUpdateFinished(final Instant updateStart) {
}
@Override
protected void onUpdateStart() {
}
@Override
protected FDate extractEndTime(final FDate element) {
return element;
}
@Override
protected void onFlush(final int flushIndex, final ATimeSeriesUpdater<String, FDate>.UpdateProgress updateProgress) {
}
@Override
public Percent getProgress() {
return null;
}
}.update();
for (int i = 1; i < dates.size(); i++) {
final FDate value = table.getPreviousValue(key, dates.get(dates.size() - 1), i);
final FDate expectedValue = dates.get(dates.size() - i - 1);
Assertions.checkEquals(value, expectedValue, i + ": expected [" + expectedValue + "] got [" + value + "]");
}
for (int i = 1; i < dates.size(); i++) {
final FDate value = table.getPreviousValue(key, FDate.MAX_DATE, i);
final FDate expectedValue = dates.get(dates.size() - i - 1);
Assertions.checkEquals(value, expectedValue, i + ": expected [" + expectedValue + "] got [" + value + "]");
}
for (int i = 1; i < dates.size(); i++) {
final FDate value = table.getPreviousValue(key, FDate.MIN_DATE, i);
final FDate expectedValue = dates.get(0);
Assertions.checkEquals(value, expectedValue, i + ": expected [" + expectedValue + "] got [" + value + "]");
}
for (int i = 1; i < dates.size(); i++) {
final FDate value = table.getNextValue(key, dates.get(0), i);
final FDate expectedValue = dates.get(i);
Assertions.checkEquals(value, expectedValue, i + ": expected [" + expectedValue + "] got [" + value + "]");
}
for (int i = 1; i < dates.size(); i++) {
final FDate value = table.getNextValue(key, FDate.MIN_DATE, i);
final FDate expectedValue = dates.get(i);
Assertions.checkEquals(value, expectedValue, i + ": expected [" + expectedValue + "] got [" + value + "]");
}
for (int i = 1; i < dates.size(); i++) {
final FDate value = table.getNextValue(key, FDate.MAX_DATE, i);
final FDate expectedValue = dates.get(dates.size() - 1);
Assertions.checkEquals(value, expectedValue, i + ": expected [" + expectedValue + "] got [" + value + "]");
}
}
Aggregations