This query works:
choose(V().hasLabel("user"), V().hasLabel("user").drop())
But is searching 2 times for the vertex "user", I want to optimize it to 1 time, so I changed it for this one:
choose(V().hasLabel("user").fold(), unfold().drop())
If I do that trick multiple times in my query it throws:
Error: ResponseError: Server error: Vertex with id 70 was removed. (500)
I don't understand what that means, maybe fold() does not overwrite previous fold() calls, I don't know.
Is there an alternative to fold() unfold() for this use case? I've tried:
choose(V().hasLabel("user").as("u"), select("u").drop())
but that does not work, it seems .as()
don't save anything when is called inside choose()
I also tried:
choose(V().hasLabel("user").store("u"), select("u").drop())
But throws another error I don't understand: The incoming object is not removable
Your first attempt with:
choose(V().hasLabel("user"), V().hasLabel("user").drop())
is not as expensive as you think. The if
portion of choose()
(i.e. the first child traversal) does not iterate all users. It immediately returns true
if one "user" is found". Your then
child traversal will iterate all "user" vertices. Also, you don't provide a false
value to choose()
so in that case, I believe that you will end up calling the drop()
traversal for either situation:
gremlin> g.inject(1).choose(V().hasLabel('no-exist'),constant(1))
==>1
gremlin> g.inject(1).choose(V().hasLabel('no-exist'),constant(1),constant(2))
==>2
Of course, I'd wonder why you need to do an if/then
here at all because calling V().hasLabel("user").drop()
without choose()
will remove all "user" vertices found, or if none are found, just do nothing.
For this traversal:
choose(V().hasLabel("user").fold(), unfold().drop())
note that V().hasLabel("user").fold()
will always return "true" because you fold()
which is a reducing step which will return a List
of items in the stream. If the stream is empty you get an empty List
and thus choose()
will use the true
path there. In any event, your unfold()
is not unrolling what is returned from the first choose()
parameter - it unfolds the same Traverser
object handed to the choose()
step. You don't have what comes before the choose()
so I can't say what that is.
I'm not completely sure but based on your remaining traversal examples, I think you might be misusing choose()
in general. You don't seem to have need for an if/then
or switch
style operation. With Gremlin you don't need to check if something is present in order to remove it and it is in fact cheaper not to do so as mentioned earlier.