Search in sources :

Example 1 with FileHandle

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

the class ZipUtils method zip.

/**
 * Create zip archive for requested <code>sourceToCompress</code>.
 * If <code>sourceToCompress</code> is a directory then content of that directory and all its sub-directories will be added to the archive.
 * If <code>sourceToCompress</code> does not exist or is an empty directory then archive will not be created.
 * @param fileSystem source file system
 * @param sourceToCompress source file to compress
 * @param destinationZip zip file compress source to
 * @param includeSourceDirectoryInRelativePath true if relative path of content should include sourceToCompress, otherwise false.
 *                                             This is only meaningful sourceToCompress is a directory.
 * @throws IOException when underlying file system access produce IOException
 */
public static void zip(FileSystemAbstraction fileSystem, Path sourceToCompress, Path destinationZip, boolean includeSourceDirectoryInRelativePath) throws IOException {
    if (!fileSystem.fileExists(sourceToCompress)) {
        return;
    }
    if (isEmptyDirectory(fileSystem, sourceToCompress)) {
        return;
    }
    Map<String, String> env = Map.of("create", "true");
    URI archiveAbsoluteURI = URI.create("jar:file:" + destinationZip.toUri().getRawPath());
    Path baseForRelativePath = sourceToCompress;
    if (includeSourceDirectoryInRelativePath) {
        baseForRelativePath = sourceToCompress.getParent();
    }
    try (FileSystem zipFs = FileSystems.newFileSystem(archiveAbsoluteURI, env)) {
        List<FileHandle> fileHandles = fileSystem.streamFilesRecursive(sourceToCompress).collect(toList());
        for (FileHandle fileHandle : fileHandles) {
            Path sourcePath = fileHandle.getPath();
            Path zipFsPath = fileSystem.isDirectory(sourceToCompress) ? zipFs.getPath(baseForRelativePath.relativize(sourcePath).toString()) : zipFs.getPath(sourcePath.getFileName().toString());
            if (zipFsPath.getParent() != null) {
                Files.createDirectories(zipFsPath.getParent());
            }
            Files.copy(sourcePath, zipFsPath);
        }
    }
}
Also used : Path(java.nio.file.Path) FileHandle(org.neo4j.io.fs.FileHandle) FileSystem(java.nio.file.FileSystem) URI(java.net.URI)

Example 2 with FileHandle

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

the class ConsistencyCheckWithCorruptGBPTreeIT method schemaIndexFiles.

private Path[] schemaIndexFiles(FileSystemAbstraction fs, Path databaseDir, GraphDatabaseSettings.SchemaIndex schemaIndex) throws IOException {
    final String fileNameFriendlyProviderName = IndexDirectoryStructure.fileNameFriendly(schemaIndex.providerName());
    Path indexDir = databaseDir.resolve("schema/index/");
    return fs.streamFilesRecursive(indexDir).map(FileHandle::getPath).filter(path -> path.toAbsolutePath().toString().contains(fileNameFriendlyProviderName)).toArray(Path[]::new);
}
Also used : Path(java.nio.file.Path) BeforeEach(org.junit.jupiter.api.BeforeEach) DatabaseReadOnlyChecker.readOnly(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker.readOnly) GBPTreeCorruption.pageSpecificCorruption(org.neo4j.index.internal.gbptree.GBPTreeCorruption.pageSpecificCorruption) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Config(org.neo4j.configuration.Config) NullLogProvider(org.neo4j.logging.NullLogProvider) InspectingVisitor(org.neo4j.index.internal.gbptree.InspectingVisitor) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) NATIVE30(org.neo4j.configuration.GraphDatabaseSettings.SchemaIndex.NATIVE30) BeforeAll(org.junit.jupiter.api.BeforeAll) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Transaction(org.neo4j.graphdb.Transaction) Path(java.nio.file.Path) UncloseableDelegatingFileSystemAbstraction(org.neo4j.io.fs.UncloseableDelegatingFileSystemAbstraction) DatabaseReadOnlyChecker.writable(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker.writable) TestDirectory(org.neo4j.test.rule.TestDirectory) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) List(java.util.List) NATIVE_BTREE10(org.neo4j.configuration.GraphDatabaseSettings.SchemaIndex.NATIVE_BTREE10) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Writer(java.io.Writer) DatabaseReadOnlyChecker(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) IOUtils.readLines(org.apache.commons.io.IOUtils.readLines) NONE(org.neo4j.internal.helpers.progress.ProgressMonitorFactory.NONE) ImmutableLongList(org.eclipse.collections.api.list.primitive.ImmutableLongList) GBPTreeInspection(org.neo4j.index.internal.gbptree.GBPTreeInspection) Arrays(org.bouncycastle.util.Arrays) Label(org.neo4j.graphdb.Label) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) LogProvider(org.neo4j.logging.LogProvider) NULL(org.neo4j.io.pagecache.tracing.PageCacheTracer.NULL) JobSchedulerFactory.createInitialisedScheduler(org.neo4j.kernel.impl.scheduler.JobSchedulerFactory.createInitialisedScheduler) GBPTreeBootstrapper(org.neo4j.index.internal.gbptree.GBPTreeBootstrapper) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) GBPTree(org.neo4j.index.internal.gbptree.GBPTree) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) SchemaLayouts(org.neo4j.kernel.impl.index.schema.SchemaLayouts) CountsLayout(org.neo4j.internal.counts.CountsLayout) GBPTreeCorruption(org.neo4j.index.internal.gbptree.GBPTreeCorruption) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JobScheduler(org.neo4j.scheduler.JobScheduler) FileHandle(org.neo4j.io.fs.FileHandle) DEFAULT(org.neo4j.consistency.checking.full.ConsistencyFlags.DEFAULT) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StringWriter(java.io.StringWriter) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) IOException(java.io.IOException) IndexDirectoryStructure(org.neo4j.kernel.api.index.IndexDirectoryStructure) ProgressMonitorFactory(org.neo4j.internal.helpers.progress.ProgressMonitorFactory) GBPTreePointerType(org.neo4j.index.internal.gbptree.GBPTreePointerType) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ConsistencyCheckIncompleteException(org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) NO_FLUSH_ON_CLOSE(org.neo4j.index.internal.gbptree.GBPTreeOpenOptions.NO_FLUSH_ON_CLOSE) GraphDatabaseSettings.neo4j_home(org.neo4j.configuration.GraphDatabaseSettings.neo4j_home) ConsistencyFlags(org.neo4j.consistency.checking.full.ConsistencyFlags) LayoutBootstrapper(org.neo4j.index.internal.gbptree.LayoutBootstrapper) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) FileHandle(org.neo4j.io.fs.FileHandle)

Aggregations

Path (java.nio.file.Path)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 String.format (java.lang.String.format)1 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 FileSystem (java.nio.file.FileSystem)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 IOUtils.readLines (org.apache.commons.io.IOUtils.readLines)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 Arrays (org.bouncycastle.util.Arrays)1 ImmutableLongList (org.eclipse.collections.api.list.primitive.ImmutableLongList)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1