Search code examples
gremlintinkerpopamazon-neptune

Is it possible to get which or condition resulted true in traversals?


We have one photo sharing service in which one can allow or deny other set of users to view or not. We exposed this service as an API /view?caller=userId&photoId=photoId. We're using AWS Neptune Graph database service to maintain this authorization and using tinkerpop java library.

For the code maintainability, we fetch possible paths from other class methods and call canUserView method from the outside.

public boolean canUserView(User user, String photoId) {
    return graph.V(user.getId()).hasLabel("user").or(getPossibleTraversals(user)).hasNext();
}

private GraphTraversal<Object, Vertex>[] getPossibleTraversals(User user) {
    List<GraphTraversal<Vertex, Vertex>> traversals = collectTraversalsFromExternal();
    return traversals.toArray(GraphTraversal[]::new);
}

collectTraversalsFromExternal() queries our other datastore and based on result, we form the query. In every or traversal at the end we inject unique constant value to identify the traversal.

We were using .union() earlier to get the result and the constant value. But due to performance issues using .or() condition now.

This might be a dumb question. Is it possible to get reference which traversal was true ?


Solution

  • Using the air routes data set, here is one way you can achieve what you are looking for.

    The or will filter out all airports not in either Texas or Georgia. After that the choose step returns a constant indicating which or path was taken. You can of course do something more interesting that return a constant value in your query.

    gremlin> g.V(1,3,12).values('region')
    ==>US-GA
    ==>US-TX
    ==>US-NY
    
    gremlin> g.V(1,3,12).
    ......1>   or(has('region','US-GA'),
    ......2>      has('region', 'US-TX')).
    ......3>   choose(has('region','US-GA'),constant(1),constant(2))      
    
    ==>1
    ==>2