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);
}
}
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();
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
}
Aggregations