Skip to content

Commit 40c2d41

Browse files
committed
get node edges
1 parent 8053523 commit 40c2d41

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/main/java/com/redislabs/redisgraph/Client.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ public HashMap<String, String> getEdge(String graphId, String id) {
132132
return attributes;
133133
}
134134

135+
public List<String> getNodeEdges(String graphId, String nodeId, String edgeType, int direction) {
136+
Jedis conn = _conn();
137+
List<String> args = new ArrayList<String>(4);
138+
args.add(graphId);
139+
args.add(nodeId);
140+
args.add(edgeType);
141+
args.add(String.valueOf(direction));
142+
143+
String[] stringArgs = args.toArray(new String[args.size()]);
144+
145+
List<String> edges = conn.getClient()
146+
.sendCommand(Commands.Command.GETNODEEDGES, stringArgs)
147+
.getMultiBulkReply();
148+
149+
return edges;
150+
}
151+
135152
public List<String> getNeighbours(String graphId, String nodeId, String edgeType, int direction) {
136153
Jedis conn = _conn();
137154
List<String> args = new ArrayList<String>(4);

src/main/java/com/redislabs/redisgraph/Commands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public enum Command implements ProtocolCommand {
99
ADDEDGE("graph.ADDEDGE"),
1010
GETEDGE("graph.GETEDGE"),
1111
GETNODE("graph.GETNODE"),
12+
GETNODEEDGES("graph.GETNODEEDGES"),
1213
GETNEIGHBOURS("graph.GETNEIGHBOURS"),
1314
REMOVEEDGE("graph.REMOVEEDGE"),
1415
DELETEGRAPH("graph.DELETE"),

src/main/java/com/redislabs/redisgraph/RedisGraphAPI.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public RedisEdge getEdge(String id) {
6969
return new RedisEdge(edgeId, srcNode, destNode, relation, attributes);
7070
}
7171

72+
public List<RedisEdge> getNodeEdges(String nodeId, String edgeType, int direction) {
73+
List<String> edgeIds = client.getNodeEdges(this.graphId, nodeId, edgeType, direction);
74+
ArrayList<RedisEdge> edges = new ArrayList<RedisEdge>();
75+
76+
for(String id: edgeIds) {
77+
edges.add(getEdge(id));
78+
}
79+
80+
return edges;
81+
}
82+
7283
public List<RedisNode> getNeighbours(String nodeId, String edgeType, int direction) {
7384
List<String> nodeIds = client.getNeighbours(this.graphId, nodeId, edgeType, direction);
7485
ArrayList<RedisNode> nodes = new ArrayList<RedisNode>();

src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,45 @@ public void testSetProperty() throws Exception {
134134
Assert.assertSame(Integer.parseInt(node.getAttributes().get("age")), 61);
135135
}
136136

137+
@org.testng.annotations.Test
138+
public void testGetNodeEdges() throws Exception {
139+
RedisGraphAPI api = new RedisGraphAPI("social");
140+
141+
// Create both source and destination nodes
142+
RedisNode roi = api.createNode("name", "roi", "age", 32);
143+
RedisNode amit = api.createNode("name", "amit", "age", 30);
144+
RedisNode shany = api.createNode("name", "shany", "age", 23);
145+
146+
// Connect source and destination nodes.
147+
api.connectNodes(roi, "knows", amit);
148+
api.connectNodes(roi, "knows", shany);
149+
api.connectNodes(amit, "knows", roi);
150+
api.connectNodes(shany, "knows", roi);
151+
152+
int DIR_OUT = 0;
153+
int DIR_IN = 1;
154+
int DIR_BOTH = 2;
155+
156+
List<RedisEdge> edges;
157+
edges = api.getNodeEdges(roi.getId(), "knows", DIR_OUT);
158+
Assert.assertEquals(edges.size(), 2);
159+
160+
edges = api.getNodeEdges(roi.getId(), "knows", DIR_IN);
161+
Assert.assertEquals(edges.size(), 2);
162+
163+
edges = api.getNodeEdges(roi.getId(), "knows", DIR_BOTH);
164+
Assert.assertEquals(edges.size(), 4);
165+
166+
edges = api.getNodeEdges(amit.getId(), "knows", DIR_OUT);
167+
Assert.assertEquals(edges.size(), 1);
168+
169+
edges = api.getNodeEdges(amit.getId(), "knows", DIR_IN);
170+
Assert.assertEquals(edges.size(), 1);
171+
172+
edges = api.getNodeEdges(amit.getId(), "knows", DIR_BOTH);
173+
Assert.assertEquals(edges.size(), 2);
174+
}
175+
137176
@org.testng.annotations.Test
138177
public void testGetNeighbours() throws Exception {
139178
RedisGraphAPI api = new RedisGraphAPI("social");
@@ -171,6 +210,5 @@ public void testGetNeighbours() throws Exception {
171210

172211
neighbours = api.getNeighbours(amit.getId(), "knows", DIR_BOTH);
173212
Assert.assertEquals(neighbours.size(), 2);
174-
175213
}
176214
}

0 commit comments

Comments
 (0)