question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to generate idempotent Gremlin query using Coalesce

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
GarethJamescommented, Apr 18, 2020

Ah, okay; that makes sense. I really appreciate you taking the time to investigate the issue anyway. Many thanks, Gareth

0reactions
GarethJamescommented, Apr 21, 2020

Thanks for the tip; we’ll certainly try that and see what performance impact there is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Importance of Writing Idempotent Queries in Gremlin
In Gremlin, a query executes as a self-contained transaction. That means the query must completely succeed in order for any changes it has ......
Read more >
Making efficient Gremlin upserts with fold()/coalesce()/ ...
An upsert (or conditional insert) reuses a vertex or edge if it already exists, or creates it if it doesn't. Efficient upserts can...
Read more >
Gremlin- unable to select 2 variables together when using ...
I get the same result, again- as expected. the weird behavior is when I try to use select("a","b") at the end: g.V( ...
Read more >
Cosmos DB Graph/Gremlin API: Thoughts about transaction ...
The idea is really simple — i.e., check if the vertex/edge exists before trying to create it, and if it is present then...
Read more >
TinkerPop Upgrade Information
Create connection to Gremlin Server with transaction enabled graph. Spawn a GraphTraversalSource with opened transaction. Make some updates to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found