Skip to content

Commit

Permalink
Merge pull request #12 from FalkorDB/refactor-driver
Browse files Browse the repository at this point in the history
refactor to driver
  • Loading branch information
gkorland authored Dec 12, 2023
2 parents ff8fc2c + f4831e4 commit d73062f
Show file tree
Hide file tree
Showing 25 changed files with 548 additions and 733 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,29 @@ package com.falkordb;
import com.falkordb.graph_entities.Edge;
import com.falkordb.graph_entities.Node;
import com.falkordb.graph_entities.Path;
import com.falkordb.impl.api.Graph;
import com.falkordb.Graph;
import com.falkordb.FalkorDB;
import com.falkordb.Driver;

import java.util.List;

public class GraphExample {
public static void main(String[] args) {

// general context api. Not bound to graph key or connection
Graph graph = new Graph();
Driver driver = FalkorDB.driver();
Graph graph = driver.graph("social");

Map<String, Object> params = new HashMap<>();
params.put("age", 30);
params.put("name", "amit");

// send queries to a specific graph called "social"
graph.query("social","CREATE (:person{name:'roi',age:32})");
graph.query("social","CREATE (:person{name:$name,age:$age})", params);
graph.query("social","MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
graph.query("CREATE (:person{name:'roi',age:32})");
graph.query("CREATE (:person{name:$name,age:$age})", params);
graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");

ResultSet resultSet = graph.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b");
ResultSet resultSet = graph.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b");
while(resultSet.hasNext()) {
Record record = resultSet.next();
// get values
Expand All @@ -83,7 +86,7 @@ public class GraphExample {
System.out.println(record.toString());
}

resultSet = graph.query("social", "MATCH p = (:person)-[:knows]->(:person) RETURN p");
resultSet = graph.query("MATCH p = (:person)-[:knows]->(:person) RETURN p");
while(resultSet.hasNext()) {
Record record = resultSet.next();
Path p = record.getValue("p");
Expand All @@ -93,26 +96,26 @@ public class GraphExample {
}

// delete graph
graph.deleteGraph("social");
graph.deleteGraph();

Graph contextGraph = driver.graph("contextSocial");
// get connection context - closable object
try(GraphContext context = graph.getContext()) {
context.query("contextSocial","CREATE (:person{name:'roi',age:32})");
context.query("social","CREATE (:person{name:$name,age:$age})", params);
context.query("contextSocial", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
try(GraphContext context = contextGraph.getContext()) {
context.query("CREATE (:person{name:'roi',age:32})");
context.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
// WATCH/MULTI/EXEC
context.watch("contextSocial");
context.watch();

GraphTransaction t = context.multi();
t.query("contextSocial", "MATCH (a:person)-[r:knows]->(b:person{name:$name,age:$age}) RETURN a, r, b", params);
t.query("MATCH (a:person)-[r:knows]->(b:person{name:$name,age:$age}) RETURN a, r, b", params);
// support for Redis/Jedis native commands in transaction
t.set("x", "1");
t.get("x");
// get multi/exec results
List<Object> execResults = t.exec();
System.out.println(execResults.toString());

context.deleteGraph("contextSocial");
context.deleteGraph();
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/falkordb/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.falkordb;

import java.io.Closeable;

import redis.clients.jedis.Jedis;

public interface Driver extends Closeable{
GraphContextGenerator graph(String graphId);

Jedis getConnection();
}
16 changes: 16 additions & 0 deletions src/main/java/com/falkordb/FalkorDB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.falkordb;

import com.falkordb.impl.api.DriverImpl;

final public class FalkorDB {

private FalkorDB() {}

public static Driver driver (){
return new DriverImpl();
}

public static Driver driver (String host, int port){
return new DriverImpl(host, port);
}
}
42 changes: 12 additions & 30 deletions src/main/java/com/falkordb/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,101 @@
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

public interface Graph extends Closeable {

static Graph with(Jedis jedis) {
return new com.falkordb.impl.api.Graph(jedis);
}

/**
* Execute a Cypher query.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @return a result set
*/
ResultSet query(String graphId, String query);
ResultSet query(String query);

/**
* Execute a Cypher read-only query.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @return a result set
*/
ResultSet readOnlyQuery(String graphId, String query);
ResultSet readOnlyQuery(String query);

/**
* Execute a Cypher query with timeout.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param timeout timeout in milliseconds
* @return a result set
*/
ResultSet query(String graphId, String query, long timeout);
ResultSet query(String query, long timeout);

/**
* Execute a Cypher read-only query with timeout.
* @param graphId a graph to perform the query on
* @param query Cypher query
* @param timeout timeout in milliseconds
* @return a result set
*/
ResultSet readOnlyQuery(String graphId, String query, long timeout);
ResultSet readOnlyQuery(String query, long timeout);

/**
* Executes a cypher query with parameters.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @return a result set.
*/
ResultSet query(String graphId, String query, Map<String, Object> params);
ResultSet query(String query, Map<String, Object> params);

/**
* Executes a cypher read-only query with parameters.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @return a result set.
*/
ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params);
ResultSet readOnlyQuery(String query, Map<String, Object> params);

/**
* Executes a cypher query with parameters and timeout.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @param timeout
* @return a result set.
*/
ResultSet query(String graphId, String query, Map<String, Object> params, long timeout);
ResultSet query(String query, Map<String, Object> params, long timeout);

/**
* Executes a cypher read-only query with parameters and timeout.
* @param graphId a graph to perform the query on.
* @param query Cypher query.
* @param params parameters map.
* @param timeout
* @return a result set.
*/
ResultSet readOnlyQuery(String graphId, String query, Map<String, Object> params, long timeout);
ResultSet readOnlyQuery(String query, Map<String, Object> params, long timeout);

/**
* 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
*/
ResultSet callProcedure(String graphId, String procedure);
ResultSet callProcedure(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 result set with the procedure data
*/
ResultSet callProcedure(String graphId, String procedure, List<String> args);
ResultSet callProcedure(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 result set with the procedure data
*/
ResultSet callProcedure(String graphId, String procedure, List<String> args , Map<String, List<String>> kwargs);
ResultSet callProcedure(String procedure, List<String> args , Map<String, List<String>> kwargs);

/**
* Deletes the entire graph
* @param graphId graph to delete
* @return delete running time statistics
*/
String deleteGraph(String graphId);
String deleteGraph();

@Override
void close();
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/falkordb/GraphContext.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package com.falkordb;

import redis.clients.jedis.Jedis;

public interface GraphContext extends Graph {


/**
* 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
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/falkordb/GraphContextGenerator.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.falkordb;

public interface GraphContextGenerator extends Graph {

/**
* Generate a connection bounded api
* @return a connection bounded api
*/
GraphContext getContext();

}
Loading

0 comments on commit d73062f

Please sign in to comment.