use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class StateRecoveryManagerTest method shouldReturnTheFullFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry.
@Test
public void shouldReturnTheFullFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
File fileA = fileA();
StoreChannel channel = fsa.create(fileA);
ByteBuffer buffer = writeLong(42);
channel.writeAll(buffer);
channel.force(false);
buffer.clear();
// extraneous bytes
buffer.putLong(101);
buffer.flip();
channel.writeAll(buffer);
channel.force(false);
File fileB = fileB();
fsa.create(fileB);
StateRecoveryManager<Long> manager = new StateRecoveryManager<>(fsa, new LongMarshal());
// when
final StateRecoveryManager.RecoveryStatus recoveryStatus = manager.recover(fileA, fileB);
// then
assertEquals(fileB, recoveryStatus.activeFile());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class StateRecoveryManagerTest method writeSomeLongsIn.
private void writeSomeLongsIn(EphemeralFileSystemAbstraction fsa, File file, long... longs) throws IOException {
final StoreChannel channel = fsa.open(file, "rw");
ByteBuffer buffer = ByteBuffer.allocate(longs.length * 8);
for (long aLong : longs) {
buffer.putLong(aLong);
}
buffer.flip();
channel.writeAll(buffer);
channel.force(false);
channel.close();
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SegmentFile method getOrCreateWriter.
private synchronized PhysicalFlushableChannel getOrCreateWriter() throws IOException {
if (bufferedWriter == null) {
if (!refCount.increase()) {
throw new IOException("Writer has been closed");
}
StoreChannel channel = fileSystem.open(file, "rw");
channel.position(channel.size());
bufferedWriter = new PhysicalFlushableChannel(channel);
}
return bufferedWriter;
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class DurableStateStorageTest method shouldClearFileOnFirstUse.
@Test
public void shouldClearFileOnFirstUse() throws Throwable {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
int rotationCount = 10;
DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), rotationCount, NullLogProvider.getInstance());
int largestValueWritten = 0;
try (Lifespan lifespan = new Lifespan(storage)) {
for (; largestValueWritten < rotationCount * 2; largestValueWritten++) {
storage.persistStoreData(new AtomicInteger(largestValueWritten));
}
}
// now both files are full. We reopen, then write some more.
storage = lifeRule.add(new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), rotationCount, NullLogProvider.getInstance()));
storage.persistStoreData(new AtomicInteger(largestValueWritten++));
storage.persistStoreData(new AtomicInteger(largestValueWritten++));
storage.persistStoreData(new AtomicInteger(largestValueWritten));
/*
* We have written stuff in fileA but not gotten to the end (resulting in rotation). The largestValueWritten
* should nevertheless be correct
*/
ByteBuffer forReadingBackIn = ByteBuffer.allocate(10_000);
StoreChannel lastWrittenTo = fsa.open(stateFileA(), "r");
lastWrittenTo.read(forReadingBackIn);
forReadingBackIn.flip();
AtomicInteger lastRead = null;
while (true) {
try {
lastRead = new AtomicInteger(forReadingBackIn.getInt());
} catch (BufferUnderflowException e) {
break;
}
}
// then
assertNotNull(lastRead);
assertEquals(largestValueWritten, lastRead.get());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class ReadAheadChannelTest method shouldReturnValueIfSufficientBytesAreBufferedEvenIfEOFHasBeenEncountered.
@Test
public void shouldReturnValueIfSufficientBytesAreBufferedEvenIfEOFHasBeenEncountered() throws Exception {
// Given
FileSystemAbstraction fileSystem = fileSystemRule.get();
StoreChannel storeChannel = fileSystem.open(new File("foo.txt"), "rw");
ByteBuffer buffer = ByteBuffer.allocate(1);
buffer.put((byte) 1);
buffer.flip();
storeChannel.writeAll(buffer);
storeChannel.force(false);
storeChannel.close();
storeChannel = fileSystem.open(new File("foo.txt"), "r");
ReadAheadChannel<StoreChannel> channel = new ReadAheadChannel<>(storeChannel);
try {
channel.getShort();
fail("Should have thrown exception signalling end of file reached");
} catch (ReadPastEndException endOfFile) {
// outstanding
}
assertEquals((byte) 1, channel.get());
try {
channel.get();
fail("Should have thrown exception signalling end of file reached");
} catch (ReadPastEndException endOfFile) {
// outstanding
}
}
Aggregations