Search in sources :

Example 41 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldThrowClosedChannelExceptionWhenChannelUnexpectedlyClosed.

@Test
void shouldThrowClosedChannelExceptionWhenChannelUnexpectedlyClosed() throws Exception {
    // GIVEN
    final Path file = directory.homePath().resolve("file");
    StoreChannel storeChannel = fileSystem.write(file);
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, file, nativeChannelAccessor);
    PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel, INSTANCE);
    // just close the underlying channel
    storeChannel.close();
    // WHEN just appending something to the buffer
    channel.put((byte) 0);
    // and wanting to empty that into the channel
    assertThrows(ClosedChannelException.class, channel::prepareForFlush);
}
Also used : Path(java.nio.file.Path) PhysicalFlushableChannel(org.neo4j.io.fs.PhysicalFlushableChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Example 42 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldThrowIllegalStateExceptionAfterClosed.

@Test
void shouldThrowIllegalStateExceptionAfterClosed() throws Exception {
    // GIVEN
    final Path file = directory.homePath().resolve("file");
    StoreChannel storeChannel = fileSystem.write(file);
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, file, nativeChannelAccessor);
    PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel, INSTANCE);
    // closing the WritableLogChannel, then the underlying channel is what PhysicalLogFile does
    channel.close();
    storeChannel.close();
    // WHEN just appending something to the buffer
    channel.put((byte) 0);
    // and wanting to empty that into the channel
    assertThrows(IllegalStateException.class, channel::prepareForFlush);
}
Also used : Path(java.nio.file.Path) PhysicalFlushableChannel(org.neo4j.io.fs.PhysicalFlushableChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Example 43 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldBeAbleToWriteValuesGreaterThanTheBufferSize.

@Test
void shouldBeAbleToWriteValuesGreaterThanTheBufferSize() throws IOException {
    final Path firstFile = directory.homePath().resolve("file1");
    StoreChannel storeChannel = fileSystem.write(firstFile);
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, firstFile, nativeChannelAccessor);
    int length = 1_000_000;
    byte[] bytes;
    try (PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel, INSTANCE)) {
        bytes = generateBytes(length);
        channel.put(bytes, length);
    }
    byte[] writtenBytes = new byte[length];
    try (InputStream in = Files.newInputStream(firstFile)) {
        in.read(writtenBytes);
    }
    assertArrayEquals(bytes, writtenBytes);
}
Also used : Path(java.nio.file.Path) PhysicalFlushableChannel(org.neo4j.io.fs.PhysicalFlushableChannel) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Example 44 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldBeAbleToWriteSmallNumberOfBytes.

@Test
void shouldBeAbleToWriteSmallNumberOfBytes() throws IOException {
    final Path firstFile = directory.homePath().resolve("file1");
    StoreChannel storeChannel = fileSystem.write(firstFile);
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, firstFile, nativeChannelAccessor);
    int length = 26_145;
    byte[] bytes;
    try (PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel, new HeapScopedBuffer(100, INSTANCE))) {
        bytes = generateBytes(length);
        channel.put(bytes, length);
    }
    byte[] writtenBytes = new byte[length];
    try (InputStream in = Files.newInputStream(firstFile)) {
        in.read(writtenBytes);
    }
    assertArrayEquals(bytes, writtenBytes);
}
Also used : Path(java.nio.file.Path) HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) PhysicalFlushableChannel(org.neo4j.io.fs.PhysicalFlushableChannel) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Example 45 with StoreChannel

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

the class PhysicalFlushableChannelTest method releaseBufferMemoryOnClose.

@Test
void releaseBufferMemoryOnClose() throws IOException {
    var memoryTracker = new LocalMemoryTracker();
    final Path firstFile = directory.homePath().resolve("file2");
    StoreChannel storeChannel = fileSystem.write(firstFile);
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, firstFile, nativeChannelAccessor);
    assertThat(memoryTracker.estimatedHeapMemory()).isZero();
    assertThat(memoryTracker.usedNativeMemory()).isZero();
    try (PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel, memoryTracker)) {
        channel.put((byte) 1);
        assertThat(memoryTracker.usedNativeMemory()).isZero();
        assertThat(memoryTracker.estimatedHeapMemory()).isGreaterThan(0);
    }
    assertThat(memoryTracker.estimatedHeapMemory()).isZero();
    assertThat(memoryTracker.usedNativeMemory()).isZero();
}
Also used : Path(java.nio.file.Path) PhysicalFlushableChannel(org.neo4j.io.fs.PhysicalFlushableChannel) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)173 ByteBuffer (java.nio.ByteBuffer)65 Test (org.junit.jupiter.api.Test)52 Path (java.nio.file.Path)50 File (java.io.File)44 Test (org.junit.Test)42 DelegatingStoreChannel (org.neo4j.io.fs.DelegatingStoreChannel)26 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)25 IOException (java.io.IOException)24 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)13 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)13 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)13 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)13 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)11 InputStream (java.io.InputStream)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)8 HeapScopedBuffer (org.neo4j.io.memory.HeapScopedBuffer)8 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)8 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)8