net.alantea.redpill.internal.GraphManager Maven / Gradle / Ivy
package net.alantea.redpill.internal;
import java.io.File;
import java.util.Map;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import net.alantea.redpill.DbRunnable;
import net.alantea.redpill.exceptions.DatabaseException;
public class GraphManager
{
/** Real database. */
private GraphDatabaseService graphDb;
private int transactionLevel = 0;
/**
* Instantiates a new database.
*
* @param path the path
* @throws DatabaseException the database exception
*/
protected GraphManager(String path) throws DatabaseException
{
if (path == null)
{
throw new DatabaseException(path);
}
File file = new File(path);
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(file);
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
// public GraphManager(String url, String user, String password)
// {
// System.exit(0);
// // graphDb = new CypherRestGraphDatabase (url, user, password);
// }
/**
* Gets the graph db.
*
* @return the graphDb
*/
protected GraphDatabaseService getGraphDb()
{
return graphDb;
}
/**
* Close the graph db.
*
* @throws DatabaseException the database exception
*/
public void closeGraphDb() throws DatabaseException
{
if (! graphDb.isAvailable(100))
{
throw new DatabaseException("Unable to shut down database");
}
graphDb.shutdown();
}
/**
* Clear the graph db.
*
* @throws DatabaseException the database exception
*/
public void clearGraphDb() throws DatabaseException
{
if (! graphDb.isAvailable(100))
{
throw new DatabaseException("Unable to shut down database");
}
for (Relationship relation : graphDb.getAllRelationships())
{
relation.delete();
}
for (Node node : graphDb.getAllNodes())
{
node.delete();
}
}
/**
* Do in transaction.
*
* @param runnable the runnable
* @throws DatabaseException the database exception
*/
public void doInTransaction(Runnable runnable) throws DatabaseException
{
transactionLevel++;
if (transactionLevel == 1)
{
try ( Transaction tx = getGraphDb().beginTx() )
{
runnable.run();
tx.success();
}
}
else
{
runnable.run();
}
transactionLevel--;
}
/**
* Gets the in transaction.
*
* @param the generic type
* @param runnable the runnable
* @return the in transaction
* @throws DatabaseException the database exception
*/
@SuppressWarnings("unchecked")
public final T getInTransaction(DbRunnable runnable) throws DatabaseException
{
Object ret = null;
transactionLevel++;
if (transactionLevel == 1)
{
try ( Transaction tx = getGraphDb().beginTx() )
{
ret = internalGetInTransaction(runnable);
if (!(ret instanceof DatabaseException))
{
tx.success();
}
}
}
else
{
ret = internalGetInTransaction(runnable);
}
transactionLevel--;
if (ret instanceof DatabaseException)
{
throw (DatabaseException)ret;
}
return (T) ret;
}
/**
* Gets the in transaction.
*
* @param the generic type
* @param runnable the runnable
* @return the in transaction
* @throws DatabaseException
*/
private Object internalGetInTransaction(DbRunnable runnable)
{
Object ret = null;
transactionLevel++;
try
{
if (transactionLevel == 1)
{
try ( Transaction tx = getGraphDb().beginTx() )
{
ret = runnable.run();
tx.success();
}
}
else
{
ret = runnable.run();
}
}
catch (Exception e)
{
ret = e;
}
transactionLevel--;
return ret;
}
/**
* Execute a query.
*
* @param query the query
* @return the result
* @throws DatabaseException the database exception
*/
public Result execute(String query) throws DatabaseException
{
return (Result) getInTransaction(() -> {
return getGraphDb().execute(query);
});
}
/**
* Execute a query.
*
* @param query the query
* @param map the query parameters map
* @return the result
* @throws DatabaseException the database exception
*/
public Result execute(String query, Map map) throws DatabaseException
{
return (Result) getInTransaction(() -> {
return getGraphDb().execute(query, map);
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy