You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by is...@apache.org on 2022/05/17 20:46:23 UTC

[airavata-data-lake] branch master updated: use try blocks to create neo4j driver sessions

This is an automated email from the ASF dual-hosted git repository.

isjarana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-data-lake.git


The following commit(s) were added to refs/heads/master by this push:
     new 3590ffa  use try blocks to create neo4j driver sessions
     new b3748a4  Merge pull request #95 from isururanawaka/test_framework
3590ffa is described below

commit 3590ffacda816619a53126208b7fe7a9e13b2b01
Author: Isuru Ranawaka <ir...@gmail.com>
AuthorDate: Tue May 17 16:45:39 2022 -0400

    use try blocks to create neo4j driver sessions
---
 .../apache/airavata/drms/core/Neo4JConnector.java  | 149 +++++++++------------
 1 file changed, 67 insertions(+), 82 deletions(-)

diff --git a/data-resource-management-service/drms-core/src/main/java/org/apache/airavata/drms/core/Neo4JConnector.java b/data-resource-management-service/drms-core/src/main/java/org/apache/airavata/drms/core/Neo4JConnector.java
index 9ab1a51..33a95e9 100644
--- a/data-resource-management-service/drms-core/src/main/java/org/apache/airavata/drms/core/Neo4JConnector.java
+++ b/data-resource-management-service/drms-core/src/main/java/org/apache/airavata/drms/core/Neo4JConnector.java
@@ -54,124 +54,109 @@ public class Neo4JConnector {
     }
 
     public List<Record> searchNodes(String query) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try (Session session = driver.session()) {
+            Result result = session.run(query);
+            return result.list();
         }
-        Result result = session.run(query);
-        return result.list();
     }
 
     public List<Record> searchNodes(Map<String, Object> properties, String query) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try (Session session = driver.session()) {
+            Result result = session.run(query, properties);
+            return result.list();
         }
-        Result result = session.run(query, properties);
-        return result.list();
     }
 
     public void mergeNode(Map<String, Object> properties, String label, String userId, String entityId,
                           String tenantId) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try (Session session = driver.session()) {
+            Map<String, Object> parameters = new HashMap<>();
+            properties.put("entityId", entityId);
+            properties.put("tenantId", tenantId);
+            parameters.put("props", properties);
+            parameters.put("username", userId);
+            parameters.put("entityId", entityId);
+            parameters.put("tenantId", tenantId);
+            Transaction tx = session.beginTransaction();
+            tx.run("MATCH (u:User)  where u.username = $username AND  u.tenantId = $tenantId " +
+                    " MERGE (n:" + label + " {entityId: $entityId,tenantId: $tenantId}) ON MATCH  SET n += $props ON CREATE SET n += $props" +
+                    " MERGE (n)-[r2:SHARED_WITH {permission:'OWNER'}]->(u) return n", parameters);
+            tx.commit();
+            tx.close();
         }
-        Map<String, Object> parameters = new HashMap<>();
-        properties.put("entityId", entityId);
-        properties.put("tenantId", tenantId);
-        parameters.put("props", properties);
-        parameters.put("username", userId);
-        parameters.put("entityId", entityId);
-        parameters.put("tenantId", tenantId);
-        Transaction tx = session.beginTransaction();
-        tx.run("MATCH (u:User)  where u.username = $username AND  u.tenantId = $tenantId " +
-                " MERGE (n:" + label + " {entityId: $entityId,tenantId: $tenantId}) ON MATCH  SET n += $props ON CREATE SET n += $props" +
-                " MERGE (n)-[r2:SHARED_WITH {permission:'OWNER'}]->(u) return n", parameters);
-        tx.commit();
-        tx.close();
     }
 
     public void mergeNodesWithParentChildRelationShip(Map<String, Object> childProperties, Map<String, Object> parentProperties,
                                                       String childLabel, String parentLablel, String userId, String childEntityId,
                                                       String parentEntityId,
                                                       String tenantId) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try (Session session = driver.session()) {
+            Map<String, Object> parameters = new HashMap<>();
+            childProperties.put("childEntityId", childEntityId);
+            childProperties.put("tenantId", tenantId);
+            parentProperties.put("parentEntityId", parentEntityId);
+            parentProperties.put("tenantId", tenantId);
+            parameters.put("childProps", childProperties);
+            parameters.put("parentProps", parentProperties);
+            parameters.put("username", userId);
+            parameters.put("childEntityId", childEntityId);
+            parameters.put("parentEntityId", parentEntityId);
+            parameters.put("tenantId", tenantId);
+            Transaction tx = session.beginTransaction();
+            tx.run("MATCH (u:User)  where u.username = $username AND  u.tenantId = $tenantId " +
+                    " MERGE (p:" + parentLablel + " {entityId: $parentEntityId,tenantId: $tenantId}) ON MATCH  SET p += $parentProps ON CREATE SET p += $parentProps" +
+                    " MERGE (c:" + childLabel + " {entityId: $childEntityId,tenantId: $tenantId}) ON MATCH  SET c += $childProps ON CREATE SET c += $childProps" +
+                    " MERGE (c)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
+                    " MERGE (p)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
+                    " MERGE (c)-[:CHILD_OF]->(p) return c", parameters);
+            tx.commit();
+            tx.close();
         }
-        Map<String, Object> parameters = new HashMap<>();
-        childProperties.put("childEntityId", childEntityId);
-        childProperties.put("tenantId", tenantId);
-        parentProperties.put("parentEntityId", parentEntityId);
-        parentProperties.put("tenantId", tenantId);
-        parameters.put("childProps", childProperties);
-        parameters.put("parentProps", parentProperties);
-        parameters.put("username", userId);
-        parameters.put("childEntityId", childEntityId);
-        parameters.put("parentEntityId", parentEntityId);
-        parameters.put("tenantId", tenantId);
-        Transaction tx = session.beginTransaction();
-        tx.run("MATCH (u:User)  where u.username = $username AND  u.tenantId = $tenantId " +
-                " MERGE (p:" + parentLablel + " {entityId: $parentEntityId,tenantId: $tenantId}) ON MATCH  SET p += $parentProps ON CREATE SET p += $parentProps" +
-                " MERGE (c:" + childLabel + " {entityId: $childEntityId,tenantId: $tenantId}) ON MATCH  SET c += $childProps ON CREATE SET c += $childProps" +
-                " MERGE (c)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
-                " MERGE (p)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
-                " MERGE (c)-[:CHILD_OF]->(p) return c", parameters);
-        tx.commit();
-        tx.close();
     }
 
 
-
     public void deleteNode(String label, String entityId,
                            String tenantId) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try (Session session = driver.session()) {
+            Map<String, Object> parameters = new HashMap<>();
+            parameters.put("entityId", entityId);
+            parameters.put("tenantId", tenantId);
+            Transaction tx = session.beginTransaction();
+            tx.run("MATCH (n:" + label + ") where n.entityId= $entityId AND n.tenantId= $tenantId detach delete n", parameters);
+            tx.commit();
+            tx.close();
         }
-        Map<String, Object> parameters = new HashMap<>();
-        parameters.put("entityId", entityId);
-        parameters.put("tenantId", tenantId);
-        Transaction tx = session.beginTransaction();
-        tx.run("MATCH (n:" + label + ") where n.entityId= $entityId AND n.tenantId= $tenantId detach delete n", parameters);
-        tx.commit();
-        tx.close();
     }
 
     public void runTransactionalQuery(Map<String, Object> parameters, String query) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try (Session session = driver.session()) {
+            Transaction tx = session.beginTransaction();
+            Result result = tx.run(query, parameters);
+            tx.commit();
+            tx.close();
         }
-        Transaction tx = session.beginTransaction();
-        Result result = tx.run(query, parameters);
-        tx.commit();
-        tx.close();
     }
 
 
     public void runTransactionalQuery(String query) {
-        Session session = driver.session();
-        Transaction tx = session.beginTransaction();
-        Result result = tx.run(query);
-        tx.commit();
-        tx.close();
+        try (Session session = driver.session()) {
+            Transaction tx = session.beginTransaction();
+            Result result = tx.run(query);
+            tx.commit();
+            tx.close();
+        }
     }
 
     public void createMetadataNode(String parentLabel, String parentIdName, String parentIdValue,
                                    String userId, String key, String value) {
-        Session session = driver.session();
-        if (!session.isOpen()) {
-            session = resume();
+        try(Session session = driver.session()) {
+            Transaction tx = session.beginTransaction();
+            tx.run("match (u:User)-[r1:MEMBER_OF]->(g:Group)<-[r2:SHARED_WITH]-(s:" + parentLabel + ") where u.userId='" + userId +
+                    "' and s." + parentIdName + "='" + parentIdValue +
+                    "' merge (m:Metadata)<-[r3:HAS_METADATA]-(s) set m." + key + "='" + value + "' return m");
+            tx.commit();
+            tx.close();
         }
-
-        Transaction tx = session.beginTransaction();
-        tx.run("match (u:User)-[r1:MEMBER_OF]->(g:Group)<-[r2:SHARED_WITH]-(s:" + parentLabel + ") where u.userId='" + userId +
-                "' and s." + parentIdName + "='" + parentIdValue +
-                "' merge (m:Metadata)<-[r3:HAS_METADATA]-(s) set m." + key + "='" + value + "' return m");
-        tx.commit();
-        tx.close();
     }
 
     public boolean isOpen() {