From b42bc7bfe989d22d98bdb6881df17c9413ebe918 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Thu, 7 Mar 2019 17:18:45 +0000
Subject: [PATCH] Updates the utility functions used to delete a subgraph

---
 src/service/clmcservice/graphapi/utilities.py | 23 ++++++++++---------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/service/clmcservice/graphapi/utilities.py b/src/service/clmcservice/graphapi/utilities.py
index 05082e9..9d8dd68 100644
--- a/src/service/clmcservice/graphapi/utilities.py
+++ b/src/service/clmcservice/graphapi/utilities.py
@@ -223,14 +223,15 @@ def delete_nodes_with_type(graph, node_type):
 
     log.info("Deleting {0} nodes.".format(node_type))
 
-    subgraph = graph.nodes.match(node_type)
-    deleted_nodes = len(subgraph)
-    for node in subgraph:
-        graph.delete(node)
+    # this is the recommended way to delete a number of nodes, rather than deleting them one by one
+    query = "MATCH (node:{0}) DETACH DELETE node RETURN count(node) as count;".format(node_type)
+    log.info("Executing query {0}".format(query))
+    result = graph.run(query)
+    nodes_matched = result.data()[0]["count"]  # we expect exactly one result, which is a dictionary with key 'count'
 
-    log.info("Deleted {0} {1} nodes.".format(deleted_nodes, node_type))
+    log.info("Deleted {0} {1} nodes.".format(nodes_matched, node_type))
 
-    return deleted_nodes
+    return nodes_matched
 
 
 def build_temporal_subgraph(request_id, from_timestamp, to_timestamp, json_queries, graph, influx_client):
@@ -338,11 +339,11 @@ def delete_temporal_subgraph(graph, subgraph_id):
 
     log.info("Deleting subgraph associated with ID {0}".format(subgraph_id))
 
-    subgraph = graph.nodes.match(uuid=subgraph_id)
-    nodes_matched = 0
-    for node in subgraph:
-        graph.delete(node)
-        nodes_matched += 1
+    # this is the recommended way to delete a number of nodes, rather than deleting them one by one
+    query = "MATCH (node {{uuid: '{0}'}}) DETACH DELETE node RETURN count(node) as count;".format(subgraph_id)
+    log.info("Executing query {0}".format(query))
+    result = graph.run(query)
+    nodes_matched = result.data()[0]["count"]  # we expect exactly one result, which is a dictionary with key 'count'
 
     log.info("Deleted {0} nodes associated with ID {1}".format(nodes_matched, subgraph_id))
 
-- 
GitLab