Skip to content

Commit 4eadb76

Browse files
authored
Merge pull request #35 from RedisGraph/new-resultset-structure
New resultset structure
2 parents 5b7d416 + f3ee260 commit 4eadb76

File tree

19 files changed

+1699
-301
lines changed

19 files changed

+1699
-301
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
docker:
99
- image: circleci/openjdk:8u171-jdk
1010

11-
- image: redislabs/redisgraph:edge
11+
- image: redislabs/redisgraph:2.0-edge
1212
port: 6379:6379
1313

1414
working_directory: ~/repo
@@ -44,7 +44,7 @@ jobs:
4444

4545
- run: bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
4646

47-
- run: mvn -s .circleci.settings.xml -DskipTests deploy
47+
- run: mvn -s .circleci.settings.xml -DskipTests deploy
4848

4949
workflows:
5050
version: 2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ hs_err_pid*
2929
.classpath
3030
.project
3131
/.settings/
32+
33+
#intelij
34+
.idea

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.redislabs</groupId>
88
<artifactId>jredisgraph</artifactId>
9-
<version>1.0.7-SNAPSHOT</version>
9+
<version>2.0.0-SNAPSHOT</version>
1010

1111
<name>JRedisGraph</name>
1212
<description>Official client for Redis-Graph</description>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.redislabs.redisgraph;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Query response header interface. Represents the response schame (column names and types)
7+
*/
8+
public interface Header {
9+
10+
11+
public enum ResultSetColumnTypes {
12+
COLUMN_UNKNOWN,
13+
COLUMN_SCALAR,
14+
COLUMN_NODE,
15+
COLUMN_RELATION;
16+
17+
}
18+
19+
20+
List<String> getSchemaNames();
21+
22+
List<ResultSetColumnTypes> getSchemaTypes();
23+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.redislabs.redisgraph;
2+
3+
import com.redislabs.redisgraph.impl.GraphCache;
4+
import com.redislabs.redisgraph.impl.ResultSetImpl;
5+
import org.apache.commons.text.translate.AggregateTranslator;
6+
import org.apache.commons.text.translate.CharSequenceTranslator;
7+
import org.apache.commons.text.translate.LookupTranslator;
8+
import redis.clients.jedis.BinaryClient;
9+
import redis.clients.jedis.Jedis;
10+
import redis.clients.jedis.JedisPool;
11+
import redis.clients.jedis.commands.ProtocolCommand;
12+
import redis.clients.jedis.util.Pool;
13+
14+
import java.io.Closeable;
15+
import java.util.*;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.stream.Collectors;
18+
19+
20+
/**
21+
*
22+
*/
23+
public class RedisGraph implements Closeable {
24+
25+
26+
27+
private final Pool<Jedis> client;
28+
private final Map<String, GraphCache> graphCaches = new ConcurrentHashMap<>();
29+
30+
31+
32+
private static final CharSequenceTranslator ESCAPE_CHYPER;
33+
static {
34+
final Map<CharSequence, CharSequence> escapeJavaMap = new HashMap<>();
35+
escapeJavaMap.put("\'", "\\'");
36+
escapeJavaMap.put("\"", "\\\"");
37+
ESCAPE_CHYPER = new AggregateTranslator(new LookupTranslator(Collections.unmodifiableMap(escapeJavaMap)));
38+
}
39+
40+
/**
41+
* Creates a client running on the local machine
42+
43+
*/
44+
public RedisGraph() {
45+
this("localhost", 6379);
46+
}
47+
48+
/**
49+
* Creates a client running on the specific host/post
50+
*
51+
* @param host Redis host
52+
* @param port Redis port
53+
*/
54+
public RedisGraph(String host, int port) {
55+
this( new JedisPool(host, port));
56+
}
57+
58+
/**
59+
* Creates a client using provided Jedis pool
60+
*
61+
* @param jedis bring your own Jedis pool
62+
*/
63+
public RedisGraph( Pool<Jedis> jedis) {
64+
65+
this.client = jedis;
66+
}
67+
68+
@Override
69+
public void close(){
70+
this.client.close();
71+
}
72+
73+
74+
/**
75+
* Execute a Cypher query with arguments
76+
*
77+
* @param graphId a graph to perform the query on
78+
* @param query Cypher query
79+
* @param args
80+
* @return a result set
81+
*/
82+
public ResultSet query(String graphId, String query, Object ...args) {
83+
if(args.length > 0) {
84+
for(int i=0; i<args.length; ++i) {
85+
if(args[i] instanceof String) {
86+
args[i] = "\'" + ESCAPE_CHYPER.translate((String)args[i]) + "\'";
87+
}
88+
}
89+
query = String.format(query, args);
90+
}
91+
graphCaches.putIfAbsent(graphId, new GraphCache(graphId, this));
92+
List<Object> rawResponse = null;
93+
try(Jedis conn = getConnection()){
94+
rawResponse= sendCompactCommand(conn, Command.QUERY, graphId, query).getObjectMultiBulkReply();
95+
}
96+
return new ResultSetImpl(rawResponse, graphCaches.get(graphId));
97+
98+
}
99+
100+
/**
101+
* Invokes stored procedures without arguments
102+
* @param graphId a graph to perform the query on
103+
* @param procedure procedure name to invoke
104+
* @return result set with the procedure data
105+
*/
106+
public ResultSet callProcedure(String graphId, String procedure ){
107+
return callProcedure(graphId, procedure, new ArrayList<>(), new HashMap<>());
108+
}
109+
110+
111+
/**
112+
* Invokes stored procedure with arguments
113+
* @param graphId a graph to perform the query on
114+
* @param procedure procedure name to invoke
115+
* @param args procedure arguments
116+
* @return result set with the procedure data
117+
*/
118+
public ResultSet callProcedure(String graphId, String procedure, List<String> args ){
119+
return callProcedure(graphId, procedure, args, new HashMap<>());
120+
}
121+
122+
123+
/**
124+
* Deletes the entire graph
125+
*
126+
* @return delete running time statistics
127+
*/
128+
public String deleteGraph(String graphId) {
129+
//clear local state
130+
graphCaches.remove(graphId);
131+
try (Jedis conn = getConnection()) {
132+
return sendCommand(conn, Command.DELETE, graphId).getBulkReply();
133+
}
134+
135+
}
136+
137+
138+
/**
139+
* Sends command - will be replaced with sendCompactCommand once graph.delete support --compact flag
140+
* @param conn - connection
141+
* @param provider - command type
142+
* @param args - command arguments
143+
* @return
144+
*/
145+
private BinaryClient sendCommand(Jedis conn, ProtocolCommand provider, String ...args) {
146+
BinaryClient binaryClient = conn.getClient();
147+
binaryClient.sendCommand(provider, args);
148+
return binaryClient;
149+
}
150+
151+
152+
/**
153+
* Sends the command with --COMPACT flag
154+
* @param conn - connection
155+
* @param provider - command type
156+
* @param args - command arguments
157+
* @return
158+
*/
159+
private BinaryClient sendCompactCommand(Jedis conn, ProtocolCommand provider, String ...args) {
160+
String[] t = new String[args.length +1];
161+
System.arraycopy(args, 0 , t, 0, args.length);
162+
t[args.length]="--COMPACT";
163+
return sendCommand(conn, provider, t);
164+
}
165+
166+
private Jedis getConnection() {
167+
return this.client.getResource();
168+
}
169+
170+
171+
/**
172+
* Invoke a stored procedure
173+
* @param graphId a graph to perform the query on
174+
* @param procedure - procedure to execute
175+
* @param args - procedure arguments
176+
* @param kwargs - procedure output arguments
177+
* @return
178+
*/
179+
public ResultSet callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs ){
180+
181+
args = args.stream().map( s -> Utils.quoteString(s)).collect(Collectors.toList());
182+
StringBuilder queryString = new StringBuilder();
183+
queryString.append(String.format("CALL %s(%s)", procedure, String.join(",", args)));
184+
List<String> kwargsList = kwargs.getOrDefault("y", null);
185+
if(kwargsList != null){
186+
queryString.append(String.join(",", kwargsList));
187+
}
188+
return query(graphId, queryString.toString());
189+
}
190+
}

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

Lines changed: 0 additions & 108 deletions
This file was deleted.

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
/**
77
* Hold a query result
88
*/
9-
public interface ResultSet extends Iterator<Record>{
10-
/**
11-
* Return the query statistics
12-
* @return statistics object
13-
*/
14-
Statistics getStatistics();
9+
public interface ResultSet extends Iterator<Record> {
1510

16-
List<String> getHeader();
17-
}
11+
public enum ResultSetScalarTypes {
12+
PROPERTY_UNKNOWN,
13+
PROPERTY_NULL,
14+
PROPERTY_STRING,
15+
PROPERTY_INTEGER,
16+
PROPERTY_BOOLEAN,
17+
PROPERTY_DOUBLE,
18+
}
19+
20+
public int size();
21+
22+
Statistics getStatistics();
23+
24+
Header getHeader();
25+
26+
}

0 commit comments

Comments
 (0)