I'm using Amazon Neptune for a personal project and I'm having trouble writing a query. I'm using Gremlin-Java to query and mutate the graph.
The best way I can think of to represent the desired outputs for different inputs is with diagrams, so below is the problem statement in diagram form, broken into 3 cases. Lastly, a full solution isn't necessary (though would be accepted) - even a nudge in the right direction would be much appreciated!
Here's the code I have that should perform this mutation, but it must have a flaw (or multiple flaws!) because it doesn't mutate the graph at all in Case 1 and I've thus been unable to test Case 2. Case 3, Case 4, and Case 5 would also not exhibit the desired behavior with this code.
void createShard(String username, String shardName, Set<String> inheritedShardNames, Set<String> inheritedUsers) {}
g.V()
.hasLabel("shard)
.has("shardName", P.within(inheritedShardNames))
.as("inheritedShards")
.V()
.hasLabel("user")
.has("username", P.within(inheritedUsers))
.as("inheritedUsers")
.V()
.has("user", "username", username)
.as("user")
.addV("shard)
.property(single, "shardName", shardName)
.property(single, "createdAt", new Date())
.as("newShard")
.addE("shardInheritsShard").from("newShard").to("inheritedShards")
.addE("shardInheritsUser").from("newShard").to("inheritedUsers")
.addE("userOwnsShard").from("user").to("newShard")
.addE("userFollowsShard").from("user").to("newShard")
.iterate();
}
Case 1
Function call:
createShard(
"john",
"newShard",
new HashSet<>(),
new HashSet<>()
)
Case 2
Function call:
createShard(
"john",
"newShard",
new HashSet<>(Arrays.asList("firstShardToInherit", "secondShardToInherit")),
new HashSet<>(Arrays.asList("john", "userToInherit"))
)
Case 3
Function call:
createShard(
"john",
"newShard",
new HashSet<>(Arrays.asList("userWhoDoesNotExist")),
new HashSet<>(),
)
Desired Final Graph State: Same as initial graph state. Would be ideal if the mutation query would throw an exception like java.util.NoSuchElementException
or give some other indication that the graph wasn't mutated by the query.
Case 4
Function call:
createShard(
"john",
"newShard",
new HashSet<>(Arrays.asList()),
new HashSet<>(Arrays.asList("shardWhichDoesNotExist"))
)
Desired Final Graph State: Same as initial graph state. Would be ideal if the mutation query would throw an exception like java.util.NoSuchElementException
or give some other indication that the graph wasn't mutated by the query.
Case 5
Function call:
createShard(
"john",
"existingShardName",
new HashSet<>(),
new HashSet<>()
)
Desired Final Graph State: Same as initial graph state. Would be ideal if the mutation query would throw an exception or give some other indication that the graph wasn't mutated by the query because a vertex with the same label and "shardName" property already exists.