Skip to content

Commit

Permalink
get node edges
Browse files Browse the repository at this point in the history
  • Loading branch information
swilly22 committed Jun 1, 2017
1 parent 8053523 commit 40c2d41
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/main/java/com/redislabs/redisgraph/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ public HashMap<String, String> getEdge(String graphId, String id) {
return attributes;
}

public List<String> getNodeEdges(String graphId, String nodeId, String edgeType, int direction) {
Jedis conn = _conn();
List<String> args = new ArrayList<String>(4);
args.add(graphId);
args.add(nodeId);
args.add(edgeType);
args.add(String.valueOf(direction));

String[] stringArgs = args.toArray(new String[args.size()]);

List<String> edges = conn.getClient()
.sendCommand(Commands.Command.GETNODEEDGES, stringArgs)
.getMultiBulkReply();

return edges;
}

public List<String> getNeighbours(String graphId, String nodeId, String edgeType, int direction) {
Jedis conn = _conn();
List<String> args = new ArrayList<String>(4);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/redislabs/redisgraph/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum Command implements ProtocolCommand {
ADDEDGE("graph.ADDEDGE"),
GETEDGE("graph.GETEDGE"),
GETNODE("graph.GETNODE"),
GETNODEEDGES("graph.GETNODEEDGES"),
GETNEIGHBOURS("graph.GETNEIGHBOURS"),
REMOVEEDGE("graph.REMOVEEDGE"),
DELETEGRAPH("graph.DELETE"),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/redislabs/redisgraph/RedisGraphAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public RedisEdge getEdge(String id) {
return new RedisEdge(edgeId, srcNode, destNode, relation, attributes);
}

public List<RedisEdge> getNodeEdges(String nodeId, String edgeType, int direction) {
List<String> edgeIds = client.getNodeEdges(this.graphId, nodeId, edgeType, direction);
ArrayList<RedisEdge> edges = new ArrayList<RedisEdge>();

for(String id: edgeIds) {
edges.add(getEdge(id));
}

return edges;
}

public List<RedisNode> getNeighbours(String nodeId, String edgeType, int direction) {
List<String> nodeIds = client.getNeighbours(this.graphId, nodeId, edgeType, direction);
ArrayList<RedisNode> nodes = new ArrayList<RedisNode>();
Expand Down
40 changes: 39 additions & 1 deletion src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,45 @@ public void testSetProperty() throws Exception {
Assert.assertSame(Integer.parseInt(node.getAttributes().get("age")), 61);
}

@org.testng.annotations.Test
public void testGetNodeEdges() throws Exception {
RedisGraphAPI api = new RedisGraphAPI("social");

// Create both source and destination nodes
RedisNode roi = api.createNode("name", "roi", "age", 32);
RedisNode amit = api.createNode("name", "amit", "age", 30);
RedisNode shany = api.createNode("name", "shany", "age", 23);

// Connect source and destination nodes.
api.connectNodes(roi, "knows", amit);
api.connectNodes(roi, "knows", shany);
api.connectNodes(amit, "knows", roi);
api.connectNodes(shany, "knows", roi);

int DIR_OUT = 0;
int DIR_IN = 1;
int DIR_BOTH = 2;

List<RedisEdge> edges;
edges = api.getNodeEdges(roi.getId(), "knows", DIR_OUT);
Assert.assertEquals(edges.size(), 2);

edges = api.getNodeEdges(roi.getId(), "knows", DIR_IN);
Assert.assertEquals(edges.size(), 2);

edges = api.getNodeEdges(roi.getId(), "knows", DIR_BOTH);
Assert.assertEquals(edges.size(), 4);

edges = api.getNodeEdges(amit.getId(), "knows", DIR_OUT);
Assert.assertEquals(edges.size(), 1);

edges = api.getNodeEdges(amit.getId(), "knows", DIR_IN);
Assert.assertEquals(edges.size(), 1);

edges = api.getNodeEdges(amit.getId(), "knows", DIR_BOTH);
Assert.assertEquals(edges.size(), 2);
}

@org.testng.annotations.Test
public void testGetNeighbours() throws Exception {
RedisGraphAPI api = new RedisGraphAPI("social");
Expand Down Expand Up @@ -171,6 +210,5 @@ public void testGetNeighbours() throws Exception {

neighbours = api.getNeighbours(amit.getId(), "knows", DIR_BOTH);
Assert.assertEquals(neighbours.size(), 2);

}
}

0 comments on commit 40c2d41

Please sign in to comment.