Search in sources :

Example 16 with TitanTransaction

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();
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) Test(org.junit.Test)

Example 17 with TitanTransaction

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();
    }
}
Also used : TitanException(com.thinkaurelius.titan.core.TitanException) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction)

Example 18 with TitanTransaction

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;
}
Also used : Order(org.apache.tinkerpop.gremlin.process.traversal.Order) ArrayList(java.util.ArrayList) List(java.util.List) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) Comparator(java.util.Comparator)

Example 19 with TitanTransaction

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();
}
Also used : Graph(org.apache.tinkerpop.gremlin.structure.Graph) TitanBlueprintsGraph(com.thinkaurelius.titan.graphdb.tinkerpop.TitanBlueprintsGraph) FulgoraElementTraversal(com.thinkaurelius.titan.graphdb.olap.computer.FulgoraElementTraversal) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) TitanBlueprintsGraph(com.thinkaurelius.titan.graphdb.tinkerpop.TitanBlueprintsGraph)

Example 20 with TitanTransaction

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();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Geoshape(com.thinkaurelius.titan.core.attribute.Geoshape) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Aggregations

TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)24 Test (org.junit.Test)12 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)10 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)10 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)5 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)5 SchemaViolationException (com.thinkaurelius.titan.core.SchemaViolationException)4 TitanException (com.thinkaurelius.titan.core.TitanException)4 PerformanceTest (com.thinkaurelius.titan.testutil.PerformanceTest)4 Vertex (com.tinkerpop.blueprints.Vertex)4 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 TitanConfigurationException (com.thinkaurelius.titan.core.TitanConfigurationException)3 TitanKey (com.thinkaurelius.titan.core.TitanKey)3 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)3 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)3 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)2 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)2 InternalVertex (com.thinkaurelius.titan.graphdb.internal.InternalVertex)2 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)2