Search in sources :

Example 1 with VirtualRelationship

use of apoc.result.VirtualRelationship in project neo4j-apoc-procedures by neo4j-contrib.

the class Bolt method toRelationship.

private Object toRelationship(Object value, boolean virtual, Map<Long, Object> nodesCache) {
    Value internalValue = ((InternalEntity) value).asValue();
    Relationship relationship = internalValue.asRelationship();
    if (virtual) {
        VirtualNode start = (VirtualNode) nodesCache.getOrDefault(relationship.startNodeId(), new VirtualNode(relationship.startNodeId(), db));
        VirtualNode end = (VirtualNode) nodesCache.getOrDefault(relationship.endNodeId(), new VirtualNode(relationship.endNodeId(), db));
        VirtualRelationship virtualRelationship = new VirtualRelationship(relationship.id(), start, end, RelationshipType.withName(relationship.type()), relationship.asMap());
        return virtualRelationship;
    } else
        return Util.map("entityType", internalValue.type().name(), "type", relationship.type(), "id", relationship.id(), "start", relationship.startNodeId(), "end", relationship.endNodeId(), "properties", relationship.asMap());
}
Also used : VirtualRelationship(apoc.result.VirtualRelationship) InternalEntity(org.neo4j.driver.internal.InternalEntity) VirtualNode(apoc.result.VirtualNode) VirtualRelationship(apoc.result.VirtualRelationship) Relationship(org.neo4j.driver.v1.types.Relationship)

Example 2 with VirtualRelationship

use of apoc.result.VirtualRelationship in project neo4j-apoc-procedures by neo4j-contrib.

the class Cluster method graph.

@Procedure
@Description("apoc.cluster.graph - visually displays the servers participating in the Causal Cluster, their " + "roles, and which server in the cluster you are connected to.")
public Stream<GraphResult> graph() {
    Result execute = db.execute("CALL dbms.cluster.overview()");
    List<Node> servers = new LinkedList<>();
    List<Relationship> relationships = new LinkedList<>();
    while (execute.hasNext()) {
        Map<String, Object> next = execute.next();
        String role = (String) next.get("role");
        String id = (String) next.get("id");
        Label roleLabel = Label.label(role);
        String[] addresses = ((List<String>) next.get("addresses")).toArray(new String[0]);
        Map<String, Object> properties = new HashMap<>();
        properties.put("name", shortName.get(role));
        properties.put("title", role);
        properties.put(boltAddressKey, addresses[0]);
        properties.put("http_address", addresses[1]);
        properties.put("cluster_id", id);
        Node server = new VirtualNode(new Label[] { roleLabel }, properties, db);
        servers.add(server);
    }
    Optional<Node> leaderNode = getLeaderNode(servers);
    if (leaderNode.isPresent()) {
        for (Node server : servers) {
            if (server.hasLabel(Label.label("FOLLOWER"))) {
                VirtualRelationship follows = new VirtualRelationship(server, leaderNode.get(), RelationshipType.withName("FOLLOWS"));
                relationships.add(follows);
            }
        }
    }
    VirtualNode client = new VirtualNode(new Label[] { Label.label("CLIENT") }, singletonMap("name", "Client"), db);
    Optional<Relationship> clientConnection = determineClientConnection(servers, client);
    if (clientConnection.isPresent()) {
        servers.add(client);
        relationships.add(clientConnection.get());
    }
    GraphResult graphResult = new GraphResult(servers, relationships);
    return Stream.of(graphResult);
}
Also used : HashMap(java.util.HashMap) GraphResult(apoc.result.GraphResult) VirtualNode(apoc.result.VirtualNode) VirtualNode(apoc.result.VirtualNode) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) LinkedList(java.util.LinkedList) GraphResult(apoc.result.GraphResult) Result(org.neo4j.graphdb.Result) VirtualRelationship(apoc.result.VirtualRelationship) VirtualRelationship(apoc.result.VirtualRelationship) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList) List(java.util.List) Description(apoc.Description) Procedure(org.neo4j.procedure.Procedure)

Example 3 with VirtualRelationship

use of apoc.result.VirtualRelationship in project neo4j-apoc-procedures by neo4j-contrib.

the class Meta method metaGraph.

private Stream<GraphResult> metaGraph(Collection<String> labelNames, Collection<String> relTypeNames, boolean removeMissing) {
    try (Statement stmt = kernelTx.acquireStatement()) {
        ReadOperations ops = stmt.readOperations();
        Map<String, Integer> labels = labelsInUse(ops, labelNames);
        Map<String, Integer> relTypes = relTypesInUse(ops, relTypeNames);
        Map<String, Node> vNodes = new TreeMap<>();
        Map<Pattern, Relationship> vRels = new HashMap<>(relTypes.size() * 2);
        labels.forEach((labelName, id) -> {
            long count = ops.countsForNodeWithoutTxState(id);
            if (count > 0) {
                mergeMetaNode(Label.label(labelName), vNodes, count);
            }
        });
        relTypes.forEach((typeName, typeId) -> {
            long global = ops.countsForRelationshipWithoutTxState(ANY_LABEL, typeId, ANY_LABEL);
            labels.forEach((labelNameA, labelIdA) -> {
                long relCountOut = ops.countsForRelationshipWithoutTxState(labelIdA, typeId, ANY_LABEL);
                if (relCountOut > 0) {
                    labels.forEach((labelNameB, labelIdB) -> {
                        long relCountIn = ops.countsForRelationshipWithoutTxState(ANY_LABEL, typeId, labelIdB);
                        if (relCountIn > 0) {
                            Node nodeA = vNodes.get(labelNameA);
                            Node nodeB = vNodes.get(labelNameB);
                            Relationship vRel = new VirtualRelationship(nodeA, nodeB, RelationshipType.withName(typeName)).withProperties(map("type", typeName, "out", relCountOut, "in", relCountIn, "count", global));
                            vRels.put(Pattern.of(labelNameA, typeName, labelNameB), vRel);
                        }
                    });
                }
            });
        });
        if (removeMissing)
            filterNonExistingRelationships(vRels);
        GraphResult graphResult = new GraphResult(new ArrayList<>(vNodes.values()), new ArrayList<>(vRels.values()));
        return Stream.of(graphResult);
    }
}
Also used : GraphResult(apoc.result.GraphResult) Statement(org.neo4j.kernel.api.Statement) VirtualNode(apoc.result.VirtualNode) ReadOperations(org.neo4j.kernel.api.ReadOperations) VirtualRelationship(apoc.result.VirtualRelationship) VirtualRelationship(apoc.result.VirtualRelationship)

Example 4 with VirtualRelationship

use of apoc.result.VirtualRelationship in project neo4j-apoc-procedures by neo4j-contrib.

the class Meta method addRel.

private void addRel(Map<List<String>, Relationship> rels, Map<String, Node> labels, Relationship rel, boolean strict) {
    String typeName = rel.getType().name();
    Node startNode = rel.getStartNode();
    Node endNode = rel.getEndNode();
    for (Label labelA : startNode.getLabels()) {
        Node nodeA = strict ? labels.get(labelA.name()) : mergeMetaNode(labelA, labels, 0);
        if (nodeA == null)
            continue;
        for (Label labelB : endNode.getLabels()) {
            List<String> key = asList(labelA.name(), labelB.name(), typeName);
            Relationship vRel = rels.get(key);
            if (vRel == null) {
                Node nodeB = strict ? labels.get(labelB.name()) : mergeMetaNode(labelB, labels, 0);
                if (nodeB == null)
                    continue;
                vRel = new VirtualRelationship(nodeA, nodeB, rel.getType()).withProperties(singletonMap("type", typeName));
                rels.put(key, vRel);
            }
            vRel.setProperty("count", ((long) vRel.getProperty("count", 0L)) + 1);
        }
    }
}
Also used : VirtualRelationship(apoc.result.VirtualRelationship) VirtualNode(apoc.result.VirtualNode) VirtualRelationship(apoc.result.VirtualRelationship)

Aggregations

VirtualNode (apoc.result.VirtualNode)4 VirtualRelationship (apoc.result.VirtualRelationship)4 GraphResult (apoc.result.GraphResult)2 Description (apoc.Description)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 InternalEntity (org.neo4j.driver.internal.InternalEntity)1 Relationship (org.neo4j.driver.v1.types.Relationship)1 Label (org.neo4j.graphdb.Label)1 Node (org.neo4j.graphdb.Node)1 Relationship (org.neo4j.graphdb.Relationship)1 Result (org.neo4j.graphdb.Result)1 ReadOperations (org.neo4j.kernel.api.ReadOperations)1 Statement (org.neo4j.kernel.api.Statement)1 Procedure (org.neo4j.procedure.Procedure)1