Search in sources :

Example 6 with ISerde

use of de.invesdwin.util.marshallers.serde.ISerde in project invesdwin-context-persistence by subes.

the class ALiveSegmentedTimeSeriesDBWithCacheTest 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 FDate extractEndTime(final FDate value) {
            return value;
        }

        @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
        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 7 with ISerde

use of de.invesdwin.util.marshallers.serde.ISerde in project invesdwin-context-persistence by subes.

the class ALiveSegmentedTimeSeriesDBWithNoCacheAndNoQueryCacheTest 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 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) IEvaluateGenericFDate(de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate) FDate(de.invesdwin.util.time.date.FDate) PeriodicalSegmentFinder(de.invesdwin.context.persistence.timeseriesdb.segmented.PeriodicalSegmentFinder) TimeRange(de.invesdwin.util.time.range.TimeRange) IFDateProvider(de.invesdwin.util.time.date.IFDateProvider) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Example 8 with ISerde

use of de.invesdwin.util.marshallers.serde.ISerde in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesDBWithoutShiftKeysAndQueryInterceptorTest 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";
        }
    };
}
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 9 with ISerde

use of de.invesdwin.util.marshallers.serde.ISerde in project invesdwin-context-persistence by subes.

the class TimeSeriesStorageCache method newResult.

private SerializingCollection<V> newResult(final String method, final MemoryFileSummary summary, final Lock readLock) {
    final TextDescription name = new TextDescription("%s[%s]: %s(%s)", ATimeSeriesUpdater.class.getSimpleName(), hashKey, method, summary);
    final File memoryFile = new File(summary.getMemoryResourceUri());
    return new SerializingCollection<V>(name, memoryFile, true) {

        @Override
        protected ISerde<V> newSerde() {
            return new ISerde<V>() {

                @Override
                public V fromBytes(final byte[] bytes) {
                    return valueSerde.fromBytes(bytes);
                }

                @Override
                public V fromBuffer(final IByteBuffer buffer, final int length) {
                    return valueSerde.fromBuffer(buffer, length);
                }

                @Override
                public int toBuffer(final IByteBuffer buffer, final V obj) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public byte[] toBytes(final V obj) {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        protected InputStream newFileInputStream(final File file) throws IOException {
            if (TimeseriesProperties.FILE_BUFFER_CACHE_MMAP_ENABLED) {
                readLock.lock();
                final MemoryMappedFile mmapFile = FileBufferCache.getFile(hashKey, summary.getMemoryResourceUri());
                if (mmapFile.incrementRefCount()) {
                    return new MmapInputStream(readLock, summary.newBuffer(mmapFile).asInputStream(), mmapFile);
                } else {
                    readLock.unlock();
                }
            }
            if (TimeseriesProperties.FILE_BUFFER_CACHE_SEGMENTS_ENABLED) {
                readLock.lock();
                // file buffer cache will close the file quickly
                final PreLockedBufferedFileDataInputStream in = new PreLockedBufferedFileDataInputStream(readLock, memoryFile);
                in.position(summary.getMemoryOffset());
                in.limit(summary.getMemoryOffset() + summary.getMemoryLength());
                return in;
            } else {
                // keep file input stream open as shorty as possible to prevent too many open files error
                readLock.lock();
                try (BufferedFileDataInputStream in = new BufferedFileDataInputStream(memoryFile)) {
                    in.position(summary.getMemoryOffset());
                    in.limit(summary.getMemoryOffset() + summary.getMemoryLength());
                    final PooledFastByteArrayOutputStream bos = PooledFastByteArrayOutputStream.newInstance();
                    IOUtils.copy(in, bos.asNonClosing());
                    return bos.asInputStream();
                } catch (final FileNotFoundException e) {
                    // maybe retry because of this in the outer iterator?
                    throw new RetryLaterRuntimeException("File might have been deleted in the mean time between read locks: " + file.getAbsolutePath(), e);
                } finally {
                    readLock.unlock();
                }
            }
        }

        @Override
        protected Integer getFixedLength() {
            return fixedLength;
        }

        @Override
        protected OutputStream newCompressor(final OutputStream out) {
            return storage.getCompressionFactory().newCompressor(out, ATimeSeriesUpdater.LARGE_COMPRESSOR);
        }

        @Override
        protected InputStream newDecompressor(final InputStream inputStream) {
            return storage.getCompressionFactory().newDecompressor(inputStream);
        }
    };
}
Also used : PreLockedBufferedFileDataInputStream(de.invesdwin.util.streams.pool.buffered.PreLockedBufferedFileDataInputStream) PreLockedDelegateInputStream(de.invesdwin.util.streams.PreLockedDelegateInputStream) PreLockedBufferedFileDataInputStream(de.invesdwin.util.streams.pool.buffered.PreLockedBufferedFileDataInputStream) BufferedFileDataInputStream(de.invesdwin.util.streams.pool.buffered.BufferedFileDataInputStream) InputStream(java.io.InputStream) TextDescription(de.invesdwin.util.lang.description.TextDescription) OutputStream(java.io.OutputStream) PooledFastByteArrayOutputStream(de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream) FileNotFoundException(java.io.FileNotFoundException) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) ISerde(de.invesdwin.util.marshallers.serde.ISerde) IByteBuffer(de.invesdwin.util.streams.buffer.bytes.IByteBuffer) PooledFastByteArrayOutputStream(de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream) PreLockedBufferedFileDataInputStream(de.invesdwin.util.streams.pool.buffered.PreLockedBufferedFileDataInputStream) BufferedFileDataInputStream(de.invesdwin.util.streams.pool.buffered.BufferedFileDataInputStream) ATimeSeriesUpdater(de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater) MemoryMappedFile(de.invesdwin.util.streams.buffer.MemoryMappedFile) MemoryMappedFile(de.invesdwin.util.streams.buffer.MemoryMappedFile) File(java.io.File)

Example 10 with ISerde

use of de.invesdwin.util.marshallers.serde.ISerde 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)

Aggregations

ISerde (de.invesdwin.util.marshallers.serde.ISerde)13 File (java.io.File)13 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)12 AHistoricalCache (de.invesdwin.util.collections.loadingcache.historical.AHistoricalCache)12 FDate (de.invesdwin.util.time.date.FDate)12 Duration (de.invesdwin.util.time.duration.Duration)12 TimeRange (de.invesdwin.util.time.range.TimeRange)12 IEvaluateGenericFDate (de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate)2 IFDateProvider (de.invesdwin.util.time.date.IFDateProvider)2 ExecutorService (java.util.concurrent.ExecutorService)2 RetryLaterRuntimeException (de.invesdwin.context.integration.retry.RetryLaterRuntimeException)1 PeriodicalSegmentFinder (de.invesdwin.context.persistence.timeseriesdb.segmented.PeriodicalSegmentFinder)1 ATimeSeriesUpdater (de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater)1 TextDescription (de.invesdwin.util.lang.description.TextDescription)1 PreLockedDelegateInputStream (de.invesdwin.util.streams.PreLockedDelegateInputStream)1 MemoryMappedFile (de.invesdwin.util.streams.buffer.MemoryMappedFile)1 IByteBuffer (de.invesdwin.util.streams.buffer.bytes.IByteBuffer)1 PooledFastByteArrayOutputStream (de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream)1 BufferedFileDataInputStream (de.invesdwin.util.streams.pool.buffered.BufferedFileDataInputStream)1 PreLockedBufferedFileDataInputStream (de.invesdwin.util.streams.pool.buffered.PreLockedBufferedFileDataInputStream)1