Search in sources :

Example 1 with RefactorConfig

use of apoc.refactor.util.RefactorConfig in project neo4j-apoc-procedures by neo4j-contrib.

the class GraphRefactoring method mergeNodes.

/**
 * Merges the nodes onto the first node.
 * The other nodes are deleted and their relationships moved onto that first node.
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.refactor.mergeNodes([node1,node2],[{properties:'override' or 'discard' or 'combine'}]) merge nodes onto first in list")
public Stream<NodeResult> mergeNodes(@Name("nodes") List<Node> nodes, @Name(value = "config", defaultValue = "") Map<String, Object> config) {
    if (nodes == null || nodes.isEmpty())
        return Stream.empty();
    // grab write locks upfront consistently ordered
    try (Transaction tx = db.beginTx()) {
        nodes.stream().distinct().sorted(Comparator.comparingLong(Node::getId)).forEach(tx::acquireWriteLock);
        tx.success();
    }
    RefactorConfig conf = new RefactorConfig(config);
    final Node first = nodes.get(0);
    nodes.stream().skip(1).distinct().forEach(node -> mergeNodes(node, first, true, conf));
    return Stream.of(new NodeResult(first));
}
Also used : NodeResult(apoc.result.NodeResult) RefactorConfig(apoc.refactor.util.RefactorConfig)

Example 2 with RefactorConfig

use of apoc.refactor.util.RefactorConfig in project neo4j-apoc-procedures by neo4j-contrib.

the class GraphRefactoring method mergeRelationships.

/**
 * Merges the relationships onto the first relationship and delete them.
 * All relationships must have the same starting node and ending node.
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.refactor.mergeRelationships([rel1,rel2]) merge relationships onto first in list")
public Stream<RelationshipResult> mergeRelationships(@Name("rels") List<Relationship> relationships, @Name(value = "config", defaultValue = "") Map<String, Object> config) {
    if (relationships == null || relationships.isEmpty())
        return Stream.empty();
    RefactorConfig conf = new RefactorConfig(config);
    Iterator<Relationship> it = relationships.iterator();
    Relationship first = it.next();
    while (it.hasNext()) {
        Relationship other = it.next();
        if (first.getStartNode().equals(other.getStartNode()) && first.getEndNode().equals(other.getEndNode()))
            mergeRels(other, first, true, conf);
        else
            throw new RuntimeException("All Relationships must have the same start and end nodes.");
    }
    return Stream.of(new RelationshipResult(first));
}
Also used : RelationshipResult(apoc.result.RelationshipResult) RefactorConfig(apoc.refactor.util.RefactorConfig)

Aggregations

RefactorConfig (apoc.refactor.util.RefactorConfig)2 NodeResult (apoc.result.NodeResult)1 RelationshipResult (apoc.result.RelationshipResult)1