Search in sources :

Example 6 with Edge

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;
}
Also used : Statement(org.neo4j.ogm.request.Statement) RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel) NewRelationshipStatementBuilder(org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder) Edge(org.neo4j.ogm.model.Edge) DefaultRelationshipBuilder(org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)

Example 7 with Edge

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;
}
Also used : Statement(org.neo4j.ogm.request.Statement) Edge(org.neo4j.ogm.model.Edge) DeletedRelationshipStatementBuilder(org.neo4j.ogm.cypher.compiler.builders.statement.DeletedRelationshipStatementBuilder)

Example 8 with Edge

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Edge(org.neo4j.ogm.model.Edge) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with Edge

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) Edge(org.neo4j.ogm.model.Edge) Map(java.util.Map) HashMap(java.util.HashMap) Property(org.neo4j.ogm.model.Property)

Aggregations

Edge (org.neo4j.ogm.model.Edge)9 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Property (org.neo4j.ogm.model.Property)3 Statement (org.neo4j.ogm.request.Statement)3 OptimisticLockingConfig (org.neo4j.ogm.request.OptimisticLockingConfig)2 DefaultRelationshipBuilder (org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)1 DeletedRelationshipEntityStatementBuilder (org.neo4j.ogm.cypher.compiler.builders.statement.DeletedRelationshipEntityStatementBuilder)1 DeletedRelationshipStatementBuilder (org.neo4j.ogm.cypher.compiler.builders.statement.DeletedRelationshipStatementBuilder)1 NewRelationshipStatementBuilder (org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder)1 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)1 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)1 RelationshipModel (org.neo4j.ogm.response.model.RelationshipModel)1