Skip to content

Commit

Permalink
Add support for --mode="csv" doing the absolute simplest thing possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
srbaker committed Sep 15, 2016
1 parent 4f47dd6 commit 0152e73
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
5 changes: 5 additions & 0 deletions community/dbms/pom.xml
Expand Up @@ -74,6 +74,11 @@
<artifactId>neo4j-io</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-import-tool</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
@@ -0,0 +1,41 @@
/*
* 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.dbms;

import java.io.IOException;

import org.neo4j.kernel.configuration.Config;
import org.neo4j.tooling.ImportTool;

class CsvImporter implements Importer
{
private final String[] args;

CsvImporter( String[] args, Config config )
{
this.args = args;
}

@Override
public void doImport() throws IOException
{
ImportTool.main( args );
}
}
Expand Up @@ -33,28 +33,31 @@
import static org.neo4j.dbms.DatabaseManagementSystemSettings.database_path;
import static org.neo4j.helpers.collection.MapUtil.stringMap;

public class DatabaseImporter
class DatabaseImporter implements Importer
{
private final String database;
private final File from;
private final Config config;

public DatabaseImporter( Args parsedArgs, Config config ) throws IncorrectUsage
DatabaseImporter( String[] args, Config config ) throws IncorrectUsage
{
Args parsedArgs = Args.parse( args );
String database;

try
{
this.database = parsedArgs.interpretOption( "database", Converters.<String>mandatory(), s -> s );
this.from = parsedArgs.interpretOption( "from", Converters.<File>mandatory(), Converters.toFile(),
database = parsedArgs.interpretOption( "database", Converters.mandatory(), s -> s );
this.from = parsedArgs.interpretOption( "from", Converters.mandatory(), Converters.toFile(),
Validators.CONTAINS_EXISTING_DATABASE );
}
catch ( IllegalArgumentException e )
{
throw new IncorrectUsage( e.getMessage() );
}
this.config =
config.with( stringMap( DatabaseManagementSystemSettings.active_database.name(), this.database ) );

this.config = config.with( stringMap( DatabaseManagementSystemSettings.active_database.name(), database ) );
}

@Override
public void doImport() throws IOException
{
copyDatabase( from, config );
Expand Down
Expand Up @@ -68,9 +68,9 @@ public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWo

private final Path homeDir;
private final Path configDir;
private final String[] allowedModes = {"database"};
private final String[] allowedModes = {"database", "csv"};

public ImportCommand( Path homeDir, Path configDir )
ImportCommand( Path homeDir, Path configDir )
{
this.homeDir = homeDir;
this.configDir = configDir;
Expand All @@ -80,10 +80,12 @@ public ImportCommand( Path homeDir, Path configDir )
public void execute( String[] args ) throws IncorrectUsage, CommandFailed
{
Args parsedArgs = Args.parse( args );
String mode;

try
{
parsedArgs.interpretOption( "mode", Converters.<String>mandatory(), s -> s,
Validators.inList( allowedModes ) );
mode = parsedArgs
.interpretOption( "mode", Converters.mandatory(), s -> s, Validators.inList( allowedModes ) );
}
catch ( IllegalArgumentException e )
{
Expand All @@ -93,8 +95,21 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed
try
{
Config config = loadNeo4jConfig( homeDir, configDir );
DatabaseImporter databaseImporter = new DatabaseImporter( parsedArgs, config );
databaseImporter.doImport();
Importer importer;

switch ( mode )
{
case "database":
importer = new DatabaseImporter( args, config );
break;
case "csv":
importer = new CsvImporter( args, config );
break;
default:
throw new CommandFailed( "Invalid mode specified." ); // This won't happen because mode is mandatory.
}

importer.doImport();
}
catch ( IOException e )
{
Expand All @@ -105,10 +120,9 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed
private static Config loadNeo4jConfig( Path homeDir, Path configDir )
{
ConfigLoader configLoader = new ConfigLoader( settings() );
Config config = configLoader.loadConfig( Optional.of( homeDir.toFile() ),
Optional.of( configDir.resolve( "neo4j.conf" ).toFile() ) );

return config;
return configLoader.loadConfig( Optional.of( homeDir.toFile() ),
Optional.of( configDir.resolve( "neo4j.conf" ).toFile() ) );
}

private static List<Class<?>> settings()
Expand Down
@@ -0,0 +1,27 @@
/*
* 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.dbms;

import java.io.IOException;

interface Importer
{
void doImport() throws IOException;
}

0 comments on commit 0152e73

Please sign in to comment.