Search in sources :

Example 6 with HeapScopedBuffer

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

the class TransactionLogChannelAllocator method createLogChannel.

public PhysicalLogVersionedStoreChannel createLogChannel(long version, LongSupplier lastCommittedTransactionId) throws IOException {
    AllocatedFile allocatedFile = allocateFile(version);
    var storeChannel = allocatedFile.getStoreChannel();
    var logFile = allocatedFile.getPath();
    try (var scopedBuffer = new HeapScopedBuffer(CURRENT_FORMAT_LOG_HEADER_SIZE, logFilesContext.getMemoryTracker())) {
        var buffer = scopedBuffer.getBuffer();
        LogHeader header = readLogHeader(buffer, storeChannel, false, logFile);
        if (header == null) {
            try (LogFileCreateEvent ignored = databaseTracer.createLogFile()) {
                // we always write file header from the beginning of the file
                storeChannel.position(0);
                long lastTxId = lastCommittedTransactionId.getAsLong();
                LogHeader logHeader = new LogHeader(version, lastTxId, logFilesContext.getStoreId());
                LogHeaderWriter.writeLogHeader(storeChannel, logHeader, logFilesContext.getMemoryTracker());
                logHeaderCache.putHeader(version, logHeader);
            }
        }
        byte formatVersion = header == null ? CURRENT_LOG_FORMAT_VERSION : header.getLogFormatVersion();
        return new PhysicalLogVersionedStoreChannel(storeChannel, version, formatVersion, logFile, nativeChannelAccessor);
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) LogFileCreateEvent(org.neo4j.kernel.impl.transaction.tracing.LogFileCreateEvent) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 7 with HeapScopedBuffer

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

the class SchemaRuleSerialization35 method serialize.

/**
 * Serialize the provided ConstraintDescriptor onto the target buffer
 * @param constraint the ConstraintDescriptor to serialize
 * @throws IllegalStateException if the ConstraintDescriptor is of type unique, but the owned index has not been set
 */
public static byte[] serialize(ConstraintDescriptor constraint, MemoryTracker memoryTracker) {
    try (var scopedBuffer = new HeapScopedBuffer(lengthOf(constraint), memoryTracker)) {
        ByteBuffer target = scopedBuffer.getBuffer();
        target.putInt(LEGACY_LABEL_OR_REL_TYPE_ID);
        target.put(CONSTRAINT_RULE);
        switch(constraint.type()) {
            case EXISTS:
                target.put(EXISTS_CONSTRAINT);
                break;
            case UNIQUE:
                target.put(UNIQUE_CONSTRAINT);
                target.putLong(constraint.asIndexBackedConstraint().ownedIndexId());
                break;
            case UNIQUE_EXISTS:
                target.put(UNIQUE_EXISTS_CONSTRAINT);
                target.putLong(constraint.asIndexBackedConstraint().ownedIndexId());
                break;
            default:
                throw new UnsupportedOperationException(format("Got unknown index descriptor type '%s'.", constraint.type()));
        }
        constraint.schema().processWith(new SchemaDescriptorSerializer(target));
        UTF8.putEncodedStringInto(constraint.getName(), target);
        return target.array();
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 8 with HeapScopedBuffer

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

the class DetachedCheckpointLogEntryWriterTest method longCheckpointReasonIsTrimmedToFit.

@Test
void longCheckpointReasonIsTrimmedToFit() throws IOException {
    try (var buffer = new HeapScopedBuffer((int) kibiBytes(1), INSTANCE)) {
        StoreChannel storeChannel = fs.write(directory.createFile("b"));
        try (PhysicalFlushableChecksumChannel writeChannel = new PhysicalFlushableChecksumChannel(storeChannel, buffer)) {
            var checkpointLogEntryWriter = new DetachedCheckpointLogEntryWriter(writeChannel);
            long initialPosition = writeChannel.position();
            writeCheckpoint(checkpointLogEntryWriter, StringUtils.repeat("b", 1024));
            long recordLength = writeChannel.position() - initialPosition;
            assertThat(recordLength).isEqualTo(RECORD_LENGTH_BYTES);
        }
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) PhysicalFlushableChecksumChannel(org.neo4j.io.fs.PhysicalFlushableChecksumChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Example 9 with HeapScopedBuffer

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

the class DetachedCheckpointLogEntryWriterTest method detachedCheckpointEntryHasSpecificLength.

@Test
void detachedCheckpointEntryHasSpecificLength() throws IOException {
    try (var buffer = new HeapScopedBuffer((int) kibiBytes(1), INSTANCE)) {
        StoreChannel storeChannel = fs.write(directory.createFile("a"));
        try (PhysicalFlushableChecksumChannel writeChannel = new PhysicalFlushableChecksumChannel(storeChannel, buffer)) {
            var checkpointLogEntryWriter = new DetachedCheckpointLogEntryWriter(writeChannel);
            long initialPosition = writeChannel.position();
            writeCheckpoint(checkpointLogEntryWriter, "checkpoint reason");
            assertThat(writeChannel.position() - initialPosition).isEqualTo(RECORD_LENGTH_BYTES);
        }
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) PhysicalFlushableChecksumChannel(org.neo4j.io.fs.PhysicalFlushableChecksumChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

Example 10 with HeapScopedBuffer

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

the class DetachedCheckpointLogEntryWriterTest method anyCheckpointEntryHaveTheSameSize.

@Test
void anyCheckpointEntryHaveTheSameSize() throws IOException {
    try (var buffer = new HeapScopedBuffer((int) kibiBytes(1), INSTANCE)) {
        StoreChannel storeChannel = fs.write(directory.createFile("b"));
        try (PhysicalFlushableChecksumChannel writeChannel = new PhysicalFlushableChecksumChannel(storeChannel, buffer)) {
            var checkpointLogEntryWriter = new DetachedCheckpointLogEntryWriter(writeChannel);
            for (int i = 0; i < 100; i++) {
                long initialPosition = writeChannel.position();
                writeCheckpoint(checkpointLogEntryWriter, randomAlphabetic(10, 512));
                long recordLength = writeChannel.position() - initialPosition;
                assertThat(recordLength).isEqualTo(RECORD_LENGTH_BYTES);
            }
        }
    }
}
Also used : HeapScopedBuffer(org.neo4j.io.memory.HeapScopedBuffer) PhysicalFlushableChecksumChannel(org.neo4j.io.fs.PhysicalFlushableChecksumChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.jupiter.api.Test)

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