use of com.orientechnologies.orient.core.db.document.ODatabaseDocument 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.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class OGraphCommandExecutorSQLFactory method runInTx.
public static <T> T runInTx(final OrientGraph graph, final GraphCallBack<T> callBack) {
final ODatabaseDocument databaseRecord = getDatabase();
final boolean txWasActive = databaseRecord.getTransaction().isActive();
if (!txWasActive)
graph.getRawGraph().begin();
try {
final T result = callBack.call(graph);
if (!txWasActive)
graph.commit();
return result;
} catch (RuntimeException e) {
if (!txWasActive)
graph.rollback();
throw e;
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class IndexTest method testLinkedIndexedPropertyInTx.
@Test(dependsOnMethods = "linkedIndexedProperty")
public void testLinkedIndexedPropertyInTx() {
ODatabaseDocument db = new ODatabaseDocumentTx(database.getURL());
db.open("admin", "admin");
db.begin();
ODocument testClassDocument = db.newInstance("TestClass");
testClassDocument.field("name", "Test Class 2");
ODocument testLinkClassDocument = new ODocument("TestLinkClass");
testLinkClassDocument.field("testString", "Test Link Class 2");
testLinkClassDocument.field("testBoolean", true);
testClassDocument.field("testLink", testLinkClassDocument);
testClassDocument.save();
db.commit();
// THIS WILL THROW A java.lang.ClassCastException: com.orientechnologies.orient.core.id.ORecordId cannot be cast to
// java.lang.Boolean
List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>("select from TestClass where testLink.testBoolean = true"));
Assert.assertEquals(result.size(), 2);
// THIS WILL THROW A java.lang.ClassCastException: com.orientechnologies.orient.core.id.ORecordId cannot be cast to
// java.lang.String
result = db.query(new OSQLSynchQuery<ODocument>("select from TestClass where testLink.testString = 'Test Link Class 2'"));
Assert.assertEquals(result.size(), 1);
db.close();
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class IndexTest method createInheritanceIndex.
public void createInheritanceIndex() {
ODatabaseDocument db = new ODatabaseDocumentTx(database.getURL());
try {
db.open("admin", "admin");
if (!db.getMetadata().getSchema().existsClass("BaseTestClass")) {
OClass baseClass = db.getMetadata().getSchema().createClass("BaseTestClass", 1, null);
OClass childClass = db.getMetadata().getSchema().createClass("ChildTestClass", 1, null);
OClass anotherChildClass = db.getMetadata().getSchema().createClass("AnotherChildTestClass", 1, null);
if (!baseClass.isSuperClassOf(childClass))
childClass.setSuperClass(baseClass);
if (!baseClass.isSuperClassOf(anotherChildClass))
anotherChildClass.setSuperClass(baseClass);
baseClass.createProperty("testParentProperty", OType.LONG).createIndex(OClass.INDEX_TYPE.NOTUNIQUE);
db.getMetadata().getSchema().save();
}
ODocument childClassDocument = db.newInstance("ChildTestClass");
childClassDocument.field("testParentProperty", 10L);
childClassDocument.save();
ODocument anotherChildClassDocument = db.newInstance("AnotherChildTestClass");
anotherChildClassDocument.field("testParentProperty", 11L);
anotherChildClassDocument.save();
Assert.assertFalse(new ORecordId(-1, ORID.CLUSTER_POS_INVALID).equals(childClassDocument.getIdentity()));
Assert.assertFalse(new ORecordId(-1, ORID.CLUSTER_POS_INVALID).equals(anotherChildClassDocument.getIdentity()));
} finally {
db.close();
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class IndexTest method testConcurrentRemoveDelete.
public void testConcurrentRemoveDelete() {
ODatabaseDocument db = new ODatabaseDocumentTx(database.getURL());
db.open("admin", "admin");
if (!db.getMetadata().getSchema().existsClass("MyFruit")) {
OClass fruitClass = db.getMetadata().getSchema().createClass("MyFruit", 1, null);
fruitClass.createProperty("name", OType.STRING);
fruitClass.createProperty("color", OType.STRING);
db.getMetadata().getSchema().getClass("MyFruit").getProperty("name").createIndex(OClass.INDEX_TYPE.UNIQUE);
db.getMetadata().getSchema().getClass("MyFruit").getProperty("color").createIndex(OClass.INDEX_TYPE.NOTUNIQUE);
db.getMetadata().getSchema().save();
}
long expectedIndexSize = 0;
final int passCount = 10;
final int chunkSize = getEnvironment() == DatabaseAbstractTest.ENV.DEV ? 10 : 1000;
for (int pass = 0; pass < passCount; pass++) {
List<ODocument> recordsToDelete = new ArrayList<ODocument>();
db.begin();
for (int i = 0; i < chunkSize; i++) {
ODocument d = new ODocument("MyFruit").field("name", "ABC" + pass + 'K' + i).field("color", "FOO" + pass);
d.save();
if (i < chunkSize / 2) {
recordsToDelete.add(d);
}
}
db.commit();
expectedIndexSize += chunkSize;
Assert.assertEquals(db.getMetadata().getIndexManager().getClassIndex("MyFruit", "MyFruit.color").getSize(), expectedIndexSize, "After add");
// do delete
db.begin();
for (final ODocument recordToDelete : recordsToDelete) {
Assert.assertNotNull(db.delete(recordToDelete));
}
db.commit();
expectedIndexSize -= recordsToDelete.size();
Assert.assertEquals(db.getMetadata().getIndexManager().getClassIndex("MyFruit", "MyFruit.color").getSize(), expectedIndexSize, "After delete");
}
db.close();
}
Aggregations