Skip to content

Commit

Permalink
Merge pull request #27 from JoonasC/copy-command
Browse files Browse the repository at this point in the history
Implemented graph copy
  • Loading branch information
gkorland authored Apr 15, 2024
2 parents e01c816 + 4bf4b44 commit 0d2ff09
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/falkordb/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public interface Graph extends Closeable {
*/
ResultSet callProcedure(String procedure, List<String> args , Map<String, List<String>> kwargs);

/**
* Copies the graph
* @param destinationGraphId duplicated graph name
* @return copy running time statistics
*/
String copyGraph(String destinationGraphId);

/**
* Deletes the entire graph
* @return delete running time statistics
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/falkordb/GraphPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public interface GraphPipeline extends
*/
Response<ResultSet> callProcedure(String procedure, List<String> args , Map<String, List<String>> kwargs);

/**
* Copies the graph
* @param destinationGraphId duplicated graph name
* @return a response which builds the copy running time statistics
*/
Response<String> copyGraph(String destinationGraphId);

/**
* Deletes the entire graph
* @return a response which builds the delete running time statistics
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/falkordb/GraphTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public interface GraphTransaction extends
*/
Response<ResultSet> callProcedure(String procedure, List<String> args , Map<String, List<String>> kwargs);

// Disabled due to bug in FalkorDB caused by using transactions in conjunction with graph copy
/**
* Copies the graph
* @param destinationGraphId duplicated graph name
* @return a response which builds the copy running time statistics
*/
// Response<String> copyGraph(String destinationGraphId);

/**
* Deletes the entire graph
* @return a response which builds the delete running time statistics
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/falkordb/impl/api/GraphCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum GraphCommand implements ProtocolCommand {
QUERY("graph.QUERY"),
RO_QUERY("graph.RO_QUERY"),
COPY("graph.COPY"),
DELETE("graph.DELETE");

private final byte[] raw;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/falkordb/impl/api/GraphContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ public String unwatch() {
return connection.unwatch();
}

/**
* Copies the graph
* @param destinationGraphId duplicated graph name
* @return copy running time statistics
*/
@Override
public String copyGraph(String destinationGraphId) {
Object response = connection.sendCommand(GraphCommand.COPY, graphId, destinationGraphId);
return SafeEncoder.encode((byte[]) response);
}

/**
* Deletes the entire graph
* @return delete running time statistics
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/falkordb/impl/api/GraphImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ protected ResultSet sendReadOnlyQuery(String preparedQuery, long timeout) {
}
}

/**
* Copies the graph
*
* @param destinationGraphId duplicated graph name
* @return copy running time statistics
*/
@Override
public String copyGraph(String destinationGraphId) {
try (Jedis conn = driver.getConnection()) {
Object response = conn.sendCommand(GraphCommand.COPY, graphId, destinationGraphId);
return SafeEncoder.encode((byte[]) response);
}
}

/**
* Deletes the entire graph
*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/falkordb/impl/api/GraphPipelineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ public Response<ResultSet> callProcedure(String procedure, List<String> args,
return query(preparedProcedure);
}

/**
* Copies the graph
* @param destinationGraphId duplicated graph name
* @return response with the copy running time statistics
*/
@Override
public Response<String> copyGraph(String destinationGraphId) {
client.sendCommand(GraphCommand.COPY, graphId, destinationGraphId);
return getResponse(BuilderFactory.STRING);
}


/**
* Deletes the entire graph
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/falkordb/impl/api/GraphTransactionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ public Response<ResultSet> callProcedure(String procedure, List<String> args,
return query(preparedProcedure);
}

// Disabled due to bug in FalkorDB caused by using transactions in conjunction with graph copy
/**
* Copies the graph, in multi/exec context
* @param destinationGraphId duplicated graph name
* @return response with the copy running time statistics
*/
/* @Override
public Response<String> copyGraph(String destinationGraphId) {
client.sendCommand(GraphCommand.COPY, graphId, destinationGraphId);
return getResponse(BuilderFactory.STRING);
} */

/**
* Deletes the entire graph, in multi/exec context
* @return response with the deletion running time statistics
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/com/falkordb/GraphAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -937,4 +937,59 @@ public void testSimpleReadOnlyWithTimeOut() {
Assert.assertTrue(e.getMessage().contains("Query timed out"));
}
}

@Test
public void testGraphCopy() {
// Create sample data
client.query("CREATE (:person{name:'roi',age:32})-[:knows]->(:person{name:'amit',age:30})");
ResultSet originalResultSet = client.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
Iterator<Record> originalResultSetIterator = originalResultSet.iterator();

// Copy the graph
client.copyGraph("social-copied");

GraphContextGenerator client2 = FalkorDB.driver().graph("social-copied");
try {
// Compare graph contents
ResultSet copiedResultSet = client2.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
Iterator<Record> copiedResultSetIterator = copiedResultSet.iterator();
while (originalResultSetIterator.hasNext()) {
Assert.assertTrue(copiedResultSetIterator.hasNext());
Assert.assertEquals(originalResultSetIterator.next(), copiedResultSetIterator.next());
}
} finally {
// Cleanup
client2.deleteGraph();
client2.close();
}
}

@Test
public void testGraphCopyContextedAPI() {
Iterator<Record> originalResultSetIterator;
try (GraphContext c = client.getContext()) {
// Create sample data
c.query("CREATE (:person{name:'roi',age:32})-[:knows]->(:person{name:'amit',age:30})");
ResultSet originalResultSet = c.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
originalResultSetIterator = originalResultSet.iterator();

// Copy the graph
c.copyGraph("social-copied");
}

GraphContextGenerator client2 = FalkorDB.driver().graph("social-copied");
try {
// Compare graph contents
ResultSet copiedResultSet = client2.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
Iterator<Record> copiedResultSetIterator = copiedResultSet.iterator();
while (originalResultSetIterator.hasNext()) {
Assert.assertTrue(copiedResultSetIterator.hasNext());
Assert.assertEquals(originalResultSetIterator.next(), copiedResultSetIterator.next());
}
} finally {
// Cleanup
client2.deleteGraph();
client2.close();
}
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/falkordb/PipelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,35 @@ public void testWaitReplicas() {
Assert.assertEquals(0L, results.get(3));
}
}

@Test
public void testGraphCopy() {
Iterator<Record> originalResultSetIterator;
try (GraphContext c = api.getContext()) {
// Create sample data and copy the graph
GraphPipeline pipeline = c.pipelined();
pipeline.query("CREATE (:person{name:'roi',age:32})-[:knows]->(:person{name:'amit',age:30})");
pipeline.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
pipeline.copyGraph("social-copied");
List<Object> results = pipeline.syncAndReturnAll();

ResultSet originalResultSet = (ResultSet) results.get(1);
originalResultSetIterator = originalResultSet.iterator();
}

GraphContextGenerator api2 = FalkorDB.driver().graph("social-copied");
try {
// Compare graph contents
ResultSet copiedResultSet = api2.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
Iterator<Record> copiedResultSetIterator = copiedResultSet.iterator();
while (originalResultSetIterator.hasNext()) {
Assert.assertTrue(copiedResultSetIterator.hasNext());
Assert.assertEquals(originalResultSetIterator.next(), copiedResultSetIterator.next());
}
} finally {
// Cleanup
api2.deleteGraph();
api2.close();
}
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/falkordb/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,36 @@ record = iterator.next();
Assert.assertEquals("Person", record.getValue("label"));
}
}

// Disabled due to bug in FalkorDB caused by using transactions in conjunction with graph copy
/* @Test
public void testGraphCopy() {
Iterator<Record> originalResultSetIterator;
try (GraphContext c = api.getContext()) {
// Create sample data and copy the graph
GraphTransaction transaction = c.multi();
transaction.query("CREATE (:person{name:'roi',age:32})-[:knows]->(:person{name:'amit',age:30})");
transaction.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
transaction.copyGraph("social-copied");
List<Object> results = transaction.exec();
ResultSet originalResultSet = (ResultSet) results.get(1);
originalResultSetIterator = originalResultSet.iterator();
}
GraphContextGenerator api2 = FalkorDB.driver().graph("social-copied");
try {
// Compare graph contents
ResultSet copiedResultSet = api2.query("MATCH (p:person)-[rel:knows]->(p2:person) RETURN p,rel,p2");
Iterator<Record> copiedResultSetIterator = copiedResultSet.iterator();
while (originalResultSetIterator.hasNext()) {
Assert.assertTrue(copiedResultSetIterator.hasNext());
Assert.assertEquals(originalResultSetIterator.next(), copiedResultSetIterator.next());
}
} finally {
// Cleanup
api2.deleteGraph();
api2.close();
}
} */
}

0 comments on commit 0d2ff09

Please sign in to comment.