use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanGraphTest method testTransactionIsolation.
/**
* Verifies transactional isolation and internal vertex existence checking
*/
@Test
public void testTransactionIsolation() {
// Create edge label before attempting to write it from concurrent transactions
makeLabel("knows");
finishSchema();
TitanTransaction tx1 = graph.newTransaction();
TitanTransaction tx2 = graph.newTransaction();
//Verify that using vertices across transactions is prohibited
TitanVertex v11 = tx1.addVertex();
TitanVertex v12 = tx1.addVertex();
v11.addEdge("knows", v12);
TitanVertex v21 = tx2.addVertex();
try {
v21.addEdge("knows", v11);
fail();
} catch (IllegalStateException e) {
}
TitanVertex v22 = tx2.addVertex();
v21.addEdge("knows", v22);
tx2.commit();
try {
v22.addEdge("knows", v21);
fail();
} catch (IllegalStateException e) {
}
tx1.rollback();
try {
v11.property(VertexProperty.Cardinality.single, "test", 5);
fail();
} catch (IllegalStateException e) {
}
//Test unidirected edge with and without internal existence check
newTx();
v21 = getV(tx, v21);
tx.makeEdgeLabel("link").unidirected().make();
TitanVertex v3 = tx.addVertex();
v21.addEdge("link", v3);
newTx();
v21 = getV(tx, v21);
v3 = getOnlyElement(v21.query().direction(Direction.OUT).labels("link").vertices());
assertFalse(v3.isRemoved());
v3.remove();
newTx();
v21 = getV(tx, v21);
v3 = getOnlyElement(v21.query().direction(Direction.OUT).labels("link").vertices());
assertFalse(v3.isRemoved());
newTx();
TitanTransaction tx3 = graph.buildTransaction().checkInternalVertexExistence(true).start();
v21 = getV(tx3, v21);
v3 = getOnlyElement(v21.query().direction(Direction.OUT).labels("link").vertices());
assertTrue(v3.isRemoved());
tx3.commit();
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanGraphTest method executeLockConflictingTransactionJobs.
/**
* Executes a transaction job in two parallel transactions under the assumptions that the two transactions
* should conflict and the one committed later should throw a locking exception.
*
* @param graph
* @param job
*/
private void executeLockConflictingTransactionJobs(TitanGraph graph, TransactionJob job) {
TitanTransaction tx1 = graph.newTransaction();
TitanTransaction tx2 = graph.newTransaction();
job.run(tx1);
job.run(tx2);
/*
* Under pessimistic locking, tx1 should abort and tx2 should commit.
* Under optimistic locking, tx1 may commit and tx2 may abort.
*/
if (isLockingOptimistic()) {
tx1.commit();
try {
tx2.commit();
fail("Storage backend does not abort conflicting transactions");
} catch (TitanException e) {
}
} else {
try {
tx1.commit();
fail("Storage backend does not abort conflicting transactions");
} catch (TitanException e) {
}
tx2.commit();
}
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class HasStepFolder method validTitanOrder.
public static boolean validTitanOrder(OrderGlobalStep ostep, Traversal rootTraversal, boolean isVertexOrder) {
for (Comparator comp : (List<Comparator>) ostep.getComparators()) {
if (!(comp instanceof ElementValueComparator))
return false;
ElementValueComparator evc = (ElementValueComparator) comp;
if (!(evc.getValueComparator() instanceof Order))
return false;
TitanTransaction tx = TitanTraversalUtil.getTx(rootTraversal.asAdmin());
String key = evc.getPropertyKey();
PropertyKey pkey = tx.getPropertyKey(key);
if (pkey == null || !(Comparable.class.isAssignableFrom(pkey.dataType())))
return false;
if (isVertexOrder && pkey.cardinality() != Cardinality.SINGLE)
return false;
}
return true;
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanTraversalUtil method getTx.
public static TitanTransaction getTx(Traversal.Admin<?, ?> traversal) {
TitanTransaction tx = null;
Optional<Graph> optGraph = TraversalHelper.getRootTraversal(traversal.asAdmin()).getGraph();
if (traversal instanceof FulgoraElementTraversal) {
tx = (TitanTransaction) optGraph.get();
} else {
if (!optGraph.isPresent())
throw new IllegalArgumentException("Traversal is not bound to a graph: " + traversal);
Graph graph = optGraph.get();
if (graph instanceof TitanTransaction)
tx = (TitanTransaction) graph;
else if (graph instanceof TitanBlueprintsGraph)
tx = ((TitanBlueprintsGraph) graph).getCurrentThreadTx();
else
throw new IllegalArgumentException("Traversal is not bound to a Titan Graph, but: " + graph);
}
if (tx == null)
throw new IllegalArgumentException("Not a valid start step for a Titan traversal: " + traversal);
if (tx.isOpen())
return tx;
else
return ((StandardTitanTx) tx).getNextTx();
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class GraphOfTheGodsFactory method load.
public static void load(final TitanGraph graph, String mixedIndexName, boolean uniqueNameCompositeIndex) {
//Create Schema
TitanManagement mgmt = graph.openManagement();
final PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
TitanManagement.IndexBuilder nameIndexBuilder = mgmt.buildIndex("name", Vertex.class).addKey(name);
if (uniqueNameCompositeIndex)
nameIndexBuilder.unique();
TitanGraphIndex namei = nameIndexBuilder.buildCompositeIndex();
mgmt.setConsistency(namei, ConsistencyModifier.LOCK);
final PropertyKey age = mgmt.makePropertyKey("age").dataType(Integer.class).make();
if (null != mixedIndexName)
mgmt.buildIndex("vertices", Vertex.class).addKey(age).buildMixedIndex(mixedIndexName);
final PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).make();
final PropertyKey reason = mgmt.makePropertyKey("reason").dataType(String.class).make();
final PropertyKey place = mgmt.makePropertyKey("place").dataType(Geoshape.class).make();
if (null != mixedIndexName)
mgmt.buildIndex("edges", Edge.class).addKey(reason).addKey(place).buildMixedIndex(mixedIndexName);
mgmt.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
mgmt.makeEdgeLabel("mother").multiplicity(Multiplicity.MANY2ONE).make();
EdgeLabel battled = mgmt.makeEdgeLabel("battled").signature(time).make();
mgmt.buildEdgeIndex(battled, "battlesByTime", Direction.BOTH, Order.decr, time);
mgmt.makeEdgeLabel("lives").signature(reason).make();
mgmt.makeEdgeLabel("pet").make();
mgmt.makeEdgeLabel("brother").make();
mgmt.makeVertexLabel("titan").make();
mgmt.makeVertexLabel("location").make();
mgmt.makeVertexLabel("god").make();
mgmt.makeVertexLabel("demigod").make();
mgmt.makeVertexLabel("human").make();
mgmt.makeVertexLabel("monster").make();
mgmt.commit();
TitanTransaction tx = graph.newTransaction();
// vertices
Vertex saturn = tx.addVertex(T.label, "titan", "name", "saturn", "age", 10000);
Vertex sky = tx.addVertex(T.label, "location", "name", "sky");
Vertex sea = tx.addVertex(T.label, "location", "name", "sea");
Vertex jupiter = tx.addVertex(T.label, "god", "name", "jupiter", "age", 5000);
Vertex neptune = tx.addVertex(T.label, "god", "name", "neptune", "age", 4500);
Vertex hercules = tx.addVertex(T.label, "demigod", "name", "hercules", "age", 30);
Vertex alcmene = tx.addVertex(T.label, "human", "name", "alcmene", "age", 45);
Vertex pluto = tx.addVertex(T.label, "god", "name", "pluto", "age", 4000);
Vertex nemean = tx.addVertex(T.label, "monster", "name", "nemean");
Vertex hydra = tx.addVertex(T.label, "monster", "name", "hydra");
Vertex cerberus = tx.addVertex(T.label, "monster", "name", "cerberus");
Vertex tartarus = tx.addVertex(T.label, "location", "name", "tartarus");
// edges
jupiter.addEdge("father", saturn);
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
jupiter.addEdge("brother", neptune);
jupiter.addEdge("brother", pluto);
neptune.addEdge("lives", sea).property("reason", "loves waves");
neptune.addEdge("brother", jupiter);
neptune.addEdge("brother", pluto);
hercules.addEdge("father", jupiter);
hercules.addEdge("mother", alcmene);
hercules.addEdge("battled", nemean, "time", 1, "place", Geoshape.point(38.1f, 23.7f));
hercules.addEdge("battled", hydra, "time", 2, "place", Geoshape.point(37.7f, 23.9f));
hercules.addEdge("battled", cerberus, "time", 12, "place", Geoshape.point(39f, 22f));
pluto.addEdge("brother", jupiter);
pluto.addEdge("brother", neptune);
pluto.addEdge("lives", tartarus, "reason", "no fear of death");
pluto.addEdge("pet", cerberus);
cerberus.addEdge("lives", tartarus);
// commit the transaction to disk
tx.commit();
}
Aggregations