Skip to content

Commit

Permalink
Merge pull request #7667 from srbaker/3.1-admin-tool-import
Browse files Browse the repository at this point in the history
Add `import` to new AdminTool.
  • Loading branch information
srbaker committed Aug 4, 2016
2 parents 15ec429 + 53cc306 commit 475700e
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 168 deletions.
19 changes: 19 additions & 0 deletions community/command-line/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-io</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-dbms</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public static void main( String[] args )
{
Path homeDir = Paths.get( System.getenv( "NEO4J_HOME" ) );
Path configDir = Paths.get( System.getenv( "NEO4J_CONF" ) );
String extraHelp = System.getenv( "NEO4J_EXTRA_HELP" );
boolean debug = System.getenv( "NEO4J_DEBUG" ) != null;

AdminTool tool = new AdminTool( CommandLocator.fromServiceLocator(), System.out::println, extraHelp, debug );
AdminTool tool = new AdminTool( CommandLocator.fromServiceLocator(), System.out::println, debug );
Result result = tool.execute( homeDir, configDir, args );
result.exit();
}
Expand All @@ -47,18 +46,22 @@ public static void main( String[] args )
private final boolean debug;
private final Usage usage;

public AdminTool( CommandLocator locator, Output out, String extraHelp, boolean debug )
public AdminTool( CommandLocator locator, Output out, boolean debug )
{
this.locator = CommandLocator.withAdditionalCommand( help(), locator );
this.out = out;
this.debug = debug;
this.usage = new Usage( scriptName, out, this.locator, extraHelp );
this.usage = new Usage( scriptName, out, this.locator );
}

public Result execute( Path homeDir, Path configDir, String... args )
{
try
{
if ( args.length == 0 )
{
return badUsage( "you must provide a command" );
}
String name = args[0];
String[] commandArgs = Arrays.copyOfRange( args, 1, args.length );

Expand All @@ -69,7 +72,7 @@ public Result execute( Path homeDir, Path configDir, String... args )
}
catch ( NoSuchElementException e )
{
return badUsage( name, commandArgs );
return badUsage( format( "unrecognized command: %s", name ) );
}

AdminCommand command = provider.create( homeDir, configDir );
Expand Down Expand Up @@ -104,10 +107,9 @@ private Result badUsage( AdminCommand.Provider command, IncorrectUsage e )
return failure( e.getMessage() );
}

private Result badUsage( String name, String[] commandArgs )
private Result badUsage( String message )
{
usage.print();
String message = commandArgs.length == 0 ? format( "unrecognized command: %s", name ) : commandArgs[0];
return failure( message );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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.commandline.admin;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.neo4j.dbms.DatabaseManagementSystemSettings;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.util.Converters;
import org.neo4j.kernel.impl.util.Validators;
import org.neo4j.server.configuration.ConfigLoader;

import static org.neo4j.dbms.DatabaseManagementSystemSettings.database_path;
import static org.neo4j.helpers.collection.MapUtil.stringMap;

public class ImportCommand implements AdminCommand
{
public static class Provider extends AdminCommand.Provider
{
public Provider()
{
super( "import" );
}

@Override
public Optional<String> arguments()
{
return Optional.of( "--mode=<mode> --database=<database-name> --from=<source-directory>" );
}

@Override
public String description()
{
return "Import a database from a pre-3.0 Neo4j installation. <source-directory> " +
"is the database location (e.g. <neo4j-root>/data/graph.db).";
}

@Override
public AdminCommand create( Path homeDir, Path configDir )
{
return new ImportCommand( homeDir, configDir );
}
}

private final Path homeDir;
private final Path configDir;

public ImportCommand( Path homeDir, Path configDir )
{
this.homeDir = homeDir;
this.configDir = configDir;
}

@Override
public void execute( String[] args ) throws IncorrectUsage, CommandFailed
{
File from;
String database;

Args parsedArgs = Args.parse( args );
try
{
parsedArgs.interpretOption( "mode", Converters.<String>mandatory(), s -> s,
Validators.inList( new String[] { "database" } ) );
database = parsedArgs.interpretOption( "database", Converters.<String>mandatory(), s -> s );
from = parsedArgs.interpretOption( "from", Converters.<File>mandatory(), Converters.toFile(),
Validators.CONTAINS_EXISTING_DATABASE );
}
catch ( IllegalArgumentException e )
{
throw new IncorrectUsage( e.getMessage() );
}

try
{
Config config = loadNeo4jConfig( homeDir, configDir, database );
copyDatabase( from, config );
removeMessagesLog( config );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}

}

private void copyDatabase( File from, Config config ) throws IOException
{
FileUtils.copyRecursively( from, config.get( database_path ) );
}

private void removeMessagesLog( Config config )
{
FileUtils.deleteFile( new File( config.get( database_path ), "messages.log" ) );
}

private static Config loadNeo4jConfig( Path homeDir, Path configDir, String databaseName )
{
ConfigLoader configLoader = new ConfigLoader( settings() );
Config config = configLoader.loadConfig(
Optional.of( homeDir.toFile() ),
Optional.of( configDir.resolve( "neo4j.conf" ).toFile() ));

return config.with( stringMap( DatabaseManagementSystemSettings.active_database.name(), databaseName ) );
}

private static List<Class<?>> settings()
{
List<Class<?>> settings = new ArrayList<>();
settings.add( GraphDatabaseSettings.class );
settings.add( DatabaseManagementSystemSettings.class );
return settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ public class Usage
private final String scriptName;
private final Output out;
private final CommandLocator commands;
private final String extraHelp;

public Usage( String scriptName, Output out, CommandLocator commands, String extraHelp )
public Usage( String scriptName, Output out, CommandLocator commands )
{
this.scriptName = scriptName;
this.out = out;
this.commands = commands;
this.extraHelp = extraHelp;
}

public void print()
Expand All @@ -46,9 +44,6 @@ public void print()
{
new CommandUsage( command, out, scriptName ).print();
}

out.line( extraHelp );
out.line( "" );
}

public static class CommandUsage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.neo4j.commandline.admin.ImportCommand$Provider
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void shouldExecuteTheCommand()
{
RecordingCommand command = new RecordingCommand();
AdminCommand.Provider provider = command.provider();
AdminTool tool = new AdminTool( new CannedLocator( provider ), new NullOutput(), "", false );
AdminTool tool = new AdminTool( new CannedLocator( provider ), new NullOutput(), false );
tool.execute( null, null, provider.name() );
assertThat( command.executed, is( true ) );
}
Expand All @@ -47,10 +47,19 @@ public void shouldExecuteTheCommand()
public void shouldAddTheHelpCommand()
{
Output output = mock( Output.class );
new AdminTool( new NullCommandLocator(), output, "", false ).execute( null, null, "help" );
new AdminTool( new NullCommandLocator(), output, false ).execute( null, null, "help" );
verify( output ).line( "neo4j-admin help" );
}

@Test
public void shouldProvideErrorMessageWhenNoCommandIsProvided()
{
Output output = mock( Output.class );
new AdminTool( new NullCommandLocator(), output, false ).execute( null, null, new String[]{} );

verify( output ).line( "Usage:" );
}

private static class RecordingCommand implements AdminCommand
{
public boolean executed;
Expand Down

0 comments on commit 475700e

Please sign in to comment.