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