ashoksl
ashoksl

Reputation: 383

Is there a way to chain traversals in tinkerpop ? (other than .flatMap())

We've one photo sharing application and I'm using tinkerpop 3.4.3 java library and AWS Neptune graph. In our application, we're already using .flatMap() step to chain the traversals from the other methods. Our current code looks like this.

    public boolean isViewable(String photoId, String userId) {   
       return graph.V(photoId).hasLabel("photo")
                   .flatMap(getDirection(userId))
                   .otherV().hasId(userId).hasNext(); 
    }

Based on the userId, we retrieve the correct direction/relation information from the other systems and use it here for the result.

Unfortunately we're facing marginal performance issue when using the .flatMap() step when the number of edges of the photoId are high (100K edges).

...flatMap(inE()).otherV().profile() results in ~5000 milli seconds but the same query without .flatMap results in less than 200 milli seconds.

To avoid this, we've modified our current code like the below.

public boolean isViewable(String photoId, String userId) {   
   GraphTraversal<Vertex, Vertex> traversal = graph.V(photoId).hasLabel("photo");
   applyDirection(traversal, userId);
   traversal.otherV().hasId(userId).hasNext(); 
}

private void applyDirection(GraphTraversal<Vertex, Vertex> traversal, String userId) {
   if(condition) {
      traversal.inE();
   } else {
      traversal.outE();
   }
}

But code looks complex without the chaining. Is there any other steps are available to chain the traversals ?

Upvotes: 0

Views: 216

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

I don't think your code without the chaining is all that complex or hard to read. It's quite common to take that approach when dynamically building a traversal. If you really dislike it you could build a DSL to make a custom step to encapsulate that logic:

graph.V(photoId).hasLabel("photo")
     .eitherInOrOut(userId)
     .otherV().hasId(userId).hasNext();

If your logic is truly that simple for determining the Direction you could also use the little known to() step:

graph.V(photoId).hasLabel("photo")
     .toE(getDirection(userId))
     .otherV().hasId(userId).hasNext(); 

Upvotes: 2

Related Questions