Skip to content

Commit

Permalink
Updated SubGraphExporter to output indexes, constraints and queries w…
Browse files Browse the repository at this point in the history
…ith trailing semicolon and option to pass in begin-commit commands
  • Loading branch information
jexp committed Feb 22, 2016
1 parent 6e28dc9 commit 91020d4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 46 deletions.
Expand Up @@ -49,12 +49,32 @@ public SubGraphExporter( SubGraph graph )


public void export( PrintWriter out ) public void export( PrintWriter out )
{ {
export(out, null, null);
}

public void export( PrintWriter out, String begin, String commit )
{
output( out, begin );
appendIndexes( out ); appendIndexes( out );
appendConstraints( out ); appendConstraints( out );
appendNodes( out ); output( out, commit, begin );
appendRelationships( out ); long nodes = appendNodes( out );
long relationships = appendRelationships( out );
if ( nodes + relationships > 0 )
{
out.println( ";" );
}
output( out, commit );
} }


private void output( PrintWriter out, String ... commands )
{
for ( String command : commands )
{
if ( command == null ) continue;
out.println( command );
}
}
private Collection<String> exportIndexes() private Collection<String> exportIndexes()
{ {
final List<String> result = new ArrayList<>(); final List<String> result = new ArrayList<>();
Expand Down Expand Up @@ -140,27 +160,32 @@ private void appendIndexes( PrintWriter out )
{ {
for ( String line : exportIndexes() ) for ( String line : exportIndexes() )
{ {
out.println( line ); out.print( line );
out.println( ";" );
} }
} }


private void appendConstraints( PrintWriter out ) private void appendConstraints( PrintWriter out )
{ {
for ( String line : exportConstraints() ) for ( String line : exportConstraints() )
{ {
out.println( line ); out.print( line );
out.println( ";" );
} }
} }


private void appendRelationships( PrintWriter out ) private long appendRelationships( PrintWriter out )
{ {
long relationships = 0;
for ( Node node : graph.getNodes() ) for ( Node node : graph.getNodes() )
{ {
for ( Relationship rel : node.getRelationships( Direction.OUTGOING ) ) for ( Relationship rel : node.getRelationships( Direction.OUTGOING ) )
{ {
appendRelationship( out, rel ); appendRelationship( out, rel );
relationships++;
} }
} }
return relationships;
} }


private void appendRelationship( PrintWriter out, Relationship rel ) private void appendRelationship( PrintWriter out, Relationship rel )
Expand All @@ -175,12 +200,15 @@ private void appendRelationship( PrintWriter out, Relationship rel )
out.println( ")" ); out.println( ")" );
} }


private void appendNodes( PrintWriter out ) private long appendNodes( PrintWriter out )
{ {
long nodes = 0;
for ( Node node : graph.getNodes() ) for ( Node node : graph.getNodes() )
{ {
appendNode( out, node ); appendNode( out, node );
nodes++;
} }
return nodes;
} }


private void appendNode( PrintWriter out, Node node ) private void appendNode( PrintWriter out, Node node )
Expand Down
Expand Up @@ -78,7 +78,7 @@ public void testEmptyGraph() throws Exception
public void testNodeWithProperties() throws Exception public void testNodeWithProperties() throws Exception
{ {
gdb.createNode().setProperty( "name", "Andres" ); gdb.createNode().setProperty( "name", "Andres" );
assertEquals( "create (_0 {`name`:\"Andres\"})" + lineSeparator(), doExportGraph( gdb ) ); assertEquals( "create (_0 {`name`:\"Andres\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( gdb ) );
} }


@Test @Test
Expand All @@ -87,7 +87,7 @@ public void testNodeWithFloatProperty() throws Exception
final float floatValue = 10.1f; final float floatValue = 10.1f;
final String expected = "10.100000"; final String expected = "10.100000";
gdb.createNode().setProperty( "float", floatValue ); gdb.createNode().setProperty( "float", floatValue );
assertEquals( "create (_0 {`float`:" + expected + "})" + lineSeparator(), doExportGraph( gdb ) ); assertEquals( "create (_0 {`float`:" + expected + "})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( gdb ) );
} }


@Test @Test
Expand All @@ -96,7 +96,7 @@ public void testNodeWithDoubleProperty() throws Exception
final double doubleValue = 123456.123456; final double doubleValue = 123456.123456;
final String expected = "123456.123456"; final String expected = "123456.123456";
gdb.createNode().setProperty( "double", doubleValue ); gdb.createNode().setProperty( "double", doubleValue );
assertEquals( "create (_0 {`double`:" + expected + "})" + lineSeparator(), doExportGraph( gdb ) ); assertEquals( "create (_0 {`double`:" + expected + "})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( gdb ) );
} }


private String doExportGraph( GraphDatabaseService db ) private String doExportGraph( GraphDatabaseService db )
Expand All @@ -118,7 +118,7 @@ public void testFromSimpleCypherResult() throws Exception
Node n = gdb.createNode(); Node n = gdb.createNode();
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + ")" + lineSeparator(), doExportGraph( graph ) ); assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -127,7 +127,7 @@ public void testSingleNode() throws Exception
Node n = gdb.createNode(); Node n = gdb.createNode();
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + ")" + lineSeparator(), doExportGraph( graph ) ); assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -138,7 +138,7 @@ public void testSingleNodeWithProperties() throws Exception
n.setProperty( "age", 42 ); n.setProperty( "age", 42 );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + " {`age`:42, `name`:\"Node1\"})" + lineSeparator(), doExportGraph( graph ) ); assertEquals( "create (_" + n.getId() + " {`age`:42, `name`:\"Node1\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -148,7 +148,7 @@ public void testEscapingOfNodeStringPropertyValue() throws Exception
n.setProperty( "name", "Brutus \"Brutal\" Howell" ); n.setProperty( "name", "Brutus \"Brutal\" Howell" );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + " {`name`:\"Brutus \\\"Brutal\\\" Howell\"})" + lineSeparator(), assertEquals( "create (_" + n.getId() + " {`name`:\"Brutus \\\"Brutal\\\" Howell\"})" + lineSeparator() + ";" + lineSeparator(),
doExportGraph( graph ) ); doExportGraph( graph ) );
} }


Expand All @@ -159,7 +159,7 @@ public void testEscapingOfNodeStringArrayPropertyValue() throws Exception
n.setProperty( "name", new String[]{"Brutus \"Brutal\" Howell", "Dr."} ); n.setProperty( "name", new String[]{"Brutus \"Brutal\" Howell", "Dr."} );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + " {`name`:[\"Brutus \\\"Brutal\\\" Howell\", \"Dr.\"]})" + lineSeparator(), assertEquals( "create (_" + n.getId() + " {`name`:[\"Brutus \\\"Brutal\\\" Howell\", \"Dr.\"]})" + lineSeparator() + ";" + lineSeparator(),
doExportGraph( graph ) ); doExportGraph( graph ) );
} }


Expand All @@ -172,7 +172,7 @@ public void testEscapingOfRelationshipStringPropertyValue() throws Exception
final ExecutionResult result = result( "rel", rel ); final ExecutionResult result = result( "rel", rel );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
assertEquals( "create (_0)" + lineSeparator() + assertEquals( "create (_0)" + lineSeparator() +
"create (_0)-[:`REL` {`name`:\"Brutus \\\"Brutal\\\" Howell\"}]->(_0)" + lineSeparator(), doExportGraph( graph ) ); "create (_0)-[:`REL` {`name`:\"Brutus \\\"Brutal\\\" Howell\"}]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -184,7 +184,7 @@ public void testEscapingOfRelationshipStringArrayPropertyValue() throws Exceptio
final ExecutionResult result = result( "rel", rel ); final ExecutionResult result = result( "rel", rel );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
assertEquals( "create (_0)" + lineSeparator() + assertEquals( "create (_0)" + lineSeparator() +
"create (_0)-[:`REL` {`name`:[\"Brutus \\\"Brutal\\\" Howell\", \"Dr.\"]}]->(_0)" + lineSeparator(), "create (_0)-[:`REL` {`name`:[\"Brutus \\\"Brutal\\\" Howell\", \"Dr.\"]}]->(_0)" + lineSeparator() + ";" + lineSeparator(),
doExportGraph( graph ) ); doExportGraph( graph ) );
} }


Expand All @@ -195,7 +195,7 @@ public void testEscapingStringPropertyWithBackslash() throws Exception
n.setProperty( "name", "Some\\thing" ); n.setProperty( "name", "Some\\thing" );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + " {`name`:\"Some\\\\thing\"})" + lineSeparator(), assertEquals( "create (_" + n.getId() + " {`name`:\"Some\\\\thing\"})" + lineSeparator() + ";" + lineSeparator(),
doExportGraph( graph ) ); doExportGraph( graph ) );
} }


Expand All @@ -206,7 +206,7 @@ public void testEscapingStringPropertyWithBackslashAndDoubleQuote() throws Excep
n.setProperty( "name", "Some\\\"thing" ); n.setProperty( "name", "Some\\\"thing" );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + " {`name`:\"Some\\\\\\\"thing\"})" + lineSeparator(), assertEquals( "create (_" + n.getId() + " {`name`:\"Some\\\\\\\"thing\"})" + lineSeparator() + ";" + lineSeparator(),
doExportGraph( graph ) ); doExportGraph( graph ) );
} }


Expand All @@ -218,7 +218,7 @@ public void testSingleNodeWithArrayProperties() throws Exception
n.setProperty( "age", new int[]{1, 2} ); n.setProperty( "age", new int[]{1, 2} );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + " {`age`:[1, 2], `name`:[\"a\", \"b\"]})" + lineSeparator(), doExportGraph( graph ) ); assertEquals( "create (_" + n.getId() + " {`age`:[1, 2], `name`:[\"a\", \"b\"]})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -229,21 +229,21 @@ public void testSingleNodeLabels() throws Exception
n.addLabel( Label.label( "Bar" ) ); n.addLabel( Label.label( "Bar" ) );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
assertEquals( "create (_" + n.getId() + ":`Foo`:`Bar`)" + lineSeparator(), doExportGraph( graph ) ); assertEquals( "create (_" + n.getId() + ":`Foo`:`Bar`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
public void testExportIndex() throws Exception public void testExportIndex() throws Exception
{ {
gdb.schema().indexFor( Label.label( "Foo" ) ).on( "bar" ).create(); gdb.schema().indexFor( Label.label( "Foo" ) ).on( "bar" ).create();
assertEquals( "create index on :`Foo`(`bar`)" + lineSeparator() , doExportGraph( gdb ) ); assertEquals( "create index on :`Foo`(`bar`);" + lineSeparator() , doExportGraph( gdb ) );
} }


@Test @Test
public void testExportUniquenessConstraint() throws Exception public void testExportUniquenessConstraint() throws Exception
{ {
gdb.schema().constraintFor( Label.label( "Foo" ) ).assertPropertyIsUnique( "bar" ).create(); gdb.schema().constraintFor( Label.label( "Foo" ) ).assertPropertyIsUnique( "bar" ).create();
assertEquals( "create constraint on (n:`Foo`) assert n.`bar` is unique" + lineSeparator(), doExportGraph( gdb ) ); assertEquals( "create constraint on (n:`Foo`) assert n.`bar` is unique;" + lineSeparator(), doExportGraph( gdb ) );
} }


@Test @Test
Expand All @@ -256,9 +256,9 @@ public void testExportIndexesViaCypherResult() throws Exception
Node n = gdb.createNode( label ); Node n = gdb.createNode( label );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
assertEquals( "create index on :`Foo`(`bar2`)" + lineSeparator() + assertEquals( "create index on :`Foo`(`bar2`);" + lineSeparator() +
"create index on :`Foo`(`bar`)" + lineSeparator() + "create index on :`Foo`(`bar`);" + lineSeparator() +
"create (_0:`Foo`)" + lineSeparator(), doExportGraph( graph ) ); "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -271,9 +271,9 @@ public void testExportConstraintsViaCypherResult() throws Exception
Node n = gdb.createNode( label ); Node n = gdb.createNode( label );
final ExecutionResult result = result( "node", n ); final ExecutionResult result = result( "node", n );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
assertEquals( "create constraint on (n:`Foo`) assert n.`bar2` is unique" + lineSeparator() + assertEquals( "create constraint on (n:`Foo`) assert n.`bar2` is unique;" + lineSeparator() +
"create constraint on (n:`Foo`) assert n.`bar` is unique" + lineSeparator() + "create constraint on (n:`Foo`) assert n.`bar` is unique;" + lineSeparator() +
"create (_0:`Foo`)" + lineSeparator(), doExportGraph( graph ) ); "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


private void commitAndStartNewTransactionAfterSchemaChanges() private void commitAndStartNewTransactionAfterSchemaChanges()
Expand All @@ -291,7 +291,7 @@ public void testFromRelCypherResult() throws Exception
final ExecutionResult result = result( "rel", rel ); final ExecutionResult result = result( "rel", rel );
final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
assertEquals( "create (_0)" + lineSeparator() + assertEquals( "create (_0)" + lineSeparator() +
"create (_0)-[:`REL`]->(_0)" + lineSeparator(), doExportGraph( graph ) ); "create (_0)-[:`REL`]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@Test @Test
Expand All @@ -305,7 +305,7 @@ public void testFromPathCypherResult() throws Exception
final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
assertEquals( "create (_0)" + lineSeparator() + assertEquals( "create (_0)" + lineSeparator() +
"create (_1)" + lineSeparator() + "create (_1)" + lineSeparator() +
"create (_0)-[:`REL`]->(_1)" + lineSeparator(), doExportGraph( graph ) ); "create (_0)-[:`REL`]->(_1)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Expand Down Expand Up @@ -374,6 +374,6 @@ public void testFromSimpleGraph() throws Exception
assertEquals( "create (_" + n0.getId() + ")" + lineSeparator() + assertEquals( "create (_" + n0.getId() + ")" + lineSeparator() +
"create (_" + n1.getId() + " {`name`:\"Node1\"})" + lineSeparator() + "create (_" + n1.getId() + " {`name`:\"Node1\"})" + lineSeparator() +
"create (_" + n0.getId() + ")-[:`REL` {`related`:true}]->(_" + n1.getId() + ")" + "create (_" + n0.getId() + ")-[:`REL` {`related`:true}]->(_" + n1.getId() + ")" +
lineSeparator(), doExportGraph( graph ) ); lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
} }
} }
Expand Up @@ -39,23 +39,10 @@ public class Exporter


public void export( Output out ) throws RemoteException, ShellException public void export( Output out ) throws RemoteException, ShellException
{ {
begin( out ); exporter.export( asWriter(out), "begin", "commit" );
exporter.export(asWriter(out));
out.println(";");
commit(out);
} }


private PrintWriter asWriter(Output out) { private PrintWriter asWriter(Output out) {
return new PrintWriter( new OutputAsWriter( out ) ); return new PrintWriter( new OutputAsWriter( out ) );
} }

private void begin( Output out ) throws RemoteException
{
out.println( "begin" );
}

private void commit( Output out ) throws RemoteException
{
out.println( "commit" );
}
} }
Expand Up @@ -184,7 +184,9 @@ public void testDumpDatabase() throws Exception
doc.add( "create index on :Person(name);", "", "create an index" ); doc.add( "create index on :Person(name);", "", "create an index" );
doc.add( "create (m:Person:Hacker {name:'Mattias'}), (m)-[:KNOWS]->(m);", "", "create one labeled node and a relationship" ); doc.add( "create (m:Person:Hacker {name:'Mattias'}), (m)-[:KNOWS]->(m);", "", "create one labeled node and a relationship" );
doc.add( "dump", "begin" + doc.add( "dump", "begin" +
lineSeparator() + "create index on :`Person`(`name`)" + lineSeparator() + "create index on :`Person`(`name`);" +
lineSeparator() + "commit" +
lineSeparator() + "begin" +
lineSeparator() + "create (_0:`Person`:`Hacker` {`name`:\"Mattias\"})" + lineSeparator() + "create (_0:`Person`:`Hacker` {`name`:\"Mattias\"})" +
lineSeparator() + "create (_0)-[:`KNOWS`]->(_0)" + lineSeparator() + "create (_0)-[:`KNOWS`]->(_0)" +
lineSeparator() + ";" + lineSeparator() + ";" +
Expand Down

0 comments on commit 91020d4

Please sign in to comment.