Skip to content

Commit

Permalink
core: A single session shared by all methods is replaced by multiple …
Browse files Browse the repository at this point in the history
…sessions per method, closes #28
  • Loading branch information
Swaathik committed Aug 22, 2016
1 parent a249bcc commit f3cb35a
Showing 1 changed file with 108 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class Neo4JNetworkDBAdaptor implements NetworkDBAdaptor {

private Driver driver;
private Session session;
// private Session session;

private BioNetDBConfiguration configuration;
private CellBaseClient cellBaseClient;
Expand Down Expand Up @@ -68,20 +68,29 @@ public Neo4JNetworkDBAdaptor(String database, BioNetDBConfiguration configuratio
String password = databaseConfiguration.getPassword();

driver = GraphDatabase.driver("bolt://" + databaseURI, AuthTokens.basic(user, password));
session = driver.session();
// session = driver.session();

registerShutdownHook(this.driver, this.session);
// registerShutdownHook(this.driver, this.session);
registerShutdownHook(this.driver);

if (createIndex) {
createIndexes();
}
}

private void registerShutdownHook(final Driver driver, final Session session) {
// private void registerShutdownHook(final Driver driver, final Session session) {
// Runtime.getRuntime().addShutdownHook(new Thread() {
// @Override
// public void run() {
// session.close();
// driver.close();
// }
// });
// }
private void registerShutdownHook(final Driver driver) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
session.close();
driver.close();
}
});
Expand All @@ -98,8 +107,9 @@ private DatabaseConfiguration getDatabaseConfiguration(String database) {
}

private void createIndexes() {
if (this.session != null) {
try (Transaction tx = this.session.beginTransaction()) {
Session session = this.driver.session();
if (session != null) {
try (Transaction tx = session.beginTransaction()) {
tx.run("CREATE INDEX ON :" + NodeTypes.PHYSICAL_ENTITY + "(id)");
tx.run("CREATE INDEX ON :" + NodeTypes.PHYSICAL_ENTITY + "(name)");

Expand All @@ -112,6 +122,7 @@ private void createIndexes() {

tx.success();
}
session.close();
}
}

Expand All @@ -135,7 +146,8 @@ public void insert(Network network, QueryOptions queryOptions) throws BioNetDBEx
*/
@Override
public void addXrefs(String nodeID, List<Xref> xrefList) throws BioNetDBException {
try (Transaction tx = this.session.beginTransaction()) {
Session session = this.driver.session();
try (Transaction tx = session.beginTransaction()) {
StatementResult xrefNode = getNode(tx, NodeTypes.XREF.toString(), new ObjectMap("id", nodeID));
if (xrefNode.hasNext()) {
// Look for the physical entity to which the xref is associated with
Expand All @@ -153,6 +165,7 @@ public void addXrefs(String nodeID, List<Xref> xrefList) throws BioNetDBExceptio
}
tx.success();
}
session.close();
}

@Override
Expand Down Expand Up @@ -338,7 +351,9 @@ private String concatenateLabels(Value labels) {
* @param queryOptions Additional params for the query
*/
private void insertPhysicalEntities(List<PhysicalEntity> physicalEntityList, QueryOptions queryOptions) throws BioNetDBException {
try (Transaction tx = this.session.beginTransaction()) {
Session session = this.driver.session();

try (Transaction tx = session.beginTransaction()) {
// 1. Insert the Physical Entities and the basic nodes they are connected to
for (PhysicalEntity p : physicalEntityList) {
String peLabel = NodeTypes.PHYSICAL_ENTITY + ":" + p.getType();
Expand Down Expand Up @@ -368,7 +383,8 @@ private void insertPhysicalEntities(List<PhysicalEntity> physicalEntityList, Que
}
tx.success();
}
try (Transaction tx = this.session.beginTransaction()) {

try (Transaction tx = session.beginTransaction()) {
// 2. Insert the existing relationships between Physical Entities
for (PhysicalEntity p : physicalEntityList) {
if (p.getComponentOfComplex().size() > 0) {
Expand Down Expand Up @@ -400,6 +416,7 @@ private void insertPhysicalEntities(List<PhysicalEntity> physicalEntityList, Que
}
tx.success();
}
session.close();
}

/**
Expand All @@ -409,9 +426,10 @@ private void insertPhysicalEntities(List<PhysicalEntity> physicalEntityList, Que
* @param queryOptions Additional params for the query
*/
private void insertInteractions(List<Interaction> interactionList, QueryOptions queryOptions) {
Session session = this.driver.session();

// 1. Insert all interactions as nodes
try (Transaction tx = this.session.beginTransaction()) {
try (Transaction tx = session.beginTransaction()) {
for (Interaction i : interactionList) {
String interactionLabel = NodeTypes.INTERACTION + ":" + i.getType();
getOrCreateNode(tx, interactionLabel, parseInteraction(i));
Expand All @@ -420,7 +438,7 @@ private void insertInteractions(List<Interaction> interactionList, QueryOptions
}

// 2. Insert the interactions
try (Transaction tx = this.session.beginTransaction()) {
try (Transaction tx = session.beginTransaction()) {
for (Interaction i : interactionList) {
String interactionLabel = NodeTypes.INTERACTION + ":" + i.getType();
StatementResult interaction = getNode(tx, interactionLabel, new ObjectMap("id", i.getId()));
Expand Down Expand Up @@ -485,6 +503,7 @@ private void insertInteractions(List<Interaction> interactionList, QueryOptions
}
tx.success();
}
session.close();
}

private StatementResult getNode(Transaction tx, String label, ObjectMap properties) {
Expand Down Expand Up @@ -739,6 +758,8 @@ private Ontology node2Ontology(Node node) throws BioNetDBException {

@Override
public QueryResult getNodes(Query query, QueryOptions queryOptions) throws BioNetDBException {
Session session = this.driver.session();

long startTime = System.currentTimeMillis();
String nodeName = "n";
String myQuery = "MATCH " + Neo4JQueryParser.parse(nodeName, query, queryOptions) + " RETURN " + nodeName;
Expand All @@ -750,11 +771,15 @@ public QueryResult getNodes(Query query, QueryOptions queryOptions) throws BioNe
System.out.println(run.next().asMap());
}
int time = (int) (stopTime - startTime) / 1000;

session.close();
return new QueryResult("get", time, 0, 0, null, null, Arrays.asList(new Network()));
}

@Override
public QueryResult getNodes(Query queryN, Query queryM, QueryOptions queryOptions) throws BioNetDBException {
Session session = this.driver.session();

long startTime = System.currentTimeMillis();
String nQuery = Neo4JQueryParser.parse("n", queryN, queryOptions);
String mQuery = Neo4JQueryParser.parse("m", queryM, queryOptions);
Expand Down Expand Up @@ -811,11 +836,15 @@ public QueryResult getNodes(Query queryN, Query queryM, QueryOptions queryOption
System.out.println(run.next().asMap());
}
int time = (int) (stopTime - startTime) / 1000;

session.close();
return new QueryResult("get", time, 0, 0, null, null, Arrays.asList(new Network()));
}

@Override
public QueryResult getAnnotations(Query query, String annotateField) {
Session session = this.driver.session();

QueryOptions queryOptions = new QueryOptions("include", annotateField);
long startTime = System.currentTimeMillis();
StringBuilder myQuery = new StringBuilder();
Expand Down Expand Up @@ -864,69 +893,115 @@ public QueryResult getAnnotations(Query query, String annotateField) {
System.out.println(run.next().asMap());
}
int time = (int) (stopTime - startTime) / 1000;

session.close();
return new QueryResult("get", time, 0, 0, null, null, Arrays.asList(new Network()));
}

private int getTotalNodes() {
return this.session.run("MATCH (n) RETURN count(n) AS count").peek().get("count").asInt();
Session session = this.driver.session();
int count = session.run("MATCH (n) RETURN count(n) AS count").peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n) RETURN count(n) AS count").peek().get("count").asInt();
}

private int getTotalRelationships() {
return this.session.run("MATCH ()-[r]-() RETURN count(r) AS count").peek().get("count").asInt();
Session session = this.driver.session();
int count = session.run("MATCH ()-[r]-() RETURN count(r) AS count").peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH ()-[r]-() RETURN count(r) AS count").peek().get("count").asInt();
}

private ObjectMap getTotalPhysicalEntities() {
Session session = this.driver.session();

ObjectMap myResult = new ObjectMap();
myResult.put("undefined", this.session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
myResult.put("undefined", session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
+ PhysicalEntity.Type.UNDEFINED + ") RETURN count(n) AS count")).peek().get("count").asInt());
myResult.put("protein", this.session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
myResult.put("protein", session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
+ PhysicalEntity.Type.PROTEIN + ") RETURN count(n) AS count")).peek().get("count").asInt());
myResult.put("dna", this.session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
myResult.put("dna", session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
+ PhysicalEntity.Type.DNA + ") RETURN count(n) AS count")).peek().get("count").asInt());
myResult.put("rna", this.session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
myResult.put("rna", session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
+ PhysicalEntity.Type.RNA + ") RETURN count(n) AS count")).peek().get("count").asInt());
myResult.put("complex", this.session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
myResult.put("complex", session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
+ PhysicalEntity.Type.COMPLEX + ") RETURN count(n) AS count")).peek().get("count").asInt());
myResult.put("small_molecule", this.session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
myResult.put("small_molecule", session.run(("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ":"
+ PhysicalEntity.Type.SMALL_MOLECULE + ") RETURN count(n) AS count")).peek().get("count").asInt());
int total = 0;
for (String key : myResult.keySet()) {
total += (int) myResult.get(key);
}
myResult.put("totalPE", total);

session.close();
return myResult;
}

private int getTotalXrefNodes() {
return this.session.run("MATCH (n:" + NodeTypes.XREF + ") RETURN count(n) AS count")
Session session = this.driver.session();
int count = session.run("MATCH (n:" + NodeTypes.XREF + ") RETURN count(n) AS count")
.peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n:" + NodeTypes.XREF + ") RETURN count(n) AS count")
// .peek().get("count").asInt();
}

private int getTotalXrefRelationships() {
return this.session.run("MATCH (n:"
Session session = this.driver.session();
int count = session.run("MATCH (n:"
+ NodeTypes.PHYSICAL_ENTITY + ")-[r:" + RelTypes.XREF + "]->(m:"
+ NodeTypes.XREF + ") RETURN count(r) AS count").peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n:"
// + NodeTypes.PHYSICAL_ENTITY + ")-[r:" + RelTypes.XREF + "]->(m:"
// + NodeTypes.XREF + ") RETURN count(r) AS count").peek().get("count").asInt();
}

private int getTotalOntologyNodes() {
return this.session.run("MATCH (n:" + NodeTypes.ONTOLOGY + ") RETURN count(n) AS count")
Session session = this.driver.session();
int count = session.run("MATCH (n:" + NodeTypes.ONTOLOGY + ") RETURN count(n) AS count")
.peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n:" + NodeTypes.ONTOLOGY + ") RETURN count(n) AS count")
// .peek().get("count").asInt();
}

private int getTotalOntologyRelationships() {
return this.session.run("MATCH (n)-[r:" + RelTypes.ONTOLOGY + "|" + RelTypes.CELLOC_ONTOLOGY
Session session = this.driver.session();
int count = session.run("MATCH (n)-[r:" + RelTypes.ONTOLOGY + "|" + RelTypes.CELLOC_ONTOLOGY
+ "]->(m:" + NodeTypes.ONTOLOGY + ") RETURN count(r) AS count").peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n)-[r:" + RelTypes.ONTOLOGY + "|" + RelTypes.CELLOC_ONTOLOGY
// + "]->(m:" + NodeTypes.ONTOLOGY + ") RETURN count(r) AS count").peek().get("count").asInt();
}

private int getTotalCelLocationNodes() {
return this.session.run("MATCH (n:" + NodeTypes.CELLULAR_LOCATION + ") RETURN count(n) AS count")
Session session = this.driver.session();
int count = session.run("MATCH (n:" + NodeTypes.CELLULAR_LOCATION + ") RETURN count(n) AS count")
.peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n:" + NodeTypes.CELLULAR_LOCATION + ") RETURN count(n) AS count")
// .peek().get("count").asInt();
}

private int getTotalCelLocationRelationships() {
return this.session.run("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY
Session session = this.driver.session();
int count = session.run("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY
+ ")-[r:" + RelTypes.CELLULAR_LOCATION + "]->(m:" + NodeTypes.CELLULAR_LOCATION
+ ") RETURN count(r) AS count").peek().get("count").asInt();
session.close();
return count;
// return this.session.run("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY
// + ")-[r:" + RelTypes.CELLULAR_LOCATION + "]->(m:" + NodeTypes.CELLULAR_LOCATION
// + ") RETURN count(r) AS count").peek().get("count").asInt();
}

@Override
Expand Down Expand Up @@ -983,6 +1058,7 @@ public QueryResult clusteringCoefficient(Query query) {
// clusteringCoefficient = r/NumberOfPossibleConnectionsBetweenTwoNeighbors. Where:
// NumberOfPossibleConnectionsBetweenTwoNeighbors: n!/(2!(n-2)!).

Session session = this.driver.session();
long startTime = System.currentTimeMillis();

String ids = query.getString("id");
Expand All @@ -999,7 +1075,7 @@ public QueryResult clusteringCoefficient(Query query) {
cypherQuery.append(" RETURN a.name, c.id, n, count(DISTINCT r) AS r");

System.out.println(cypherQuery.toString());
StatementResult execute = this.session.run(cypherQuery.toString());
StatementResult execute = session.run(cypherQuery.toString());

StringBuilder sb = new StringBuilder();
if (execute.hasNext()) {
Expand Down Expand Up @@ -1028,15 +1104,16 @@ public QueryResult clusteringCoefficient(Query query) {

int time = (int) (System.currentTimeMillis() - startTime);

session.close();
return new QueryResult<>("clustCoeff", time, 1, 1, null, null, Arrays.asList(sb.toString()));
}

public boolean isClosed() {
return !this.session.isOpen();
}
// public boolean isClosed() {
// return !this.session.isOpen();
// }

public void close() {
this.session.close();
// this.session.close();
this.driver.close();
}

Expand Down

0 comments on commit f3cb35a

Please sign in to comment.