Search in sources :

Example 91 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class PhysicalFlushableChannelTest method readFile.

private ByteBuffer readFile(File file) throws IOException {
    try (StoreChannel channel = fileSystemRule.get().open(file, "r")) {
        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);
        buffer.flip();
        return buffer;
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) ByteBuffer(java.nio.ByteBuffer)

Example 92 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class LogTestUtils method filterNeostoreLogicalLog.

public static File filterNeostoreLogicalLog(FileSystemAbstraction fileSystem, File file, final LogHook<LogEntry> filter) throws IOException {
    filter.file(file);
    File tempFile = new File(file.getAbsolutePath() + ".tmp");
    fileSystem.deleteFile(tempFile);
    try (StoreChannel in = fileSystem.open(file, "r");
        StoreChannel out = fileSystem.open(tempFile, "rw")) {
        LogHeader logHeader = transferLogicalLogHeader(in, out, ByteBuffer.allocate(LOG_HEADER_SIZE));
        PhysicalLogVersionedStoreChannel outChannel = new PhysicalLogVersionedStoreChannel(out, logHeader.logVersion, logHeader.logFormatVersion);
        PhysicalLogVersionedStoreChannel inChannel = new PhysicalLogVersionedStoreChannel(in, logHeader.logVersion, logHeader.logFormatVersion);
        ReadableLogChannel inBuffer = new ReadAheadLogChannel(inChannel, LogVersionBridge.NO_MORE_CHANNELS);
        LogEntryReader<ReadableLogChannel> entryReader = new VersionAwareLogEntryReader<>();
        LogEntry entry;
        while ((entry = entryReader.readLogEntry(inBuffer)) != null) {
            if (filter.test(entry)) {
            // TODO allright, write to outBuffer
            }
        }
    }
    return tempFile;
}
Also used : ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) File(java.io.File) ReadAheadLogChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel) 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) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry)

Example 93 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class LogTestUtils method copyLogicalLog.

public static NonCleanLogCopy copyLogicalLog(FileSystemAbstraction fileSystem, File logBaseFileName) throws IOException {
    char active = '1';
    File activeLog = new File(logBaseFileName.getPath() + ".active");
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    File activeLogBackup;
    try (StoreChannel af = fileSystem.open(activeLog, "r")) {
        af.read(buffer);
        buffer.flip();
        activeLogBackup = new File(logBaseFileName.getPath() + ".bak.active");
        try (StoreChannel activeCopy = fileSystem.open(activeLogBackup, "rw")) {
            activeCopy.write(buffer);
        }
    }
    buffer.flip();
    active = buffer.asCharBuffer().get();
    buffer.clear();
    File currentLog = new File(logBaseFileName.getPath() + "." + active);
    File currentLogBackup = new File(logBaseFileName.getPath() + ".bak." + active);
    try (StoreChannel source = fileSystem.open(currentLog, "r");
        StoreChannel dest = fileSystem.open(currentLogBackup, "rw")) {
        int read = -1;
        do {
            read = source.read(buffer);
            buffer.flip();
            dest.write(buffer);
            buffer.clear();
        } while (read == 1024);
    }
    return new NonCleanLogCopy(new FileBackup(activeLog, activeLogBackup, fileSystem), new FileBackup(currentLog, currentLogBackup, fileSystem));
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) PhysicalLogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 94 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class TestEphemeralFileChannel method absoluteVersusRelative.

@Test
public void absoluteVersusRelative() throws Exception {
    // GIVEN
    File file = new File("myfile");
    EphemeralFileSystemAbstraction fs = fileSystemRule.get();
    StoreChannel channel = fs.open(file, "rw");
    byte[] bytes = "test".getBytes();
    channel.write(ByteBuffer.wrap(bytes));
    channel.close();
    // WHEN
    channel = fs.open(new File(file.getAbsolutePath()), "r");
    byte[] readBytes = new byte[bytes.length];
    int nrOfReadBytes = channel.read(ByteBuffer.wrap(readBytes));
    // THEN
    assertEquals(bytes.length, nrOfReadBytes);
    assertTrue(Arrays.equals(bytes, readBytes));
    fs.close();
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 95 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class FailureStorage method storeIndexFailure.

/**
     * Store failure in failure file for index with the given id
     *
     * @param failure message describing the failure that needs to be stored
     * @throws IOException if the failure could not be stored
     */
public synchronized void storeIndexFailure(String failure) throws IOException {
    File failureFile = failureFile();
    try (StoreChannel channel = fs.open(failureFile, "rw")) {
        byte[] existingData = new byte[(int) channel.size()];
        channel.read(ByteBuffer.wrap(existingData));
        channel.position(lengthOf(existingData));
        byte[] data = UTF8.encode(failure);
        channel.write(ByteBuffer.wrap(data, 0, Math.min(data.length, MAX_FAILURE_SIZE)));
        channel.force(true);
        channel.close();
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)113 Test (org.junit.Test)71 File (java.io.File)65 ByteBuffer (java.nio.ByteBuffer)48 IOException (java.io.IOException)20 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)17 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)15 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)13 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)11 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)10 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)7 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)7 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)7 InputStream (java.io.InputStream)6 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)6 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)6 HashSet (java.util.HashSet)5 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)5 PageSwapper (org.neo4j.io.pagecache.PageSwapper)5 PagedFile (org.neo4j.io.pagecache.PagedFile)5