Janus Graph Query to remove visited nodes

31 views Asked by At

I am using janus graph and cassandra as Db, while traversing from source to it 2,3 or 4 degree node from that src there can be multiple vertex which was visited during lower degree of traversal, so I want to remove them.

Let me explain with example.

Src = user1

user1 -----knows----> [user2, user3, user4, user5] user2 -----knows----> [user3, user5, user8, user9]

user3 -----knows----> [user2, user6]

And you try to get 3 degree relations of user1.

So user1 1-Degree relation = [user2, user3, user4, user5]

2nd Degree relation = [ [user3, user5, user8, user9], [user2, user6]]

now In 2nd degree **user2, user3 and user5 should not be present as there were already in 1-degree and visited. **

Now extending this person to generic n-degree relations, I want to go to nextdegree and ignore the already visited once and then move forward with remaining one's for next degree of traversal.

I have tried this query and that worked.

graph.traversal().V().has("name", "a")
      // Mark "a" as visited
      .property("visited", true)
      // Follow "knows" edges
      .out("knows")
      // Filter non-visited connections
      .where(not(has("visited", true)))
      // Mark visited
      .property("visited", true)
      // Follow next "knows" edges
      .out("knows")
      // Filter non-visited connections again
      .where(not(has("visited", true)))
      // Follow next "knows" edges
      .out("knows")
      // Filter non-visited connections again
      .where(not(has("visited", true)))
      // Remove "visited" property (optional)
      .valueMap().forEachRemaining(vertx -> vertex.add(vertx.get("name").toString()));

But here we are marking property of each node as visited, which involved unnecessary writes and this will not work for concurrent thread working on same vertex (as A transaction changes the property visted but utilised by B transaction which is also, I know we can maintain that)

So I am wondering is there any correct or better approach for this to get the required results.

0

There are 0 answers