Search in sources :

Example 61 with StoreChannel

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();
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) ByteBuffer(java.nio.ByteBuffer)

Example 62 with StoreChannel

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));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 63 with StoreChannel

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 }));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) ByteBuffer(java.nio.ByteBuffer) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 64 with StoreChannel

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));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) ByteBuffer(java.nio.ByteBuffer) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 65 with StoreChannel

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));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) FileLock(java.nio.channels.FileLock) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)113 Test (org.junit.Test)71 File (java.io.File)65 ByteBuffer (java.nio.ByteBuffer)48 IOException (java.io.IOException)20 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)17 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)15 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)13 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)11 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)10 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)7 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)7 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)7 InputStream (java.io.InputStream)6 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)6 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)6 HashSet (java.util.HashSet)5 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)5 PageSwapper (org.neo4j.io.pagecache.PageSwapper)5 PagedFile (org.neo4j.io.pagecache.PagedFile)5