Skip to content

Commit

Permalink
[Q8q7GXdk] apoc.export.graphml imports unwanted nodes (neo4j/apoc#380)
Browse files Browse the repository at this point in the history
* [Q8q7GXdk] apoc.export.graphml imports unwanted nodes

* [Q8q7GXdk] fix compile error

* [Q8q7GXdk] split tests
  • Loading branch information
vga91 committed May 10, 2023
1 parent 6f3dac5 commit a56e40e
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 58 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/export/cypher/ExportCypher.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Stream<DataProgressInfo> query(@Name("query") String query, @Name(value =
if (Util.isNullOrEmpty(fileName)) fileName=null;
ExportConfig c = new ExportConfig(config);
Result result = tx.execute(query);
SubGraph graph = CypherResultSubGraph.from(tx, result, c.getRelsInBetween());
SubGraph graph = CypherResultSubGraph.from(tx, result, c.getRelsInBetween(), false);

String source = String.format("statement: nodes(%d), rels(%d)",
Iterables.count(graph.getNodes()), Iterables.count(graph.getRelationships()));
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/export/graphml/ExportGraphML.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Stream<ProgressInfo> graph(@Name("graph") Map<String,Object> graph, @Name
public Stream<ProgressInfo> query(@Name("query") String query, @Name("file") String fileName, @Name("config") Map<String, Object> config) throws Exception {
ExportConfig c = new ExportConfig(config);
Result result = tx.execute(query);
SubGraph graph = CypherResultSubGraph.from(tx, result, c.getRelsInBetween());
SubGraph graph = CypherResultSubGraph.from(tx, result, c.getRelsInBetween(), false);
String source = String.format("statement: nodes(%d), rels(%d)",
Iterables.count(graph.getNodes()), Iterables.count(graph.getRelationships()));
return exportGraphML(fileName, source, graph, c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,35 @@ void addNode( long id, Node data )
labels.addAll( Iterables.asCollection( data.getLabels() ) );
}

public void add( Relationship rel )
public void add( Relationship rel, boolean addNodes )
{
final long id = rel.getId();
if ( !relationships.containsKey( id ) )
{
addRel( id, rel );
add( rel.getStartNode() );
add( rel.getEndNode() );
if (!relationships.containsKey(id)) {
addRel(id, rel);
// start and end nodes will be added only with the `apoc.meta.*` procedures,
// not with the `apoc.export.*.query` ones
if (addNodes) {
add(rel.getStartNode());
add(rel.getEndNode());
}
}
}

public static SubGraph from(Transaction tx, Result result, boolean addBetween)


public static SubGraph from(Transaction tx, Result result, boolean addBetween) {
return from(tx, result, addBetween, true);
}

public static SubGraph from(Transaction tx, Result result, boolean addBetween, boolean addRelNodes)
{
final CypherResultSubGraph graph = new CypherResultSubGraph();
final List<String> columns = result.columns();
try {
for (Map<String, Object> row : loop(result)) {
for (String column : columns) {
final Object value = row.get(column);
graph.addToGraph(value);
graph.addToGraph(value, addRelNodes);
}
}
} catch (AuthorizationViolationException e) {
Expand Down Expand Up @@ -160,21 +169,21 @@ private void addRelationshipsBetweenNodes()
}
}

private void addToGraph( Object value )
private void addToGraph( Object value, boolean addRelNodes )
{
if ( value instanceof Node )
{
add( (Node) value );
}
if ( value instanceof Relationship )
{
add( (Relationship) value );
add( (Relationship) value, addRelNodes );
}
if ( value instanceof Iterable )
{
for ( Object inner : (Iterable) value )
{
addToGraph( inner );
addToGraph( inner, addRelNodes );
}
}
}
Expand Down
Loading

0 comments on commit a56e40e

Please sign in to comment.