use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReadAheadChannelTest method createFile.
private void createFile(EphemeralFileSystemAbstraction fsa, File name, int bufferSize) throws IOException {
StoreChannel storeChannel = fsa.open(name, "w");
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
for (int i = 0; i < bufferSize; i++) {
buffer.put((byte) i);
}
buffer.flip();
storeChannel.writeAll(buffer);
storeChannel.close();
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReadAheadChannelTest method shouldHandleRunningOutOfBytesWhenRequestSpansMultipleFiles.
@Test
public void shouldHandleRunningOutOfBytesWhenRequestSpansMultipleFiles() throws Exception {
// Given
FileSystemAbstraction fileSystem = fileSystemRule.get();
StoreChannel storeChannel1 = fileSystem.open(new File("foo.1"), "rw");
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.put((byte) 0);
buffer.put((byte) 0);
buffer.flip();
storeChannel1.writeAll(buffer);
storeChannel1.force(false);
storeChannel1.close();
buffer.flip();
StoreChannel storeChannel2 = fileSystem.open(new File("foo.2"), "r");
buffer.put((byte) 0);
buffer.put((byte) 1);
buffer.flip();
storeChannel2.writeAll(buffer);
storeChannel2.force(false);
storeChannel2.close();
storeChannel1 = fileSystem.open(new File("foo.1"), "r");
final StoreChannel storeChannel2Copy = fileSystem.open(new File("foo.2"), "r");
ReadAheadChannel<StoreChannel> channel = new ReadAheadChannel<StoreChannel>(storeChannel1) {
@Override
protected StoreChannel next(StoreChannel channel) throws IOException {
return storeChannel2Copy;
}
};
try {
channel.getLong();
fail("Should have thrown exception signalling end of file reached");
} catch (ReadPastEndException endOfFile) {
// outstanding
}
assertEquals(1, channel.getInt());
try {
channel.get();
fail("Should have thrown exception signalling end of file reached");
} catch (ReadPastEndException endOfFile) {
// outstanding
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReadAheadChannelTest method shouldReturnPositionWithinBufferedStream.
@Test
public void shouldReturnPositionWithinBufferedStream() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
File file = new File("foo.txt");
int readAheadSize = 512;
int fileSize = readAheadSize * 8;
createFile(fsa, file, fileSize);
ReadAheadChannel<StoreChannel> bufferedReader = new ReadAheadChannel<>(fsa.open(file, "r"), readAheadSize);
// when
for (int i = 0; i < fileSize / Long.BYTES; i++) {
assertEquals(Long.BYTES * i, bufferedReader.position());
bufferedReader.getLong();
}
assertEquals(fileSize, bufferedReader.position());
try {
bufferedReader.getLong();
fail();
} catch (ReadPastEndException e) {
// expected
}
assertEquals(fileSize, bufferedReader.position());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalFlushableChannelTest method shouldSeeCorrectPositionEvenBeforeEmptyingDataIntoChannel.
@Test
public void shouldSeeCorrectPositionEvenBeforeEmptyingDataIntoChannel() throws Exception {
// GIVEN
final File file = new File(directory.directory(), "file");
StoreChannel storeChannel = fileSystemRule.get().open(file, "rw");
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
PositionAwarePhysicalFlushableChannel channel = new PositionAwarePhysicalFlushableChannel(versionedStoreChannel);
LogPositionMarker positionMarker = new LogPositionMarker();
LogPosition initialPosition = channel.getCurrentPosition(positionMarker).newPosition();
// WHEN
channel.putLong(67);
channel.putInt(1234);
LogPosition positionAfterSomeData = channel.getCurrentPosition(positionMarker).newPosition();
// THEN
assertEquals(12, positionAfterSomeData.getByteOffset() - initialPosition.getByteOffset());
channel.close();
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalFlushableChannelTest method shouldWriteThroughRotation.
@Test
public void shouldWriteThroughRotation() throws Exception {
// GIVEN
final File firstFile = new File(directory.directory(), "file1");
final File secondFile = new File(directory.directory(), "file2");
StoreChannel storeChannel = fileSystemRule.get().open(firstFile, "rw");
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
// 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 = fileSystemRule.get().open(secondFile, "rw");
channel.setChannel(new PhysicalLogVersionedStoreChannel(storeChannel, 2, (byte) -1));
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.0f);
assertEquals(doubleValue, secondFileContents.getDouble(), 0.0d);
byte[] readByteArray = new byte[byteArrayValue.length];
secondFileContents.get(readByteArray);
assertArrayEquals(byteArrayValue, readByteArray);
}
Aggregations