Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save salvatore-piccione/2859180 to your computer and use it in GitHub Desktop.

Select an option

Save salvatore-piccione/2859180 to your computer and use it in GitHub Desktop.
Testing SQL INSERT within a transaction
package org.example.orientdb;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexManager;
import com.orientechnologies.orient.core.index.OSimpleKeyIndexDefinition;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.core.tx.OTransaction.TXTYPE;
import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
/**
*
* @author Salvatore Piccione (salvo.picci AT gmail DOT com)
*
*/
public class TestIndexUsageInTransaction {
private static final String DB_URL = "remote:localhost/myGraphDB4Trans";
private static final String USR = "admin";
private static final String PWD = "admin";
private static final String ADMIN_USR = "root";
private static final String ADMIN_PWD = "36DE7B45EDB913E44EB7E8C34D5D3C43E979F361B3D8AAC5CA126ADAD36F4C16";
private static final String INDEX_NAME = "aManualIdx";
@Before
public void defineGraph () throws IOException, InterruptedException {
OServerAdmin adminConsole = new OServerAdmin(DB_URL).connect(ADMIN_USR, ADMIN_PWD);
if (adminConsole.existsDatabase())
adminConsole.dropDatabase();
adminConsole.createDatabase("graph", "local");
adminConsole.close();
OGraphDatabase db = new OGraphDatabase(DB_URL).open(USR, PWD);
//do not create the index through the SQL statement because it doesn't work!
OIndexManager idxManager = db.getMetadata().getIndexManager();
idxManager.createIndex(INDEX_NAME, "UNIQUE", new OSimpleKeyIndexDefinition(OType.INTEGER), db.getVertexBaseClass().getClusterIds(),null);
ODocument v;
ODocument iSourceVertex = null;
ODocument iDestVertex = null;
for (int i = 0; i < 50; i++) {
v = db.createVertex();
v.field("counter", i);
if (iSourceVertex == null)
iSourceVertex = v;
else if (iDestVertex == null) {
iDestVertex = v;
db.createEdge(iSourceVertex, iDestVertex);
iSourceVertex = v;
iDestVertex = null;
}
v.save();
if (i % 2 == 0)
db.command(new OCommandSQL("INSERT INTO index:" + INDEX_NAME +
"(key,rid) VALUES (" + i + "," + v.getIdentity().toString() + ")")).execute();
}
assertEquals(50L,db.countVertexes());
assertEquals(49L,db.countEdges());
db.close();
}
// @SuppressWarnings("unchecked")
@Test
public void testBlueprintsAPI () throws Throwable {
System.out.println("#####----- BLUEPRINTS + NATIVE API TEST -----#####");
OrientGraph graph = new OrientGraph(DB_URL, USR, PWD);
OGraphDatabase rawGraph = graph.getRawGraph();
boolean isRolledBack = false;
OIndexManager idxManager;
OIndex<Vertex> idx;
try {
//define a new vertex through API: this vertex will be removed on transaction rollback
Vertex v = graph.addVertex(null);
v.setProperty("counter", 52);
//define a new vertex through SQL: this vertex will be alive on transaction rollback
Vertex v2 = graph.addVertex(null);
v2.setProperty("counter", 54);
graph.addEdge(null, v, v2, "conn");
//This works!
idxManager = rawGraph.getMetadata().getIndexManager();
idx = (OIndex<Vertex>) idxManager.getIndex(INDEX_NAME);
assertNotNull(idx);
idx.put(52,((OrientVertex) v).getRawVertex());
idx.remove(0);
graph.stopTransaction(Conclusion.SUCCESS);
} catch (Throwable e) {
System.err.println("An exception has been caught");
graph.stopTransaction(Conclusion.FAILURE);
isRolledBack = true;
throw e;
} finally {
graph.shutdown();
verifyIndexModification(isRolledBack, 52, 0, 54);
}
}
@Test
public void testNativeAPI () throws Throwable {
System.out.println("#####----- ONLY NATIVE API TEST -----#####");
OGraphDatabase rawGraph = new OGraphDatabase(DB_URL);
rawGraph.open(USR, PWD);
ODatabaseRecordThreadLocal.INSTANCE.set(rawGraph);
boolean isRolledBack = false;
try {
rawGraph.begin(TXTYPE.OPTIMISTIC);
//define a new vertex through API: this vertex will be removed on transaction rollback
ODocument v = rawGraph.createVertex(rawGraph.getVertexBaseClass().getName());
v.field("counter", 52);
ODocument v2 = rawGraph.createVertex(rawGraph.getVertexBaseClass().getName());
v2.field("counter", 54);
//if you define edges, they do not work!
ODocument edge = rawGraph.createEdge(v, v2);
edge.field("label", "conn2");
edge.save();
v2.save();
v.save();
//this works!
OIndexManager idxManager = rawGraph.getMetadata().getIndexManager();
OIndex<Vertex> idx = (OIndex<Vertex>) idxManager.getIndex(INDEX_NAME);
assertNotNull(idx);
idx.remove(0);
idx.put(52,v);
rawGraph.commit();
} catch (Throwable e) {
rawGraph.rollback();
System.out.println("Rollback");
isRolledBack = true;
throw e;
} finally {
rawGraph.close();
verifyIndexModification(isRolledBack, 52, 0, 54);
}
}
@Test
public void testNativeAPIRollback () throws Throwable {
System.out.println("#####----- ROLLBACK TEST -----#####");
OGraphDatabase rawGraph = new OGraphDatabase(DB_URL);
rawGraph.open(USR, PWD);
ODatabaseRecordThreadLocal.INSTANCE.set(rawGraph);
boolean isRolledBack = false;
try {
rawGraph.begin(TXTYPE.OPTIMISTIC);
//define a new vertex through API: this vertex will be removed on transaction rollback
ODocument v = rawGraph.createVertex(rawGraph.getVertexBaseClass().getName());
v.field("counter", 52);
ODocument v2 = rawGraph.createVertex(rawGraph.getVertexBaseClass().getName());
v2.field("counter", 54);
//if you define edges, they do not work!
ODocument edge = rawGraph.createEdge(v, v2);
edge.field("label", "conn2");
edge.save();
v2.save();
v.save();
OIndexManager idxManager = rawGraph.getMetadata().getIndexManager();
OIndex<Vertex> idx = (OIndex<Vertex>) idxManager.getIndex(INDEX_NAME);
assertNotNull(idx);
idx.remove(0);
idx.put(52,v);
idx.put(null,null);
rawGraph.commit();
} catch (Throwable e) {
e.printStackTrace();
rawGraph.rollback();
isRolledBack = true;
} finally {
rawGraph.close();
verifyIndexModification(isRolledBack, 52, 0, 54);
}
}
private void verifyIndexModification (boolean isRolledBack, int addedItemKey, int removedItemKey, int isRolledBackItemKey) throws Throwable {
OGraphDatabase graph = new OGraphDatabase(DB_URL);
graph.open(USR, PWD);
try {
//verify index content
OIndex<?> idx = graph.getMetadata().getIndexManager().getIndex(INDEX_NAME);
assertNotNull(idx);
if (isRolledBack) {
assertFalse(idx.contains(addedItemKey));
System.out.println("The index DOESN'T CONTAIN the added item");
assertTrue(idx.contains(removedItemKey));
System.out.println("The index still CONTAINS the deleted item");
} else {
assertTrue(idx.contains(addedItemKey));
System.out.println("The index CONTAINS the added item");
assertFalse(idx.contains(removedItemKey));
System.out.println("The index DOESN'T CONTAIN the deleted item");
}
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("SELECT FROM " +
graph.getVertexBaseClass().getName() + " WHERE counter = ?");
List<ODocument> result = graph.query(query, isRolledBackItemKey);
if (isRolledBack) {
assertEquals(0,result.size());
System.out.println("The database DOESN'T CONTAIN the added vertex (NOT indexed) " + (0 < result.size()));
}
else {
assertEquals(1,result.size());
System.out.println("The database CONTAINS the added vertex (NOT indexed) " + (0 < result.size()));
}
query = new OSQLSynchQuery<ODocument>("SELECT FROM index:" +
INDEX_NAME + " WHERE key = " + addedItemKey);
result = graph.query(query, isRolledBackItemKey);
if (isRolledBack) {
assertEquals(0, result.size());
System.out.println("The index DOESN'T CONTAIN the added item (SQL query)");
} else {
assertTrue(result.size() == 1);
ORID rid = result.get(0).field("rid", ORID.class);
assertNotNull(rid);
System.out.println("The index CONTAINS the added item (SQL query)");
}
query = new OSQLSynchQuery<ODocument>("SELECT FROM index:" +
INDEX_NAME + " WHERE key = " + removedItemKey);
result = graph.query(query, isRolledBackItemKey);
if (isRolledBack) {
assertEquals(1, result.size());
System.out.println("The index still CONTAINS the deleted item");
} else {
assertTrue(result.size() == 0);
System.out.println("The index DOESN'T CONTAIN the deleted item");
}
} catch (Throwable t){
throw t;
} finally {
graph.close();
System.out.println("DB CONNECTION CLOSED");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment