Search in sources :

Example 76 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class CommonAbstractStore method checkAndLoadStorage.

/**
 * This method is called by constructors. Checks the header record and loads the store.
 * <p>
 * Note: This method will map the file with the page cache. The store file must not
 * be accessed directly until it has been unmapped - the store file must only be
 * accessed through the page cache.
 * @param createIfNotExists If true, creates and initialises the store file if it does not exist already. If false,
 * this method will instead throw an exception in that situation.
 * @return {@code true} if the store was created as part of this call, otherwise {@code false} if it already existed.
 */
private boolean checkAndLoadStorage(boolean createIfNotExists, CursorContext cursorContext) {
    try {
        determineRecordSize(storeHeaderFormat.generateHeader());
        if (getNumberOfReservedLowIds() > 0) {
            // This store has a store-specific header so we have read it before we can be sure that we can map it with correct page size.
            // Try to open the store file (w/o creating if it doesn't exist), with page size for the configured header value.
            HEADER defaultHeader = storeHeaderFormat.generateHeader();
            pagedFile = pageCache.map(storageFile, filePageSize, databaseName, openOptions.newWith(ANY_PAGE_SIZE));
            HEADER readHeader = readStoreHeaderAndDetermineRecordSize(pagedFile, cursorContext);
            if (!defaultHeader.equals(readHeader)) {
                // The header that we read was different from the default one so unmap
                pagedFile.close();
                pagedFile = null;
            }
        }
        if (pagedFile == null) {
            // Map the file with the correct page size
            pagedFile = pageCache.map(storageFile, filePageSize, databaseName, openOptions);
        }
    } catch (NoSuchFileException | StoreNotFoundException e) {
        if (pagedFile != null) {
            pagedFile.close();
            pagedFile = null;
        }
        if (createIfNotExists) {
            try {
                // Generate the header and determine correct page size
                determineRecordSize(storeHeaderFormat.generateHeader());
                // Create the id generator, and also open it because some stores may need the id generator when initializing their store
                idGenerator = idGeneratorFactory.create(pageCache, idFile, idType, getNumberOfReservedLowIds(), false, recordFormat.getMaxId(), readOnlyChecker, configuration, cursorContext, openOptions);
                // Map the file (w/ the CREATE flag) and initialize the header
                pagedFile = pageCache.map(storageFile, filePageSize, databaseName, openOptions.newWith(CREATE));
                initialiseNewStoreFile(cursorContext);
                // <-- successfully created and initialized
                return true;
            } catch (IOException e1) {
                e.addSuppressed(e1);
            }
        }
        if (e instanceof StoreNotFoundException) {
            throw (StoreNotFoundException) e;
        }
        throw new StoreNotFoundException("Store file not found: " + storageFile, e);
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to open store file: " + storageFile, e);
    }
    return false;
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException)

Example 77 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class DumpCommandIT method shouldGiveAClearMessageIfTheArchivesParentDoesntExist.

@Test
void shouldGiveAClearMessageIfTheArchivesParentDoesntExist() throws Exception {
    doThrow(new NoSuchFileException(archive.getParent().toString())).when(dumper).dump(any(), any(), any(), any(), any());
    CommandFailedException commandFailed = assertThrows(CommandFailedException.class, () -> execute("foo"));
    assertEquals("Unable to dump database: NoSuchFileException: " + archive.getParent(), commandFailed.getMessage());
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) CommandFailedException(org.neo4j.cli.CommandFailedException) Test(org.junit.jupiter.api.Test)

Example 78 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class LoaderTest method shouldGiveAClearErrorMessageIfTheDestinationsParentDirectoryDoesntExist.

@Test
void shouldGiveAClearErrorMessageIfTheDestinationsParentDirectoryDoesntExist() {
    Path archive = testDirectory.file("the-archive.dump");
    Path destination = Paths.get(testDirectory.absolutePath().toString(), "subdir", "the-destination");
    DatabaseLayout databaseLayout = DatabaseLayout.ofFlat(destination);
    NoSuchFileException noSuchFileException = assertThrows(NoSuchFileException.class, () -> new Loader().load(archive, databaseLayout));
    assertEquals(destination.getParent().toString(), noSuchFileException.getMessage());
}
Also used : Path(java.nio.file.Path) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.jupiter.api.Test)

Example 79 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class LoaderTest method shouldGiveAClearErrorMessageIfTheArchiveDoesntExist.

@Test
void shouldGiveAClearErrorMessageIfTheArchiveDoesntExist() throws IOException {
    Path archive = testDirectory.file("the-archive.dump");
    deleteLayoutFolders(databaseLayout);
    NoSuchFileException exception = assertThrows(NoSuchFileException.class, () -> new Loader().load(archive, databaseLayout));
    assertEquals(archive.toString(), exception.getMessage());
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.jupiter.api.Test)

Example 80 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class DumpCommand method dump.

private void dump(DatabaseLayout databaseLayout, Path archive) {
    Path databasePath = databaseLayout.databaseDirectory();
    try {
        var format = selectCompressionFormat(ctx.err());
        var lockFile = databaseLayout.databaseLockFile().getFileName().toString();
        var quarantineMarkerFile = databaseLayout.quarantineMarkerFile().getFileName().toString();
        dumper.dump(databasePath, databaseLayout.getTransactionLogsDirectory(), archive, format, path -> oneOf(path, lockFile, quarantineMarkerFile));
    } catch (FileAlreadyExistsException e) {
        throw new CommandFailedException("Archive already exists: " + e.getMessage(), e);
    } catch (NoSuchFileException e) {
        if (Paths.get(e.getMessage()).toAbsolutePath().equals(databasePath)) {
            throw new CommandFailedException("Database does not exist: " + databaseLayout.getDatabaseName(), e);
        }
        wrapIOException(e);
    } catch (IOException e) {
        wrapIOException(e);
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) CommandFailedException(org.neo4j.cli.CommandFailedException)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11