Java DSL - starting a query with a V(vertexId) step

27 views
Skip to first unread message

Daniel Craig

unread,
Feb 10, 2023, 2:50:17 PM2/10/23
to Gremlin-users
Hi, I have a toy java DSL at https://github.com/danielmartincraig/technic-dsl which models gears which drive other gears. This DSL aims to be declarative and idempotent - client code doesn't need to concern itself with whether or not a vertex already exists, a vertex will be created if it doesn't already exist. Here's a sample which creates 2 linked vertices:

g.gear("driverGear").drives(__.gear("followerGear")).next();

I have defined a gear(String gearId) traversal start step that looks like this:

public GraphTraversal<Vertex, Vertex> gear(String gearId) {
GraphTraversalSource clone = this.clone();

clone.getBytecode().addStep(GraphTraversal.Symbols.V);
GraphTraversal<Vertex, Vertex> traversal = new DefaultGraphTraversal<>(clone);
traversal.asAdmin().addStep(new GraphStep<>(traversal.asAdmin(), Vertex.class, true));

traversal = traversal.has(T.id, gearId);
traversal = traversal.fold().coalesce(__.unfold(), __.addV("gear").property(T.id, gearId));
return traversal;
}


So I'm concerned that this step as implemented will do a full scan because it starts with a V() and then does a .has(T.id, gearID). How can I do the equivalent of a V(gearID) instead?

Stephen Mallette

unread,
Feb 13, 2023, 6:54:09 AM2/13/23
to gremli...@googlegroups.com
V().hasId(gearId) or V().has(id,  gearId) should behave the same as V(gearId), but you could just do:

traversal.asAdmin().addStep(new GraphStep<>(traversal.asAdmin(), Vertex.class, true, gearId));

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/7018e6fc-bdba-4ae2-88e2-adcd552f7dd6n%40googlegroups.com.

Daniel Craig

unread,
Feb 13, 2023, 10:12:09 AM2/13/23
to Gremlin-users
Thank you!!
Reply all
Reply to author
Forward
0 new messages