Skip to content

Commit 32f1270

Browse files
committed
get node, get edge
1 parent 861f773 commit 32f1270

File tree

6 files changed

+181
-61
lines changed

6 files changed

+181
-61
lines changed

pom.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>groupId</groupId>
7+
<groupId>com.redislabs</groupId>
88
<artifactId>JRedisGraph</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.0.0</version>
1010
<dependencies>
1111
<dependency>
1212
<groupId>org.testng</groupId>
1313
<artifactId>testng</artifactId>
1414
<version>6.8</version>
1515
</dependency>
16+
17+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
18+
<dependency>
19+
<groupId>org.apache.commons</groupId>
20+
<artifactId>commons-pool2</artifactId>
21+
<version>2.0</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>redis.clients</groupId>
26+
<artifactId>jedis</artifactId>
27+
<version>2.9.0</version>
28+
<type>jar</type>
29+
<scope>system</scope>
30+
<systemPath>${project.basedir}/lib/jedis-3.0.0.jar</systemPath>
31+
</dependency>
1632
</dependencies>
17-
</project>
33+
</project>

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.redislabs.redisgraph;
22

3-
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
43
import redis.clients.jedis.Jedis;
54
import redis.clients.jedis.JedisPool;
65

76
import java.util.ArrayList;
7+
import java.util.HashMap;
88
import java.util.List;
99
import java.util.Map;
1010

@@ -83,26 +83,61 @@ public String connectNodes(String graph, String srcNodeID, String relation, Stri
8383
return edgeId;
8484
}
8585

86-
private Map<String, String> getGraphEntity(String id) {
86+
public HashMap<String, String> getNode(String graphId, String id) {
8787
Jedis conn = _conn();
88-
Map<String, String> properties = conn.hgetAll(id);
88+
89+
List<String> args = new ArrayList<String>(2);
90+
args.add(graphId);
91+
args.add(id);
92+
93+
String[] stringArgs = args.toArray(new String[args.size()]);
94+
95+
List<String> properties = conn.getClient()
96+
.sendCommand(Commands.Command.GETNODE, stringArgs)
97+
.getMultiBulkReply();
98+
99+
HashMap<String, String> attributes = new HashMap<String, String>(properties.size()/2);
100+
101+
for(int i = 0; i < properties.size(); i+=2) {
102+
String key = properties.get(i) ;
103+
String value = properties.get(i+1);
104+
attributes.put(key, value);
105+
}
106+
89107
conn.close();
90-
return properties;
108+
return attributes;
91109
}
92110

93-
public Map<String, String> getNode(String id) {
94-
return getGraphEntity(id);
95-
}
111+
public HashMap<String, String> getEdge(String graphId, String id) {
112+
Jedis conn = _conn();
113+
114+
List<String> args = new ArrayList<String>(2);
115+
args.add(graphId);
116+
args.add(id);
117+
118+
String[] stringArgs = args.toArray(new String[args.size()]);
119+
120+
List<String> properties = conn.getClient()
121+
.sendCommand(Commands.Command.GETEDGE, stringArgs)
122+
.getMultiBulkReply();
96123

97-
public Map<String, String> getEdge(String id) {
98-
return getGraphEntity(id);
124+
HashMap<String, String> attributes = new HashMap<String, String>(properties.size()/2);
125+
126+
for(int i = 0; i < properties.size(); i+=2) {
127+
String key = properties.get(i) ;
128+
String value = properties.get(i+1);
129+
attributes.put(key, value);
130+
}
131+
132+
conn.close();
133+
return attributes;
99134
}
100135

101-
public ResultSet query(String graphID, String query) {
136+
public ResultSet query(String graphId, String query) {
102137
Jedis conn = _conn();
103138

104139
List<Object> resp = conn.getClient()
105-
.sendCommand(Commands.Command.QUERY, graphID, query)
140+
.sendCommand(Commands.Command.QUERY, graphId, query)
106141
.getObjectMultiBulkReply();
107142

108143
return new ResultSet(resp);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class Commands {
77
public enum Command implements ProtocolCommand {
88
CREATENODE("graph.CREATENODE"),
99
ADDEDGE("graph.ADDEDGE"),
10+
GETEDGE("graph.GETEDGE"),
11+
GETNODE("graph.GETNODE"),
1012
REMOVEEDGE("graph.REMOVEEDGE"),
1113
DELETEGRAPH("graph.DELETE"),
1214
QUERY("graph.QUERY");
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
package com.redislabs.redisgraph;
22

3-
import java.util.Map;
3+
import java.util.HashMap;
44

55
public class RedisEdge {
6-
String id;
7-
String relation;
8-
Map<String, String> attributes;
9-
RedisNode src;
10-
RedisNode dest;
6+
private String id;
7+
private String relation;
8+
private HashMap<String, String> attributes;
9+
private RedisNode src;
10+
private RedisNode dest;
1111

12-
public RedisEdge(String id, RedisNode src, RedisNode dest, String relation, Map<String, String> attributes) {
12+
public RedisEdge(String id, RedisNode src, RedisNode dest, String relation, HashMap<String, String> attributes) {
1313
this.id = id;
1414
this.relation = relation;
1515
this.attributes = attributes;
1616
this.src = src;
1717
this.dest = dest;
1818
}
19+
20+
public String getId() {
21+
return id;
22+
}
23+
24+
public String getRelation() {
25+
return relation;
26+
}
27+
28+
public HashMap<String, String> getAttributes() {
29+
return attributes;
30+
}
31+
32+
public RedisNode getSrc() {
33+
return src;
34+
}
35+
36+
public RedisNode getDest() {
37+
return dest;
38+
}
1939
}

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
public class RedisGraphAPI {
77

88
Client client;
9-
String graphID;
9+
String graphId;
1010

11-
public RedisGraphAPI(String graphID) {
12-
this.graphID = graphID;
11+
public RedisGraphAPI(String graphId) {
12+
this.graphId = graphId;
1313
client = new Client("localhost", 6379);
1414
}
1515

1616
public RedisNode createNode(Object... attributes) {
17-
String id = client.createNode(this.graphID, attributes);
17+
String id = client.createNode(this.graphId, attributes);
1818

1919
Map<String, String> attr = new HashMap<String, String>();
2020

@@ -28,7 +28,7 @@ public RedisNode createNode(Object... attributes) {
2828
}
2929

3030
public RedisNode createLabeledNode(String label, Object... attributes) {
31-
String id = client.createNode(this.graphID, label, attributes);
31+
String id = client.createNode(this.graphId, label, attributes);
3232

3333
Map<String, String> attr = new HashMap<String, String>();
3434

@@ -42,13 +42,34 @@ public RedisNode createLabeledNode(String label, Object... attributes) {
4242
}
4343

4444
public RedisNode getNode(String id) {
45-
Map<String, String> attributes = client.getNode(id);
46-
return new RedisNode(id, null, attributes);
45+
HashMap<String, String> attributes = client.getNode(this.graphId, id);
46+
String label = attributes.get("label");
47+
attributes.remove("label");
48+
49+
return new RedisNode(id, label, attributes);
50+
}
51+
52+
public RedisEdge getEdge(String id) {
53+
HashMap<String, String> attributes = client.getEdge(this.graphId, id);
54+
String edgeId = attributes.get("id");
55+
String relation = attributes.get("type");
56+
String srcNodeId = attributes.get("src");
57+
String destNodeId = attributes.get("dest");
58+
59+
attributes.remove("id");
60+
attributes.remove("type");
61+
attributes.remove("src");
62+
attributes.remove("dest");
63+
64+
RedisNode srcNode = getNode(srcNodeId);
65+
RedisNode destNode = getNode(destNodeId);
66+
67+
return new RedisEdge(edgeId, srcNode, destNode, relation, attributes);
4768
}
4869

4970
public RedisEdge connectNodes(RedisNode src, String relation, RedisNode dest, Object... attributes) {
50-
String edgeId = client.connectNodes(this.graphID, src.id, relation, dest.id, attributes);
51-
Map<String, String> attr = new HashMap<String, String>();
71+
String edgeId = client.connectNodes(this.graphId, src.id, relation, dest.id, attributes);
72+
HashMap<String, String> attr = new HashMap<String, String>();
5273

5374
for(int i = 0; i < attributes.length; i+=2) {
5475
String key = attributes[i].toString();
@@ -60,6 +81,6 @@ public RedisEdge connectNodes(RedisNode src, String relation, RedisNode dest, Ob
6081
}
6182

6283
public ResultSet query(String query) {
63-
return client.query(this.graphID, query);
84+
return client.query(this.graphId, query);
6485
}
6586
}

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

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,6 @@ public void testCreateLabeledNode() throws Exception {
3131
Assert.assertSame(node.attributes.size(),2);
3232
}
3333

34-
@org.testng.annotations.Test
35-
public void testGetNode() throws Exception {
36-
RedisGraphAPI api = new RedisGraphAPI("social");
37-
38-
// Create a node with a label
39-
RedisNode node = api.createLabeledNode("human","name", "roi", "age", 32);
40-
41-
// Retrieve node
42-
RedisNode clone = api.getNode(node.id);
43-
44-
// Make sure retrieved node is the same as the one created.
45-
Assert.assertSame(clone.id, node.id);
46-
// TODO: store label as one of the node attributes.
47-
// Assert.assertSame(clone.label, node.label);
48-
Assert.assertEquals(clone.attributes.get("name"), node.attributes.get("name"));
49-
Assert.assertEquals(clone.attributes.get("age"), node.attributes.get("age"));
50-
Assert.assertEquals(clone.attributes.size(), node.attributes.size());
51-
52-
}
53-
5434
@org.testng.annotations.Test
5535
public void testConnectNodes() throws Exception {
5636
RedisGraphAPI api = new RedisGraphAPI("social");
@@ -63,33 +43,79 @@ public void testConnectNodes() throws Exception {
6343
RedisEdge edge = api.connectNodes(src, "knows", dest, "strength", "3", "from", "high-school");
6444

6545
// Validate edge
66-
Assert.assertNotNull(edge.id);
67-
Assert.assertEquals(edge.relation, "knows");
68-
Assert.assertSame(edge.attributes.size(), 2);
69-
Assert.assertSame(edge.attributes.get("strength"), "3");
70-
Assert.assertSame(edge.attributes.get("from"), "high-school");
71-
Assert.assertSame(edge.src, src);
72-
Assert.assertSame(edge.dest, dest);
46+
Assert.assertNotNull(edge.getId());
47+
Assert.assertEquals(edge.getRelation(), "knows");
48+
Assert.assertSame(edge.getAttributes().size(), 2);
49+
Assert.assertSame(edge.getAttributes().get("strength"), "3");
50+
Assert.assertSame(edge.getAttributes().get("from"), "high-school");
51+
Assert.assertSame(edge.getSrc(), src);
52+
Assert.assertSame(edge.getDest(), dest);
7353
}
7454

7555
@org.testng.annotations.Test
7656
public void testQuery() throws Exception {
7757
RedisGraphAPI api = new RedisGraphAPI("social");
7858

7959
// Create both source and destination nodes
80-
RedisNode src = api.createLabeledNode("human","name", "roi", "age", 32);
81-
RedisNode dest = api.createLabeledNode("human","name", "amit", "age", 30);
60+
RedisNode src = api.createLabeledNode("Qhuman","name", "roi", "age", 32);
61+
RedisNode dest = api.createLabeledNode("Qhuman","name", "amit", "age", 30);
8262

8363
// Connect source and destination nodes.
8464
api.connectNodes(src, "knows", dest, "strength", "3", "from", "high-school");
8565

8666
// Query
87-
ResultSet resultSet = api.query("MATCH (a:human)-[]->(:human) RETURN a");
67+
ResultSet resultSet = api.query("MATCH (a:Qhuman)-[]->(:Qhuman) RETURN a");
8868

8969
// Expecting a single result
9070
Assert.assertEquals(resultSet.totalResults, 1);
9171
Assert.assertEquals(resultSet.results.size(), resultSet.totalResults);
9272
Assert.assertEquals(resultSet.results.get(0)[0], "roi");
9373
Assert.assertEquals(resultSet.results.get(0)[1], "32");
9474
}
75+
76+
@org.testng.annotations.Test
77+
public void testGetNode() throws Exception {
78+
RedisGraphAPI api = new RedisGraphAPI("social");
79+
80+
// Create node
81+
RedisNode node = api.createLabeledNode("human","name", "shany", "age", 23);
82+
83+
// Get node
84+
RedisNode retrievedNode = api.getNode(node.id);
85+
86+
// Expecting a single result
87+
Assert.assertNotNull(retrievedNode);
88+
Assert.assertEquals(node.id, retrievedNode.id);
89+
Assert.assertEquals(node.label, retrievedNode.label);
90+
Assert.assertEquals(retrievedNode.attributes.size(), 2);
91+
Assert.assertEquals(retrievedNode.attributes.get("name"), "shany");
92+
Assert.assertEquals(Integer.parseInt(retrievedNode.attributes.get("age")), 23);
93+
}
94+
95+
@org.testng.annotations.Test
96+
public void testGetEdge() throws Exception {
97+
RedisGraphAPI api = new RedisGraphAPI("social");
98+
99+
// Create both source and destination nodes
100+
RedisNode src = api.createLabeledNode("human","name", "roi", "age", 32);
101+
RedisNode dest = api.createLabeledNode("human","name", "amit", "age", 30);
102+
103+
// Connect source and destination nodes.
104+
RedisEdge edge = api.connectNodes(src, "knows", dest, "strength", "3", "from", "high-school");
105+
106+
// Get edge
107+
RedisEdge retrievedEdge = api.getEdge(edge.getId());
108+
109+
// Expecting a single result
110+
Assert.assertNotNull(retrievedEdge);
111+
Assert.assertEquals(edge.getId(), retrievedEdge.getId());
112+
Assert.assertEquals(edge.getRelation(), retrievedEdge.getRelation());
113+
Assert.assertEquals(retrievedEdge.getAttributes().size(), 2);
114+
Assert.assertEquals(retrievedEdge.getAttributes().get("strength"), "3");
115+
Assert.assertEquals(retrievedEdge.getAttributes().get("from"), "high-school");
116+
117+
Assert.assertEquals(retrievedEdge.getSrc().id, src.id);
118+
Assert.assertEquals(retrievedEdge.getDest().id, dest.id);
119+
120+
}
95121
}

0 commit comments

Comments
 (0)