Skip to content

Commit 9657fe7

Browse files
committed
get all nodes and edges
1 parent a02c515 commit 9657fe7

File tree

4 files changed

+185
-96
lines changed

4 files changed

+185
-96
lines changed

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

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,67 +77,96 @@ public String connectNodes(String graph, String srcNodeID, String relation, Stri
7777
String edgeId = conn.getClient()
7878
.sendCommand(Commands.Command.ADDEDGE, stringArgs)
7979
.getBulkReply();
80+
8081
conn.close();
8182
return edgeId;
8283
}
8384

84-
public HashMap<String, String> getNode(String graphId, String id) {
85+
public List<HashMap<String, String>> getNodes(String graphId, Object... ids) {
8586
Jedis conn = _conn();
8687

8788
List<String> args = new ArrayList<String>(2);
8889
args.add(graphId);
89-
args.add(id);
90+
for(Object id: ids) {
91+
args.add(id.toString());
92+
}
9093

9194
String[] stringArgs = args.toArray(new String[args.size()]);
92-
List<String> properties;
95+
List<String> replay;
9396
try {
94-
properties = conn.getClient()
95-
.sendCommand(Commands.Command.GETNODE, stringArgs)
96-
.getMultiBulkReply();
97+
replay = conn.getClient()
98+
.sendCommand(Commands.Command.GETNODES, stringArgs)
99+
.getMultiBulkReply();
97100
} catch(ClassCastException e) {
98101
return null;
99102
}
100103

101-
HashMap<String, String> attributes = new HashMap<String, String>(properties.size()/2);
104+
List<HashMap<String, String>> nodes = new ArrayList<HashMap<String, String>>();
105+
106+
int numberOfNodes = Integer.parseInt(replay.get(replay.size()-1));
107+
int offset = 0;
102108

103-
for(int i = 0; i < properties.size(); i+=2) {
104-
String key = properties.get(i) ;
105-
String value = properties.get(i+1);
106-
attributes.put(key, value);
109+
for(int i = 0; i < numberOfNodes; i++) {
110+
int numberOfProperties = Integer.parseInt(replay.get(offset));
111+
offset++;
112+
113+
HashMap<String, String> nodeAttributes = new HashMap<String, String>(numberOfProperties/2);
114+
nodes.add(nodeAttributes);
115+
for(int j = 0; j < numberOfProperties; j+=2) {
116+
String key = replay.get(offset + j);
117+
String value = replay.get(offset + j + 1);
118+
nodeAttributes.put(key, value);
119+
}
120+
121+
offset += numberOfProperties;
107122
}
108123

109124
conn.close();
110-
return attributes;
125+
return nodes;
111126
}
112127

113-
public HashMap<String, String> getEdge(String graphId, String id) {
128+
public List<HashMap<String, String>> getEdges(String graphId, Object... ids) {
114129
Jedis conn = _conn();
115130

116131
List<String> args = new ArrayList<String>(2);
117132
args.add(graphId);
118-
args.add(id);
133+
for(Object id : ids) {
134+
args.add(id.toString());
135+
}
119136

120137
String[] stringArgs = args.toArray(new String[args.size()]);
121-
List<String> properties;
138+
List<String> replay;
122139

123140
try {
124-
properties = conn.getClient()
125-
.sendCommand(Commands.Command.GETEDGE, stringArgs)
126-
.getMultiBulkReply();
141+
replay = conn.getClient()
142+
.sendCommand(Commands.Command.GETEDGES, stringArgs)
143+
.getMultiBulkReply();
127144
} catch (ClassCastException e) {
128145
return null;
129146
}
130147

131-
HashMap<String, String> attributes = new HashMap<String, String>(properties.size()/2);
148+
List<HashMap<String, String>> edges = new ArrayList<HashMap<String, String>>();
132149

133-
for(int i = 0; i < properties.size(); i+=2) {
134-
String key = properties.get(i) ;
135-
String value = properties.get(i+1);
136-
attributes.put(key, value);
150+
int numberOfEdges = Integer.parseInt(replay.get(replay.size()-1));
151+
int offset = 0;
152+
153+
for(int i = 0; i < numberOfEdges; i++) {
154+
int numberOfProperties = Integer.parseInt(replay.get(offset));
155+
offset++;
156+
157+
HashMap<String, String> edgeAttributes = new HashMap<String, String>(numberOfProperties/2);
158+
edges.add(edgeAttributes);
159+
for(int j = 0; j < numberOfProperties; j+=2) {
160+
String key = replay.get(offset + j);
161+
String value = replay.get(offset + j + 1);
162+
edgeAttributes.put(key, value);
163+
}
164+
165+
offset += numberOfProperties;
137166
}
138167

139168
conn.close();
140-
return attributes;
169+
return edges;
141170
}
142171

143172
public List<String> getNodeEdges(String graphId, String nodeId, String edgeType, int direction) {
@@ -154,6 +183,7 @@ public List<String> getNodeEdges(String graphId, String nodeId, String edgeType,
154183
.sendCommand(Commands.Command.GETNODEEDGES, stringArgs)
155184
.getMultiBulkReply();
156185

186+
conn.close();
157187
return edges;
158188
}
159189

@@ -171,6 +201,7 @@ public List<String> getNeighbours(String graphId, String nodeId, String edgeType
171201
.sendCommand(Commands.Command.GETNEIGHBOURS, stringArgs)
172202
.getMultiBulkReply();
173203

204+
conn.close();
174205
return neighbours;
175206
}
176207

@@ -181,6 +212,7 @@ public ResultSet query(String graphId, String query) {
181212
.sendCommand(Commands.Command.QUERY, graphId, query)
182213
.getObjectMultiBulkReply();
183214

215+
conn.close();
184216
return new ResultSet(resp);
185217
}
186218

@@ -194,5 +226,6 @@ public void deleteGraph(String graph) {
194226
conn.getClient()
195227
.sendCommand(Commands.Command.DELETEGRAPH, graph)
196228
.getStatusCodeReply();
229+
conn.close();
197230
}
198231
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ 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"),
10+
11+
GETEDGES("graph.GETEDGES"),
12+
GETNODES("graph.GETNODES"),
13+
1214
GETNODEEDGES("graph.GETNODEEDGES"),
1315
GETNEIGHBOURS("graph.GETNEIGHBOURS"),
16+
1417
REMOVEEDGE("graph.REMOVEEDGE"),
1518
DELETEGRAPH("graph.DELETE"),
19+
1620
QUERY("graph.QUERY");
1721

1822
private final byte[] raw;

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

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,71 @@ public RedisNode createLabeledNode(String label, Object... attributes) {
4444
}
4545

4646
public RedisNode getNode(String id) {
47-
HashMap<String, String> attributes = client.getNode(this.graphId, id);
47+
List<RedisNode> node = getNodes(id);
48+
if(node != null && node.size() == 1) {
49+
return node.get(0);
50+
}
51+
return null;
52+
}
4853

49-
if(attributes == null) {
54+
public List<RedisNode> getNodes(Object... ids) {
55+
List<RedisNode> res = new ArrayList<RedisNode>();
56+
List<HashMap<String, String>> nodes = client.getNodes(this.graphId, ids);
57+
58+
if(nodes == null) {
5059
return null;
5160
}
5261

53-
String label = attributes.get("label");
54-
attributes.remove("label");
62+
// Scan through raw results
63+
for(HashMap<String, String> nodeAttributs: nodes) {
64+
65+
String id = nodeAttributs.get("id");
66+
nodeAttributs.remove("id");
67+
68+
String label = nodeAttributs.get("label");
69+
nodeAttributs.remove("label");
70+
71+
res.add(new RedisNode(id, label, nodeAttributs));
72+
}
5573

56-
return new RedisNode(id, label, attributes);
74+
return res;
5775
}
5876

5977
public RedisEdge getEdge(String id) {
60-
HashMap<String, String> attributes = client.getEdge(this.graphId, id);
78+
List<RedisEdge> edge = getEdges(id);
79+
if(edge != null && edge.size() == 1) {
80+
return edge.get(0);
81+
}
82+
return null;
83+
}
6184

62-
if(attributes == null) {
85+
public List<RedisEdge> getEdges(String... ids) {
86+
List<RedisEdge> res = new ArrayList<RedisEdge>();
87+
List<HashMap<String, String>> edges = client.getEdges(this.graphId, ids);
88+
89+
if(edges == null) {
6390
return null;
6491
}
6592

66-
String edgeId = attributes.get("id");
67-
String relation = attributes.get("type");
68-
String srcNodeId = attributes.get("src");
69-
String destNodeId = attributes.get("dest");
93+
for(HashMap<String, String> edgeAttributs: edges) {
94+
String edgeId = edgeAttributs.get("id");
95+
String relation = edgeAttributs.get("label");
96+
String srcNodeId = edgeAttributs.get("src");
97+
String destNodeId = edgeAttributs.get("dest");
7098

71-
attributes.remove("id");
72-
attributes.remove("type");
73-
attributes.remove("src");
74-
attributes.remove("dest");
99+
edgeAttributs.remove("id");
100+
edgeAttributs.remove("type");
101+
edgeAttributs.remove("src");
102+
edgeAttributs.remove("dest");
103+
edgeAttributs.remove("label");
75104

76-
RedisNode srcNode = getNode(srcNodeId);
77-
RedisNode destNode = getNode(destNodeId);
105+
List<RedisNode> nodes = getNodes(srcNodeId, destNodeId);
106+
RedisNode srcNode = nodes.get(0);
107+
RedisNode destNode = nodes.get(1);
78108

79-
return new RedisEdge(edgeId, srcNode, destNode, relation, attributes);
109+
res.add(new RedisEdge(edgeId, srcNode, destNode, relation, edgeAttributs));
110+
}
111+
return res;
80112
}
81113

82114
public List<RedisEdge> getNodeEdges(String nodeId, String edgeType, int direction) {
@@ -92,13 +124,7 @@ public List<RedisEdge> getNodeEdges(String nodeId, String edgeType, int directio
92124

93125
public List<RedisNode> getNeighbours(String nodeId, String edgeType, int direction) {
94126
List<String> nodeIds = client.getNeighbours(this.graphId, nodeId, edgeType, direction);
95-
ArrayList<RedisNode> nodes = new ArrayList<RedisNode>();
96-
97-
for(String id: nodeIds) {
98-
nodes.add(getNode(id));
99-
}
100-
101-
return nodes;
127+
return this.getNodes(nodeIds.toArray());
102128
}
103129

104130
public RedisEdge connectNodes(RedisNode src, String relation, RedisNode dest, Object... attributes) {

0 commit comments

Comments
 (0)