use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalLogFile method openForVersion.
public static PhysicalLogVersionedStoreChannel openForVersion(PhysicalLogFiles logFiles, FileSystemAbstraction fileSystem, long version, boolean write) throws IOException {
final File fileToOpen = logFiles.getLogFileForVersion(version);
if (!fileSystem.fileExists(fileToOpen)) {
throw new FileNotFoundException(String.format("File does not exist [%s]", fileToOpen.getCanonicalPath()));
}
StoreChannel rawChannel = null;
try {
rawChannel = fileSystem.open(fileToOpen, write ? "rw" : "r");
ByteBuffer buffer = ByteBuffer.allocate(LOG_HEADER_SIZE);
LogHeader header = readLogHeader(buffer, rawChannel, true, fileToOpen);
assert header != null && header.logVersion == version;
PhysicalLogVersionedStoreChannel result = new PhysicalLogVersionedStoreChannel(rawChannel, version, header.logFormatVersion);
return result;
} catch (FileNotFoundException cause) {
throw Exceptions.withCause(new FileNotFoundException(String.format("File could not be opened [%s]", fileToOpen.getCanonicalPath())), cause);
} catch (Throwable unexpectedError) {
if (rawChannel != null) {
// If we managed to open the file before failing, then close the channel
try {
rawChannel.close();
} catch (IOException e) {
unexpectedError.addSuppressed(e);
}
}
throw unexpectedError;
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class IndexConfigStore method read.
private void read() {
File fileToReadFrom = fileSystem.fileExists(file) ? file : oldFile;
if (!fileSystem.fileExists(fileToReadFrom)) {
return;
}
StoreChannel channel = null;
try {
channel = fileSystem.open(fileToReadFrom, "r");
Integer version = tryToReadVersion(channel);
if (version == null) {
close(channel);
channel = fileSystem.open(fileToReadFrom, "r");
// Legacy format, TODO
readMap(channel, nodeConfig, version);
relConfig.putAll(nodeConfig);
} else if (version < VERSION) {
// ...add version upgrade code here
throw new UnsupportedOperationException("" + version);
} else {
readMap(channel, nodeConfig, readNextInt(channel));
readMap(channel, relConfig, readNextInt(channel));
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
close(channel);
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class LogTestUtils method openLogEntryCursor.
private static LogEntryCursor openLogEntryCursor(FileSystemAbstraction fs, File firstFile, LogVersionBridge versionBridge) {
StoreChannel channel = null;
try {
channel = fs.open(firstFile, "r");
ByteBuffer buffer = ByteBuffer.allocate(LogHeader.LOG_HEADER_SIZE);
LogHeader header = LogHeaderReader.readLogHeader(buffer, channel, true, firstFile);
PhysicalLogVersionedStoreChannel logVersionedChannel = new PhysicalLogVersionedStoreChannel(channel, header.logVersion, header.logFormatVersion);
ReadableLogChannel logChannel = new ReadAheadLogChannel(logVersionedChannel, versionBridge, ReadAheadLogChannel.DEFAULT_READ_AHEAD_SIZE);
return new LogEntryCursor(new VersionAwareLogEntryReader<>(), logChannel);
} catch (Throwable t) {
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
t.addSuppressed(e);
}
}
throw new RuntimeException(t);
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class LogMatchers method logEntries.
public static List<LogEntry> logEntries(FileSystemAbstraction fileSystem, String logPath) throws IOException {
File logFile = new File(logPath);
StoreChannel fileChannel = fileSystem.open(logFile, "r");
// Always a header
LogHeader header = readLogHeader(ByteBuffer.allocateDirect(LOG_HEADER_SIZE), fileChannel, true, logFile);
// Read all log entries
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(fileChannel, header.logVersion, header.logFormatVersion);
ReadableLogChannel logChannel = new ReadAheadLogChannel(versionedStoreChannel, NO_MORE_CHANNELS);
LogEntryCursor logEntryCursor = new LogEntryCursor(new VersionAwareLogEntryReader<>(), logChannel);
return Iterables.asList(new IOCursorAsResourceIterable<>(logEntryCursor));
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapper method write.
@Override
public long write(long filePageId, Page page) throws IOException {
long fileOffset = pageIdToPosition(filePageId);
increaseFileSizeTo(fileOffset + filePageSize);
try {
StoreChannel channel = channel(filePageId);
return swapOut(page, fileOffset, channel);
} catch (ClosedChannelException e) {
// AsynchronousCloseException is a subclass of
// ClosedChannelException, and ClosedByInterruptException is in
// turn a subclass of AsynchronousCloseException.
tryReopen(filePageId, e);
boolean interrupted = Thread.interrupted();
// Recurse because this is hopefully a very rare occurrence.
long bytesWritten = write(filePageId, page);
if (interrupted) {
Thread.currentThread().interrupt();
}
return bytesWritten;
}
}
Aggregations