use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class MuninnPageCacheTest method writeInitialDataTo.
private void writeInitialDataTo(File file) throws IOException {
StoreChannel channel = fs.create(file);
ByteBuffer buf = ByteBuffer.allocate(16);
buf.putLong(x);
buf.putLong(y);
buf.flip();
channel.writeAll(buf);
channel.close();
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapperTest method swappingOutMustNotOverwriteDataBeyondPage.
@Test
public void swappingOutMustNotOverwriteDataBeyondPage() throws Exception {
byte[] initialData = new byte[] { // --- page 0:
1, 2, 3, 4, // --- page 1:
5, 6, 7, 8, // --- page 2:
9, 10 };
byte[] finalData = new byte[] { // --- page 0:
1, 2, 3, 4, // --- page 1:
8, 7, 6, 5, // --- page 2:
9, 10 };
StoreChannel channel = getFs().create(getFile());
channel.writeAll(wrap(initialData));
channel.close();
byte[] change = new byte[] { 8, 7, 6, 5 };
ByteBufferPage page = new ByteBufferPage(wrap(change));
PageSwapperFactory factory = createSwapperFactory();
PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
swapper.write(1, page);
InputStream stream = getFs().openAsInputStream(getFile());
byte[] actual = new byte[(int) getFs().getFileSize(getFile())];
assertThat(stream.read(actual), is(actual.length));
assertThat(actual, byteArray(finalData));
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustZeroFillPageBeyondEndOfFile.
@Test
public void mustZeroFillPageBeyondEndOfFile() throws Exception {
byte[] bytes = new byte[] { // --- page 0:
1, 2, 3, 4, // --- page 1:
5, 6 };
StoreChannel channel = getFs().create(getFile());
channel.writeAll(wrap(bytes));
channel.close();
PageSwapperFactory factory = createSwapperFactory();
PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
ByteBuffer target = ByteBuffer.allocateDirect(4);
ByteBufferPage page = new ByteBufferPage(target);
swapper.read(1, page);
assertThat(array(target), byteArray(new byte[] { 5, 6, 0, 0 }));
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapperTest method swappingInMustFillPageWithData.
@Test
public void swappingInMustFillPageWithData() throws Exception {
byte[] bytes = new byte[] { 1, 2, 3, 4 };
StoreChannel channel = getFs().create(getFile());
channel.writeAll(wrap(bytes));
channel.close();
PageSwapperFactory factory = createSwapperFactory();
PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
ByteBuffer target = ByteBuffer.allocateDirect(4);
ByteBufferPage page = new ByteBufferPage(target);
swapper.read(0, page);
assertThat(array(target), byteArray(bytes));
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustCloseFilesIfTakingFileLockThrows.
@Test
public void mustCloseFilesIfTakingFileLockThrows() throws Exception {
// no file locking on Windows.
assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
final AtomicInteger openFilesCounter = new AtomicInteger();
PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction(new DelegatingFileSystemAbstraction(fileSystem) {
@Override
public StoreChannel open(File fileName, String mode) throws IOException {
openFilesCounter.getAndIncrement();
return new DelegatingStoreChannel(super.open(fileName, mode)) {
@Override
public void close() throws IOException {
openFilesCounter.getAndDecrement();
super.close();
}
};
}
});
File file = testDir.file("file");
try (StoreChannel ch = fileSystem.create(file);
FileLock ignore = ch.tryLock()) {
createSwapper(factory, file, 4, NO_CALLBACK, false).close();
fail("Creating a page swapper for a locked channel should have thrown");
} catch (FileLockException e) {
// As expected.
}
assertThat(openFilesCounter.get(), is(0));
}
Aggregations