Search in sources :

Example 6 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class TitanSchemaVertex method getRelated.

@Override
public Iterable<Entry> getRelated(TypeDefinitionCategory def, Direction dir) {
    assert dir == Direction.OUT || dir == Direction.IN;
    ListMultimap<TypeDefinitionCategory, Entry> rels = dir == Direction.OUT ? outRelations : inRelations;
    if (rels == null) {
        ImmutableListMultimap.Builder<TypeDefinitionCategory, Entry> b = ImmutableListMultimap.builder();
        Iterable<TitanEdge> edges;
        if (isLoaded()) {
            StandardTitanTx tx = tx();
            edges = (Iterable) RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseLabel.SchemaDefinitionEdge, dir), tx);
        } else {
            edges = query().type(BaseLabel.SchemaDefinitionEdge).direction(dir).edges();
        }
        for (TitanEdge edge : edges) {
            TitanVertex oth = edge.vertex(dir.opposite());
            assert oth instanceof TitanSchemaVertex;
            TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc);
            Object modifier = null;
            if (desc.getCategory().hasDataType()) {
                assert desc.getModifier() != null && desc.getModifier().getClass().equals(desc.getCategory().getDataType());
                modifier = desc.getModifier();
            }
            b.put(desc.getCategory(), new Entry((TitanSchemaVertex) oth, modifier));
        }
        rels = b.build();
        if (dir == Direction.OUT)
            outRelations = rels;
        else
            inRelations = rels;
    }
    assert rels != null;
    return rels.get(def);
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) TitanEdge(com.thinkaurelius.titan.core.TitanEdge)

Example 7 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class TitanLocalQueryOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!traversal.getGraph().isPresent())
        return;
    Graph graph = traversal.getGraph().get();
    //If this is a compute graph then we can't apply local traversal optimisation at this stage.
    StandardTitanGraph titanGraph = graph instanceof StandardTitanTx ? ((StandardTitanTx) graph).getGraph() : (StandardTitanGraph) graph;
    final boolean useMultiQuery = traversal.getEngine().isStandard() && titanGraph.getConfiguration().useMultiQuery();
    /*
                ====== VERTEX STEP ======
         */
    TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalStep -> {
        TitanVertexStep vstep = new TitanVertexStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vstep, traversal);
        if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
            HasStepFolder.foldInHasContainer(vstep, traversal);
        }
        assert TitanTraversalUtil.isEdgeReturnStep(vstep) || TitanTraversalUtil.isVertexReturnStep(vstep);
        Step nextStep = TitanTraversalUtil.getNextNonIdentityStep(vstep);
        if (nextStep instanceof RangeGlobalStep) {
            int limit = QueryUtil.convertLimit(((RangeGlobalStep) nextStep).getHighRange());
            vstep.setLimit(QueryUtil.mergeLimits(limit, vstep.getLimit()));
        }
        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    });
    /*
                ====== PROPERTIES STEP ======
         */
    TraversalHelper.getStepsOfClass(PropertiesStep.class, traversal).forEach(originalStep -> {
        TitanPropertiesStep vstep = new TitanPropertiesStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vstep, traversal);
        if (vstep.getReturnType().forProperties()) {
            HasStepFolder.foldInHasContainer(vstep, traversal);
        }
        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    });
    /*
                ====== EITHER INSIDE LOCAL ======
         */
    TraversalHelper.getStepsOfClass(LocalStep.class, traversal).forEach(localStep -> {
        Traversal.Admin localTraversal = ((LocalStep<?, ?>) localStep).getLocalChildren().get(0);
        Step localStart = localTraversal.getStartStep();
        if (localStart instanceof VertexStep) {
            TitanVertexStep vstep = new TitanVertexStep((VertexStep) localStart);
            TraversalHelper.replaceStep(localStart, vstep, localTraversal);
            if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
                HasStepFolder.foldInHasContainer(vstep, localTraversal);
                HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vstep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
        }
        if (localStart instanceof PropertiesStep) {
            TitanPropertiesStep vstep = new TitanPropertiesStep((PropertiesStep) localStart);
            TraversalHelper.replaceStep(localStart, vstep, localTraversal);
            if (vstep.getReturnType().forProperties()) {
                HasStepFolder.foldInHasContainer(vstep, localTraversal);
                HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vstep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
        }
    });
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep)

Example 8 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class StandardTitanGraph method closeInternal.

private synchronized void closeInternal() {
    if (!isOpen)
        return;
    Map<TitanTransaction, RuntimeException> txCloseExceptions = new HashMap<>();
    try {
        //Unregister instance
        String uniqueid = null;
        try {
            uniqueid = config.getUniqueGraphId();
            ModifiableConfiguration globalConfig = GraphDatabaseConfiguration.getGlobalSystemConfig(backend);
            globalConfig.remove(REGISTRATION_TIME, uniqueid);
        } catch (Exception e) {
            log.warn("Unable to remove graph instance uniqueid {}", uniqueid, e);
        }
        /* Assuming a couple of properties about openTransactions:
             * 1. no concurrent modifications during graph shutdown
             * 2. all contained txs are open
             */
        for (StandardTitanTx otx : openTransactions) {
            try {
                otx.close();
            } catch (RuntimeException e) {
                // Catch and store these exceptions, but proceed wit the loop
                // Any remaining txs on the iterator should get a chance to close before we throw up
                log.warn("Unable to close transaction {}", otx, e);
                txCloseExceptions.put(otx, e);
            }
        }
        super.close();
        IOUtils.closeQuietly(idAssigner);
        IOUtils.closeQuietly(backend);
        IOUtils.closeQuietly(queryCache);
        IOUtils.closeQuietly(serializer);
    } finally {
        isOpen = false;
    }
    // Throw an exception if at least one transaction failed to close
    if (1 == txCloseExceptions.size()) {
        // TP3's test suite requires that this be of type ISE
        throw new IllegalStateException("Unable to close transaction", Iterables.getOnlyElement(txCloseExceptions.values()));
    } else if (1 < txCloseExceptions.size()) {
        throw new IllegalStateException(String.format("Unable to close %s transactions (see warnings in log output for details)", txCloseExceptions.size()));
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class CassandraGraphTest method testCustomConfigUsedByTx.

@Test
public void testCustomConfigUsedByTx() {
    close();
    WriteConfiguration wc = getConfiguration();
    wc.set(ConfigElement.getPath(CASSANDRA_READ_CONSISTENCY), "ALL");
    wc.set(ConfigElement.getPath(CASSANDRA_WRITE_CONSISTENCY), "ALL");
    graph = (StandardTitanGraph) TitanFactory.open(wc);
    StandardTitanTx tx = (StandardTitanTx) graph.buildTransaction().customOption(ConfigElement.getPath(CASSANDRA_READ_CONSISTENCY), "ONE").customOption(ConfigElement.getPath(CASSANDRA_WRITE_CONSISTENCY), "TWO").start();
    assertEquals("ONE", tx.getTxHandle().getBaseTransactionConfig().getCustomOptions().get(AbstractCassandraStoreManager.CASSANDRA_READ_CONSISTENCY));
    assertEquals("TWO", tx.getTxHandle().getBaseTransactionConfig().getCustomOptions().get(AbstractCassandraStoreManager.CASSANDRA_WRITE_CONSISTENCY));
    tx.rollback();
}
Also used : WriteConfiguration(com.thinkaurelius.titan.diskstorage.configuration.WriteConfiguration) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) Test(org.junit.Test)

Example 10 with StandardTitanTx

use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.

the class TitanGraphTest method testTransactionConfiguration.

@Test
public void testTransactionConfiguration() {
    // Superficial tests for a few transaction builder methods
    // Test read-only transaction
    TitanTransaction readOnlyTx = graph.buildTransaction().readOnly().start();
    try {
        readOnlyTx.addVertex();
        readOnlyTx.commit();
        fail("Read-only transactions should not be able to add a vertex and commit");
    } catch (Throwable t) {
        if (readOnlyTx.isOpen())
            readOnlyTx.rollback();
    }
    // Test custom log identifier
    String logID = "spam";
    StandardTitanTx customLogIDTx = (StandardTitanTx) graph.buildTransaction().logIdentifier(logID).start();
    assertEquals(logID, customLogIDTx.getConfiguration().getLogIdentifier());
    customLogIDTx.rollback();
    // Test timestamp
    Instant customTimestamp = Instant.ofEpochMilli(-42L);
    StandardTitanTx customTimeTx = (StandardTitanTx) graph.buildTransaction().commitTime(customTimestamp).start();
    assertTrue(customTimeTx.getConfiguration().hasCommitTime());
    assertEquals(customTimestamp, customTimeTx.getConfiguration().getCommitTime());
    customTimeTx.rollback();
}
Also used : Instant(java.time.Instant) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) Test(org.junit.Test)

Aggregations

StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)20 Test (org.junit.Test)5 RelationType (com.thinkaurelius.titan.core.RelationType)3 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)3 WriteConfiguration (com.thinkaurelius.titan.diskstorage.configuration.WriteConfiguration)3 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)3 IOException (java.io.IOException)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)1 TitanElement (com.thinkaurelius.titan.core.TitanElement)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 TitanGraph (com.thinkaurelius.titan.core.TitanGraph)1 TitanRelation (com.thinkaurelius.titan.core.TitanRelation)1 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)1 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)1 EntryList (com.thinkaurelius.titan.diskstorage.EntryList)1 ModifiableConfiguration (com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration)1 IndexTransaction (com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction)1