use of de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream 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);
}
};
}
use of de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream in project invesdwin-context-persistence by subes.
the class FileLiveSegment method getFlushedValues.
private SerializingCollection<V> getFlushedValues() {
synchronized (this) {
if (needsFlush) {
values.flush();
needsFlush = false;
}
}
final TextDescription name = new TextDescription("%s[%s]: getFlushedValues()", FileLiveSegment.class.getSimpleName(), segmentedKey);
return new SerializingCollection<V>(name, values.getFile(), true) {
@Override
protected ISerde<V> newSerde() {
return historicalSegmentTable.newValueSerde();
}
@Override
protected Integer getFixedLength() {
return historicalSegmentTable.newValueFixedLength();
}
@Override
protected OutputStream newCompressor(final OutputStream out) {
return compressionFactory.newCompressor(out, LARGE_COMPRESSOR);
}
@Override
protected InputStream newDecompressor(final InputStream inputStream) {
return compressionFactory.newDecompressor(inputStream);
}
@Override
protected InputStream newFileInputStream(final File file) throws IOException {
// keep file input stream open as shorty as possible to prevent too many open files error
try (InputStream fis = super.newFileInputStream(file)) {
final PooledFastByteArrayOutputStream bos = PooledFastByteArrayOutputStream.newInstance();
IOUtils.copy(fis, 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);
}
}
};
}
Aggregations