Skip to content

Commit b1865bf

Browse files
author
DvirDukhan
committed
Added documentation, send compact command method, static instance to
access initialized instance. Added call procedure method, and calls to get graph labels, property names, and relationship types.
1 parent 31997c0 commit b1865bf

File tree

2 files changed

+203
-4
lines changed

2 files changed

+203
-4
lines changed

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

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

3-
import java.util.Collections;
4-
import java.util.HashMap;
5-
import java.util.Map;
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
66

77
import com.redislabs.redisgraph.impl.ResultSetImpl;
88
import org.apache.commons.text.translate.AggregateTranslator;
@@ -22,6 +22,13 @@ public class RedisGraphAPI {
2222

2323
private final Pool<Jedis> client;
2424
private final String graphId;
25+
private List<String> labels = new ArrayList<>();
26+
private List<String> relationshipTypes = new ArrayList<>();
27+
private List<String> propertyNames = new ArrayList<>();
28+
29+
private static RedisGraphAPI redisGraphAPI;
30+
31+
2532

2633
private static final CharSequenceTranslator ESCAPE_CHYPER;
2734
static {
@@ -60,6 +67,8 @@ public RedisGraphAPI(String graphId, String host, int port) {
6067
public RedisGraphAPI(String graphId, Pool<Jedis> jedis) {
6168
this.graphId = graphId;
6269
this.client = jedis;
70+
redisGraphAPI = this;
71+
6372
}
6473

6574
/**
@@ -70,6 +79,7 @@ public RedisGraphAPI(String graphId, Pool<Jedis> jedis) {
7079
* @return a result set
7180
*/
7281
public ResultSet query(String query, Object ...args) {
82+
StringBuilder sb = new StringBuilder();
7383
if(args.length > 0) {
7484
for(int i=0; i<args.length; ++i) {
7585
if(args[i] instanceof String) {
@@ -80,7 +90,7 @@ public ResultSet query(String query, Object ...args) {
8090
}
8191

8292
try (Jedis conn = getConnection()) {
83-
return new ResultSetImpl(sendCommand(conn, Command.QUERY, graphId, query).getObjectMultiBulkReply());
93+
return new ResultSetImpl(sendCompactCommand(conn, Command.QUERY, graphId, query).getObjectMultiBulkReply());
8494
}
8595
}
8696

@@ -96,13 +106,178 @@ public String deleteGraph() {
96106
}
97107
}
98108

109+
110+
/**
111+
* Sends command
112+
* @param conn
113+
* @param provider
114+
* @param args
115+
* @return
116+
*/
99117
private BinaryClient sendCommand(Jedis conn, ProtocolCommand provider, String ...args) {
100118
BinaryClient binaryClient = conn.getClient();
101119
binaryClient.sendCommand(provider, args);
102120
return binaryClient;
103121
}
122+
123+
124+
/**
125+
* Sends the command with --COMPACT flag
126+
* @param conn
127+
* @param provider
128+
* @param args
129+
* @return
130+
*/
131+
private BinaryClient sendCompactCommand(Jedis conn, ProtocolCommand provider, String ...args) {
132+
BinaryClient binaryClient = conn.getClient();
133+
List<String> largs = new ArrayList<>(Arrays.asList(args));
134+
largs.add("--COMPACT");
135+
String[] t = new String[largs.size()];
136+
binaryClient.sendCommand(provider, largs.toArray(t));
137+
return binaryClient;
138+
}
104139

105140
private Jedis getConnection() {
106141
return this.client.getResource();
107142
}
143+
144+
145+
/**
146+
* Invokes stored procedures without arguments
147+
* @param procedure procedure name to invoke
148+
* @return
149+
*/
150+
public ResultSet callProcedure(String procedure ){
151+
return callProcedure(procedure, new ArrayList<>(), new HashMap<>());
152+
153+
154+
}
155+
156+
157+
/**
158+
* Invokes stored procedure with arguments
159+
* @param procedure
160+
* @param args
161+
* @return
162+
*/
163+
public ResultSet callProcedure(String procedure, List<String> args ){
164+
return callProcedure(procedure, args, new HashMap<>());
165+
166+
167+
}
168+
169+
170+
/**
171+
*
172+
* @param index - index of label
173+
* @return requested label
174+
*/
175+
public String getLabel(int index){
176+
if (index >= labels.size()){
177+
labels = getLabels();
178+
}
179+
return labels.get(index);
180+
}
181+
182+
/**
183+
*
184+
* @return list of all the node labels in the graph
185+
*/
186+
private List<String> getLabels() {
187+
ResultSet resultSet = callProcedure("db.labels");
188+
ArrayList<String> labels = new ArrayList<>();
189+
while (resultSet.hasNext()){
190+
Record record = resultSet.next();
191+
labels.add(record.getString(0));
192+
193+
}
194+
return labels;
195+
}
196+
197+
198+
/**
199+
*
200+
* @param index index of the relationship type
201+
* @return requested relationship type
202+
*/
203+
public String getRelationshipType(int index){
204+
if (index >= relationshipTypes.size()){
205+
relationshipTypes = getRelationshipTypes();
206+
}
207+
return relationshipTypes.get(index);
208+
}
209+
210+
211+
/**
212+
*
213+
* @return a list of the edge relationship types in the graph
214+
*/
215+
private List<String> getRelationshipTypes() {
216+
ResultSet resultSet = callProcedure("db.relationshipTypes");
217+
ArrayList<String> relationshipTypes = new ArrayList<>();
218+
while (resultSet.hasNext()){
219+
Record record = resultSet.next();
220+
relationshipTypes.add(record.getString(0));
221+
222+
}
223+
return relationshipTypes;
224+
}
225+
226+
227+
/**
228+
*
229+
* @param index index of property name
230+
* @return requested property
231+
*/
232+
public String getPropertyName(int index){
233+
if (index >= propertyNames.size()){
234+
propertyNames = getPropertyNames();
235+
}
236+
return propertyNames.get(index);
237+
}
238+
239+
240+
/**
241+
*
242+
* @return a list of all the property names in the graph
243+
*/
244+
private List<String> getPropertyNames() {
245+
ResultSet resultSet = callProcedure("db.propertyKeys");
246+
ArrayList<String> propertyNames = new ArrayList<>();
247+
while (resultSet.hasNext()){
248+
Record record = resultSet.next();
249+
propertyNames.add(record.getString(0));
250+
251+
}
252+
return propertyNames;
253+
}
254+
255+
/**
256+
* Invoke a stored procedure
257+
* @param procedure
258+
* @param args
259+
* @param kwargs
260+
* @return
261+
*/
262+
public ResultSet callProcedure(String procedure, List<String> args , Map<String, List<String>> kwargs ){
263+
264+
args = args.stream().map( s -> Utils.quoteString(s)).collect(Collectors.toList());
265+
StringBuilder q = new StringBuilder();
266+
q.append(String.format("CALL %s(%s)", procedure, String.join(",", args)));
267+
List<String> y = kwargs.getOrDefault("y", null);
268+
if(y != null){
269+
q.append(String.join(",", y));
270+
}
271+
return query(q.toString());
272+
273+
274+
}
275+
276+
/**
277+
* static function to access an initialized graph api
278+
* @return an initialized instance of RedisGraphAPI
279+
*/
280+
public static RedisGraphAPI getInstance(){
281+
return redisGraphAPI;
282+
}
108283
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.redislabs.redisgraph;
2+
3+
/**
4+
* Utilities class
5+
*/
6+
public class Utils {
7+
8+
/**
9+
*
10+
* @param str - a string
11+
* @return the input string surounded with quotation marks, if needed
12+
*/
13+
public static String quoteString(String str){
14+
StringBuilder sb = new StringBuilder();
15+
if(str.charAt(0)!='"'){
16+
sb.append('"');
17+
}
18+
sb.append(str);
19+
if (str.charAt(str.length()-1)!= '"'){
20+
sb.append('"');
21+
}
22+
return sb.toString();
23+
}
24+
}

0 commit comments

Comments
 (0)