Search in sources :

Example 16 with HeapScopedBuffer

use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.

the class LogHeaderWriter method writeLogHeader.

public static void writeLogHeader(StoreChannel channel, LogHeader logHeader, MemoryTracker memoryTracker) throws IOException {
    try (var scopedBuffer = new HeapScopedBuffer(CURRENT_FORMAT_LOG_HEADER_SIZE, memoryTracker)) {
        var buffer = scopedBuffer.getBuffer();
        buffer.putLong(encodeLogVersion(logHeader.getLogVersion(), logHeader.getLogFormatVersion()));
        buffer.putLong(logHeader.getLastCommittedTxId());
        StoreId storeId = logHeader.getStoreId();
        buffer.putLong(storeId.getCreationTime());
        buffer.putLong(storeId.getRandomId());
        buffer.putLong(storeId.getStoreVersion());
        buffer.putLong(storeId.getUpgradeTime());
        buffer.putLong(storeId.getUpgradeTxId());
        buffer.putLong(0);
        buffer.flip();
        channel.writeAll(buffer);
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) StoreId(org.neo4j.storageengine.api.StoreId)

Example 17 with HeapScopedBuffer

use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.

the class PhysicalFlushableChecksumChannelTest method beginCehcksumShouldResetCalculations.

@Test
void beginCehcksumShouldResetCalculations() 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.put((byte) 5);
        channel.beginChecksum();
        channel.put((byte) 10);
        channelChecksum = channel.putChecksum();
    }
    int fileSize = (int) fileSystem.getFileSize(firstFile);
    assertEquals(Byte.BYTES + 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(5, buffer.get());
    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)

Example 18 with HeapScopedBuffer

use of org.neo4j.io.memory.HeapScopedBuffer 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)

Example 19 with HeapScopedBuffer

use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.

the class PhysicalFlushableChannelTest method shouldWriteThroughRotation.

@Test
void shouldWriteThroughRotation() throws Exception {
    // GIVEN
    final Path firstFile = directory.homePath().resolve("file1");
    final Path secondFile = directory.homePath().resolve("file2");
    StoreChannel storeChannel = fileSystem.write(firstFile);
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, firstFile, nativeChannelAccessor);
    PhysicalFlushableLogChannel channel = new PhysicalFlushableLogChannel(versionedStoreChannel, new HeapScopedBuffer(100, INSTANCE));
    // 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 = fileSystem.write(secondFile);
    channel.setChannel(new PhysicalLogVersionedStoreChannel(storeChannel, 2, (byte) -1, secondFile, nativeChannelAccessor));
    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.001f);
    assertEquals(doubleValue, secondFileContents.getDouble(), 0.001d);
    byte[] readByteArray = new byte[byteArrayValue.length];
    secondFileContents.get(readByteArray);
    assertArrayEquals(byteArrayValue, readByteArray);
}
Also used : Path(java.nio.file.Path) StoreChannel(org.neo4j.io.fs.StoreChannel) ByteBuffer(java.nio.ByteBuffer) HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) Test(org.junit.jupiter.api.Test)

Example 20 with HeapScopedBuffer

use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.

the class UnsafeDirectByteBufferAllocator method allocate.

@Override
public synchronized ScopedBuffer allocate(int bufferSize, MemoryTracker memoryTracker) {
    assertOpen();
    try {
        var byteBuffer = new NativeScopedBuffer(bufferSize, memoryTracker);
        allocations.add(byteBuffer);
        return byteBuffer;
    } catch (NativeMemoryAllocationRefusedError allocationRefusedError) {
        // What ever went wrong fallback to on-heap buffer.
        return new HeapScopedBuffer(bufferSize, memoryTracker);
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) NativeScopedBuffer(org.neo4j.io.memory.NativeScopedBuffer) NativeMemoryAllocationRefusedError(org.neo4j.internal.unsafe.NativeMemoryAllocationRefusedError)

Aggregations

HeapScopedBuffer (org.neo4j.io.memory.HeapScopedBuffer)20 StoreChannel (org.neo4j.io.fs.StoreChannel)10 Test (org.junit.jupiter.api.Test)9 Path (java.nio.file.Path)8 PhysicalFlushableChecksumChannel (org.neo4j.io.fs.PhysicalFlushableChecksumChannel)6 ByteBuffer (java.nio.ByteBuffer)5 IOException (java.io.IOException)4 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)4 InputStream (java.io.InputStream)3 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)3 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)3 LogFile (org.neo4j.kernel.impl.transaction.log.files.LogFile)3 Checksum (java.util.zip.Checksum)2 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)2 StoreId (org.neo4j.storageengine.api.StoreId)2 UncheckedIOException (java.io.UncheckedIOException)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Instant (java.time.Instant)1 UUID.randomUUID (java.util.UUID.randomUUID)1