From 0bb76dba025dec0848138f0bba76f4ccb98676e7 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Tue, 12 Dec 2023 00:09:23 +0200 Subject: [PATCH] refactor to driver --- src/main/java/com/falkordb/Driver.java | 11 ++ src/main/java/com/falkordb/FalkorDB.java | 14 ++ src/main/java/com/falkordb/Graph.java | 42 ++--- src/main/java/com/falkordb/GraphContext.java | 9 - .../com/falkordb/GraphContextGenerator.java | 2 - src/main/java/com/falkordb/GraphPipeline.java | 41 ++--- .../java/com/falkordb/GraphTransaction.java | 36 ++-- .../com/falkordb/impl/api/AbstractGraph.java | 72 +++----- .../com/falkordb/impl/api/ContextedGraph.java | 111 +++++------- .../java/com/falkordb/impl/api/Driver.java | 45 +++++ .../java/com/falkordb/impl/api/Graph.java | 135 +++++---------- .../falkordb/impl/api/GraphCacheHolder.java | 8 - .../com/falkordb/impl/api/GraphPipeline.java | 77 ++++----- .../falkordb/impl/api/GraphTransaction.java | 77 ++++----- .../falkordb/impl/graph_cache/GraphCache.java | 16 +- .../impl/graph_cache/GraphCacheList.java | 13 +- .../impl/graph_cache/GraphCaches.java | 59 ------- src/test/java/com/falkordb/GraphAPITest.java | 163 ++++++++---------- .../java/com/falkordb/InstantiationTest.java | 27 +-- src/test/java/com/falkordb/IterableTest.java | 20 +-- src/test/java/com/falkordb/PipelineTest.java | 38 ++-- .../java/com/falkordb/TransactionTest.java | 44 +++-- .../falkordb/exceptions/GraphErrorTest.java | 32 ++-- 23 files changed, 448 insertions(+), 644 deletions(-) create mode 100644 src/main/java/com/falkordb/Driver.java create mode 100644 src/main/java/com/falkordb/FalkorDB.java create mode 100644 src/main/java/com/falkordb/impl/api/Driver.java delete mode 100644 src/main/java/com/falkordb/impl/api/GraphCacheHolder.java delete mode 100644 src/main/java/com/falkordb/impl/graph_cache/GraphCaches.java diff --git a/src/main/java/com/falkordb/Driver.java b/src/main/java/com/falkordb/Driver.java new file mode 100644 index 0000000..8e618d9 --- /dev/null +++ b/src/main/java/com/falkordb/Driver.java @@ -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(); +} diff --git a/src/main/java/com/falkordb/FalkorDB.java b/src/main/java/com/falkordb/FalkorDB.java new file mode 100644 index 0000000..8ba36ab --- /dev/null +++ b/src/main/java/com/falkordb/FalkorDB.java @@ -0,0 +1,14 @@ +package com.falkordb; + +final public class FalkorDB { + + private FalkorDB() {} + + public static Driver driver (){ + return new com.falkordb.impl.api.Driver(); + } + + public static Driver driver (String host, int port){ + return new com.falkordb.impl.api.Driver(host, port); + } +} diff --git a/src/main/java/com/falkordb/Graph.java b/src/main/java/com/falkordb/Graph.java index d91d97e..b40191d 100644 --- a/src/main/java/com/falkordb/Graph.java +++ b/src/main/java/com/falkordb/Graph.java @@ -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 params); + ResultSet query(String query, Map 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 params); + ResultSet readOnlyQuery(String query, Map 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 params, long timeout); + ResultSet query(String query, Map 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 params, long timeout); + ResultSet readOnlyQuery(String query, Map 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 args); + ResultSet callProcedure(String procedure, List 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 args , Map> kwargs); + ResultSet callProcedure(String procedure, List args , Map> kwargs); /** * Deletes the entire graph - * @param graphId graph to delete * @return delete running time statistics */ - String deleteGraph(String graphId); + String deleteGraph(); @Override void close(); diff --git a/src/main/java/com/falkordb/GraphContext.java b/src/main/java/com/falkordb/GraphContext.java index f44d585..cb77de7 100644 --- a/src/main/java/com/falkordb/GraphContext.java +++ b/src/main/java/com/falkordb/GraphContext.java @@ -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 diff --git a/src/main/java/com/falkordb/GraphContextGenerator.java b/src/main/java/com/falkordb/GraphContextGenerator.java index 9a77af7..e977689 100644 --- a/src/main/java/com/falkordb/GraphContextGenerator.java +++ b/src/main/java/com/falkordb/GraphContextGenerator.java @@ -1,11 +1,9 @@ package com.falkordb; public interface GraphContextGenerator extends Graph { - /** * Generate a connection bounded api * @return a connection bounded api */ GraphContext getContext(); - } diff --git a/src/main/java/com/falkordb/GraphPipeline.java b/src/main/java/com/falkordb/GraphPipeline.java index 5518409..209554b 100644 --- a/src/main/java/com/falkordb/GraphPipeline.java +++ b/src/main/java/com/falkordb/GraphPipeline.java @@ -25,109 +25,97 @@ public interface GraphPipeline extends /** * Execute a Cypher query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ - Response query(String graphId, String query); + Response query(String query); /** * Execute a Cypher read-only query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query); + Response readOnlyQuery(String query); /** * Execute a Cypher query with timeout. - * @param graphId a graph to perform the query on * @param query Cypher query * @param timeout * @return a response which builds the result set with the query answer. */ - Response query(String graphId, String query, long timeout); + Response 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 * @return a response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query, long timeout); + Response 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 response which builds the result set with the query answer. */ - Response query(String graphId, String query, Map params); + Response query(String query, Map 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 response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query, Map params); + Response readOnlyQuery(String query, Map 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 response which builds the result set with the query answer. */ - Response query(String graphId, String query, Map params, long timeout); + Response query(String query, Map 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 response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query, Map params, long timeout); + Response readOnlyQuery(String query, Map params, long timeout); /** * 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 callProcedure(String graphId, String procedure); + Response 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 a response which builds result set with the procedure data */ - Response callProcedure(String graphId, String procedure, List args); + Response callProcedure(String procedure, List 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 callProcedure(String graphId, String procedure, List args , Map> kwargs); + Response callProcedure(String procedure, List args , Map> kwargs); /** * Deletes the entire graph - * @param graphId graph to delete * @return a response which builds the delete running time statistics */ - Response deleteGraph(String graphId); + Response deleteGraph(); /** @@ -143,8 +131,7 @@ public interface GraphPipeline extends * get return values from pipelined commands, capture the different Response<?> of the * commands you execute. */ - public void sync(); - + void sync(); /** * Blocks until all the previous write commands are successfully transferred and acknowledged by @@ -156,5 +143,5 @@ public interface GraphPipeline extends * @return the number of replicas reached by all the writes performed in the context of the * current connection */ - public Response waitReplicas(int replicas, long timeout); + Response waitReplicas(int replicas, long timeout); } diff --git a/src/main/java/com/falkordb/GraphTransaction.java b/src/main/java/com/falkordb/GraphTransaction.java index dbf2dc6..acf6f21 100644 --- a/src/main/java/com/falkordb/GraphTransaction.java +++ b/src/main/java/com/falkordb/GraphTransaction.java @@ -25,109 +25,97 @@ public interface GraphTransaction extends /** * Execute a Cypher query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ - Response query(String graphId, String query); + Response query(String query); /** * Execute a Cypher read-only query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query); + Response readOnlyQuery(String query); /** * Execute a Cypher query with timeout. - * @param graphId a graph to perform the query on * @param query Cypher query * @param timeout * @return a response which builds the result set with the query answer. */ - Response query(String graphId, String query, long timeout); + Response 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 * @return a response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query, long timeout); + Response 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 response which builds the result set with the query answer. */ - Response query(String graphId, String query, Map params); + Response query(String query, Map 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 response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query, Map params); + Response readOnlyQuery(String query, Map 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 response which builds the result set with the query answer. */ - Response query(String graphId, String query, Map params, long timeout); + Response query(String query, Map 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 response which builds the result set with the query answer. */ - Response readOnlyQuery(String graphId, String query, Map params, long timeout); + Response readOnlyQuery(String query, Map params, long timeout); /** * 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 callProcedure(String graphId, String procedure); + Response 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 a response which builds result set with the procedure data */ - Response callProcedure(String graphId, String procedure, List args); + Response callProcedure(String procedure, List 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 callProcedure(String graphId, String procedure, List args , Map> kwargs); + Response callProcedure(String procedure, List args , Map> kwargs); /** * Deletes the entire graph - * @param graphId graph to delete * @return a response which builds the delete running time statistics */ - Response deleteGraph(String graphId); + Response deleteGraph(); /** diff --git a/src/main/java/com/falkordb/impl/api/AbstractGraph.java b/src/main/java/com/falkordb/impl/api/AbstractGraph.java index 9b3f4d0..f0f305a 100644 --- a/src/main/java/com/falkordb/impl/api/AbstractGraph.java +++ b/src/main/java/com/falkordb/impl/api/AbstractGraph.java @@ -3,8 +3,6 @@ import com.falkordb.Graph; import com.falkordb.ResultSet; import com.falkordb.impl.Utils; -import redis.clients.jedis.Jedis; - import java.util.List; import java.util.Map; @@ -13,159 +11,141 @@ */ public abstract class AbstractGraph implements Graph { - /** - * Inherited classes should return a Jedis connection, with respect to their context - * @return Jedis connection - */ - protected abstract Jedis getConnection(); - /** * Sends a query to the redis graph. Implementation and context dependent - * @param graphId graph to be queried * @param preparedQuery prepared query * @return Result set */ - protected abstract ResultSet sendQuery(String graphId, String preparedQuery); + protected abstract ResultSet sendQuery(String preparedQuery); /** * Sends a read-only query to the redis graph. Implementation and context dependent - * @param graphId graph to be queried * @param preparedQuery prepared query * @return Result set */ - protected abstract ResultSet sendReadOnlyQuery(String graphId, String preparedQuery); + protected abstract ResultSet sendReadOnlyQuery(String preparedQuery); /** * Sends a query to the redis graph.Implementation and context dependent - * @param graphId graph to be queried * @param preparedQuery prepared query * @param timeout * @return Result set */ - protected abstract ResultSet sendQuery(String graphId, String preparedQuery, long timeout); + protected abstract ResultSet sendQuery(String preparedQuery, long timeout); /** * Sends a read-query to the redis graph.Implementation and context dependent - * @param graphId graph to be queried * @param preparedQuery prepared query * @param timeout * @return Result set */ - protected abstract ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout); + protected abstract ResultSet sendReadOnlyQuery(String preparedQuery, long timeout); /** * Execute a Cypher query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a result set */ @Override - public ResultSet query(String graphId, String query) { - return sendQuery(graphId, query); + public ResultSet query(String query) { + return sendQuery(query); } /** * Execute a Cypher read-only query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a result set */ @Override - public ResultSet readOnlyQuery(String graphId, String query) { - return sendReadOnlyQuery(graphId, query); + public ResultSet readOnlyQuery(String query) { + return sendReadOnlyQuery(query); } /** * Execute a Cypher query with timeout. - * @param graphId a graph to perform the query on * @param timeout * @param query Cypher query * @return a result set */ @Override - public ResultSet query(String graphId, String query, long timeout) { - return sendQuery(graphId, query, timeout); + public ResultSet query(String query, long timeout) { + return sendQuery(query, timeout); } /** * Execute a Cypher read-only query with timeout. - * @param graphId a graph to perform the query on * @param timeout * @param query Cypher query * @return a result set */ @Override - public ResultSet readOnlyQuery(String graphId, String query, long timeout) { - return sendReadOnlyQuery(graphId, query, timeout); + public ResultSet readOnlyQuery(String query, long timeout) { + return sendReadOnlyQuery(query, 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. */ - public ResultSet query(String graphId, String query, Map params) { + public ResultSet query(String query, Map params) { String preparedQuery = Utils.prepareQuery(query, params); - return sendQuery(graphId, preparedQuery); + return sendQuery(preparedQuery); } /** * 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. */ @Override - public ResultSet readOnlyQuery(String graphId, String query, Map params) { + public ResultSet readOnlyQuery(String query, Map params) { String preparedQuery = Utils.prepareQuery(query, params); - return sendReadOnlyQuery(graphId, preparedQuery); + return sendReadOnlyQuery(preparedQuery); } /** * Executes a cypher query with parameters and timeout. - * @param graphId a graph to perform the query on. * @param timeout * @param query Cypher query. * @param params parameters map. * @return a result set. */ @Override - public ResultSet query(String graphId, String query, Map params, long timeout) { + public ResultSet query(String query, Map params, long timeout) { String preparedQuery = Utils.prepareQuery(query, params); - return sendQuery(graphId, preparedQuery, timeout); + return sendQuery(preparedQuery, timeout); } /** * Executes a cypher read-only query with parameters and timeout. - * @param graphId a graph to perform the query on. * @param timeout * @param query Cypher query. * @param params parameters map. * @return a result set. */ @Override - public ResultSet readOnlyQuery(String graphId, String query, Map params, long timeout) { + public ResultSet readOnlyQuery(String query, Map params, long timeout) { String preparedQuery = Utils.prepareQuery(query, params); - return sendReadOnlyQuery(graphId, preparedQuery, timeout); + return sendReadOnlyQuery(preparedQuery, timeout); } @Override - public ResultSet callProcedure(String graphId, String procedure){ - return callProcedure(graphId, procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); + public ResultSet callProcedure(String procedure){ + return callProcedure(procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); } @Override - public ResultSet callProcedure(String graphId, String procedure, List args){ - return callProcedure(graphId, procedure, args, Utils.DUMMY_MAP); + public ResultSet callProcedure(String procedure, List args){ + return callProcedure(procedure, args, Utils.DUMMY_MAP); } @Override - public ResultSet callProcedure(String graphId, String procedure, List args , Map> kwargs){ + public ResultSet callProcedure(String procedure, List args , Map> kwargs){ String preparedProcedure = Utils.prepareProcedure(procedure, args, kwargs); - return query(graphId, preparedProcedure); + return query(preparedProcedure); } } diff --git a/src/main/java/com/falkordb/impl/api/ContextedGraph.java b/src/main/java/com/falkordb/impl/api/ContextedGraph.java index b86528b..2e3336a 100644 --- a/src/main/java/com/falkordb/impl/api/ContextedGraph.java +++ b/src/main/java/com/falkordb/impl/api/ContextedGraph.java @@ -6,7 +6,7 @@ import com.falkordb.ResultSet; import com.falkordb.exceptions.GraphException; import com.falkordb.impl.Utils; -import com.falkordb.impl.graph_cache.GraphCaches; +import com.falkordb.impl.graph_cache.GraphCache; import com.falkordb.impl.resultset.ResultSetImpl; import redis.clients.jedis.Client; @@ -18,41 +18,33 @@ * An implementation of GraphContext. Allows sending Graph and some Redis commands, * within a specific connection context */ -public class ContextedGraph extends AbstractGraph implements GraphContext, GraphCacheHolder { +public class ContextedGraph extends AbstractGraph implements GraphContext { - private final Jedis connectionContext; - private GraphCaches caches; + private final Jedis connection; + private final String graphId; + private GraphCache cache; /** * Generates a new instance with a specific Jedis connection * @param connectionContext */ - public ContextedGraph(Jedis connectionContext) { - this.connectionContext = connectionContext; - } - - /** - * Overrides the abstract method. Return the instance only connection - * @return - */ - @Override - protected Jedis getConnection() { - return this.connectionContext; + public ContextedGraph(Jedis connection, GraphCache cache, String graphId) { + this.connection = connection; + this.graphId = graphId; + this.cache = cache; } /** * Sends the query over the instance only connection - * @param graphId graph to be queried * @param preparedQuery prepared query * @return Result set with the query answer */ @Override - protected ResultSet sendQuery(String graphId, String preparedQuery) { - Jedis conn = getConnection(); + protected ResultSet sendQuery(String preparedQuery) { try { @SuppressWarnings("unchecked") - List rawResponse = (List) conn.sendCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); - return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId)); + List rawResponse = (List) connection.sendCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); + return new ResultSetImpl(rawResponse, this, this.cache); } catch (GraphException rt) { throw rt; } catch (JedisDataException j) { @@ -62,17 +54,15 @@ protected ResultSet sendQuery(String graphId, String preparedQuery) { /** * Sends the read-only query over the instance only connection - * @param graphId graph to be queried * @param preparedQuery prepared query * @return Result set with the query answer */ @Override - protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) { - Jedis conn = getConnection(); + protected ResultSet sendReadOnlyQuery(String preparedQuery) { try { @SuppressWarnings("unchecked") - List rawResponse = (List) conn.sendCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); - return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId)); + List rawResponse = (List) connection.sendCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); + return new ResultSetImpl(rawResponse, this, this.cache); } catch (GraphException ge) { throw ge; } catch (JedisDataException de) { @@ -82,19 +72,17 @@ protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery) { /** * Sends the query over the instance only connection - * @param graphId graph to be queried * @param timeout * @param preparedQuery prepared query * @return Result set with the query answer */ @Override - protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout) { - Jedis conn = getConnection(); + protected ResultSet sendQuery(String preparedQuery, long timeout) { try { @SuppressWarnings("unchecked") - List rawResponse = (List) conn.sendBlockingCommand(GraphCommand.QUERY, + List rawResponse = (List) connection.sendBlockingCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); - return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId)); + return new ResultSetImpl(rawResponse, this, this.cache); } catch (GraphException rt) { throw rt; } catch (JedisDataException j) { @@ -104,19 +92,17 @@ protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout /** * Sends the read-only query over the instance only connection - * @param graphId graph to be queried * @param timeout * @param preparedQuery prepared query * @return Result set with the query answer */ @Override - protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout) { - Jedis conn = getConnection(); + protected ResultSet sendReadOnlyQuery(String preparedQuery, long timeout) { try { @SuppressWarnings("unchecked") - List rawResponse = (List) conn.sendBlockingCommand(GraphCommand.RO_QUERY, + List rawResponse = (List) connection.sendBlockingCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); - return new ResultSetImpl(rawResponse, this, caches.getGraphCache(graphId)); + return new ResultSetImpl(rawResponse, this, this.cache); } catch (GraphException ge) { throw ge; } catch (JedisDataException de) { @@ -124,27 +110,16 @@ protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long } } - /** - * @return Returns the instance Jedis connection. - */ - @Override - public Jedis getConnectionContext() { - return this.connectionContext; - } - /** * Creates a new GraphTransaction transactional object * @return new GraphTransaction */ @Override public GraphTransaction multi() { - Jedis jedis = getConnection(); - Client client = jedis.getClient(); + Client client = connection.getClient(); client.multi(); client.getOne(); - GraphTransaction transaction = new GraphTransaction(client, this); - transaction.setGraphCaches(caches); - return transaction; + return new GraphTransaction(client, this, this.cache, this.graphId); } /** @@ -153,11 +128,8 @@ public GraphTransaction multi() { */ @Override public GraphPipeline pipelined() { - Jedis jedis = getConnection(); - Client client = jedis.getClient(); - GraphPipeline pipeline = new GraphPipeline(client, this); - pipeline.setGraphCaches(caches); - return pipeline; + Client client = connection.getClient(); + return new GraphPipeline(client, this, this.cache, this.graphId); } /** @@ -167,7 +139,7 @@ public GraphPipeline pipelined() { */ @Override public String watch(String... keys) { - return this.getConnection().watch(keys); + return connection.watch(keys); } /** @@ -176,26 +148,25 @@ public String watch(String... keys) { */ @Override public String unwatch() { - return this.getConnection().unwatch(); + return connection.unwatch(); } /** * Deletes the entire graph - * @param graphId graph to delete * @return delete running time statistics */ @Override - public String deleteGraph(String graphId) { - Jedis conn = getConnection(); + public String deleteGraph() { Object response; try { - response = conn.sendCommand(GraphCommand.DELETE, graphId); + response = connection.sendCommand(GraphCommand.DELETE, graphId); } catch (Exception e) { - conn.close(); + connection.close(); throw e; } //clear local state - caches.removeGraphCache(graphId); + this.cache.clear(); + // caches.removeGraphCache(graphId); return SafeEncoder.encode((byte[]) response); } @@ -204,13 +175,21 @@ public String deleteGraph(String graphId) { */ @Override public void close() { - this.connectionContext.close(); - + this.connection.close(); } @Override - public void setGraphCaches(GraphCaches caches) { - this.caches = caches; - } + public boolean equals(Object o) { + if (o == this) { + return true; + } + + if (!(o instanceof ContextedGraph)) { + return false; + } + + final ContextedGraph other = (ContextedGraph) o; + return this.connection == other.connection; + } } diff --git a/src/main/java/com/falkordb/impl/api/Driver.java b/src/main/java/com/falkordb/impl/api/Driver.java new file mode 100644 index 0000000..c86b44f --- /dev/null +++ b/src/main/java/com/falkordb/impl/api/Driver.java @@ -0,0 +1,45 @@ +package com.falkordb.impl.api; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.util.Pool; + +public class Driver implements com.falkordb.Driver { + + private final Pool pool; + + /** + * Creates a client running on the local machine + */ + public Driver() { + this("localhost", 6379); + } + + /** + * Creates a client running on the specific host/post + * + * @param host Redis host + * @param port Redis port + */ + public Driver(String host, int port) { + this.pool = new JedisPool(host, port); + } + + @Override + public Graph graph(String graphId) { + return new Graph(this, graphId); + } + + @Override + public Jedis getConnection() { + return pool.getResource(); + } + + /** + * Closes the Jedis pool + */ + @Override + public void close() { + pool.close(); + } +} diff --git a/src/main/java/com/falkordb/impl/api/Graph.java b/src/main/java/com/falkordb/impl/api/Graph.java index d744e80..e58cf09 100644 --- a/src/main/java/com/falkordb/impl/api/Graph.java +++ b/src/main/java/com/falkordb/impl/api/Graph.java @@ -3,10 +3,8 @@ import com.falkordb.GraphContext; import com.falkordb.GraphContextGenerator; import com.falkordb.ResultSet; -import com.falkordb.impl.graph_cache.GraphCaches; +import com.falkordb.impl.graph_cache.GraphCache; import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.util.Pool; import redis.clients.jedis.util.SafeEncoder; /** @@ -14,150 +12,109 @@ */ public class Graph extends AbstractGraph implements GraphContextGenerator { - private final Pool pool; - private final Jedis jedis; - private final GraphCaches caches = new GraphCaches(); + private final Driver driver; + private final String graphId; + private final GraphCache cache; /** * Creates a client running on the local machine - - */ - public Graph() { - this("localhost", 6379); - } - - /** - * Creates a client running on the specific host/post - * - * @param host Redis host - * @param port Redis port - */ - public Graph(String host, int port) { - this(new JedisPool(host, port)); - } - - /** - * Creates a client using provided Jedis pool - * - * @param pool bring your own Jedis pool - */ - public Graph(Pool pool) { - this.pool = pool; - this.jedis = null; - } - - public Graph(Jedis jedis) { - this.jedis = jedis; - this.pool = null; - } - - /** - * Overrides the abstract function. Gets and returns a Jedis connection from the Jedis pool - * @return a Jedis connection */ - @Override - protected Jedis getConnection() { - return jedis != null ? jedis : pool.getResource(); + public Graph(Driver driver, String graphId) { + this.driver = driver; + this.graphId = graphId; + this.cache = new GraphCache(); } /** * Overrides the abstract function. - * Sends the query from any Jedis connection received from the Jedis pool and closes it once done - * @param graphId graph to be queried + * Sends the query from any Jedis connection received from the Jedis pool and + * closes it once done + * * @param preparedQuery prepared query * @return Result set with the query answer */ @Override - protected ResultSet sendQuery(String graphId, String preparedQuery){ - try (ContextedGraph contextedGraph = new ContextedGraph(getConnection())) { - contextedGraph.setGraphCaches(caches); - return contextedGraph.sendQuery(graphId, preparedQuery); + protected ResultSet sendQuery(String preparedQuery) { + try (ContextedGraph contextedGraph = new ContextedGraph(driver.getConnection(), this.cache, this.graphId)) { + return contextedGraph.sendQuery(preparedQuery); } } /** * Overrides the abstract function. - * Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done - * @param graphId graph to be queried + * Sends the read-only query from any Jedis connection received from the Jedis + * pool and closes it once done + * * @param preparedQuery prepared query * @return Result set with the query answer */ @Override - protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery){ - try (ContextedGraph contextedGraph = new ContextedGraph(getConnection())) { - contextedGraph.setGraphCaches(caches); - return contextedGraph.sendReadOnlyQuery(graphId, preparedQuery); + protected ResultSet sendReadOnlyQuery(String preparedQuery) { + try (ContextedGraph contextedGraph = new ContextedGraph(driver.getConnection(), this.cache, this.graphId)) { + return contextedGraph.sendReadOnlyQuery(preparedQuery); } } /** * Overrides the abstract function. - * Sends the query from any Jedis connection received from the Jedis pool and closes it once done - * @param graphId graph to be queried + * Sends the query from any Jedis connection received from the Jedis pool and + * closes it once done + * * @param preparedQuery prepared query * @param timeout * @return Result set with the query answer */ @Override - protected ResultSet sendQuery(String graphId, String preparedQuery, long timeout){ - try (ContextedGraph contextedGraph = new ContextedGraph(getConnection())) { - contextedGraph.setGraphCaches(caches); - return contextedGraph.sendQuery(graphId, preparedQuery, timeout); + protected ResultSet sendQuery(String preparedQuery, long timeout) { + try (ContextedGraph contextedGraph = new ContextedGraph(driver.getConnection(), this.cache, this.graphId)) { + return contextedGraph.sendQuery(preparedQuery, timeout); } } /** * Overrides the abstract function. - * Sends the read-only query from any Jedis connection received from the Jedis pool and closes it once done - * @param graphId graph to be queried + * Sends the read-only query from any Jedis connection received from the Jedis + * pool and closes it once done + * * @param preparedQuery prepared query * @param timeout * @return Result set with the query answer */ @Override - protected ResultSet sendReadOnlyQuery(String graphId, String preparedQuery, long timeout){ - try (ContextedGraph contextedGraph = new ContextedGraph(getConnection())) { - contextedGraph.setGraphCaches(caches); - return contextedGraph.sendReadOnlyQuery(graphId, preparedQuery, timeout); - } - } - - /** - * Closes the Jedis pool - */ - @Override - public void close() { - if (pool != null) { - pool.close(); - } - if (jedis != null) { - jedis.close(); + protected ResultSet sendReadOnlyQuery(String preparedQuery, long timeout) { + try (ContextedGraph contextedGraph = new ContextedGraph(driver.getConnection(), this.cache, this.graphId)) { + return contextedGraph.sendReadOnlyQuery(preparedQuery, timeout); } } /** * Deletes the entire graph - * @param graphId graph to delete + * * @return delete running time statistics */ @Override - public String deleteGraph(String graphId) { - try (Jedis conn = getConnection()) { + public String deleteGraph() { + try (Jedis conn = driver.getConnection()) { Object response = conn.sendCommand(GraphCommand.DELETE, graphId); - //clear local state - caches.removeGraphCache(graphId); + // clear local state + cache.clear(); return SafeEncoder.encode((byte[]) response); } } /** - * Returns a new ContextedGraph bounded to a Jedis connection from the Jedis pool + * Returns a new ContextedGraph bounded to a Jedis connection from the Jedis + * pool + * * @return ContextedGraph */ @Override public GraphContext getContext() { - ContextedGraph contextedGraph = new ContextedGraph(getConnection()); - contextedGraph.setGraphCaches(this.caches); - return contextedGraph; + return new ContextedGraph(driver.getConnection(), this.cache, this.graphId); + } + + @Override + public void close() { + this.cache.clear(); } } diff --git a/src/main/java/com/falkordb/impl/api/GraphCacheHolder.java b/src/main/java/com/falkordb/impl/api/GraphCacheHolder.java deleted file mode 100644 index 8cdfaaa..0000000 --- a/src/main/java/com/falkordb/impl/api/GraphCacheHolder.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.falkordb.impl.api; - -import com.falkordb.impl.graph_cache.GraphCaches; - -public interface GraphCacheHolder { - - void setGraphCaches(GraphCaches caches); -} diff --git a/src/main/java/com/falkordb/impl/api/GraphPipeline.java b/src/main/java/com/falkordb/impl/api/GraphPipeline.java index 1aea841..ad2b6b7 100644 --- a/src/main/java/com/falkordb/impl/api/GraphPipeline.java +++ b/src/main/java/com/falkordb/impl/api/GraphPipeline.java @@ -3,7 +3,7 @@ import com.falkordb.Graph; import com.falkordb.ResultSet; import com.falkordb.impl.Utils; -import com.falkordb.impl.graph_cache.GraphCaches; +import com.falkordb.impl.graph_cache.GraphCache; import com.falkordb.impl.resultset.ResultSetImpl; import redis.clients.jedis.Builder; import redis.clients.jedis.BuilderFactory; @@ -17,49 +17,49 @@ /** * This class is extending Jedis Pipeline */ -public class GraphPipeline extends Pipeline implements com.falkordb.GraphPipeline, GraphCacheHolder { +public class GraphPipeline extends Pipeline implements com.falkordb.GraphPipeline { private final Graph graph; - private GraphCaches caches; + private GraphCache cache; + private final String graphId; - - public GraphPipeline(Client client, Graph graph){ + public GraphPipeline(Client client, Graph graph, GraphCache cache, String graphId){ super.setClient(client); this.graph = graph; + this.cache = cache; + this.graphId = graphId; } /** * Execute a Cypher query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query) { + public Response query(String query) { client.sendCommand(GraphCommand.QUERY, graphId, query, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * Execute a Cypher read-oly query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query) { + public Response readOnlyQuery(String query) { client.sendCommand(GraphCommand.RO_QUERY, graphId, query, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -68,20 +68,19 @@ public ResultSet build(Object o) { * Execute a Cypher query with timeout. * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. - * @param graphId a graph to perform the query on * @param query Cypher query * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query, long timeout) { + public Response query(String query, long timeout) { client.sendCommand(GraphCommand.QUERY, graphId, query, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -90,60 +89,57 @@ public ResultSet build(Object o) { * Execute a Cypher read-only query with timeout. * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. - * @param graphId a graph to perform the query on * @param query Cypher query * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query, long timeout) { + public Response readOnlyQuery(String query, long timeout) { client.sendCommand(GraphCommand.RO_QUERY, graphId, query, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * 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 response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query, Map params) { + public Response query(String query, Map params) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * 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 response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query, Map params) { + public Response readOnlyQuery(String query, Map params) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -153,14 +149,13 @@ public ResultSet build(Object o) { * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. * timeout. - * @param graphId a graph to perform the query on. * @param query Cypher query. * @param params parameters map. * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query, Map params, long timeout) { + public Response query(String query, Map params, long timeout) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); @@ -168,7 +163,7 @@ public Response query(String graphId, String query, Map) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -178,14 +173,13 @@ public ResultSet build(Object o) { * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. * timeout. - * @param graphId a graph to perform the query on. * @param query Cypher query. * @param params parameters map. * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query, Map params, long timeout) { + public Response readOnlyQuery(String query, Map params, long timeout) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, @@ -194,66 +188,57 @@ public Response readOnlyQuery(String graphId, String query, Map) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * Invokes stored procedures without arguments - * @param graphId a graph to perform the query on * @param procedure procedure name to invoke * @return response with result set with the procedure data */ @Override - public Response callProcedure(String graphId, String procedure){ - return callProcedure(graphId, procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); + public Response callProcedure(String procedure){ + return callProcedure(procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); } /** * 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 response with result set with the procedure data */ @Override - public Response callProcedure(String graphId, String procedure, List args ){ - return callProcedure(graphId, procedure, args, Utils.DUMMY_MAP); + public Response callProcedure(String procedure, List args ){ + return callProcedure(procedure, args, Utils.DUMMY_MAP); } /** * 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 response with result set with the procedure data */ @Override - public Response callProcedure(String graphId, String procedure, List args, + public Response callProcedure(String procedure, List args, Map> kwargs) { String preparedProcedure = Utils.prepareProcedure(procedure, args, kwargs); - return query(graphId, preparedProcedure); + return query(preparedProcedure); } /** * Deletes the entire graph - * @param graphId graph to delete * @return response with the deletion running time statistics */ @Override - public Response deleteGraph(String graphId){ + public Response deleteGraph(){ client.sendCommand(GraphCommand.DELETE, graphId); Response response = getResponse(BuilderFactory.STRING); - caches.removeGraphCache(graphId); + cache.clear(); return response; } - - @Override - public void setGraphCaches(GraphCaches caches) { - this.caches = caches; - } } diff --git a/src/main/java/com/falkordb/impl/api/GraphTransaction.java b/src/main/java/com/falkordb/impl/api/GraphTransaction.java index 119fa52..feee590 100644 --- a/src/main/java/com/falkordb/impl/api/GraphTransaction.java +++ b/src/main/java/com/falkordb/impl/api/GraphTransaction.java @@ -3,7 +3,7 @@ import com.falkordb.Graph; import com.falkordb.ResultSet; import com.falkordb.impl.Utils; -import com.falkordb.impl.graph_cache.GraphCaches; +import com.falkordb.impl.graph_cache.GraphCache; import com.falkordb.impl.resultset.ResultSetImpl; import redis.clients.jedis.Builder; import redis.clients.jedis.BuilderFactory; @@ -18,50 +18,50 @@ * This class is extending Jedis Transaction */ public class GraphTransaction extends Transaction - implements com.falkordb.GraphTransaction, GraphCacheHolder { + implements com.falkordb.GraphTransaction { private final Graph graph; - private GraphCaches caches; + private final String graphId; + private GraphCache cache; - public GraphTransaction(Client client, Graph graph) { + public GraphTransaction(Client client, Graph graph, GraphCache cache, String graphId) { // init as in Jedis super(client); - this.graph = graph; + this.graphId = graphId; + this.cache = cache; } /** * Execute a Cypher query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query) { + public Response query(String query) { client.sendCommand(GraphCommand.QUERY, graphId, query, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * Execute a Cypher read-oly query. - * @param graphId a graph to perform the query on * @param query Cypher query * @return a response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query) { + public Response readOnlyQuery(String query) { client.sendCommand(GraphCommand.RO_QUERY, graphId, query, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -70,20 +70,19 @@ public ResultSet build(Object o) { * Execute a Cypher query with timeout. * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. - * @param graphId a graph to perform the query on * @param query Cypher query * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query, long timeout) { + public Response query(String query, long timeout) { client.sendCommand(GraphCommand.QUERY, graphId, query, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -92,60 +91,57 @@ public ResultSet build(Object o) { * Execute a Cypher read-only query with timeout. * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. - * @param graphId a graph to perform the query on * @param query Cypher query * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query, long timeout) { + public Response readOnlyQuery(String query, long timeout) { client.sendCommand(GraphCommand.RO_QUERY, graphId, query, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * 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 response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query, Map params) { + public Response query(String query, Map params) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * 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 response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query, Map params) { + public Response readOnlyQuery(String query, Map params) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING); return getResponse(new Builder() { @SuppressWarnings("unchecked") @Override public ResultSet build(Object o) { - return new ResultSetImpl((List) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -154,14 +150,13 @@ public ResultSet build(Object o) { * Executes a cypher query with parameters and timeout. * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. - * @param graphId a graph to perform the query on. * @param query Cypher query. * @param params parameters map. * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response query(String graphId, String query, Map params, long timeout) { + public Response query(String query, Map params, long timeout) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.QUERY, graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); @@ -169,7 +164,7 @@ public Response query(String graphId, String query, Map) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } @@ -178,14 +173,13 @@ public ResultSet build(Object o) { * Executes a cypher read-only query with parameters and timeout. * * NOTE: timeout is simply sent to DB. Socket timeout will not be changed. - * @param graphId a graph to perform the query on. * @param query Cypher query. * @param params parameters map. * @param timeout * @return a response which builds the result set with the query answer. */ @Override - public Response readOnlyQuery(String graphId, String query, Map params, long timeout) { + public Response readOnlyQuery(String query, Map params, long timeout) { String preparedQuery = Utils.prepareQuery(query, params); client.sendCommand(GraphCommand.RO_QUERY, graphId, preparedQuery, Utils.COMPACT_STRING, Utils.TIMEOUT_STRING, Long.toString(timeout)); @@ -193,64 +187,55 @@ public Response readOnlyQuery(String graphId, String query, Map) o, graph, caches.getGraphCache(graphId)); + return new ResultSetImpl((List) o, graph, cache); } }); } /** * Invokes stored procedures without arguments, in multi/exec context - * @param graphId a graph to perform the query on * @param procedure procedure name to invoke * @return response with result set with the procedure data */ @Override - public Response callProcedure(String graphId, String procedure) { - return callProcedure(graphId, procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); + public Response callProcedure(String procedure) { + return callProcedure(procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); } /** * Invokes stored procedure with arguments, in multi/exec context - * @param graphId a graph to perform the query on * @param procedure procedure name to invoke * @param args procedure arguments * @return response with result set with the procedure data */ @Override - public Response callProcedure(String graphId, String procedure, List args) { - return callProcedure(graphId, procedure, args, Utils.DUMMY_MAP); + public Response callProcedure(String procedure, List args) { + return callProcedure(procedure, args, Utils.DUMMY_MAP); } /** * Invoke a stored procedure, in multi/exec context - * @param graphId a graph to perform the query on * @param procedure - procedure to execute * @param args - procedure arguments * @param kwargs - procedure output arguments * @return response with result set with the procedure data */ @Override - public Response callProcedure(String graphId, String procedure, List args, + public Response callProcedure(String procedure, List args, Map> kwargs) { String preparedProcedure = Utils.prepareProcedure(procedure, args, kwargs); - return query(graphId, preparedProcedure); + return query(preparedProcedure); } /** * Deletes the entire graph, in multi/exec context - * @param graphId graph to delete * @return response with the deletion running time statistics */ @Override - public Response deleteGraph(String graphId) { + public Response deleteGraph() { client.sendCommand(GraphCommand.DELETE, graphId); Response response = getResponse(BuilderFactory.STRING); - caches.removeGraphCache(graphId); + cache.clear(); return response; } - - @Override - public void setGraphCaches(GraphCaches caches) { - this.caches = caches; - } } diff --git a/src/main/java/com/falkordb/impl/graph_cache/GraphCache.java b/src/main/java/com/falkordb/impl/graph_cache/GraphCache.java index 8f08f16..9d05b1f 100644 --- a/src/main/java/com/falkordb/impl/graph_cache/GraphCache.java +++ b/src/main/java/com/falkordb/impl/graph_cache/GraphCache.java @@ -14,12 +14,11 @@ public class GraphCache { /** * - * @param graphId - graph Id */ - public GraphCache(String graphId) { - this.labels = new GraphCacheList(graphId, "db.labels"); - this.propertyNames = new GraphCacheList(graphId, "db.propertyKeys"); - this.relationshipTypes = new GraphCacheList(graphId, "db.relationshipTypes"); + public GraphCache() { + this.labels = new GraphCacheList("db.labels"); + this.propertyNames = new GraphCacheList("db.propertyKeys"); + this.relationshipTypes = new GraphCacheList("db.relationshipTypes"); } /** @@ -43,7 +42,12 @@ public String getRelationshipType(int index, Graph graph) { * @return requested property */ public String getPropertyName(int index, Graph graph) { - return propertyNames.getCachedData(index, graph); } + + public void clear() { + labels.clear(); + propertyNames.clear(); + relationshipTypes.clear(); + } } diff --git a/src/main/java/com/falkordb/impl/graph_cache/GraphCacheList.java b/src/main/java/com/falkordb/impl/graph_cache/GraphCacheList.java index aedaf45..9b2020b 100644 --- a/src/main/java/com/falkordb/impl/graph_cache/GraphCacheList.java +++ b/src/main/java/com/falkordb/impl/graph_cache/GraphCacheList.java @@ -14,21 +14,16 @@ */ class GraphCacheList { - private final String graphId; private final String procedure; private final List data = new CopyOnWriteArrayList<>(); /** - * - * @param graphId - graph id * @param procedure - exact procedure command */ - public GraphCacheList(String graphId, String procedure) { - this.graphId = graphId; + public GraphCacheList(String procedure) { this.procedure = procedure; } - /** * A method to return a cached item if it is in the cache, or re-validate the cache if its invalidated * @param index index of data item @@ -50,7 +45,7 @@ public String getCachedData(int index, Graph graph) { * Auxiliary method to parse a procedure result set and refresh the cache */ private void getProcedureInfo(Graph graph) { - ResultSet resultSet = graph.callProcedure(graphId, procedure); + ResultSet resultSet = graph.callProcedure(procedure); List newData = new ArrayList<>(); int i = 0; for(Record record : resultSet){ @@ -61,4 +56,8 @@ private void getProcedureInfo(Graph graph) { } data.addAll(newData); } + + public void clear() { + data.clear(); + } } diff --git a/src/main/java/com/falkordb/impl/graph_cache/GraphCaches.java b/src/main/java/com/falkordb/impl/graph_cache/GraphCaches.java deleted file mode 100644 index 87b4c3c..0000000 --- a/src/main/java/com/falkordb/impl/graph_cache/GraphCaches.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.falkordb.impl.graph_cache; - -import com.falkordb.Graph; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class GraphCaches { - - private final Map graphCaches = new ConcurrentHashMap<>(); - - public GraphCache getGraphCache(String graphId){ - if (!graphCaches.containsKey(graphId)){ - graphCaches.putIfAbsent(graphId, new GraphCache(graphId)); - } - return graphCaches.get(graphId); - } - - /** - * Returns a String which represents the name of the label mapped to the label id - * @param graphId graph to perform the query - * @param index label index - * @param graph GraphAPI implementation - * @return label name - */ - public String getLabel(String graphId, int index, Graph graph) { - return getGraphCache(graphId).getLabel(index, graph); - } - - /** - * Returns a String which represents the name of the relationship mapped to the label id - * @param graphId graph to perform the query - * @param index relationship index - * @param graph GraphAPI implementation - * @return relationship name - */ - public String getRelationshipType(String graphId, int index, Graph graph){ - return getGraphCache(graphId).getRelationshipType(index, graph); - } - - /** - * Returns a String which represents the name of the property mapped to the label id - * @param graphId graph to perform the query - * @param index property index - * @param graph GraphAPI implementation - * @return property name - */ - public String getPropertyName(String graphId, int index, Graph graph){ - return getGraphCache(graphId).getPropertyName(index, graph); - } - - /** - * Removes a graph meta data cache - * @param graphId - */ - public void removeGraphCache(String graphId){ - graphCaches.remove(graphId); - } -} diff --git a/src/test/java/com/falkordb/GraphAPITest.java b/src/test/java/com/falkordb/GraphAPITest.java index 95ea0d3..b403484 100644 --- a/src/test/java/com/falkordb/GraphAPITest.java +++ b/src/test/java/com/falkordb/GraphAPITest.java @@ -17,7 +17,6 @@ import com.falkordb.graph_entities.Path; import com.falkordb.graph_entities.Point; import com.falkordb.graph_entities.Property; -import com.falkordb.impl.api.Graph; import com.falkordb.test.utils.PathBuilder; public class GraphAPITest { @@ -26,19 +25,19 @@ public class GraphAPITest { @Before public void createApi() { - client = new Graph(); + client = FalkorDB.driver().graph("social"); } @After public void deleteGraph() { - client.deleteGraph("social"); + client.deleteGraph(); client.close(); } @Test public void testCreateNode() { // Create a node - ResultSet resultSet = client.query("social", "CREATE ({name:'roi',age:32})"); + ResultSet resultSet = client.query("CREATE ({name:'roi',age:32})"); Assert.assertEquals(1, resultSet.getStatistics().nodesCreated()); Assert.assertNull(resultSet.getStatistics().getStringValue(Label.NODES_DELETED)); @@ -60,7 +59,7 @@ public void testCreateNode() { @Test public void testCreateLabeledNode() { // Create a node with a label - ResultSet resultSet = client.query("social", "CREATE (:human{name:'danny',age:12})"); + ResultSet resultSet = client.query("CREATE (:human{name:'danny',age:12})"); Assert.assertFalse(resultSet.iterator().hasNext()); Assert.assertEquals("1", resultSet.getStatistics().getStringValue(Label.NODES_CREATED)); Assert.assertEquals("2", resultSet.getStatistics().getStringValue(Label.PROPERTIES_SET)); @@ -70,12 +69,11 @@ public void testCreateLabeledNode() { @Test public void testConnectNodes() { // Create both source and destination nodes - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'amit',age:30})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'amit',age:30})")); // Connect source and destination nodes. - ResultSet resultSet = client.query("social", - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"); + ResultSet resultSet = client.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"); Assert.assertFalse(resultSet.iterator().hasNext()); Assert.assertNull(resultSet.getStatistics().getStringValue(Label.NODES_CREATED)); @@ -87,9 +85,9 @@ public void testConnectNodes() { @Test public void testDeleteNodes() { - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'amit',age:30})")); - ResultSet deleteResult = client.query("social", "MATCH (a:person) WHERE (a.name = 'roi') DELETE a"); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'amit',age:30})")); + ResultSet deleteResult = client.query("MATCH (a:person) WHERE (a.name = 'roi') DELETE a"); Assert.assertFalse(deleteResult.iterator().hasNext()); Assert.assertNull(deleteResult.getStatistics().getStringValue(Label.NODES_CREATED)); @@ -100,10 +98,9 @@ public void testDeleteNodes() { Assert.assertEquals(1, deleteResult.getStatistics().nodesDeleted()); Assert.assertNotNull(deleteResult.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); - Assert.assertNotNull(client.query("social", - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)")); - deleteResult = client.query("social", "MATCH (a:person) WHERE (a.name = 'roi') DELETE a"); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)")); + deleteResult = client.query("MATCH (a:person) WHERE (a.name = 'roi') DELETE a"); Assert.assertFalse(deleteResult.iterator().hasNext()); Assert.assertNull(deleteResult.getStatistics().getStringValue(Label.NODES_CREATED)); @@ -120,11 +117,10 @@ public void testDeleteNodes() { @Test public void testDeleteRelationship() { - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'amit',age:30})")); - Assert.assertNotNull(client.query("social", - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)")); - ResultSet deleteResult = client.query("social", "MATCH (a:person)-[e]->() WHERE (a.name = 'roi') DELETE e"); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'amit',age:30})")); + Assert.assertNotNull(client.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)")); + ResultSet deleteResult = client.query("MATCH (a:person)-[e]->() WHERE (a.name = 'roi') DELETE e"); Assert.assertFalse(deleteResult.iterator().hasNext()); Assert.assertNull(deleteResult.getStatistics().getStringValue(Label.NODES_CREATED)); @@ -141,27 +137,27 @@ public void testDeleteRelationship() { @Test public void testIndex() { // Create both source and destination nodes - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); - ResultSet createIndexResult = client.query("social", "CREATE INDEX ON :person(age)"); + ResultSet createIndexResult = client.query("CREATE INDEX ON :person(age)"); Assert.assertFalse(createIndexResult.iterator().hasNext()); Assert.assertEquals(1, createIndexResult.getStatistics().indicesAdded()); // since RediSearch as index, those action are allowed - ResultSet createNonExistingIndexResult = client.query("social", "CREATE INDEX ON :person(age1)"); + ResultSet createNonExistingIndexResult = client.query("CREATE INDEX ON :person(age1)"); Assert.assertFalse(createNonExistingIndexResult.iterator().hasNext()); Assert.assertNotNull(createNonExistingIndexResult.getStatistics().getStringValue(Label.INDICES_ADDED)); Assert.assertEquals(1, createNonExistingIndexResult.getStatistics().indicesAdded()); try { - client.query("social", "CREATE INDEX ON :person(age)"); + client.query("CREATE INDEX ON :person(age)"); fail(); } catch (Exception e) { // TODO: handle exception } - ResultSet deleteExistingIndexResult = client.query("social", "DROP INDEX ON :person(age)"); + ResultSet deleteExistingIndexResult = client.query("DROP INDEX ON :person(age)"); Assert.assertFalse(deleteExistingIndexResult.iterator().hasNext()); Assert.assertNotNull(deleteExistingIndexResult.getStatistics().getStringValue(Label.INDICES_DELETED)); Assert.assertEquals(1, deleteExistingIndexResult.getStatistics().indicesDeleted()); @@ -171,12 +167,11 @@ public void testIndex() { @Test public void testHeader() { - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'amit',age:30})")); - Assert.assertNotNull(client.query("social", - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)")); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'amit',age:30})")); + Assert.assertNotNull(client.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)")); - ResultSet queryResult = client.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age"); + ResultSet queryResult = client.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age"); Header header = queryResult.getHeader(); Assert.assertNotNull(header); @@ -250,14 +245,13 @@ public void testRecord() { params.put("boolValue", boolValue); params.put("doubleValue", doubleValue); - Assert.assertNotNull(client.query("social", - "CREATE (:person{name:$name,age:$age, doubleValue:$doubleValue, boolValue:$boolValue})", params)); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'amit',age:30})")); + Assert.assertNotNull(client.query("CREATE (:person{name:$name,age:$age, doubleValue:$doubleValue, boolValue:$boolValue})", params)); + Assert.assertNotNull(client.query("CREATE (:person{name:'amit',age:30})")); Assert.assertNotNull( - client.query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') " + + client.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') " + "CREATE (a)-[:knows{place:'TLV', since:2000,doubleValue:3.14, boolValue:false}]->(b)")); - ResultSet resultSet = client.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, " + + ResultSet resultSet = client.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, " + "a.name, a.age, a.doubleValue, a.boolValue, " + "r.place, r.since, r.doubleValue, r.boolValue"); Assert.assertNotNull(resultSet); @@ -317,11 +311,10 @@ public void testRecord() { @Test public void testMultiThread() { - Assert.assertNotNull(client.query("social", - "CREATE (:person {name:'roi', age:32})-[:knows]->(:person {name:'amit',age:30}) ")); + Assert.assertNotNull(client.query("CREATE (:person {name:'roi', age:32})-[:knows]->(:person {name:'amit',age:30}) ")); List resultSets = IntStream.range(0, 16).parallel() - .mapToObj(i -> client.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age")) + .mapToObj(i -> client.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age")) .collect(Collectors.toList()); Property nameProperty = new Property<>("name", "roi"); @@ -372,13 +365,12 @@ public void testMultiThread() { expectedEdge.setDestination(3); expectedEdge.setId(1); - Assert.assertNotNull(client.query("social", "CREATE (:worker{lastName:'a'})")); - Assert.assertNotNull(client.query("social", "CREATE (:worker{lastName:'b'})")); - Assert.assertNotNull(client.query("social", - "MATCH (a:worker), (b:worker) WHERE (a.lastName = 'a' AND b.lastName='b') CREATE (a)-[:worksWith]->(b)")); + Assert.assertNotNull(client.query("CREATE (:worker{lastName:'a'})")); + Assert.assertNotNull(client.query("CREATE (:worker{lastName:'b'})")); + Assert.assertNotNull(client.query("MATCH (a:worker), (b:worker) WHERE (a.lastName = 'a' AND b.lastName='b') CREATE (a)-[:worksWith]->(b)")); resultSets = IntStream.range(0, 16).parallel() - .mapToObj(i -> client.query("social", "MATCH (a:worker)-[r:worksWith]->(b:worker) RETURN a,r")) + .mapToObj(i -> client.query("MATCH (a:worker)-[r:worksWith]->(b:worker) RETURN a,r")) .collect(Collectors.toList()); for (ResultSet resultSet : resultSets) { @@ -403,10 +395,9 @@ public void testMultiThread() { @Test public void testAdditionToProcedures() { - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'roi',age:32})")); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'amit',age:30})")); - Assert.assertNotNull(client.query("social", - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)")); + Assert.assertNotNull(client.query("CREATE (:person{name:'roi',age:32})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'amit',age:30})")); + Assert.assertNotNull(client.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)")); // expected objects init Property nameProperty = new Property<>("name", "roi"); @@ -425,7 +416,7 @@ public void testAdditionToProcedures() { expectedEdge.setDestination(1); expectedEdge.setRelationshipType("knows"); - ResultSet resultSet = client.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r"); + ResultSet resultSet = client.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a,r"); Assert.assertNotNull(resultSet.getHeader()); Header header = resultSet.getHeader(); List schemaNames = header.getSchemaNames(); @@ -454,11 +445,10 @@ public void testAdditionToProcedures() { expectedEdge.setSource(2); expectedEdge.setDestination(3); expectedEdge.setId(1); - Assert.assertNotNull(client.query("social", "CREATE (:worker{lastName:'a'})")); - Assert.assertNotNull(client.query("social", "CREATE (:worker{lastName:'b'})")); - Assert.assertNotNull(client.query("social", - "MATCH (a:worker), (b:worker) WHERE (a.lastName = 'a' AND b.lastName='b') CREATE (a)-[:worksWith]->(b)")); - resultSet = client.query("social", "MATCH (a:worker)-[r:worksWith]->(b:worker) RETURN a,r"); + Assert.assertNotNull(client.query("CREATE (:worker{lastName:'a'})")); + Assert.assertNotNull(client.query("CREATE (:worker{lastName:'b'})")); + Assert.assertNotNull(client.query("MATCH (a:worker), (b:worker) WHERE (a.lastName = 'a' AND b.lastName='b') CREATE (a)-[:worksWith]->(b)")); + resultSet = client.query("MATCH (a:worker)-[r:worksWith]->(b:worker) RETURN a,r"); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); schemaNames = header.getSchemaNames(); @@ -482,14 +472,14 @@ public void testEscapedQuery() { Map params1 = new HashMap(); params1.put("s1", "S\"'"); params1.put("s2", "S'\""); - Assert.assertNotNull(client.query("social", "CREATE (:escaped{s1:$s1,s2:$s2})", params1)); + Assert.assertNotNull(client.query("CREATE (:escaped{s1:$s1,s2:$s2})", params1)); Map params2 = new HashMap(); params2.put("s1", "S\"'"); params2.put("s2", "S'\""); - Assert.assertNotNull(client.query("social", "MATCH (n) where n.s1=$s1 and n.s2=$s2 RETURN n", params2)); + Assert.assertNotNull(client.query("MATCH (n) where n.s1=$s1 and n.s2=$s2 RETURN n", params2)); - Assert.assertNotNull(client.query("social", "MATCH (n) where n.s1='S\"' RETURN n")); + Assert.assertNotNull(client.query("MATCH (n) where n.s1='S\"' RETURN n")); } @@ -537,14 +527,13 @@ public void testContextedAPI() { params.put("boolValue", boolValue); params.put("doubleValue", doubleValue); try (GraphContext c = client.getContext()) { - Assert.assertNotNull(c.query("social", - "CREATE (:person{name:$name, age:$age, doubleValue:$doubleValue, boolValue:$boolValue})", params)); - Assert.assertNotNull(c.query("social", "CREATE (:person{name:'amit',age:30})")); + Assert.assertNotNull(c.query("CREATE (:person{name:$name, age:$age, doubleValue:$doubleValue, boolValue:$boolValue})", params)); + Assert.assertNotNull(c.query("CREATE (:person{name:'amit',age:30})")); Assert.assertNotNull( - c.query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') " + + c.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') " + "CREATE (a)-[:knows{place:'TLV', since:2000,doubleValue:3.14, boolValue:false}]->(b)")); - ResultSet resultSet = c.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, " + + ResultSet resultSet = c.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, " + "a.name, a.age, a.doubleValue, a.boolValue, " + "r.place, r.since, r.doubleValue, r.boolValue"); Assert.assertNotNull(resultSet); @@ -624,12 +613,12 @@ public void testArraySupport() { expectedBNode.addProperty(bAgeProperty); expectedBNode.addProperty(bListProperty); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'a',age:32,array:[0,1,2]})")); - Assert.assertNotNull(client.query("social", "CREATE (:person{name:'b',age:30,array:[3,4,5]})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'a',age:32,array:[0,1,2]})")); + Assert.assertNotNull(client.query("CREATE (:person{name:'b',age:30,array:[3,4,5]})")); // test array - ResultSet resultSet = client.query("social", "WITH [0,1,2] as x return x"); + ResultSet resultSet = client.query("WITH [0,1,2] as x return x"); // check header Assert.assertNotNull(resultSet.getHeader()); @@ -653,7 +642,7 @@ public void testArraySupport() { Assert.assertEquals(Arrays.asList(0L, 1L, 2L), x); // test collect - resultSet = client.query("social", "MATCH(n) return collect(n) as x"); + resultSet = client.query("MATCH(n) return collect(n) as x"); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); @@ -675,7 +664,7 @@ record = iterator.next(); Assert.assertEquals(Arrays.asList(expectedANode, expectedBNode), x); // test unwind - resultSet = client.query("social", "unwind([0,1,2]) as x return x"); + resultSet = client.query("unwind([0,1,2]) as x return x"); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); @@ -730,9 +719,9 @@ public void testPath() { expectedPaths.add(path12); expectedPaths.add(path02); - client.query("social", "CREATE (:L1)-[:R1]->(:L1)-[:R1]->(:L1)"); + client.query("CREATE (:L1)-[:R1]->(:L1)-[:R1]->(:L1)"); - ResultSet resultSet = client.query("social", "MATCH p = (:L1)-[:R1*]->(:L1) RETURN p"); + ResultSet resultSet = client.query("MATCH p = (:L1)-[:R1*]->(:L1) RETURN p"); Assert.assertEquals(expectedPaths.size(), resultSet.size()); for (Record record : resultSet) { @@ -746,9 +735,9 @@ public void testPath() { @Test public void testNullGraphEntities() { // Create two nodes connected by a single outgoing edge. - Assert.assertNotNull(client.query("social", "CREATE (:L)-[:E]->(:L2)")); + Assert.assertNotNull(client.query("CREATE (:L)-[:E]->(:L2)")); // Test a query that produces 1 record with 3 null values. - ResultSet resultSet = client.query("social", "OPTIONAL MATCH (a:NONEXISTENT)-[e]->(b) RETURN a, e, b"); + ResultSet resultSet = client.query("OPTIONAL MATCH (a:NONEXISTENT)-[e]->(b) RETURN a, e, b"); Assert.assertEquals(1, resultSet.size()); Iterator iterator = resultSet.iterator(); @@ -758,7 +747,7 @@ public void testNullGraphEntities() { Assert.assertEquals(Arrays.asList(null, null, null), record.values()); // Test a query that produces 2 records, with 2 null values in the second. - resultSet = client.query("social", "MATCH (a) OPTIONAL MATCH (a)-[e]->(b) RETURN a, e, b ORDER BY ID(a)"); + resultSet = client.query("MATCH (a) OPTIONAL MATCH (a)-[e]->(b) RETURN a, e, b ORDER BY ID(a)"); Assert.assertEquals(2, resultSet.size()); iterator = resultSet.iterator(); @@ -778,7 +767,7 @@ record = iterator.next(); // Test a query that produces 2 records, the first containing a path and the // second containing a null value. - resultSet = client.query("social", "MATCH (a) OPTIONAL MATCH p = (a)-[e]->(b) RETURN p"); + resultSet = client.query("MATCH (a) OPTIONAL MATCH p = (a)-[e]->(b) RETURN p"); Assert.assertEquals(2, resultSet.size()); iterator = resultSet.iterator(); @@ -796,7 +785,7 @@ public void test64bitnumber() { long value = 1 << 40; Map params = new HashMap<>(); params.put("val", value); - ResultSet resultSet = client.query("social", "CREATE (n {val:$val}) RETURN n.val", params); + ResultSet resultSet = client.query("CREATE (n {val:$val}) RETURN n.val", params); Assert.assertEquals(1, resultSet.size()); Record r = resultSet.iterator().next(); Assert.assertEquals(Long.valueOf(value), r.getValue(0)); @@ -804,12 +793,12 @@ public void test64bitnumber() { @Test public void testCachedExecution() { - client.query("social", "CREATE (:N {val:1}), (:N {val:2})"); + client.query("CREATE (:N {val:1}), (:N {val:2})"); // First time should not be loaded from execution cache Map params = new HashMap<>(); params.put("val", 1L); - ResultSet resultSet = client.query("social", "MATCH (n:N {val:$val}) RETURN n.val", params); + ResultSet resultSet = client.query("MATCH (n:N {val:$val}) RETURN n.val", params); Assert.assertEquals(1, resultSet.size()); Record r = resultSet.iterator().next(); Assert.assertEquals(params.get("val"), r.getValue(0)); @@ -818,7 +807,7 @@ public void testCachedExecution() { // Run in loop many times to make sure the query will be loaded // from cache at least once for (int i = 0; i < 64; i++) { - resultSet = client.query("social", "MATCH (n:N {val:$val}) RETURN n.val", params); + resultSet = client.query("MATCH (n:N {val:$val}) RETURN n.val", params); } Assert.assertEquals(1, resultSet.size()); r = resultSet.iterator().next(); @@ -842,7 +831,7 @@ public void testMapDataType() { f.put("x", (long) 1); f.put("y", (long) 2); expected.put("f", f); - ResultSet res = client.query("social", "RETURN {a:1, b:'str', c:NULL, d:[1,2,3], e:True, f:{x:1, y:2}}"); + ResultSet res = client.query("RETURN {a:1, b:'str', c:NULL, d:[1,2,3], e:True, f:{x:1, y:2}}"); Assert.assertEquals(1, res.size()); Record r = res.iterator().next(); Map actual = r.getValue(0); @@ -851,7 +840,7 @@ public void testMapDataType() { @Test public void testGeoPointLatLon() { - ResultSet rs = client.query("social", "CREATE (:restaurant" + ResultSet rs = client.query("CREATE (:restaurant" + " {location: point({latitude:30.27822306, longitude:-97.75134723})})"); Assert.assertEquals(1, rs.getStatistics().nodesCreated()); Assert.assertEquals(1, rs.getStatistics().propertiesSet()); @@ -861,7 +850,7 @@ public void testGeoPointLatLon() { @Test public void testGeoPointLonLat() { - ResultSet rs = client.query("social", "CREATE (:restaurant" + ResultSet rs = client.query("CREATE (:restaurant" + " {location: point({longitude:-97.75134723, latitude:30.27822306})})"); Assert.assertEquals(1, rs.getStatistics().nodesCreated()); Assert.assertEquals(1, rs.getStatistics().propertiesSet()); @@ -870,7 +859,7 @@ public void testGeoPointLonLat() { } private void assertTestGeoPoint() { - ResultSet results = client.query("social", "MATCH (restaurant) RETURN restaurant"); + ResultSet results = client.query("MATCH (restaurant) RETURN restaurant"); Assert.assertEquals(1, results.size()); Record record = results.iterator().next(); Assert.assertEquals(1, record.size()); @@ -882,7 +871,7 @@ private void assertTestGeoPoint() { @Test public void timeoutArgument() { - ResultSet rs = client.query("social", "UNWIND range(0,100) AS x WITH x AS x WHERE x = 100 RETURN x", 1L); + ResultSet rs = client.query("UNWIND range(0,100) AS x WITH x AS x WHERE x = 100 RETURN x", 1L); Assert.assertEquals(1, rs.size()); Record r = rs.iterator().next(); Assert.assertEquals(Long.valueOf(100), r.getValue(0)); @@ -890,12 +879,12 @@ public void timeoutArgument() { @Test public void testCachedExecutionReadOnly() { - client.query("social", "CREATE (:N {val:1}), (:N {val:2})"); + client.query("CREATE (:N {val:1}), (:N {val:2})"); // First time should not be loaded from execution cache Map params = new HashMap<>(); params.put("val", 1L); - ResultSet resultSet = client.readOnlyQuery("social", "MATCH (n:N {val:$val}) RETURN n.val", params); + ResultSet resultSet = client.readOnlyQuery("MATCH (n:N {val:$val}) RETURN n.val", params); Assert.assertEquals(1, resultSet.size()); Record r = resultSet.iterator().next(); Assert.assertEquals(params.get("val"), r.getValue(0)); @@ -904,7 +893,7 @@ public void testCachedExecutionReadOnly() { // Run in loop many times to make sure the query will be loaded // from cache at least once for (int i = 0; i < 64; i++) { - resultSet = client.readOnlyQuery("social", "MATCH (n:N {val:$val}) RETURN n.val", params); + resultSet = client.readOnlyQuery("MATCH (n:N {val:$val}) RETURN n.val", params); } Assert.assertEquals(1, resultSet.size()); r = resultSet.iterator().next(); @@ -914,8 +903,8 @@ public void testCachedExecutionReadOnly() { @Test public void testSimpleReadOnly() { - client.query("social", "CREATE (:person{name:'filipe',age:30})"); - ResultSet rsRo = client.readOnlyQuery("social", "MATCH (a:person) WHERE (a.name = 'filipe') RETURN a.age"); + client.query("CREATE (:person{name:'filipe',age:30})"); + ResultSet rsRo = client.readOnlyQuery("MATCH (a:person) WHERE (a.name = 'filipe') RETURN a.age"); Assert.assertEquals(1, rsRo.size()); Record r = rsRo.iterator().next(); Assert.assertEquals(Long.valueOf(30), r.getValue(0)); diff --git a/src/test/java/com/falkordb/InstantiationTest.java b/src/test/java/com/falkordb/InstantiationTest.java index c1ef2f1..12f615b 100644 --- a/src/test/java/com/falkordb/InstantiationTest.java +++ b/src/test/java/com/falkordb/InstantiationTest.java @@ -3,42 +3,25 @@ import org.junit.After; import org.junit.Assert; -import com.falkordb.impl.api.Graph; - -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; - public class InstantiationTest { private GraphContextGenerator client; public void createDefaultClient() { - client = new Graph(); - ResultSet resultSet = client.query("g", "CREATE ({name:'bsb'})"); + client = FalkorDB.driver().graph("g"); + ResultSet resultSet = client.query("CREATE ({name:'bsb'})"); Assert.assertEquals(1, resultSet.getStatistics().nodesCreated()); } public void createClientWithHostAndPort() { - client = new Graph("localhost", 6379); - ResultSet resultSet = client.query("g", "CREATE ({name:'bsb'})"); - Assert.assertEquals(1, resultSet.getStatistics().nodesCreated()); - } - - public void createClientWithJedisInstance() { - client = new Graph(new Jedis()); - ResultSet resultSet = client.query("g", "CREATE ({name:'bsb'})"); - Assert.assertEquals(1, resultSet.getStatistics().nodesCreated()); - } - - public void createClientWithJedisPool() { - client = new Graph(new JedisPool()); - ResultSet resultSet = client.query("g", "CREATE ({name:'bsb'})"); + client = FalkorDB.driver("localhost", 6379).graph("g"); + ResultSet resultSet = client.query("CREATE ({name:'bsb'})"); Assert.assertEquals(1, resultSet.getStatistics().nodesCreated()); } @After public void closeClient() { if (client != null) { - client.deleteGraph("g"); + client.deleteGraph(); client.close(); } } diff --git a/src/test/java/com/falkordb/IterableTest.java b/src/test/java/com/falkordb/IterableTest.java index 66967bd..48edc83 100644 --- a/src/test/java/com/falkordb/IterableTest.java +++ b/src/test/java/com/falkordb/IterableTest.java @@ -1,33 +1,31 @@ package com.falkordb; import static org.junit.jupiter.api.Assertions.assertEquals; - + import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.falkordb.impl.api.Graph; public class IterableTest { private GraphContextGenerator api; @Before public void createApi() { - api = new Graph(); + api = FalkorDB.driver().graph("social"); } @After public void deleteGraph() { - - api.deleteGraph("social"); + api.deleteGraph(); api.close(); } @Test public void testRecordsIterator() { - api.query("social", "UNWIND(range(0,50)) as i CREATE(:N{i:i})"); + api.query("UNWIND(range(0,50)) as i CREATE(:N{i:i})"); - ResultSet rs = api.query("social", "MATCH(n) RETURN n"); + ResultSet rs = api.query("MATCH(n) RETURN n"); int count = 0; for (Record record : rs) { count++; @@ -37,9 +35,9 @@ public void testRecordsIterator() { @Test public void testRecordsIterable() { - api.query("social", "UNWIND(range(0,50)) as i CREATE(:N{i:i})"); + api.query("UNWIND(range(0,50)) as i CREATE(:N{i:i})"); - ResultSet rs = api.query("social", "MATCH(n) RETURN n"); + ResultSet rs = api.query("MATCH(n) RETURN n"); int count = 0; for (@SuppressWarnings("unused") Record row : rs) { @@ -50,9 +48,9 @@ public void testRecordsIterable() { @Test public void testRecordsIteratorAndIterable() { - api.query("social", "UNWIND(range(0,50)) as i CREATE(:N{i:i})"); + api.query("UNWIND(range(0,50)) as i CREATE(:N{i:i})"); - ResultSet rs = api.query("social", "MATCH(n) RETURN n"); + ResultSet rs = api.query("MATCH(n) RETURN n"); rs.iterator().next(); int count = 0; for (@SuppressWarnings("unused") diff --git a/src/test/java/com/falkordb/PipelineTest.java b/src/test/java/com/falkordb/PipelineTest.java index a233a54..4e46d7b 100644 --- a/src/test/java/com/falkordb/PipelineTest.java +++ b/src/test/java/com/falkordb/PipelineTest.java @@ -11,7 +11,6 @@ import com.falkordb.graph_entities.Node; import com.falkordb.graph_entities.Property; -import com.falkordb.impl.api.Graph; import com.falkordb.impl.resultset.ResultSetImpl; public class PipelineTest { @@ -20,12 +19,13 @@ public class PipelineTest { @Before public void createApi() { - api = new Graph(); + api = FalkorDB.driver().graph("social"); + } @After public void deleteGraph() { - api.deleteGraph("social"); + api.deleteGraph(); api.close(); } @@ -34,13 +34,12 @@ public void testSync() { try (GraphContext c = api.getContext()) { GraphPipeline pipeline = c.pipelined(); pipeline.set("x", "1"); - pipeline.query("social", "CREATE (:Person {name:'a'})"); - pipeline.query("g", "CREATE (:Person {name:'a'})"); + pipeline.query("CREATE (:Person {name:'a'})"); + pipeline.query("CREATE (:Person {name:'b'})"); pipeline.incr("x"); pipeline.get("x"); - pipeline.query("social", "MATCH (n:Person) RETURN n"); - pipeline.deleteGraph("g"); - pipeline.callProcedure("social", "db.labels"); + pipeline.query("MATCH (n:Person{name:'a'}) RETURN n"); + pipeline.callProcedure("db.labels"); List results = pipeline.syncAndReturnAll(); // Redis set command @@ -94,8 +93,8 @@ public void testSync() { Assert.assertEquals(Arrays.asList("n"), record.keys()); Assert.assertEquals(expectedNode, record.getValue("n")); - Assert.assertEquals(ResultSetImpl.class, results.get(7).getClass()); - resultSet = (ResultSet) results.get(7); + Assert.assertEquals(ResultSetImpl.class, results.get(6).getClass()); + resultSet = (ResultSet) results.get(6); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); @@ -122,11 +121,10 @@ public void testReadOnlyQueries() { GraphPipeline pipeline = c.pipelined(); pipeline.set("x", "1"); - pipeline.query("social", "CREATE (:Person {name:'a'})"); - pipeline.query("g", "CREATE (:Person {name:'a'})"); - pipeline.readOnlyQuery("social", "MATCH (n:Person) RETURN n"); - pipeline.deleteGraph("g"); - pipeline.callProcedure("social", "db.labels"); + pipeline.query("CREATE (:Person {name:'a'})"); + pipeline.query("CREATE (:Person {name:'b'})"); + pipeline.readOnlyQuery("MATCH (n:Person{name:'a'}) RETURN n"); + pipeline.callProcedure("db.labels"); List results = pipeline.syncAndReturnAll(); // Redis set command @@ -145,7 +143,7 @@ public void testReadOnlyQueries() { Assert.assertEquals(1, resultSet.getStatistics().propertiesSet()); // Graph read-only query result - Assert.assertEquals(ResultSetImpl.class, results.get(5).getClass()); + Assert.assertEquals(ResultSetImpl.class, results.get(4).getClass()); resultSet = (ResultSet) results.get(3); Assert.assertNotNull(resultSet.getHeader()); @@ -172,8 +170,8 @@ public void testReadOnlyQueries() { Assert.assertEquals(Arrays.asList("n"), record.keys()); Assert.assertEquals(expectedNode, record.getValue("n")); - Assert.assertEquals(ResultSetImpl.class, results.get(5).getClass()); - resultSet = (ResultSet) results.get(5); + Assert.assertEquals(ResultSetImpl.class, results.get(4).getClass()); + resultSet = (ResultSet) results.get(4); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); @@ -199,8 +197,8 @@ public void testWaitReplicas() { try (GraphContext c = api.getContext()) { GraphPipeline pipeline = c.pipelined(); pipeline.set("x", "1"); - pipeline.query("social", "CREATE (:Person {name:'a'})"); - pipeline.query("g", "CREATE (:Person {name:'a'})"); + pipeline.query("CREATE (:Person {name:'a'})"); + pipeline.query("CREATE (:Person {name:'b'})"); pipeline.waitReplicas(0, 100L); List results = pipeline.syncAndReturnAll(); Assert.assertEquals(0L, results.get(3)); diff --git a/src/test/java/com/falkordb/TransactionTest.java b/src/test/java/com/falkordb/TransactionTest.java index f5c691e..4e422d1 100644 --- a/src/test/java/com/falkordb/TransactionTest.java +++ b/src/test/java/com/falkordb/TransactionTest.java @@ -25,12 +25,12 @@ public TransactionTest() { @Before public void createApi(){ - api = new Graph(); + api = FalkorDB.driver().graph("social"); } @After public void deleteGraph() { - api.deleteGraph("social"); + api.deleteGraph(); api.close(); } @@ -40,13 +40,12 @@ public void testMultiExec(){ GraphTransaction transaction = c.multi(); transaction.set("x", "1"); - transaction.query("social", "CREATE (:Person {name:'a'})"); - transaction.query("g", "CREATE (:Person {name:'a'})"); + transaction.query("CREATE (:Person {name:'a'})"); + transaction.query("CREATE (:Person {name:'b'})"); transaction.incr("x"); transaction.get("x"); - transaction.query("social", "MATCH (n:Person) RETURN n"); - transaction.deleteGraph("g"); - transaction.callProcedure("social", "db.labels"); + transaction.query("MATCH (n:Person{name:'a'}) RETURN n"); + transaction.callProcedure("db.labels"); List results = transaction.exec(); // Redis set command @@ -102,8 +101,8 @@ public void testMultiExec(){ Assert.assertEquals(Arrays.asList("n"), record.keys()); Assert.assertEquals(expectedNode, record.getValue("n")); - Assert.assertEquals(ResultSetImpl.class, results.get(7).getClass()); - resultSet = (ResultSet) results.get(7); + Assert.assertEquals(ResultSetImpl.class, results.get(6).getClass()); + resultSet = (ResultSet) results.get(6); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); @@ -135,8 +134,8 @@ public void testWriteTransactionWatch(){ GraphTransaction t1 = c1.multi(); - t1.query("social", "CREATE (:Person {name:'a'})"); - c2.query("social", "CREATE (:Person {name:'b'})"); + t1.query("CREATE (:Person {name:'a'})"); + c2.query("CREATE (:Person {name:'b'})"); List returnValue = t1.exec(); Assert.assertNull(returnValue); c1.close(); @@ -148,15 +147,15 @@ public void testReadTransactionWatch(){ GraphContext c1 = api.getContext(); GraphContext c2 = api.getContext(); - Assert.assertNotEquals(c1.getConnectionContext(), c2.getConnectionContext()); - c1.query("social", "CREATE (:Person {name:'a'})"); + Assert.assertNotEquals(c1, c2); + c1.query("CREATE (:Person {name:'a'})"); c1.watch("social"); GraphTransaction t1 = c1.multi(); Map params = new HashMap<>(); params.put("name", 'b'); - t1.query("social", "CREATE (:Person {name:$name})", params); - c2.query("social", "MATCH (n) return n"); + t1.query("CREATE (:Person {name:$name})", params); + c2.query("MATCH (n) return n"); List returnValue = t1.exec(); Assert.assertNotNull(returnValue); @@ -170,11 +169,10 @@ public void testMultiExecWithReadOnlyQueries(){ GraphTransaction transaction = c.multi(); transaction.set("x", "1"); - transaction.query("social", "CREATE (:Person {name:'a'})"); - transaction.query("g", "CREATE (:Person {name:'a'})"); - transaction.readOnlyQuery("social", "MATCH (n:Person) RETURN n"); - transaction.deleteGraph("g"); - transaction.callProcedure("social", "db.labels"); + transaction.query("CREATE (:Person {name:'a'})"); + transaction.query("CREATE (:Person {name:'b'})"); + transaction.readOnlyQuery("MATCH (n:Person{name:'a'}) RETURN n"); + transaction.callProcedure("db.labels"); List results = transaction.exec(); // Redis set command @@ -194,7 +192,7 @@ public void testMultiExecWithReadOnlyQueries(){ Assert.assertEquals(1, resultSet.getStatistics().propertiesSet()); // Graph read-only query result - Assert.assertEquals(ResultSetImpl.class, results.get(5).getClass()); + Assert.assertEquals(ResultSetImpl.class, results.get(4).getClass()); resultSet = (ResultSet) results.get(3); Assert.assertNotNull(resultSet.getHeader()); @@ -221,8 +219,8 @@ public void testMultiExecWithReadOnlyQueries(){ Assert.assertEquals(Arrays.asList("n"), record.keys()); Assert.assertEquals(expectedNode, record.getValue("n")); - Assert.assertEquals(ResultSetImpl.class, results.get(5).getClass()); - resultSet = (ResultSet) results.get(5); + Assert.assertEquals(ResultSetImpl.class, results.get(4).getClass()); + resultSet = (ResultSet) results.get(4); Assert.assertNotNull(resultSet.getHeader()); header = resultSet.getHeader(); diff --git a/src/test/java/com/falkordb/exceptions/GraphErrorTest.java b/src/test/java/com/falkordb/exceptions/GraphErrorTest.java index 6c5321e..3100c7e 100644 --- a/src/test/java/com/falkordb/exceptions/GraphErrorTest.java +++ b/src/test/java/com/falkordb/exceptions/GraphErrorTest.java @@ -10,9 +10,9 @@ import org.junit.Before; import org.junit.Test; +import com.falkordb.FalkorDB; import com.falkordb.GraphContext; import com.falkordb.GraphContextGenerator; -import com.falkordb.impl.api.Graph; public class GraphErrorTest { @@ -20,28 +20,28 @@ public class GraphErrorTest { @Before public void createApi() { - api = new Graph(); - Assert.assertNotNull(api.query("social", "CREATE (:person{mixed_prop: 'strval'}), (:person{mixed_prop: 50})")); + api = FalkorDB.driver().graph("social"); + Assert.assertNotNull(api.query("CREATE (:person{mixed_prop: 'strval'}), (:person{mixed_prop: 50})")); } @After - public void deleteGraph() { + public void deleteGraph() throws Exception{ - api.deleteGraph("social"); + api.deleteGraph(); api.close(); } @Test public void testSyntaxErrorReporting() { GraphException exception = assertThrows(GraphException.class, - () -> api.query("social", "RETURN toUpper(5)")); + () -> api.query("RETURN toUpper(5)")); assertTrue(exception.getMessage().contains("Type mismatch: expected String or Null but was Integer")); } @Test public void testRuntimeErrorReporting() { GraphException exception = assertThrows(GraphException.class, - () -> api.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)")); + () -> api.query("MATCH (p:person) RETURN toUpper(p.mixed_prop)")); assertTrue(exception.getMessage().contains("Type mismatch: expected String or Null but was Integer")); } @@ -50,7 +50,7 @@ public void testExceptionFlow() { try { // Issue a query that causes a compile-time error - api.query("social", "RETURN toUpper(5)"); + api.query("RETURN toUpper(5)"); } catch (Exception e) { Assert.assertEquals(GraphException.class, e.getClass()); Assert.assertTrue(e.getMessage().contains("Type mismatch: expected String or Null but was Integer")); @@ -60,7 +60,7 @@ public void testExceptionFlow() { try { // Issue a query that causes a compile-time error - api.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + api.query("MATCH (p:person) RETURN toUpper(p.mixed_prop)"); } catch (Exception e) { Assert.assertEquals(GraphException.class, e.getClass()); Assert.assertTrue(e.getMessage().contains("Type mismatch: expected String or Null but was Integer")); @@ -72,21 +72,21 @@ public void testContextSyntaxErrorReporting() { GraphContext c = api.getContext(); GraphException exception = assertThrows(GraphException.class, - () -> c.query("social", "RETURN toUpper(5)")); + () -> c.query("RETURN toUpper(5)")); assertTrue(exception.getMessage().contains("Type mismatch: expected String or Null but was Integer")); } @Test public void testMissingParametersSyntaxErrorReporting() { GraphException exception = assertThrows(GraphException.class, - () -> api.query("social", "RETURN $param")); + () -> api.query("RETURN $param")); assertTrue(exception.getMessage().contains("Missing parameters")); } @Test public void testMissingParametersSyntaxErrorReporting2() { GraphException exception = assertThrows(GraphException.class, - () -> api.query("social", "RETURN $param", new HashMap<>())); + () -> api.query("RETURN $param", new HashMap<>())); assertTrue(exception.getMessage().contains("Missing parameters")); } @@ -95,7 +95,7 @@ public void testContextRuntimeErrorReporting() { GraphContext c = api.getContext(); GraphException exception = assertThrows(GraphException.class, - () -> c.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)")); + () -> c.query("MATCH (p:person) RETURN toUpper(p.mixed_prop)")); assertTrue(exception.getMessage().contains("Type mismatch: expected String or Null but was Integer")); } @@ -105,7 +105,7 @@ public void testContextExceptionFlow() { GraphContext c = api.getContext(); try { // Issue a query that causes a compile-time error - c.query("social", "RETURN toUpper(5)"); + c.query("RETURN toUpper(5)"); } catch (Exception e) { Assert.assertEquals(GraphException.class, e.getClass()); Assert.assertTrue(e.getMessage().contains("Type mismatch: expected String or Null but was Integer")); @@ -114,7 +114,7 @@ public void testContextExceptionFlow() { // On contexted api usage, connection should stay open try { // Issue a query that causes a compile-time error - c.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + c.query("MATCH (p:person) RETURN toUpper(p.mixed_prop)"); } catch (Exception e) { Assert.assertEquals(GraphException.class, e.getClass()); Assert.assertTrue(e.getMessage().contains("Type mismatch: expected String or Null but was Integer")); @@ -124,7 +124,7 @@ public void testContextExceptionFlow() { @Test public void timeoutException() { GraphException exception = assertThrows(GraphException.class, - () -> api.query("social", "UNWIND range(0,100000) AS x WITH x AS x WHERE x = 10000 RETURN x", 1L)); + () -> api.query("UNWIND range(0,100000) AS x WITH x AS x WHERE x = 10000 RETURN x", 1L)); assertTrue(exception.getMessage().contains("Query timed out")); } }