为什么叶节点的深度是垃圾值而不是一个?

Why is the depth of leaf node a garbage value instead of being one?

在 Tinkerpop/Gremlin 的 recipes 部分中,以下命令用于计算节点的深度。此命令通过计算节点本身来计算节点的深度。但是,当我们在叶节点上 运行 这个命令时,它 return 是一个垃圾值而不是 1。有人可以澄清一下吗?

命令:

g.V().has('name','F').repeat(__.in()).emit().path().count(local).max()

如果 'F' 是叶节点,那么它 return 的值不正确。我觉得应该 return 1.

使用来自 maximum depth recipe 的相同数据,以下是一些结果:

gremlin> g.V().has('name', 'F').repeat(__.in()).emit().path().count(local).max()
==>5
gremlin> g.V().has('name', 'C').repeat(__.in()).emit().path().count(local).max()
==>3
gremlin> g.V().has('name', 'A').repeat(__.in()).emit().path().count(local).max()
==>-2147483648

我们可以通过删除最后几个步骤来了解有关该行为的更多信息:

gremlin> g.V().has('name', 'C').repeat(__.in()).emit().path()
==>[v[4],v[6]]
==>[v[4],v[2]]
==>[v[4],v[2],v[0]]
gremlin> g.V().has('name', 'A').repeat(__.in()).emit().path()
gremlin>

可以看到'C'有3条路径,'A'有0条路径。这是因为在发出任何东西之前所有的遍历器都被杀死了。如果将 emit() 步骤移动到 repeat() 步骤之前,您将获得所需的行为:

gremlin> g.V().has('name', 'A').emit().repeat(__.in()).path()
==>[v[0]]
gremlin> g.V().has('name', 'A').emit().repeat(__.in()).path().count(local).max()
==>1

您可以在 TinkerPop documentation 中详细了解 repeat() 步骤及其与 emit() 步骤的交互。具体来说,有一个标注框指出:

If emit() is placed after repeat(), it is evaluated on the traversers leaving the repeat-traversal. If emit() is placed before repeat(), it is evaluated on the traversers prior to entering the repeat-traversal.