Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<distribution>repo</distribution>
</license>
</licenses>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -62,7 +62,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -179,7 +179,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.0</version>
</plugin>
</plugins>
</build>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.redislabs.redisgraph.RedisGraphAPI
Manifest-Version: 1.0
Main-Class: com.redislabs.redisgraph.RedisGraph

6 changes: 3 additions & 3 deletions src/main/java/com/redislabs/redisgraph/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import java.util.List;

/**
* Query response header interface. Represents the response schame (column names and types)
* Query response header interface. Represents the response schema (column names and types)
*/
public interface Header {


public enum ResultSetColumnTypes {
enum ResultSetColumnTypes {
COLUMN_UNKNOWN,
COLUMN_SCALAR,
COLUMN_NODE,
COLUMN_RELATION;
COLUMN_RELATION

}

Expand Down
168 changes: 15 additions & 153 deletions src/main/java/com/redislabs/redisgraph/RedisGraph.java
Original file line number Diff line number Diff line change
@@ -1,112 +1,27 @@
package com.redislabs.redisgraph;

import com.redislabs.redisgraph.impl.graph_cache.GraphCache;
import com.redislabs.redisgraph.impl.ResultSetImpl;
import org.apache.commons.text.translate.AggregateTranslator;
import org.apache.commons.text.translate.CharSequenceTranslator;
import org.apache.commons.text.translate.LookupTranslator;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.util.Pool;

import java.io.Closeable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;


/**
*
*/
public class RedisGraph implements Closeable {



private final Pool<Jedis> client;
private final Map<String, GraphCache> graphCaches = new ConcurrentHashMap<>();



private static final CharSequenceTranslator ESCAPE_CHYPER;
static {
final Map<CharSequence, CharSequence> escapeJavaMap = new HashMap<>();
escapeJavaMap.put("\'", "\\'");
escapeJavaMap.put("\"", "\\\"");
ESCAPE_CHYPER = new AggregateTranslator(new LookupTranslator(Collections.unmodifiableMap(escapeJavaMap)));
}

/**
* Creates a client running on the local machine

*/
public RedisGraph() {
this("localhost", 6379);
}

/**
* Creates a client running on the specific host/post
*
* @param host Redis host
* @param port Redis port
*/
public RedisGraph(String host, int port) {
this( new JedisPool(host, port));
}

/**
* Creates a client using provided Jedis pool
*
* @param jedis bring your own Jedis pool
*/
public RedisGraph( Pool<Jedis> jedis) {

this.client = jedis;
}

@Override
public void close(){
this.client.close();
}
import java.util.List;
import java.util.Map;

public interface RedisGraph extends Closeable {

/**
* Execute a Cypher query with arguments
*
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param args
* @return a result set
*/
public ResultSet query(String graphId, String query, Object ...args) {
if(args.length > 0) {
for(int i=0; i<args.length; ++i) {
if(args[i] instanceof String) {
args[i] = "\'" + ESCAPE_CHYPER.translate((String)args[i]) + "\'";
}
}
query = String.format(query, args);
}
graphCaches.putIfAbsent(graphId, new GraphCache(graphId, this));
List<Object> rawResponse = null;
try(Jedis conn = getConnection()){
rawResponse= sendCompactCommand(conn, Command.QUERY, graphId, query).getObjectMultiBulkReply();
}
return new ResultSetImpl(rawResponse, graphCaches.get(graphId));

}
ResultSet query(String graphId, String query, Object ...args);

/**
* Invokes stored procedures without arguments
* @param graphId a graph to perform the query on
* @param procedure procedure name to invoke
* @return result set with the procedure data
*/
public ResultSet callProcedure(String graphId, String procedure ){
return callProcedure(graphId, procedure, new ArrayList<>(), new HashMap<>());
}

ResultSet callProcedure(String graphId, String procedure);

/**
* Invokes stored procedure with arguments
Expand All @@ -115,76 +30,23 @@ public ResultSet callProcedure(String graphId, String procedure ){
* @param args procedure arguments
* @return result set with the procedure data
*/
public ResultSet callProcedure(String graphId, String procedure, List<String> args ){
return callProcedure(graphId, procedure, args, new HashMap<>());
}


/**
* Deletes the entire graph
*
* @return delete running time statistics
*/
public String deleteGraph(String graphId) {
//clear local state
graphCaches.remove(graphId);
try (Jedis conn = getConnection()) {
return sendCommand(conn, Command.DELETE, graphId).getBulkReply();
}

}


/**
* Sends command - will be replaced with sendCompactCommand once graph.delete support --compact flag
* @param conn - connection
* @param provider - command type
* @param args - command arguments
* @return
*/
private BinaryClient sendCommand(Jedis conn, ProtocolCommand provider, String ...args) {
BinaryClient binaryClient = conn.getClient();
binaryClient.sendCommand(provider, args);
return binaryClient;
}


/**
* Sends the command with --COMPACT flag
* @param conn - connection
* @param provider - command type
* @param args - command arguments
* @return
*/
private BinaryClient sendCompactCommand(Jedis conn, ProtocolCommand provider, String ...args) {
String[] t = new String[args.length +1];
System.arraycopy(args, 0 , t, 0, args.length);
t[args.length]="--COMPACT";
return sendCommand(conn, provider, t);
}

private Jedis getConnection() {
return this.client.getResource();
}

ResultSet callProcedure(String graphId, String procedure, List<String> args);

/**
* Invoke a stored procedure
* @param graphId a graph to perform the query on
* @param procedure - procedure to execute
* @param args - procedure arguments
* @param kwargs - procedure output arguments
* @return
* @return result set with the procedure data
*/
ResultSet callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs);

/**
* Deletes the entire graph
* @param graphId graph to delete
* @return delete running time statistics
*/
public ResultSet callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs ){
String deleteGraph(String graphId);

args = args.stream().map( s -> Utils.quoteString(s)).collect(Collectors.toList());
StringBuilder queryString = new StringBuilder();
queryString.append(String.format("CALL %s(%s)", procedure, String.join(",", args)));
List<String> kwargsList = kwargs.getOrDefault("y", null);
if(kwargsList != null){
queryString.append(String.join(",", kwargsList));
}
return query(graphId, queryString.toString());
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/redislabs/redisgraph/RedisGraphContexted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redislabs.redisgraph;

import redis.clients.jedis.Jedis;

public interface RedisGraphContexted extends RedisGraph {


/**
* Returns implementing class connection context
* @return Jedis connection
*/
Jedis getConnectionContext();

/**
* Returns a Redis transactional object, over the connection context, with graph API capabilities
* @return Redis transactional object, over the connection context, with graph API capabilities
*/
RedisGraphTransaction multi();

/**
* Perform watch over given Redis keys
* @param keys
* @return "OK"
*/
String watch(String... keys);

/**
* Removes watch from all keys
* @return
*/
String unwatch();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.redislabs.redisgraph;

public interface RedisGraphGeneralContext extends RedisGraph {

/**
* Generate a connection bounded api
* @return a connection bounded api
*/
RedisGraphContexted getContextedAPI();

}
91 changes: 91 additions & 0 deletions src/main/java/com/redislabs/redisgraph/RedisGraphTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.redislabs.redisgraph;

import redis.clients.jedis.Response;
import redis.clients.jedis.commands.BasicRedisPipeline;
import redis.clients.jedis.commands.BinaryRedisPipeline;
import redis.clients.jedis.commands.BinaryScriptingCommandsPipeline;
import redis.clients.jedis.commands.ClusterPipeline;
import redis.clients.jedis.commands.MultiKeyBinaryRedisPipeline;
import redis.clients.jedis.commands.MultiKeyCommandsPipeline;
import redis.clients.jedis.commands.RedisPipeline;
import redis.clients.jedis.commands.ScriptingCommandsPipeline;

import java.io.Closeable;
import java.util.List;
import java.util.Map;

/**
* An interface which aligned to Jedis transactional interface
*/
public interface RedisGraphTransaction extends
MultiKeyBinaryRedisPipeline,
MultiKeyCommandsPipeline, ClusterPipeline,
BinaryScriptingCommandsPipeline, ScriptingCommandsPipeline,
BasicRedisPipeline, BinaryRedisPipeline, RedisPipeline, Closeable {
/**
* Execute a Cypher query with arguments
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param args
* @return a response which builds the result set with the query answer
*/
Response<ResultSet> query(String graphId, String query, Object ...args);

/**
* Invokes stored procedures without arguments
* @param graphId a graph to perform the query on
* @param procedure procedure name to invoke
* @return a response which builds result set with the procedure data
*/
Response<ResultSet> callProcedure(String graphId, String procedure);

/**
* Invokes stored procedure with arguments
* @param graphId a graph to perform the query on
* @param procedure procedure name to invoke
* @param args procedure arguments
* @return a response which builds result set with the procedure data
*/
Response<ResultSet> callProcedure(String graphId, String procedure, List<String> args);

/**
* Invoke a stored procedure
* @param graphId a graph to perform the query on
* @param procedure - procedure to execute
* @param args - procedure arguments
* @param kwargs - procedure output arguments
* @return a response which builds result set with the procedure data
*/
Response<ResultSet> callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs);

/**
* Deletes the entire graph
* @param graphId graph to delete
* @return a response which builds the delete running time statistics
*/
Response<String> deleteGraph(String graphId);


/**
* executes the transaction
* @return a list of the executed transaction commands answers, in case of successful transaction, null otherwise
*/
List<Object> exec();

/**
* If object is in transaction mode,
* flushes all previously queued commands in a transaction and restores the connection state to normal
*/
void clear();

/**
*
* @return
*/
List<Response<?>> execGetResponse();

/**
* Flushes all previously queued commands in a transaction and restores the connection state to normal
*/
String discard();
}
Loading