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);
}
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);
}
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());
}
}
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);
}
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);
}
Aggregations