use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReaderLogVersionBridgeTest method shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet.
@Test
public void shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet() throws Exception {
// given
final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(fs, logFiles);
final StoreChannel nextVersionWithIncompleteHeader = mock(StoreChannel.class);
when(nextVersionWithIncompleteHeader.read(any(ByteBuffer.class))).thenReturn(LOG_HEADER_SIZE / 2);
when(channel.getVersion()).thenReturn(version);
when(fs.fileExists(file)).thenReturn(true);
when(logFiles.getLogFileForVersion(version + 1)).thenReturn(file);
when(fs.open(file, "r")).thenReturn(nextVersionWithIncompleteHeader);
// when
final LogVersionedStoreChannel result = bridge.next(channel);
// then
assertEquals(channel, result);
verify(channel, never()).close();
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReaderLogVersionBridgeTest method shouldOpenTheNextChannelWhenItExists.
@Test
public void shouldOpenTheNextChannelWhenItExists() throws IOException {
// given
final StoreChannel newStoreChannel = mock(StoreChannel.class);
final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge(fs, logFiles);
when(channel.getVersion()).thenReturn(version);
when(channel.getLogFormatVersion()).thenReturn(CURRENT_LOG_VERSION);
when(logFiles.getLogFileForVersion(version + 1)).thenReturn(file);
when(fs.fileExists(file)).thenReturn(true);
when(fs.open(file, "r")).thenReturn(newStoreChannel);
when(newStoreChannel.read(Matchers.<ByteBuffer>any())).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
ByteBuffer buffer = (ByteBuffer) invocationOnMock.getArguments()[0];
buffer.putLong(encodeLogVersion(version + 1));
buffer.putLong(42);
return LOG_HEADER_SIZE;
}
});
// when
final LogVersionedStoreChannel result = bridge.next(channel);
// then
PhysicalLogVersionedStoreChannel expected = new PhysicalLogVersionedStoreChannel(newStoreChannel, version + 1, CURRENT_LOG_VERSION);
assertEquals(expected, result);
verify(channel, times(1)).close();
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class MigrationTestUtils method verifyFilesHaveSameContent.
public static void verifyFilesHaveSameContent(FileSystemAbstraction fileSystem, File original, File other) throws IOException {
final int bufferBatchSize = 32 * 1024;
File[] files = fileSystem.listFiles(original);
for (File originalFile : files) {
File otherFile = new File(other, originalFile.getName());
if (!fileSystem.isDirectory(originalFile)) {
try (StoreChannel originalChannel = fileSystem.open(originalFile, "r");
StoreChannel otherChannel = fileSystem.open(otherFile, "r")) {
ByteBuffer buffer = ByteBuffer.allocate(bufferBatchSize);
while (true) {
if (!readAndFlip(originalChannel, buffer, bufferBatchSize)) {
break;
}
byte[] originalBytes = new byte[buffer.limit()];
buffer.get(originalBytes);
if (!readAndFlip(otherChannel, buffer, bufferBatchSize)) {
fail("Files have different sizes");
}
byte[] otherBytes = new byte[buffer.limit()];
buffer.get(otherBytes);
assertArrayEquals("Different content in " + originalFile, originalBytes, otherBytes);
}
}
}
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class MigrationTestUtils method truncateFile.
/**
* Removes the version trailer from the store files.
*/
public static void truncateFile(FileSystemAbstraction fileSystem, File storeFile, String suffixToDetermineTruncationLength) throws IOException {
byte[] versionBytes = UTF8.encode(suffixToDetermineTruncationLength);
if (!fileSystem.fileExists(storeFile)) {
return;
}
try (StoreChannel fileChannel = fileSystem.open(storeFile, "rw")) {
long fileSize = fileSystem.getFileSize(storeFile);
fileChannel.truncate(Math.max(0, fileSize - versionBytes.length));
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReadAheadLogChannelTest method writeSomeData.
private void writeSomeData(File file, Visitor<ByteBuffer, IOException> visitor) throws IOException {
try (StoreChannel channel = fileSystemRule.get().open(file, "rw")) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
visitor.visit(buffer);
buffer.flip();
channel.write(buffer);
}
}
Aggregations