From 91020d4925da34a542e335b7885e870705ef1ad0 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Mon, 22 Feb 2016 14:42:07 +0100 Subject: [PATCH] Updated SubGraphExporter to output indexes, constraints and queries with trailing semicolon and option to pass in begin-commit commands --- .../neo4j/cypher/export/SubGraphExporter.java | 40 ++++++++++++--- .../org/neo4j/cypher/export/ExportTest.java | 50 +++++++++---------- .../shell/kernel/apps/cypher/Exporter.java | 15 +----- .../java/org/neo4j/shell/ShellDocTest.java | 4 +- 4 files changed, 63 insertions(+), 46 deletions(-) diff --git a/community/cypher/cypher/src/main/java/org/neo4j/cypher/export/SubGraphExporter.java b/community/cypher/cypher/src/main/java/org/neo4j/cypher/export/SubGraphExporter.java index 30319b1dee4a..5967f0db1f9a 100644 --- a/community/cypher/cypher/src/main/java/org/neo4j/cypher/export/SubGraphExporter.java +++ b/community/cypher/cypher/src/main/java/org/neo4j/cypher/export/SubGraphExporter.java @@ -49,12 +49,32 @@ public SubGraphExporter( SubGraph graph ) public void export( PrintWriter out ) { + export(out, null, null); + } + + public void export( PrintWriter out, String begin, String commit ) + { + output( out, begin ); appendIndexes( out ); appendConstraints( out ); - appendNodes( out ); - appendRelationships( out ); + output( out, commit, begin ); + 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 exportIndexes() { final List result = new ArrayList<>(); @@ -140,7 +160,8 @@ private void appendIndexes( PrintWriter out ) { for ( String line : exportIndexes() ) { - out.println( line ); + out.print( line ); + out.println( ";" ); } } @@ -148,19 +169,23 @@ private void appendConstraints( PrintWriter out ) { 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 ( Relationship rel : node.getRelationships( Direction.OUTGOING ) ) { appendRelationship( out, rel ); + relationships++; } } + return relationships; } private void appendRelationship( PrintWriter out, Relationship rel ) @@ -175,12 +200,15 @@ private void appendRelationship( PrintWriter out, Relationship rel ) out.println( ")" ); } - private void appendNodes( PrintWriter out ) + private long appendNodes( PrintWriter out ) { + long nodes = 0; for ( Node node : graph.getNodes() ) { appendNode( out, node ); + nodes++; } + return nodes; } private void appendNode( PrintWriter out, Node node ) diff --git a/community/cypher/cypher/src/test/java/org/neo4j/cypher/export/ExportTest.java b/community/cypher/cypher/src/test/java/org/neo4j/cypher/export/ExportTest.java index 46306464706b..15136716e567 100644 --- a/community/cypher/cypher/src/test/java/org/neo4j/cypher/export/ExportTest.java +++ b/community/cypher/cypher/src/test/java/org/neo4j/cypher/export/ExportTest.java @@ -78,7 +78,7 @@ public void testEmptyGraph() throws Exception public void testNodeWithProperties() throws Exception { gdb.createNode().setProperty( "name", "Andres" ); - assertEquals( "create (_0 {`name`:\"Andres\"})" + lineSeparator(), doExportGraph( gdb ) ); + assertEquals( "create (_0 {`name`:\"Andres\"})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( gdb ) ); } @Test @@ -87,7 +87,7 @@ public void testNodeWithFloatProperty() throws Exception final float floatValue = 10.1f; final String expected = "10.100000"; gdb.createNode().setProperty( "float", floatValue ); - assertEquals( "create (_0 {`float`:" + expected + "})" + lineSeparator(), doExportGraph( gdb ) ); + assertEquals( "create (_0 {`float`:" + expected + "})" + lineSeparator() + ";" + lineSeparator(), doExportGraph( gdb ) ); } @Test @@ -96,7 +96,7 @@ public void testNodeWithDoubleProperty() throws Exception final double doubleValue = 123456.123456; final String expected = "123456.123456"; 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 ) @@ -118,7 +118,7 @@ public void testFromSimpleCypherResult() throws Exception Node n = gdb.createNode(); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); - assertEquals( "create (_" + n.getId() + ")" + lineSeparator(), doExportGraph( graph ) ); + assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } @Test @@ -127,7 +127,7 @@ public void testSingleNode() throws Exception Node n = gdb.createNode(); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); - assertEquals( "create (_" + n.getId() + ")" + lineSeparator(), doExportGraph( graph ) ); + assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } @Test @@ -138,7 +138,7 @@ public void testSingleNodeWithProperties() throws Exception n.setProperty( "age", 42 ); final ExecutionResult result = result( "node", n ); 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 @@ -148,7 +148,7 @@ public void testEscapingOfNodeStringPropertyValue() throws Exception n.setProperty( "name", "Brutus \"Brutal\" Howell" ); final ExecutionResult result = result( "node", n ); 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 ) ); } @@ -159,7 +159,7 @@ public void testEscapingOfNodeStringArrayPropertyValue() throws Exception n.setProperty( "name", new String[]{"Brutus \"Brutal\" Howell", "Dr."} ); final ExecutionResult result = result( "node", n ); 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 ) ); } @@ -172,7 +172,7 @@ public void testEscapingOfRelationshipStringPropertyValue() throws Exception final ExecutionResult result = result( "rel", rel ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); 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 @@ -184,7 +184,7 @@ public void testEscapingOfRelationshipStringArrayPropertyValue() throws Exceptio final ExecutionResult result = result( "rel", rel ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); 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 ) ); } @@ -195,7 +195,7 @@ public void testEscapingStringPropertyWithBackslash() throws Exception n.setProperty( "name", "Some\\thing" ); final ExecutionResult result = result( "node", n ); 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 ) ); } @@ -206,7 +206,7 @@ public void testEscapingStringPropertyWithBackslashAndDoubleQuote() throws Excep n.setProperty( "name", "Some\\\"thing" ); final ExecutionResult result = result( "node", n ); 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 ) ); } @@ -218,7 +218,7 @@ public void testSingleNodeWithArrayProperties() throws Exception n.setProperty( "age", new int[]{1, 2} ); final ExecutionResult result = result( "node", n ); 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 @@ -229,21 +229,21 @@ public void testSingleNodeLabels() throws Exception n.addLabel( Label.label( "Bar" ) ); final ExecutionResult result = result( "node", n ); 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 public void testExportIndex() throws Exception { 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 public void testExportUniquenessConstraint() throws Exception { 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 @@ -256,9 +256,9 @@ public void testExportIndexesViaCypherResult() throws Exception Node n = gdb.createNode( label ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); - assertEquals( "create index on :`Foo`(`bar2`)" + lineSeparator() + - "create index on :`Foo`(`bar`)" + lineSeparator() + - "create (_0:`Foo`)" + lineSeparator(), doExportGraph( graph ) ); + assertEquals( "create index on :`Foo`(`bar2`);" + lineSeparator() + + "create index on :`Foo`(`bar`);" + lineSeparator() + + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } @Test @@ -271,9 +271,9 @@ public void testExportConstraintsViaCypherResult() throws Exception Node n = gdb.createNode( label ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); - assertEquals( "create constraint on (n:`Foo`) assert n.`bar2` is unique" + lineSeparator() + - "create constraint on (n:`Foo`) assert n.`bar` is unique" + lineSeparator() + - "create (_0:`Foo`)" + lineSeparator(), doExportGraph( graph ) ); + assertEquals( "create constraint on (n:`Foo`) assert n.`bar2` is unique;" + lineSeparator() + + "create constraint on (n:`Foo`) assert n.`bar` is unique;" + lineSeparator() + + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } private void commitAndStartNewTransactionAfterSchemaChanges() @@ -291,7 +291,7 @@ public void testFromRelCypherResult() throws Exception final ExecutionResult result = result( "rel", rel ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create (_0)" + lineSeparator() + - "create (_0)-[:`REL`]->(_0)" + lineSeparator(), doExportGraph( graph ) ); + "create (_0)-[:`REL`]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } @Test @@ -305,7 +305,7 @@ public void testFromPathCypherResult() throws Exception final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create (_0)" + lineSeparator() + "create (_1)" + lineSeparator() + - "create (_0)-[:`REL`]->(_1)" + lineSeparator(), doExportGraph( graph ) ); + "create (_0)-[:`REL`]->(_1)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } @SuppressWarnings("unchecked") @@ -374,6 +374,6 @@ public void testFromSimpleGraph() throws Exception assertEquals( "create (_" + n0.getId() + ")" + lineSeparator() + "create (_" + n1.getId() + " {`name`:\"Node1\"})" + lineSeparator() + "create (_" + n0.getId() + ")-[:`REL` {`related`:true}]->(_" + n1.getId() + ")" + - lineSeparator(), doExportGraph( graph ) ); + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } } diff --git a/community/shell/src/main/java/org/neo4j/shell/kernel/apps/cypher/Exporter.java b/community/shell/src/main/java/org/neo4j/shell/kernel/apps/cypher/Exporter.java index 80614a87d4f7..20853daa4cc5 100644 --- a/community/shell/src/main/java/org/neo4j/shell/kernel/apps/cypher/Exporter.java +++ b/community/shell/src/main/java/org/neo4j/shell/kernel/apps/cypher/Exporter.java @@ -39,23 +39,10 @@ public class Exporter public void export( Output out ) throws RemoteException, ShellException { - begin( out ); - exporter.export(asWriter(out)); - out.println(";"); - commit(out); + exporter.export( asWriter(out), "begin", "commit" ); } private PrintWriter asWriter(Output 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" ); - } } diff --git a/community/shell/src/test/java/org/neo4j/shell/ShellDocTest.java b/community/shell/src/test/java/org/neo4j/shell/ShellDocTest.java index 38ccc92da063..0a0e3bed65d8 100644 --- a/community/shell/src/test/java/org/neo4j/shell/ShellDocTest.java +++ b/community/shell/src/test/java/org/neo4j/shell/ShellDocTest.java @@ -184,7 +184,9 @@ public void testDumpDatabase() throws Exception 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( "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)-[:`KNOWS`]->(_0)" + lineSeparator() + ";" +