Skip to content

Commit

Permalink
Always use canonical representation in database and store layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Aug 21, 2018
1 parent 6a29314 commit 9ffe0ca
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 16 deletions.
8 changes: 4 additions & 4 deletions community/io/src/main/java/org/neo4j/io/fs/FileUtils.java
Expand Up @@ -36,6 +36,7 @@
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.io.UncheckedIOException;
import java.io.Writer; import java.io.Writer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -584,16 +585,15 @@ public static String relativePath( File baseDir, File storeFile )
return path; return path;
} }


// TODO javadoc what this one does. It comes from Serverutil initially. public static File getCanonicalFile( File file )
public static File getMostCanonicalFile( File file )
{ {
try try
{ {
return file.getCanonicalFile().getAbsoluteFile(); return file.getCanonicalFile();
} }
catch ( IOException e ) catch ( IOException e )
{ {
return file.getAbsoluteFile(); throw new UncheckedIOException( e );
} }
} }


Expand Down
Expand Up @@ -23,6 +23,8 @@
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.Objects; import java.util.Objects;


import static org.neo4j.io.fs.FileUtils.getCanonicalFile;

public class DatabaseLayout public class DatabaseLayout
{ {
private static final File[] EMPTY_FILES_ARRAY = new File[0]; private static final File[] EMPTY_FILES_ARRAY = new File[0];
Expand All @@ -37,7 +39,8 @@ public static DatabaseLayout of( StoreLayout storeLayout, String databaseName )


public static DatabaseLayout of( File databaseDirectory ) public static DatabaseLayout of( File databaseDirectory )
{ {
return of( databaseDirectory.getParentFile(), databaseDirectory.getName() ); File canonicalFile = getCanonicalFile( databaseDirectory );
return of( canonicalFile.getParentFile(), canonicalFile.getName() );
} }


public static DatabaseLayout of( File rootDirectory, String databaseName ) public static DatabaseLayout of( File rootDirectory, String databaseName )
Expand Down
Expand Up @@ -22,6 +22,8 @@
import java.io.File; import java.io.File;
import java.util.Objects; import java.util.Objects;


import static org.neo4j.io.fs.FileUtils.getCanonicalFile;

public class StoreLayout public class StoreLayout
{ {
private static final String STORE_LOCK_FILENAME = "store_lock"; private static final String STORE_LOCK_FILENAME = "store_lock";
Expand All @@ -30,7 +32,7 @@ public class StoreLayout


public static StoreLayout of( File storeDirectory ) public static StoreLayout of( File storeDirectory )
{ {
return new StoreLayout( storeDirectory ); return new StoreLayout( getCanonicalFile( storeDirectory ) );
} }


private StoreLayout( File rootStoreDirectory ) private StoreLayout( File rootStoreDirectory )
Expand Down
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.io.layout;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.neo4j.test.extension.Inject;
import org.neo4j.test.extension.TestDirectoryExtension;
import org.neo4j.test.rule.TestDirectory;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith( TestDirectoryExtension.class )
class DatabaseLayoutTest
{
@Inject
private TestDirectory testDirectory;

@Test
void databaseLayoutForAbsoluteFile()
{
File databaseDir = testDirectory.databaseDir();
DatabaseLayout databaseLayout = DatabaseLayout.of( databaseDir );
assertEquals( databaseLayout.databaseDirectory(), databaseDir );
}

@Test
void databaseLayoutResolvesLinks() throws IOException
{
Path basePath = testDirectory.directory().toPath();
File databaseDir = testDirectory.databaseDir("notAbsolute");
Path linkPath = basePath.resolve( "link" );
Path symbolicLink = Files.createSymbolicLink( linkPath, databaseDir.toPath() );
DatabaseLayout databaseLayout = DatabaseLayout.of( symbolicLink.toFile() );
assertEquals( databaseLayout.databaseDirectory(), databaseDir );
}

@Test
void databaseLayoutUseCanonicalRepresentation()
{
File storeDir = testDirectory.storeDir( "notCanonical" );
Path basePath = testDirectory.databaseDir( storeDir ).toPath();
Path notCanonicalPath = basePath.resolve( "../anotherDatabase" );
DatabaseLayout databaseLayout = DatabaseLayout.of( notCanonicalPath.toFile() );
File expectedDirectory = StoreLayout.of( storeDir ).databaseLayout( "anotherDatabase" ).databaseDirectory();
assertEquals( expectedDirectory, databaseLayout.databaseDirectory() );
}

@Test
void databaseLayoutForName()
{
String databaseName = "testDatabase";
StoreLayout storeLayout = testDirectory.storeLayout();
DatabaseLayout testDatabase = DatabaseLayout.of( storeLayout, databaseName );
assertEquals( new File( storeLayout.storeDirectory(), databaseName ), testDatabase.databaseDirectory() );
}

@Test
void databaseLayoutForFolderAndName()
{
String database = "database";
DatabaseLayout databaseLayout = DatabaseLayout.of( testDirectory.storeDir(), database );
assertEquals( testDirectory.databaseLayout( database ).databaseDirectory(), databaseLayout.databaseDirectory() );
}
}
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.io.layout;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.neo4j.test.extension.Inject;
import org.neo4j.test.extension.TestDirectoryExtension;
import org.neo4j.test.rule.TestDirectory;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith( TestDirectoryExtension.class )
class StoreLayoutTest
{
@Inject
private TestDirectory testDirectory;

@Test
void storeLayoutForAbsoluteFile()
{
File storeDir = testDirectory.storeDir();
StoreLayout storeLayout = StoreLayout.of( storeDir );
assertEquals( storeDir, storeLayout.storeDirectory() );
}

@Test
void storeLayoutResolvesLinks() throws IOException
{
Path basePath = testDirectory.directory().toPath();
File storeDir = testDirectory.storeDir("notAbsolute");
Path linkPath = basePath.resolve( "link" );
Path symbolicLink = Files.createSymbolicLink( linkPath, storeDir.toPath() );
StoreLayout storeLayout = StoreLayout.of( symbolicLink.toFile() );
assertEquals( storeDir, storeLayout.storeDirectory() );
}

@Test
void storeLayoutUseCanonicalRepresentation()
{
Path basePath = testDirectory.storeDir("notCanonical").toPath();
Path notCanonicalPath = basePath.resolve( "../anotherLocation" );
StoreLayout storeLayout = StoreLayout.of( notCanonicalPath.toFile() );
assertEquals( testDirectory.directory( "anotherLocation" ), storeLayout.storeDirectory() );
}
}
Expand Up @@ -109,6 +109,7 @@ public class RecoveryTest
@Before @Before
public void setUp() throws Exception public void setUp() throws Exception
{ {
storeDir = directory.storeDir();
logFiles = LogFilesBuilder.builder( directory.databaseLayout(), fileSystemRule.get() ) logFiles = LogFilesBuilder.builder( directory.databaseLayout(), fileSystemRule.get() )
.withLogVersionRepository( logVersionRepository ) .withLogVersionRepository( logVersionRepository )
.withTransactionIdStore( transactionIdStore ) .withTransactionIdStore( transactionIdStore )
Expand Down
Expand Up @@ -47,9 +47,9 @@


public class FileUtilsTest public class FileUtilsTest
{ {
public TestDirectory testDirectory = TestDirectory.testDirectory(); public final TestDirectory testDirectory = TestDirectory.testDirectory();
public ExpectedException expected = ExpectedException.none(); public final ExpectedException expected = ExpectedException.none();
public FileSystemAbstraction fs = new DefaultFileSystemAbstraction(); public final FileSystemAbstraction fs = new DefaultFileSystemAbstraction();


@Rule @Rule
public RuleChain chain = RuleChain.outerRule( testDirectory ).around( expected ); public RuleChain chain = RuleChain.outerRule( testDirectory ).around( expected );
Expand Down
Expand Up @@ -150,14 +150,12 @@ public PlatformModule( File providedStoreDir, Config config, DatabaseInfo databa
dependencies.satisfyDependency( databaseInfo ); dependencies.satisfyDependency( databaseInfo );


clock = dependencies.satisfyDependency( createClock() ); clock = dependencies.satisfyDependency( createClock() );

life = dependencies.satisfyDependency( createLife() ); life = dependencies.satisfyDependency( createLife() );


// SPI - provided services this.storeLayout = StoreLayout.of( providedStoreDir );
config.augmentDefaults( GraphDatabaseSettings.neo4j_home, providedStoreDir.getAbsolutePath() );
this.config = dependencies.satisfyDependency( config );


this.storeLayout = StoreLayout.of( providedStoreDir.getAbsoluteFile() ); config.augmentDefaults( GraphDatabaseSettings.neo4j_home, storeLayout.storeDirectory().getPath() );
this.config = dependencies.satisfyDependency( config );


fileSystem = dependencies.satisfyDependency( createFileSystemAbstraction() ); fileSystem = dependencies.satisfyDependency( createFileSystemAbstraction() );
life.add( new FileSystemLifecycleAdapter( fileSystem ) ); life.add( new FileSystemLifecycleAdapter( fileSystem ) );
Expand Down
Expand Up @@ -43,7 +43,7 @@
import org.neo4j.storageengine.api.StoreFileMetadata; import org.neo4j.storageengine.api.StoreFileMetadata;


import static org.neo4j.com.RequestContext.anonymous; import static org.neo4j.com.RequestContext.anonymous;
import static org.neo4j.io.fs.FileUtils.getMostCanonicalFile; import static org.neo4j.io.fs.FileUtils.getCanonicalFile;
import static org.neo4j.io.fs.FileUtils.relativePath; import static org.neo4j.io.fs.FileUtils.relativePath;


/** /**
Expand Down Expand Up @@ -128,7 +128,7 @@ public StoreCopyServer( NeoStoreDataSource dataSource, CheckPointer checkPointer
this.dataSource = dataSource; this.dataSource = dataSource;
this.checkPointer = checkPointer; this.checkPointer = checkPointer;
this.fileSystem = fileSystem; this.fileSystem = fileSystem;
this.databaseDirectory = getMostCanonicalFile( databaseDirectory ); this.databaseDirectory = getCanonicalFile( databaseDirectory );
this.monitor = monitor; this.monitor = monitor;
} }


Expand Down

0 comments on commit 9ffe0ca

Please sign in to comment.