限制基于游标的节点遍历只能到出边。

huangapple go评论64阅读模式
英文:

Limit cursor-based node traversal to outgoing edges

问题

我正在尝试将Max de Marzi的Parallel K-Hop Counts示例适应到一个有向图的问题上,使用关系和节点游标。

RelationshipTraversalCursor relsCursor = cursors.allocateRelationshipTraversalCursor();
NodeCursor nodeCursor = cursors.allocateNodeCursor();

read.singleNode(startingNode.getId(), nodeCursor);
nodeCursor.next();

nodeCursor.allRelationships(relsCursor);

如何将对关系的迭代限制为出边?到目前为止,我是使用org.neo4j.graphdb.NodeIterable<Relationship> getRelationships(Direction var1)函数来实现的,但由于我对Neo4j Java API的内部了解有限,我想比较相对性能。

英文:

I'm trying to adapt Max de Marzi's Parallel K-Hop Counts example to a problem on a directed graph. Using relationship and node cursors

RelationshipTraversalCursor relsCursor = cursors.allocateRelationshipTraversalCursor();
NodeCursor nodeCursor = cursors.allocateNodeCursor();

read.singleNode(startingNode.getId(), nodeCursor);
nodeCursor.next();

nodeCursor.allRelationships(relsCursor);

how would I limit the iteration over the relationships while (relsCursor.next()) {...} to the outgoing edges?

So far I achieved this using the Iterable<Relationship> getRelationships(Direction var1) function of the org.neo4j.graphdb.Node, but since my intuition of the internal Neo4j Java API is limited, I would like to compare relative performance.

答案1

得分: 1

这是一个有用的代码片段,来自相关的neo4j测试方法,展示了如何确定游标当前关系的方向:

private static String computeKey( ..., RelationshipTraversalCursor r )
{
    Direction d;
    if ( r.sourceNodeReference() == r.targetNodeReference() )
    {
        d = Direction.BOTH;
    }
    else if ( r.sourceNodeReference() == r.originNodeReference() )
    {
        d = Direction.OUTGOING;
    }
    else
    {
        d = Direction.INCOMING;
    }

    .
    .
    .
}

所以,对你来说,类似这样的代码应该可以工作:

while (relsCursor.next()) {
    if (relsCursor.sourceNodeReference() == relsCursor.originNodeReference()) {
        // 进行OUTGOING关系处理
    }
}
英文:

Here is a helpful snippet from a relevant neo4j test method that shows how to determine the direction of the cursor's current relationship:

private static String computeKey( ..., RelationshipTraversalCursor r )
{
    Direction d;
    if ( r.sourceNodeReference() == r.targetNodeReference() )
    {
        d = Direction.BOTH;
    }
    else if ( r.sourceNodeReference() == r.originNodeReference() )
    {
        d = Direction.OUTGOING;
    }
    else
    {
        d = Direction.INCOMING;
    }

    .
    .
    .
}

So, something like this should work for you:

while (relsCursor.next()) {
    if (relsCursor.sourceNodeReference() == relsCursor.originNodeReference()) {
        // Do OUTGOING relationship processing
    }
}

huangapple
  • 本文由 发表于 2020年8月6日 01:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63270133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定