Skip to content

Commit bf88a23

Browse files
committed
initial commit
0 parents  commit bf88a23

File tree

11 files changed

+390
-0
lines changed

11 files changed

+390
-0
lines changed

lib/commons-pool2-2.4.2.jar

109 KB
Binary file not shown.

lib/jedis-3.0.0.jar

544 KB
Binary file not shown.

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>groupId</groupId>
8+
<artifactId>JRedisGraph</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.testng</groupId>
13+
<artifactId>testng</artifactId>
14+
<version>6.8</version>
15+
</dependency>
16+
</dependencies>
17+
</project>

src/main/java/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.redislabs.redisgraph.RedisGraphAPI
3+
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.redislabs.redisgraph;
2+
3+
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.JedisPool;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class Client {
12+
private JedisPool pool;
13+
14+
Jedis _conn() {
15+
return pool.getResource();
16+
}
17+
18+
public Client(String host, int port) {
19+
pool = new JedisPool(host, port);
20+
}
21+
22+
public String createNode(String graph, String label, Object... attributes) {
23+
Jedis conn = _conn();
24+
25+
List<String> args = new ArrayList<String>(2 + attributes.length);
26+
args.add(graph);
27+
args.add(label);
28+
29+
for(Object attr: attributes) {
30+
args.add(attr.toString());
31+
}
32+
33+
String[] stringArgs = args.toArray(new String[args.size()]);
34+
35+
String nodeId = conn.getClient()
36+
.sendCommand(Commands.Command.CREATENODE, stringArgs)
37+
.getBulkReply();
38+
39+
conn.close();
40+
return nodeId;
41+
}
42+
43+
public String createNode(String graph, Object... attributes) {
44+
Jedis conn = _conn();
45+
46+
List<String> args = new ArrayList<String>(1 + attributes.length);
47+
args.add(graph);
48+
49+
for(Object attr: attributes) {
50+
args.add(attr.toString());
51+
}
52+
53+
String[] stringArgs = args.toArray(new String[args.size()]);
54+
55+
String nodeId = conn.getClient()
56+
.sendCommand(Commands.Command.CREATENODE, stringArgs)
57+
.getBulkReply();
58+
59+
conn.close();
60+
return nodeId;
61+
}
62+
63+
64+
public String connectNodes(String graph, String srcNodeID, String relation, String destNodeID, Object... attributes) {
65+
Jedis conn = _conn();
66+
67+
List<Object> args = new ArrayList<Object>(4 + attributes.length);
68+
args.add(graph);
69+
args.add(srcNodeID);
70+
args.add(relation);
71+
args.add(destNodeID);
72+
73+
for(Object attr: attributes) {
74+
args.add(attr);
75+
}
76+
77+
String[] stringArgs = args.toArray(new String[args.size()]);
78+
79+
String edgeId = conn.getClient()
80+
.sendCommand(Commands.Command.ADDEDGE, stringArgs)
81+
.getBulkReply();
82+
conn.close();
83+
return edgeId;
84+
}
85+
86+
private Map<String, String> getGraphEntity(String id) {
87+
Jedis conn = _conn();
88+
Map<String, String> properties = conn.hgetAll(id);
89+
conn.close();
90+
return properties;
91+
}
92+
93+
public Map<String, String> getNode(String id) {
94+
return getGraphEntity(id);
95+
}
96+
97+
public Map<String, String> getEdge(String id) {
98+
return getGraphEntity(id);
99+
}
100+
101+
public ResultSet query(String graphID, String query) {
102+
Jedis conn = _conn();
103+
104+
List<Object> resp = conn.getClient()
105+
.sendCommand(Commands.Command.QUERY, graphID, query)
106+
.getObjectMultiBulkReply();
107+
108+
return new ResultSet(resp);
109+
}
110+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.redislabs.redisgraph;
2+
import redis.clients.util.SafeEncoder;
3+
import redis.clients.jedis.commands.ProtocolCommand;
4+
5+
6+
public class Commands {
7+
public enum Command implements ProtocolCommand {
8+
CREATENODE("graph.CREATENODE"),
9+
ADDEDGE("graph.ADDEDGE"),
10+
REMOVEEDGE("graph.REMOVEEDGE"),
11+
DELETEGRAPH("graph.DELETE"),
12+
QUERY("graph.QUERY");
13+
14+
private final byte[] raw;
15+
16+
Command(String alt) {
17+
raw = SafeEncoder.encode(alt);
18+
}
19+
20+
public byte[] getRaw() {
21+
return raw;
22+
}
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.redislabs.redisgraph;
2+
3+
import java.util.Map;
4+
5+
public class RedisEdge {
6+
String id;
7+
String relation;
8+
Map<String, String> attributes;
9+
RedisNode src;
10+
RedisNode dest;
11+
12+
public RedisEdge(String id, RedisNode src, RedisNode dest, String relation, Map<String, String> attributes) {
13+
this.id = id;
14+
this.relation = relation;
15+
this.attributes = attributes;
16+
this.src = src;
17+
this.dest = dest;
18+
}
19+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.redislabs.redisgraph;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class RedisGraphAPI {
7+
8+
Client client;
9+
String graphID;
10+
11+
public RedisGraphAPI(String graphID) {
12+
this.graphID = graphID;
13+
client = new Client("localhost", 6379);
14+
}
15+
16+
public RedisNode createNode(Object... attributes) {
17+
String id = client.createNode(this.graphID, attributes);
18+
19+
Map<String, String> attr = new HashMap<String, String>();
20+
21+
for(int i = 0; i < attributes.length; i+=2) {
22+
String key = attributes[i].toString();
23+
String value = attributes[i+1].toString();
24+
attr.put(key, value);
25+
}
26+
27+
return new RedisNode(id, null, attr);
28+
}
29+
30+
public RedisNode createLabeledNode(String label, Object... attributes) {
31+
String id = client.createNode(this.graphID, label, attributes);
32+
33+
Map<String, String> attr = new HashMap<String, String>();
34+
35+
for(int i = 0; i < attributes.length; i+=2) {
36+
String key = attributes[i].toString();
37+
String value = attributes[i+1].toString();
38+
attr.put(key, value);
39+
}
40+
41+
return new RedisNode(id, label, attr);
42+
}
43+
44+
public RedisNode getNode(String id) {
45+
Map<String, String> attributes = client.getNode(id);
46+
return new RedisNode(id, null, attributes);
47+
}
48+
49+
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>();
52+
53+
for(int i = 0; i < attributes.length; i+=2) {
54+
String key = attributes[i].toString();
55+
String value = attributes[i+1].toString();
56+
attr.put(key, value);
57+
}
58+
59+
return new RedisEdge(edgeId, src, dest, relation, attr);
60+
}
61+
62+
public ResultSet query(String query) {
63+
return client.query(this.graphID, query);
64+
}
65+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.redislabs.redisgraph;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
public class RedisNode {
8+
String id;
9+
String label;
10+
Map<String, String> attributes;
11+
12+
List<RedisEdge> incomingEdges;
13+
List<RedisEdge> outgoingEdges;
14+
15+
public RedisNode(String id, String label, Map<String, String> attributes) {
16+
this.id = id;
17+
this.label = label;
18+
this.attributes = attributes;
19+
this.incomingEdges = new ArrayList<RedisEdge>();
20+
this.outgoingEdges = new ArrayList<RedisEdge>();
21+
}
22+
23+
public String getId(){
24+
return this.id;
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.redislabs.redisgraph;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ResultSet {
7+
public int totalResults;
8+
public String[] header;
9+
public List<String[]> results;
10+
11+
public ResultSet(List<Object> resp) {
12+
13+
// Empty result set
14+
if(resp.size() == 0) {
15+
totalResults = 0;
16+
results = new ArrayList<String[]>(0);
17+
}
18+
19+
// First row is a header row
20+
String header_row = new String((byte[]) resp.get(0));
21+
header = header_row.split(",");
22+
totalResults = resp.size()-2;
23+
results = new ArrayList<String[]>(totalResults);
24+
25+
// Skips last row (runtime info)
26+
for (int i = 1; i < resp.size()-1; i++) {
27+
String row = new String((byte[]) resp.get(i));
28+
results.add(row.split(","));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)