我的 Java 应用程序中有一个图形遍历,在遍历完成后需要 300 毫秒以上才能填充路径对象。这很奇怪,因为它只发生在某些特定的遍历上,而其他遍历会立即填充它们的路径。以下是 Java 代码示例,使用 Tinkerpop 3.3.1
我有一种情况,两个顶点由一条边直接连接。每次执行这个短遍历时,我都会得到很高的处理时间。如果我不执行 fill() 操作,遍历会立即完成。我还有其他需要遍历 10 多个边的遍历,并且它们在 < 1 毫秒内处理和填充路径。
在下面的代码中,我试图找到从“origs”中的顶点到“dests”中的顶点的最短路径,而不经过集合“avoids”中的任何顶点。遍历本身在 1 毫秒内完成,它的 fill() 方法消耗了时钟。
Date startTime = new Date ();
if (! dests.isEmpty ()){
g.V (origs).where (is (P.without (avoids))).
repeat (
out ().
simplePath ().
where (is (P.without (avoids)))
).
until (is (P.within (dests))).
limit (1).
path ().
fill (paths); // This 'fill' is the line that can take > 300ms.
// When fill is removed from the code,
// this all executes within the same milli
}
Date endTime = new Date ();
// Now compare start time and end time
int diff = DateUtil.getMillisBetween (startTime, endTime);
我也尝试过使用 toList() 方法,但这也会使代码执行时间超过 300 毫秒。
最佳答案
您的遍历是即时的,无需 fill()
或 toList()
,因为如果没有“迭代”,您不会得到结果,您只有一个 GraphTraversal
实例:
http://tinkerpop.apache.org/docs/current/tutorials/the-gremlin-console/#result-iteration
换句话说:
t = g.V()
创建一个GraphTraversal
实例,并且不将g.V()
的结果分配给t
。另一方面:
t = g.V().toList()
迭代遍历List
并将结果分配给t
。显然,前者将立即完成(即< 1ms),因为它只是构造一个对象,而后者将需要更长的时间,因为它必须与底层图形存储交互。
关于java - 从遍历中获取顶点或路径时,Tinkerpop 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49157687/