use of org.neo4j.ogm.model.Edge in project neo4j-ogm by neo4j.
the class MultiStatementCypherCompiler method createRelationshipsStatements.
@Override
public List<Statement> createRelationshipsStatements() {
assertStatementFactoryExists();
// Group relationships by type and non-null properties
// key: relationship type, value: Map where key=Set<Property strings>, value: Set of edges with those properties
Map<String, Map<String, Set<Edge>>> relsByTypeAndProps = new HashMap<>();
for (RelationshipBuilder relationshipBuilder : newRelationshipBuilders) {
if (relationshipBuilder.edge().getStartNode() == null || relationshipBuilder.edge().getEndNode() == null) {
// TODO this is a carry forward from the old emitters. We want to prevent this rel builder getting created or remove it
continue;
}
Map<String, Set<Edge>> relsByProps = relsByTypeAndProps.computeIfAbsent(relationshipBuilder.type(), (key) -> new HashMap<>());
RelationshipModel edge = (RelationshipModel) relationshipBuilder.edge();
String primaryId = edge.getPrimaryIdName();
Set<Edge> rels = relsByProps.computeIfAbsent(primaryId, (s) -> new HashSet<>());
edge.setStartNode(context.getId(edge.getStartNode()));
edge.setEndNode(context.getId(edge.getEndNode()));
rels.add(edge);
}
List<Statement> statements = new ArrayList<>();
// For each relationship type
for (Map<String, Set<Edge>> edgesByProperties : relsByTypeAndProps.values()) {
// For each set of unique property keys
for (Set<Edge> edges : edgesByProperties.values()) {
NewRelationshipStatementBuilder newRelationshipBuilder = new NewRelationshipStatementBuilder(edges, statementFactory);
statements.add(newRelationshipBuilder.build());
}
}
return statements;
}
use of org.neo4j.ogm.model.Edge in project neo4j-ogm by neo4j.
the class MultiStatementCypherCompiler method deleteRelationshipStatements.
@Override
public List<Statement> deleteRelationshipStatements() {
assertStatementFactoryExists();
// Group relationships by type
Map<String, Set<Edge>> deletedRelsByType = groupRelationshipsByType(deletedRelationshipBuilders);
List<Statement> statements = new ArrayList<>();
for (Set<Edge> edges : deletedRelsByType.values()) {
DeletedRelationshipStatementBuilder deletedRelationshipBuilder = new DeletedRelationshipStatementBuilder(edges, statementFactory);
statements.add(deletedRelationshipBuilder.build());
}
return statements;
}
use of org.neo4j.ogm.model.Edge in project neo4j-ogm by neo4j.
the class DeletedRelationshipStatementBuilder method build.
@Override
public Statement build() {
final Map<String, Object> parameters = new HashMap<>();
final StringBuilder queryBuilder = new StringBuilder();
if (deletedEdges != null && deletedEdges.size() > 0) {
Edge firstEdge = deletedEdges.iterator().next();
queryBuilder.append("UNWIND $rows as row ").append("MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode ").append("MATCH (endNode) WHERE ID(endNode) = row.endNodeId ").append("MATCH (startNode)-[rel:`").append(firstEdge.getType()).append("`]->(endNode) ").append("DELETE rel");
List<Map> rows = new ArrayList<>();
for (Edge edge : deletedEdges) {
Map<String, Object> rowMap = new HashMap<>();
rowMap.put("startNodeId", edge.getStartNode());
rowMap.put("endNodeId", edge.getEndNode());
rows.add(rowMap);
}
parameters.put("rows", rows);
}
return statementFactory.statement(queryBuilder.toString(), parameters);
}
use of org.neo4j.ogm.model.Edge in project neo4j-ogm by neo4j.
the class ExistingRelationshipStatementBuilder method build.
@Override
public Statement build() {
final Map<String, Object> parameters = new HashMap<>();
final StringBuilder queryBuilder = new StringBuilder();
Edge firstEdge = edges.iterator().next();
if (edges.size() > 0) {
queryBuilder.append("UNWIND $rows AS row MATCH ()-[r]->() WHERE ID(r) = row.relId ");
if (firstEdge.hasVersionProperty()) {
queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstEdge, "r", dirtyEdges ? 1 : 0));
}
queryBuilder.append(firstEdge.createPropertyRemovalFragment("r"));
queryBuilder.append("SET r += row.props ");
queryBuilder.append("RETURN ID(r) as ref, ID(r) as id, $type as type");
List<Map> rows = new ArrayList<>();
for (Edge edge : edges) {
Map<String, Object> rowMap = new HashMap<>();
rowMap.put("relId", edge.getId());
Map<String, Object> props = new HashMap<>();
for (Property property : edge.getPropertyList()) {
if (!property.equals(edge.getVersion())) {
props.put((String) property.getKey(), property.getValue());
}
}
rowMap.put("props", props);
if (edge.hasVersionProperty()) {
Property version = edge.getVersion();
rowMap.put((String) version.getKey(), version.getValue());
}
rows.add(rowMap);
}
parameters.put("rows", rows);
parameters.put("type", "rel");
if (firstEdge.hasVersionProperty()) {
OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), new String[] { firstEdge.getType() }, firstEdge.getVersion().getKey());
return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
}
}
return statementFactory.statement(queryBuilder.toString(), parameters);
}
Aggregations