Search in sources :

Example 86 with StoreChannel

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

the class ReadAheadChannelTest method createFile.

private void createFile(EphemeralFileSystemAbstraction fsa, File name, int bufferSize) throws IOException {
    StoreChannel storeChannel = fsa.open(name, "w");
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    for (int i = 0; i < bufferSize; i++) {
        buffer.put((byte) i);
    }
    buffer.flip();
    storeChannel.writeAll(buffer);
    storeChannel.close();
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) ByteBuffer(java.nio.ByteBuffer)

Example 87 with StoreChannel

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

the class ReadAheadChannelTest method shouldHandleRunningOutOfBytesWhenRequestSpansMultipleFiles.

@Test
public void shouldHandleRunningOutOfBytesWhenRequestSpansMultipleFiles() throws Exception {
    // Given
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    StoreChannel storeChannel1 = fileSystem.open(new File("foo.1"), "rw");
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.put((byte) 0);
    buffer.put((byte) 0);
    buffer.flip();
    storeChannel1.writeAll(buffer);
    storeChannel1.force(false);
    storeChannel1.close();
    buffer.flip();
    StoreChannel storeChannel2 = fileSystem.open(new File("foo.2"), "r");
    buffer.put((byte) 0);
    buffer.put((byte) 1);
    buffer.flip();
    storeChannel2.writeAll(buffer);
    storeChannel2.force(false);
    storeChannel2.close();
    storeChannel1 = fileSystem.open(new File("foo.1"), "r");
    final StoreChannel storeChannel2Copy = fileSystem.open(new File("foo.2"), "r");
    ReadAheadChannel<StoreChannel> channel = new ReadAheadChannel<StoreChannel>(storeChannel1) {

        @Override
        protected StoreChannel next(StoreChannel channel) throws IOException {
            return storeChannel2Copy;
        }
    };
    try {
        channel.getLong();
        fail("Should have thrown exception signalling end of file reached");
    } catch (ReadPastEndException endOfFile) {
    // outstanding
    }
    assertEquals(1, channel.getInt());
    try {
        channel.get();
        fail("Should have thrown exception signalling end of file reached");
    } catch (ReadPastEndException endOfFile) {
    // outstanding
    }
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) Test(org.junit.Test)

Example 88 with StoreChannel

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

the class ReadAheadChannelTest method shouldReturnPositionWithinBufferedStream.

@Test
public void shouldReturnPositionWithinBufferedStream() throws Exception {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    File file = new File("foo.txt");
    int readAheadSize = 512;
    int fileSize = readAheadSize * 8;
    createFile(fsa, file, fileSize);
    ReadAheadChannel<StoreChannel> bufferedReader = new ReadAheadChannel<>(fsa.open(file, "r"), readAheadSize);
    // when
    for (int i = 0; i < fileSize / Long.BYTES; i++) {
        assertEquals(Long.BYTES * i, bufferedReader.position());
        bufferedReader.getLong();
    }
    assertEquals(fileSize, bufferedReader.position());
    try {
        bufferedReader.getLong();
        fail();
    } catch (ReadPastEndException e) {
    // expected
    }
    assertEquals(fileSize, bufferedReader.position());
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) Test(org.junit.Test)

Example 89 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldSeeCorrectPositionEvenBeforeEmptyingDataIntoChannel.

@Test
public void shouldSeeCorrectPositionEvenBeforeEmptyingDataIntoChannel() throws Exception {
    // GIVEN
    final File file = new File(directory.directory(), "file");
    StoreChannel storeChannel = fileSystemRule.get().open(file, "rw");
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
    PositionAwarePhysicalFlushableChannel channel = new PositionAwarePhysicalFlushableChannel(versionedStoreChannel);
    LogPositionMarker positionMarker = new LogPositionMarker();
    LogPosition initialPosition = channel.getCurrentPosition(positionMarker).newPosition();
    // WHEN
    channel.putLong(67);
    channel.putInt(1234);
    LogPosition positionAfterSomeData = channel.getCurrentPosition(positionMarker).newPosition();
    // THEN
    assertEquals(12, positionAfterSomeData.getByteOffset() - initialPosition.getByteOffset());
    channel.close();
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 90 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldWriteThroughRotation.

@Test
public void shouldWriteThroughRotation() throws Exception {
    // GIVEN
    final File firstFile = new File(directory.directory(), "file1");
    final File secondFile = new File(directory.directory(), "file2");
    StoreChannel storeChannel = fileSystemRule.get().open(firstFile, "rw");
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
    PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
    // WHEN writing a transaction, of sorts
    byte byteValue = (byte) 4;
    short shortValue = (short) 10;
    int intValue = 3545;
    long longValue = 45849589L;
    float floatValue = 45849.332f;
    double doubleValue = 458493343D;
    byte[] byteArrayValue = new byte[] { 1, 4, 2, 5, 3, 6 };
    channel.put(byteValue);
    channel.putShort(shortValue);
    channel.putInt(intValue);
    channel.putLong(longValue);
    channel.prepareForFlush().flush();
    channel.close();
    // "Rotate" and continue
    storeChannel = fileSystemRule.get().open(secondFile, "rw");
    channel.setChannel(new PhysicalLogVersionedStoreChannel(storeChannel, 2, (byte) -1));
    channel.putFloat(floatValue);
    channel.putDouble(doubleValue);
    channel.put(byteArrayValue, byteArrayValue.length);
    channel.close();
    // The two chunks of values should end up in two different files
    ByteBuffer firstFileContents = readFile(firstFile);
    assertEquals(byteValue, firstFileContents.get());
    assertEquals(shortValue, firstFileContents.getShort());
    assertEquals(intValue, firstFileContents.getInt());
    assertEquals(longValue, firstFileContents.getLong());
    ByteBuffer secondFileContents = readFile(secondFile);
    assertEquals(floatValue, secondFileContents.getFloat(), 0.0f);
    assertEquals(doubleValue, secondFileContents.getDouble(), 0.0d);
    byte[] readByteArray = new byte[byteArrayValue.length];
    secondFileContents.get(readByteArray);
    assertArrayEquals(byteArrayValue, readByteArray);
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) 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