为什么需要使用合并来折叠/展开条件插入? [英] Why do you need to fold/unfold using coalesce for a conditional insert?

查看:42
本文介绍了为什么需要使用合并来折叠/展开条件插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解条件插入的这种模式是如何工作的:

I'm trying to understand how this pattern for a conditional insert works:

g.V()
  .hasLabel('person').has('name', 'John')
  .fold()
  .coalesce(
    __.unfold(),
    g.addV('person').property('name', 'John')
  ).next();

折叠/展开的目的是什么?为什么需要这些,为什么不起作用:

What is the purpose of the fold/unfold? Why are these necessary, and why does this not work:

g.V()
  .coalesce(
    __.hasLabel('person').has('name', 'John'),
    g.addV('person').property('name', 'John')
  ).next();

折叠-然后-展开"模式对我来说似乎是多余的,但是上述方法并不会产生相同的结果.

The fold-then-unfold pattern seems redundant to me and yet the above does not yield the same result.

推荐答案

考虑一下以下操作会发生什么:

Consider what happens when you just do the following:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko')
==>v[1]
gremlin> g.V().has('name','stephen')
gremlin> 

对于"marko",您返回某些内容,对于"stephen",您不返回某些内容. 斯蒂芬"案例是要注意的一个案例,因为在这种情况下,fold()确实变得非常重要.当遍历不返回任何内容时,此后添加的任何步骤都不会显示Traverser来触发这些步骤中的操作.因此,即使是以下情况也不会添加顶点:

For "marko" you return something and for "stephen" you do not. The "stephen" case is the one to pay attention to because that is the one where the fold() truly becomes important in this pattern. When that traversal returns nothing, any steps you add after that will not have a Traverser present to trigger actions in those steps. Therefore even the following will not add a vertex:

gremlin> g.V().has('name','stephen').addV('person')
gremlin> 

但是,如果我们fold():

gremlin> g.V().has('name','stephen').fold()
==>[]

fold()是减少障碍的步骤,因此将急切地评估到该点的遍历,并将内容返回为List,即使直到该点的遍历的内容都没有产生任何结果(在这种情况下,您可以看到,您得到一个空列表).如果您有一个空的List,则该空的List是一个遍历遍历的Traverser,因此将来的步骤将触发:

fold() is a reducing barrier step and will thus eagerly evaluate the traversal up to that point and return the contents as a List even if the contents of that traversal up to that point yield nothing (in which case, as you can see, you get an empty list). And if you have an empty List that empty List is a Traverser flowing through the traversal and therefore future steps will fire:

gremlin> g.V().has('name','stephen').fold().addV('person')
==>v[13]

所以这解释了为什么我们fold()的原因,因为我们正在检查示例中是否存在约翰",如果找到了他,则他将存在于List中,并且当带有约翰"的List命中它的第一个检查将是unfold()带有List并返回该Vertex-完成.如果List为空并且由于不存在"John"而未返回任何内容,则它将添加顶点(顺便说一句,您不需要在addV()前面的"g.",它应该只是一个匿名遍历,因此__.addV('person')).

So that explains why we fold() because we are checking for existence of "John" in your example and if he's found then he will exist in the List and when that List with "John" hits coalesce() its first check will be to unfold() that List with "John" and return that Vertex - done. If the List is empty and returns nothing because "John" does not exist then it will add the vertex (by the way, you don't need the "g." in front of addV(), it should just be an anonymous traversal and thus __.addV('person')).

以您的示例为例,我首先要指出的是,我想您想问一下这个问题:

Turning to your example, I would first point out that I think you wanted to ask about this:

g.V().
  coalesce(
    __.has('person','name', 'John'),
    __.addV('person').property('name', 'John'))

这是一个完全不同的查询.在此遍历中,您要迭代所有顶点,并为每个顶点执行coalesce()中的内容.通过将addV()替换为constant('x'),可以很清楚地看到这一点:

This is a completely different query. In this traversal, you're saying iterate all the vertices and for each one execute what is in the coalesce(). You can see this fairly plainly by replacing the addV() with constant('x'):

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().
......1>   coalesce(
......2>     has('person','name', 'John'),
......3>     constant('x'))
==>x
==>x
==>x
==>x
==>x
==>x
gremlin> g.V().
......1>   coalesce(
......2>     has('person','name', 'marko'),
......3>     constant('x'))
==>v[1]
==>x
==>x
==>x
==>x
==>x

现在,想象一下addV()和"John"会发生什么.它将调用addV() 6次,对于遇到的不是"John"的每个顶点一次:

Now, imagine what happens with addV() and "John". It will call addV() 6 times, once for each vertex it comes across that is not "John":

gremlin> g.V().
......1>   coalesce(
......2>     __.has('person','name', 'John'),
......3>     __.addV('person').property('name', 'John'))
==>v[13]
==>v[15]
==>v[17]
==>v[19]
==>v[21]
==>v[23]

个人而言,我喜欢将这种逻辑包装在

Personally, I like the idea of wrapping up this kind of logic in a Gremlin DSL - there is a good example of doing so here.

好问题-我已经将元素存在"问题描述为Gremlin食谱的一部分,可以阅读此处.

Nice question - I've described the "Element Existence" issue as part of a Gremlin Recipe that can be read here.

这篇关于为什么需要使用合并来折叠/展开条件插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆