Search in sources :

Example 21 with StoreChannel

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;
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeaderWriter.writeLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderWriter.writeLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 22 with StoreChannel

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);
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) IOException(java.io.IOException) File(java.io.File)

Example 23 with StoreChannel

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);
    }
}
Also used : ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeaderWriter.writeLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderWriter.writeLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 24 with StoreChannel

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));
}
Also used : ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) File(java.io.File) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) LogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeader)

Example 25 with StoreChannel

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;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) StoreChannel(org.neo4j.io.fs.StoreChannel)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)173 ByteBuffer (java.nio.ByteBuffer)65 Test (org.junit.jupiter.api.Test)52 Path (java.nio.file.Path)50 File (java.io.File)44 Test (org.junit.Test)42 DelegatingStoreChannel (org.neo4j.io.fs.DelegatingStoreChannel)26 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)25 IOException (java.io.IOException)24 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)13 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)13 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)13 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)13 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)11 InputStream (java.io.InputStream)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)8 HeapScopedBuffer (org.neo4j.io.memory.HeapScopedBuffer)8 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)8 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)8