Unable to generate idempotent Gremlin query using Coalesce
See original GitHub issueHi,
I’m using Gremlinq to connect to a Cosmos Graph DB. I’m trying to create a Gremlinq expression that adds vertices and edges if they don’t already exist. This is so that the query is idempotent and can be retried. The Gremlin query I’m trying to produce looks something like this (borrowed from https://medium.com/@jayanta.mondal/cosmos-db-graph-gremlin-api-how-to-executing-multiple-writes-as-a-unit-via-a-single-gremlin-2ce82d8bf365)
g.V("userId").has("pk", "pkValue1").as("creator")
.coalesce(
g.V("tweetId").has("pk", "pkValue2"),
addV("tweet")
.property("id", "tweetId")
.property("pk", "pkValue2")
.property("content", "blah")
).as ("tweet")
.coalesce(
g.E().has("id", "edgeId").has("pk", "pkValue2"),
addE("created by")
.property("id", "edgeId")
.to("creator")
);
The closest I’ve managed to get is as follows:
IVertex tweetVertex = default!;
IVertex tweetEdge = default!;
await _g
.V<IVertex>("userId")
.Where(x => x.pk == "pkValue1")
.As((__, creator) => __
.Coalesce(
(q) => _g.V<IVertex>("tweetId").Where(x => x.pk == "pkValue2"),
(q) => _g.AddV(tweetVertex)
.Property("id", "tweetId")
.Property("pk", "pkValue2")
.Property("content", "blah")
)
.As((___, tweet) => ___
.Coalesce(
(q) => _g.E<IEdge>("edgeId").Where(x => x.pk == "pkValue2"),
(q) => (IEdgeGremlinQuery<IEdge>)_g.AddE(tweetEdge)
.Property("id", "edgeId")
.To(creator)
)
)
);
NB: I’ve used IVertex and IEdge in place of real C# objects for convenience here.
The query that is generated is a lot more verbose, but the main issue I have is that the traversals in the Coalesce steps always seem to produce these underscores in the Coalesce traversals:
g.V("userId").has("pk", "pkValue1").as("creator")
.coalesce(
__.V("tweetId").has("pk", "pkValue2"),
__.addV("tweet")
.property("id", "tweetId")
.property("pk", "pkValue2")
.property("content", "blah")
).as ("tweet")
.coalesce(
__.E().has("id", "edgeId").has("pk", "pkValue2"),
__.addE("created by")
.property("id", "edgeId")
.to("creator")
);
I can’t seem to find a way of having g or nothing at all rather than __ in the traversals of the Coalesce step. Is this possible with Gremlinq?
Many thanks, Gareth
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
Ah, okay; that makes sense. I really appreciate you taking the time to investigate the issue anyway. Many thanks, Gareth
Thanks for the tip; we’ll certainly try that and see what performance impact there is.