Search in sources :

Example 1 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class DeletedRelationshipEntityStatementBuilder 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 MATCH ()-[r]->() WHERE ID(r) = row.relId ");
        if (firstEdge.hasVersionProperty()) {
            queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstEdge, "r"));
        }
        queryBuilder.append("DELETE r RETURN ID(r) as ref, ID(r) as id, $type as type");
        List<Map> rows = new ArrayList<>();
        for (Edge edge : deletedEdges) {
            Map<String, Object> rowMap = new HashMap<>();
            rowMap.put("relId", edge.getId());
            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)

Example 2 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class RelationshipDeleteStatements method delete.

@Override
public CypherQuery delete(Long id, Object object, ClassInfo classInfo) {
    FieldInfo versionField = classInfo.getVersionField();
    Long version = (Long) versionField.read(object);
    OptimisticLockingConfig optimisticLockingConfig = new OptimisticLockingConfig(1, classInfo.staticLabels().toArray(new String[] {}), versionField.property());
    Map<String, Object> params = new HashMap<>();
    params.put("id", id);
    params.put("version", version);
    params.put("type", "rel");
    return new DefaultRowModelRequest("MATCH (n)-[r0]->() " + "  WHERE ID(r0) = $id AND r0.`" + versionField.property() + "` = $version " + "SET " + " r0.`" + versionField.property() + "` = r0.`" + versionField.property() + "` + 1 " + "WITH r0 " + " WHERE r0.`" + versionField.property() + "` = $version + 1 " + "DELETE r0 " + // Use DISTINCT because node may have multiple relationships
    "RETURN DISTINCT ID(r0) AS id", params, optimisticLockingConfig);
}
Also used : HashMap(java.util.HashMap) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 3 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class OptimisticLockingChecker method checkResultsCount.

public void checkResultsCount(List<RowModel> models, Statement request) {
    OptimisticLockingConfig olConfig = request.optimisticLockingConfig().orElseThrow(() -> new IllegalArgumentException("Statement " + request + " doesn't require results count check"));
    StringBuilder message = new StringBuilder();
    if (olConfig.getExpectedResultsCount() != models.size()) {
        message.append("Optimistic locking exception failed. ");
        Map<String, Object> parameters = request.getParameters();
        Object type = parameters.get("type");
        List<Map<String, Object>> rows = (List<Map<String, Object>>) parameters.get("rows");
        if (rows != null) {
            Map<Long, Long> nodeIds = new HashMap<>();
            Map<Long, Long> relIds = new HashMap<>();
            for (Map<String, Object> row : rows) {
                if (type.equals("node")) {
                    nodeIds.put((Long) row.get("nodeId"), (Long) row.get(olConfig.getVersionProperty()));
                } else if (type.equals("rel")) {
                    relIds.put((Long) row.get("relId"), (Long) row.get(olConfig.getVersionProperty()));
                }
            }
            if (!models.isEmpty()) {
                int idPosition = indexOf(models.get(0).variables(), "id");
                for (RowModel model : models) {
                    Object id = model.getValues()[idPosition];
                    if (type.equals("node")) {
                        nodeIds.remove(id);
                    } else if (type.equals("rel")) {
                        relIds.remove(id);
                    }
                }
            }
            for (Map.Entry<Long, Long> node : nodeIds.entrySet()) {
                Long nodeId = node.getKey();
                session.context().detachNodeEntity(nodeId);
                message.append("Entity with type='").append(Arrays.toString(olConfig.getTypes())).append("' and id='").append(nodeId).append("' had incorrect version ").append(node.getValue());
            }
            for (Map.Entry<Long, Long> rel : relIds.entrySet()) {
                Long relId = rel.getKey();
                session.context().detachRelationshipEntity(relId);
                message.append("Relationship entity with id ").append(relId).append(" had incorrect version ").append(rel.getValue());
            }
        } else {
            Object id = parameters.get("id");
            if (id != null && models.isEmpty()) {
                if (type.equals("node")) {
                    session.context().detachNodeEntity((Long) id);
                } else if (type.equals("rel")) {
                    session.context().detachRelationshipEntity((Long) id);
                }
            }
        }
        throw new OptimisticLockingException(message.toString());
    }
}
Also used : HashMap(java.util.HashMap) OptimisticLockingException(org.neo4j.ogm.exception.OptimisticLockingException) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) List(java.util.List) RowModel(org.neo4j.ogm.model.RowModel) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class NodeDeleteStatements method delete.

@Override
public CypherQuery delete(Long id, Object object, ClassInfo classInfo) {
    FieldInfo versionField = classInfo.getVersionField();
    Long version = (Long) versionField.read(object);
    OptimisticLockingConfig optimisticLockingConfig = new OptimisticLockingConfig(1, classInfo.staticLabels().toArray(new String[] {}), versionField.property());
    Map<String, Object> params = new HashMap<>();
    params.put("id", id);
    params.put("version", version);
    params.put("type", "node");
    return new DefaultRowModelRequest("MATCH (n) " + "  WHERE id(n) = $id AND n.`" + versionField.property() + "` = $version " + "SET " + " n.`" + versionField.property() + "` = n.`" + versionField.property() + "` + 1 " + "WITH n " + " WHERE n.`" + versionField.property() + "` = $version + 1 " + "OPTIONAL MATCH (n)-[r0]-() " + "DELETE r0, n " + // Use DISTINCT because node may have multiple relationships
    "RETURN DISTINCT id(n) AS id", params, optimisticLockingConfig);
}
Also used : HashMap(java.util.HashMap) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 5 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class ExistingNodeStatementBuilder method build.

@Override
public Statement build() {
    final Map<String, Object> parameters = new HashMap<>();
    final StringBuilder queryBuilder = new StringBuilder();
    if (existingNodes != null && existingNodes.size() > 0) {
        Node firstNode = existingNodes.iterator().next();
        queryBuilder.append("UNWIND $rows as row MATCH (n) WHERE ID(n)=row.nodeId ");
        if (firstNode.hasVersionProperty()) {
            queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstNode, "n"));
        }
        Set<String> previousDynamicLabels = firstNode.getPreviousDynamicLabels();
        for (String label : previousDynamicLabels) {
            queryBuilder.append(String.format(" REMOVE n:`%s` ", label));
        }
        queryBuilder.append(firstNode.createPropertyRemovalFragment("n"));
        queryBuilder.append("SET n");
        for (String label : firstNode.getLabels()) {
            queryBuilder.append(":`").append(label).append("`");
        }
        queryBuilder.append(" SET n += row.props RETURN row.nodeId as ref, ID(n) as id, $type as type");
        List<Map> rows = existingNodes.stream().map(node -> node.toRow("nodeId")).collect(toList());
        parameters.put("type", "node");
        parameters.put("rows", rows);
        if (firstNode.hasVersionProperty()) {
            OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), firstNode.getLabels(), firstNode.getVersion().getKey());
            return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
        }
    }
    return statementFactory.statement(queryBuilder.toString(), parameters);
}
Also used : List(java.util.List) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) CypherStatementBuilder(org.neo4j.ogm.cypher.compiler.CypherStatementBuilder) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Node(org.neo4j.ogm.model.Node) Statement(org.neo4j.ogm.request.Statement) StatementFactory(org.neo4j.ogm.request.StatementFactory) HashMap(java.util.HashMap) Node(org.neo4j.ogm.model.Node) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)7 OptimisticLockingConfig (org.neo4j.ogm.request.OptimisticLockingConfig)7 Map (java.util.Map)5 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 CypherStatementBuilder (org.neo4j.ogm.cypher.compiler.CypherStatementBuilder)2 DefaultRowModelRequest (org.neo4j.ogm.cypher.query.DefaultRowModelRequest)2 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)2 Edge (org.neo4j.ogm.model.Edge)2 Node (org.neo4j.ogm.model.Node)2 Property (org.neo4j.ogm.model.Property)2 Statement (org.neo4j.ogm.request.Statement)2 StatementFactory (org.neo4j.ogm.request.StatementFactory)2 Arrays (java.util.Arrays)1 OptimisticLockingUtils (org.neo4j.ogm.cypher.compiler.builders.statement.OptimisticLockingUtils)1 OptimisticLockingException (org.neo4j.ogm.exception.OptimisticLockingException)1 RowModel (org.neo4j.ogm.model.RowModel)1 NodeQueryStatements (org.neo4j.ogm.session.request.strategy.impl.NodeQueryStatements)1