- CryptoFS + InMemory Benchmark test

- faster growing in-memory files
This commit is contained in:
Sebastian Stenzel
2016-01-23 02:07:13 +01:00
parent 6479573346
commit a6bbc0ed44
4 changed files with 52 additions and 5 deletions
@@ -23,11 +23,18 @@ import org.cryptomator.filesystem.WritableFile;
class InMemoryFile extends InMemoryNode implements File {
/** 1000kb */
static final int INITIAL_SIZE = 100 * 1024;
/** 140% */
static final double GROWTH_RATE = 1.4;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private ByteBuffer content = ByteBuffer.allocate(0);
private volatile ByteBuffer content = ByteBuffer.allocate(INITIAL_SIZE);
public InMemoryFile(InMemoryFolder parent, String name, Instant lastModified, Instant creationTime) {
super(parent, name, lastModified, creationTime);
content.flip();
}
@Override
@@ -22,7 +22,7 @@ class InMemoryReadableFile implements ReadableFile {
private final Supplier<ByteBuffer> contentGetter;
private final ReadLock readLock;
private boolean open = true;
private int position = 0;
private volatile int position = 0;
public InMemoryReadableFile(Supplier<ByteBuffer> contentGetter, ReadLock readLock) {
this.contentGetter = contentGetter;
@@ -28,7 +28,7 @@ public class InMemoryWritableFile implements WritableFile {
private final WriteLock writeLock;
private boolean open = true;
private int position = 0;
private volatile int position = 0;
public InMemoryWritableFile(Consumer<Instant> lastModifiedSetter, Consumer<Instant> creationTimeSetter, Supplier<ByteBuffer> contentGetter, Consumer<ByteBuffer> contentSetter, Consumer<Void> deleter,
WriteLock writeLock) {
@@ -74,15 +74,19 @@ public class InMemoryWritableFile implements WritableFile {
@Override
public int write(ByteBuffer source) throws UncheckedIOException {
ByteBuffer destination = contentGetter.get();
int oldFileSize = destination.limit();
int requiredSize = position + source.remaining();
int newFileSize = Math.max(oldFileSize, requiredSize);
if (destination.capacity() < requiredSize) {
ByteBuffer old = destination;
old.rewind();
destination = ByteBuffer.allocate(requiredSize);
old.clear();
int newBufferSize = Math.max(requiredSize, (int) (destination.capacity() * InMemoryFile.GROWTH_RATE));
destination = ByteBuffer.allocate(newBufferSize);
ByteBuffers.copy(old, destination);
contentSetter.accept(destination);
}
destination.position(position);
destination.limit(newFileSize);
int numWritten = ByteBuffers.copy(source, destination);
this.position += numWritten;
return numWritten;