Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[eqFmcQxi] Fix bug with apoc.export.cypher.* where temp. rel properties were not cleaned up. #632

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static apoc.export.cypher.formatter.CypherFormatterUtils.Q_UNIQUE_ID_LABEL;
import static apoc.export.cypher.formatter.CypherFormatterUtils.Q_UNIQUE_ID_REL;
import static apoc.export.cypher.formatter.CypherFormatterUtils.UNIQUE_ID_PROP;
import static apoc.export.cypher.formatter.CypherFormatterUtils.isUniqueRelationship;
import static apoc.export.cypher.formatter.CypherFormatterUtils.simpleKeyValue;

import apoc.export.util.ExportConfig;
Expand All @@ -34,7 +35,6 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.StringUtils;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
Expand Down Expand Up @@ -62,12 +62,19 @@ abstract class AbstractCypherFormatter implements CypherFormatter {
private static final String ID_REL_KEY = "id";

@Override
public String statementForCleanUp(int batchSize) {
public String statementForCleanUpNodes(int batchSize) {
return "MATCH (n:" + Q_UNIQUE_ID_LABEL + ") " + " WITH n LIMIT "
+ batchSize + " REMOVE n:"
+ Q_UNIQUE_ID_LABEL + " REMOVE n." + Util.quote(UNIQUE_ID_PROP) + ";";
}

@Override
public String statementForCleanUpRelationships(int batchSize) {
return "MATCH ()-[r]->() WHERE r." + Q_UNIQUE_ID_REL + " IS NOT NULL"
+ " WITH r LIMIT " + batchSize
+ " REMOVE r." + Q_UNIQUE_ID_REL + ";";
}

@Override
public String statementForNodeIndex(
String indexType, String label, Iterable<String> keys, boolean ifNotExists, String idxName) {
Expand Down Expand Up @@ -209,14 +216,8 @@ public String mergeStatementForRelationship(
final Node endNode = relationship.getEndNode();
result.append(CypherFormatterUtils.formatNodeLookup("n2", endNode, uniqueConstraints, indexedProperties));
final RelationshipType type = relationship.getType();
final boolean withMultiRels = exportConfig.isMultipleRelationshipsWithType()
&& StreamSupport.stream(
startNode
.getRelationships(Direction.OUTGOING, type)
.spliterator(),
false)
.anyMatch(r -> !r.equals(relationship) && r.getEndNode().equals(endNode));

final boolean withMultiRels =
exportConfig.isMultipleRelationshipsWithType() && !isUniqueRelationship(relationship);
String mergeUniqueKey = withMultiRels ? simpleKeyValue(Q_UNIQUE_ID_REL, relationship.getId()) : "";
result.append(" MERGE (n1)-[r:" + Util.quote(type.name()) + mergeUniqueKey + "]->(n2)");
if (relationship.getPropertyKeys().iterator().hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String statementForRelationship(
}

@Override
public String statementForCleanUp(int batchSize) {
public String statementForCleanUpNodes(int batchSize) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ String statementForCreateConstraint(

String statementForDropConstraint(String name);

String statementForCleanUp(int batchSize);
String statementForCleanUpNodes(int batchSize);

String statementForCleanUpRelationships(int batchSize);

void statementForNodes(
Iterable<Node> node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.time.temporal.Temporal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
Expand Down Expand Up @@ -157,6 +159,18 @@ public static boolean isUniqueLabelFound(Node node, Map<String, Set<String>> uni
}
}

/*
* Returns true if there exists no other relationship with the same start node, end node and type
*/
public static boolean isUniqueRelationship(Relationship rel) {
return StreamSupport.stream(
rel.getStartNode()
.getRelationships(Direction.OUTGOING, rel.getType())
.spliterator(),
false)
.noneMatch(r -> !r.equals(rel) && r.getEndNode().equals(rel.getEndNode()));
}
Comment on lines +162 to +172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really do what it says it does?

I'm thinking about this situation:

create (a), (b)
with a,b 
create (a)-[:R]->(b)
with a,b
create (a)-[:R]->(b)

Relationship.equals looks like it's implemented based on id:

@Override
public boolean equals(Object o) {
    return o instanceof Relationship && this.getId() == ((Relationship) o).getId();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, never mind, now I see it.


// ---- properties ----

public static String formatNodeProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String statementForRelationship(
}

@Override
public String statementForCleanUp(int batchSize) {
public String statementForCleanUpNodes(int batchSize) {
return "";
}

Expand Down
Loading
Loading