use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.
the class OGraphCommandExecutorSQLFactory method runInConfiguredTxMode.
public static <T> T runInConfiguredTxMode(final GraphCallBack<T> callBack) {
final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
final ODatabaseDocumentInternal curDb = ODatabaseRecordThreadLocal.INSTANCE.get();
final boolean txAlreadyBegun = curDb.getTransaction().isActive();
OrientBaseGraph graph = null;
try {
if (isTxRequiredForSQLGraphOperations()) {
graph = OGraphCommandExecutorSQLFactory.getGraph(true, shutdownFlag);
return runInTx((OrientGraph) graph, callBack);
} else {
graph = getAnyGraph(shutdownFlag);
return callBack.call(graph);
}
} finally {
if (graph != null) {
if (!txAlreadyBegun) {
graph.commit();
if (shutdownFlag.getValue())
graph.shutdown(false, false);
}
}
ODatabaseRecordThreadLocal.INSTANCE.set(curDb);
}
}
use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.
the class OGraphCommandExecutorSQLFactory method getAnyGraph.
/**
* @return any graph if available, otherwise a Non Transactional OrientGraph implementation from the current database in thread
* local.
*/
public static OrientBaseGraph getAnyGraph(final OModifiableBoolean shouldBeShutDown) {
final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
if (result != null) {
final ODatabaseDocument graphDb = result.getRawGraph();
// CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
if (canReuseActiveGraph(graphDb, database)) {
if (!graphDb.isClosed()) {
graphDb.activateOnCurrentThread();
shouldBeShutDown.setValue(false);
return result;
}
}
}
// Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
shouldBeShutDown.setValue(true);
ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
return (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database);
}
use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.
the class OGraphCommandExecutorSQLFactory method runWithAnyGraph.
public static <T> T runWithAnyGraph(final GraphCallBack<T> callBack) {
final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
final ODatabaseDocumentInternal curDb = ODatabaseRecordThreadLocal.INSTANCE.get();
final OrientBaseGraph graph = OGraphCommandExecutorSQLFactory.getAnyGraph(shutdownFlag);
try {
return callBack.call(graph);
} finally {
if (shutdownFlag.getValue())
graph.shutdown(false, false);
ODatabaseRecordThreadLocal.INSTANCE.set(curDb);
}
}
use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.
the class OGraphCommandExecutorSQLFactory method getGraph.
/**
* Returns a Transactional OrientGraph implementation from the current database in thread local.
*
* @param autoStartTx
* Whether returned graph will start transaction before each operation till commit automatically or user should do it
* explicitly be calling {@link OrientGraph#getRawGraph()} method {@link ODatabaseDocumentTx#begin()}.
*
* @return Transactional OrientGraph implementation from the current database in thread local.
*/
public static OrientGraph getGraph(final boolean autoStartTx, OModifiableBoolean shouldBeShutDown) {
final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
if (result != null && (result instanceof OrientGraph)) {
final ODatabaseDocumentTx graphDb = result.getRawGraph();
// CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
if (canReuseActiveGraph(graphDb, database)) {
if (!graphDb.isClosed()) {
ODatabaseRecordThreadLocal.INSTANCE.set(graphDb);
if (autoStartTx && autoTxStartRequired(graphDb))
((OrientGraph) result).begin();
shouldBeShutDown.setValue(false);
return (OrientGraph) result;
}
}
}
// Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
shouldBeShutDown.setValue(true);
final OrientGraph g = (OrientGraph) OrientGraphFactory.getTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database, false);
if (autoStartTx && autoTxStartRequired(database))
g.begin();
return g;
}
use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateEdge method parse.
@SuppressWarnings("unchecked")
public OCommandExecutorSQLCreateEdge parse(final OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
String originalQuery = queryText;
try {
// System.out.println("NEW PARSER FROM: " + queryText);
queryText = preParse(queryText, iRequest);
// System.out.println("NEW PARSER TO: " + queryText);
textRequest.setText(queryText);
final ODatabaseDocument database = getDatabase();
init((OCommandRequestText) iRequest);
parserRequiredKeyword("CREATE");
parserRequiredKeyword("EDGE");
String className = null;
String tempLower = parseOptionalWord(false);
String temp = tempLower == null ? null : tempLower.toUpperCase(Locale.ENGLISH);
while (temp != null) {
if (temp.equals("CLUSTER")) {
clusterName = parserRequiredWord(false);
} else if (temp.equals(KEYWORD_FROM)) {
from = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
} else if (temp.equals("TO")) {
to = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
} else if (temp.equals(KEYWORD_SET)) {
fields = new ArrayList<OPair<String, Object>>();
parseSetFields(clazz, fields);
} else if (temp.equals(KEYWORD_CONTENT)) {
parseContent();
} else if (temp.equals(KEYWORD_BATCH)) {
temp = parserNextWord(true);
if (temp != null)
batch = Integer.parseInt(temp);
} else if (className == null && temp.length() > 0) {
className = tempLower;
OrientBaseGraph graph = OrientBaseGraph.getActiveGraph();
if (graph != null && graph.isUseClassForEdgeLabel()) {
clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(temp);
} else {
clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass("E");
}
}
temp = parseOptionalWord(true);
if (parserIsEnded())
break;
}
if (className == null) {
// ASSIGN DEFAULT CLASS
className = OrientEdgeType.CLASS_NAME;
clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(className);
}
// GET/CHECK CLASS NAME
if (clazz == null)
throw new OCommandSQLParsingException("Class '" + className + "' was not found");
edgeLabel = className;
} finally {
textRequest.setText(originalQuery);
}
return this;
}
Aggregations