Search in sources :

Example 96 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class ReaderLogVersionBridgeTest method shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet.

@Test
public void shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet() throws Exception {
    // given
    final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(fs, logFiles);
    final StoreChannel nextVersionWithIncompleteHeader = mock(StoreChannel.class);
    when(nextVersionWithIncompleteHeader.read(any(ByteBuffer.class))).thenReturn(LOG_HEADER_SIZE / 2);
    when(channel.getVersion()).thenReturn(version);
    when(fs.fileExists(file)).thenReturn(true);
    when(logFiles.getLogFileForVersion(version + 1)).thenReturn(file);
    when(fs.open(file, "r")).thenReturn(nextVersionWithIncompleteHeader);
    // when
    final LogVersionedStoreChannel result = bridge.next(channel);
    // then
    assertEquals(channel, result);
    verify(channel, never()).close();
}
Also used : LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 97 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class ReaderLogVersionBridgeTest method shouldOpenTheNextChannelWhenItExists.

@Test
public void shouldOpenTheNextChannelWhenItExists() throws IOException {
    // given
    final StoreChannel newStoreChannel = mock(StoreChannel.class);
    final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(fs, logFiles);
    when(channel.getVersion()).thenReturn(version);
    when(channel.getLogFormatVersion()).thenReturn(CURRENT_LOG_VERSION);
    when(logFiles.getLogFileForVersion(version + 1)).thenReturn(file);
    when(fs.fileExists(file)).thenReturn(true);
    when(fs.open(file, "r")).thenReturn(newStoreChannel);
    when(newStoreChannel.read(Matchers.<ByteBuffer>any())).then(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
            ByteBuffer buffer = (ByteBuffer) invocationOnMock.getArguments()[0];
            buffer.putLong(encodeLogVersion(version + 1));
            buffer.putLong(42);
            return LOG_HEADER_SIZE;
        }
    });
    // when
    final LogVersionedStoreChannel result = bridge.next(channel);
    // then
    PhysicalLogVersionedStoreChannel expected = new PhysicalLogVersionedStoreChannel(newStoreChannel, version + 1, CURRENT_LOG_VERSION);
    assertEquals(expected, result);
    verify(channel, times(1)).close();
}
Also used : LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ReaderLogVersionBridge(org.neo4j.kernel.impl.transaction.log.ReaderLogVersionBridge) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 98 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class MigrationTestUtils method verifyFilesHaveSameContent.

public static void verifyFilesHaveSameContent(FileSystemAbstraction fileSystem, File original, File other) throws IOException {
    final int bufferBatchSize = 32 * 1024;
    File[] files = fileSystem.listFiles(original);
    for (File originalFile : files) {
        File otherFile = new File(other, originalFile.getName());
        if (!fileSystem.isDirectory(originalFile)) {
            try (StoreChannel originalChannel = fileSystem.open(originalFile, "r");
                StoreChannel otherChannel = fileSystem.open(otherFile, "r")) {
                ByteBuffer buffer = ByteBuffer.allocate(bufferBatchSize);
                while (true) {
                    if (!readAndFlip(originalChannel, buffer, bufferBatchSize)) {
                        break;
                    }
                    byte[] originalBytes = new byte[buffer.limit()];
                    buffer.get(originalBytes);
                    if (!readAndFlip(otherChannel, buffer, bufferBatchSize)) {
                        fail("Files have different sizes");
                    }
                    byte[] otherBytes = new byte[buffer.limit()];
                    buffer.get(otherBytes);
                    assertArrayEquals("Different content in " + originalFile, originalBytes, otherBytes);
                }
            }
        }
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 99 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class MigrationTestUtils method truncateFile.

/**
     * Removes the version trailer from the store files.
     */
public static void truncateFile(FileSystemAbstraction fileSystem, File storeFile, String suffixToDetermineTruncationLength) throws IOException {
    byte[] versionBytes = UTF8.encode(suffixToDetermineTruncationLength);
    if (!fileSystem.fileExists(storeFile)) {
        return;
    }
    try (StoreChannel fileChannel = fileSystem.open(storeFile, "rw")) {
        long fileSize = fileSystem.getFileSize(storeFile);
        fileChannel.truncate(Math.max(0, fileSize - versionBytes.length));
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel)

Example 100 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class ReadAheadLogChannelTest method writeSomeData.

private void writeSomeData(File file, Visitor<ByteBuffer, IOException> visitor) throws IOException {
    try (StoreChannel channel = fileSystemRule.get().open(file, "rw")) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        visitor.visit(buffer);
        buffer.flip();
        channel.write(buffer);
    }
}
Also used : LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) ByteBuffer(java.nio.ByteBuffer)

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