use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FileSenderTest method sendLargeFileWithSizeMultipleOfTheChunkSize.
@Test
public void sendLargeFileWithSizeMultipleOfTheChunkSize() throws Exception {
// given
byte[] bytes = new byte[MAX_SIZE * 3];
random.nextBytes(bytes);
File smallFile = testDirectory.file("smallFile");
try (StoreChannel storeChannel = fs.create(smallFile)) {
storeChannel.write(ByteBuffer.wrap(bytes));
}
FileSender fileSender = new FileSender(fs.open(smallFile, "r"));
// when + then
assertFalse(fileSender.isEndOfInput());
assertEquals(FileChunk.create(copyOfRange(bytes, 0, MAX_SIZE), false), fileSender.readChunk(allocator));
assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE, MAX_SIZE * 2), false), fileSender.readChunk(allocator));
assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE * 2, bytes.length), true), fileSender.readChunk(allocator));
assertNull(fileSender.readChunk(allocator));
assertTrue(fileSender.isEndOfInput());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FileSenderTest method sendLargeFileWithUnreliableReadBufferSize.
@Test
public void sendLargeFileWithUnreliableReadBufferSize() throws Exception {
// given
byte[] bytes = new byte[MAX_SIZE * 3];
random.nextBytes(bytes);
File smallFile = testDirectory.file("smallFile");
try (StoreChannel storeChannel = fs.create(smallFile)) {
storeChannel.write(ByteBuffer.wrap(bytes));
}
Adversary adversary = new RandomAdversary(0.9, 0.0, 0.0);
AdversarialFileSystemAbstraction afs = new AdversarialFileSystemAbstraction(adversary, fs);
FileSender fileSender = new FileSender(afs.open(smallFile, "r"));
// when + then
assertFalse(fileSender.isEndOfInput());
assertEquals(FileChunk.create(copyOfRange(bytes, 0, MAX_SIZE), false), fileSender.readChunk(allocator));
assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE, MAX_SIZE * 2), false), fileSender.readChunk(allocator));
assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE * 2, bytes.length), true), fileSender.readChunk(allocator));
assertNull(fileSender.readChunk(allocator));
assertTrue(fileSender.isEndOfInput());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class VersionAwareLogEntryReaderIT method correctlyResetPositionOfObservedZeroWhenChannelSwitchOnExactlyCheckedByte.
@Test
void correctlyResetPositionOfObservedZeroWhenChannelSwitchOnExactlyCheckedByte() throws IOException {
LogFiles logFiles = LogFilesBuilder.builder(databaseLayout, fs).withLogEntryReader(entryReader).withLogVersionRepository(new SimpleLogVersionRepository()).withTransactionIdStore(new SimpleTransactionIdStore()).withStoreId(StoreId.UNKNOWN).withKernelVersionProvider(() -> KernelVersion.V4_0).build();
try (Lifespan lifespan = new Lifespan(logFiles)) {
LogPositionMarker positionMarker = new LogPositionMarker();
LogFile logFile = logFiles.getLogFile();
long initialPosition = getLastReadablePosition(logFiles);
long checkpointsEndDataOffset = DEFAULT_READ_AHEAD_SIZE + initialPosition;
TransactionLogWriter logWriter = logFile.getTransactionLogWriter();
do {
logWriter.legacyCheckPoint(new LogPosition(4, 5));
logWriter.getCurrentPosition(positionMarker);
} while (positionMarker.getByteOffset() <= checkpointsEndDataOffset);
logFile.flush();
logFiles.getLogFile().rotate();
fs.truncate(logFiles.getLogFile().getLogFileForVersion(0), checkpointsEndDataOffset);
try (StoreChannel storeChannel = fs.write(logFiles.getLogFile().getLogFileForVersion(1))) {
storeChannel.position(CURRENT_FORMAT_LOG_HEADER_SIZE);
storeChannel.writeAll(ByteBuffer.wrap(new byte[] { 0 }));
}
try (ReadableLogChannel logChannel = logFiles.getLogFile().getReader(new LogPosition(0, initialPosition))) {
LogEntry logEntry = entryReader.readLogEntry(logChannel);
// we reading expected checkpoint records
assertThat(logEntry).isInstanceOf(LogEntryInlinedCheckPoint.class);
LogEntryInlinedCheckPoint checkPoint = (LogEntryInlinedCheckPoint) logEntry;
LogPosition logPosition = checkPoint.getLogPosition();
assertEquals(4, logPosition.getLogVersion());
assertEquals(5, logPosition.getByteOffset());
// set position to the end of the buffer to cause channel switch on next byte read
((PositionableChannel) logChannel).setCurrentPosition(checkpointsEndDataOffset);
while (entryReader.readLogEntry(logChannel) != null) {
// read to the end
}
// channel should be switched now and position should be just after the header
LogPosition position = entryReader.lastPosition();
assertEquals(CURRENT_FORMAT_LOG_HEADER_SIZE, position.getByteOffset());
assertEquals(1, position.getLogVersion());
}
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalFlushableChannelTest method readFile.
private ByteBuffer readFile(Path file) throws IOException {
try (StoreChannel channel = fileSystem.read(file)) {
ByteBuffer buffer = ByteBuffers.allocate((int) channel.size(), INSTANCE);
channel.readAll(buffer);
buffer.flip();
return buffer;
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalFlushableChannelTest method shouldBeAbleToWriteValuesGreaterThanHalfTheBufferSize.
@Test
void shouldBeAbleToWriteValuesGreaterThanHalfTheBufferSize() throws IOException {
final Path firstFile = directory.homePath().resolve("file1");
StoreChannel storeChannel = fileSystem.write(firstFile);
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1, firstFile, nativeChannelAccessor);
int length = 262_145;
byte[] bytes;
try (PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel, INSTANCE)) {
bytes = generateBytes(length);
channel.put(bytes, length);
}
byte[] writtenBytes = new byte[length];
try (InputStream in = Files.newInputStream(firstFile)) {
in.read(writtenBytes);
}
assertArrayEquals(bytes, writtenBytes);
}
Aggregations