Search in sources :

Example 6 with PhysicalFlushableChecksumChannel

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

the class PhysicalFlushableChecksumChannelTest method calculateChecksum.

@Test
void calculateChecksum() throws IOException {
    final Path firstFile = directory.homePath().resolve("file1");
    StoreChannel storeChannel = fileSystem.write(firstFile);
    int channelChecksum;
    try (PhysicalFlushableChecksumChannel channel = new PhysicalFlushableChecksumChannel(storeChannel, new HeapScopedBuffer(100, INSTANCE))) {
        channel.beginChecksum();
        channel.put((byte) 10);
        channelChecksum = channel.putChecksum();
    }
    int fileSize = (int) fileSystem.getFileSize(firstFile);
    assertEquals(Byte.BYTES + Integer.BYTES, fileSize);
    byte[] writtenBytes = new byte[fileSize];
    try (InputStream in = Files.newInputStream(firstFile)) {
        in.read(writtenBytes);
    }
    ByteBuffer buffer = ByteBuffer.wrap(writtenBytes);
    Checksum checksum = CHECKSUM_FACTORY.get();
    checksum.update(10);
    assertEquals(checksum.getValue(), channelChecksum);
    assertEquals(10, buffer.get());
    assertEquals(checksum.getValue(), buffer.getInt());
}
Also used : Path(java.nio.file.Path) HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) PhysicalFlushableChecksumChannel(org.neo4j.io.fs.PhysicalFlushableChecksumChannel) InputStream(java.io.InputStream) Checksum(java.util.zip.Checksum) StoreChannel(org.neo4j.io.fs.StoreChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

PhysicalFlushableChecksumChannel (org.neo4j.io.fs.PhysicalFlushableChecksumChannel)6 StoreChannel (org.neo4j.io.fs.StoreChannel)6 HeapScopedBuffer (org.neo4j.io.memory.HeapScopedBuffer)6 Test (org.junit.jupiter.api.Test)5 InputStream (java.io.InputStream)2 ByteBuffer (java.nio.ByteBuffer)2 Path (java.nio.file.Path)2 Checksum (java.util.zip.Checksum)2 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)1 ReadableLogChannel (org.neo4j.kernel.impl.transaction.log.ReadableLogChannel)1 LogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader)1 VersionAwareLogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader)1 LogFile (org.neo4j.kernel.impl.transaction.log.files.LogFile)1 LogFiles (org.neo4j.kernel.impl.transaction.log.files.LogFiles)1