Search in sources :

Example 16 with StandardJanusGraphTx

use of org.janusgraph.graphdb.transaction.StandardJanusGraphTx in project janusgraph by JanusGraph.

the class JanusGraphTest method testTransactionConfiguration.

@Test
public void testTransactionConfiguration() {
    // Superficial tests for a few transaction builder methods
    // Test read-only transaction
    JanusGraphTransaction 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";
    StandardJanusGraphTx customLogIDTx = (StandardJanusGraphTx) graph.buildTransaction().logIdentifier(logID).start();
    assertEquals(logID, customLogIDTx.getConfiguration().getLogIdentifier());
    customLogIDTx.rollback();
    // Test timestamp
    Instant customTimestamp = Instant.ofEpochMilli(-42L);
    StandardJanusGraphTx customTimeTx = (StandardJanusGraphTx) graph.buildTransaction().commitTime(customTimestamp).start();
    assertTrue(customTimeTx.getConfiguration().hasCommitTime());
    assertEquals(customTimestamp, customTimeTx.getConfiguration().getCommitTime());
    customTimeTx.rollback();
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) Instant(java.time.Instant) Test(org.junit.Test)

Example 17 with StandardJanusGraphTx

use of org.janusgraph.graphdb.transaction.StandardJanusGraphTx in project janusgraph by JanusGraph.

the class JanusGraphLocalQueryOptimizerStrategy 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.
    StandardJanusGraph janusGraph = graph instanceof StandardJanusGraphTx ? ((StandardJanusGraphTx) graph).getGraph() : (StandardJanusGraph) graph;
    final boolean useMultiQuery = !TraversalHelper.onGraphComputer(traversal) && janusGraph.getConfiguration().useMultiQuery();
    /*
                ====== VERTEX STEP ======
         */
    TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalStep -> {
        JanusGraphVertexStep vertexStep = new JanusGraphVertexStep(originalStep);
        TraversalHelper.replaceStep(originalStep, vertexStep, traversal);
        if (JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep)) {
            HasStepFolder.foldInHasContainer(vertexStep, traversal);
        // We cannot fold in orders or ranges since they are not local
        }
        assert JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep) || JanusGraphTraversalUtil.isVertexReturnStep(vertexStep);
        Step nextStep = JanusGraphTraversalUtil.getNextNonIdentityStep(vertexStep);
        if (nextStep instanceof RangeGlobalStep) {
            int limit = QueryUtil.convertLimit(((RangeGlobalStep) nextStep).getHighRange());
            vertexStep.setLimit(QueryUtil.mergeLimits(limit, vertexStep.getLimit()));
        }
        if (useMultiQuery) {
            vertexStep.setUseMultiQuery(true);
        }
    });
    /*
                ====== PROPERTIES STEP ======
         */
    TraversalHelper.getStepsOfClass(PropertiesStep.class, traversal).forEach(originalStep -> {
        JanusGraphPropertiesStep propertiesStep = new JanusGraphPropertiesStep(originalStep);
        TraversalHelper.replaceStep(originalStep, propertiesStep, traversal);
        if (propertiesStep.getReturnType().forProperties()) {
            HasStepFolder.foldInHasContainer(propertiesStep, traversal);
        // We cannot fold in orders or ranges since they are not local
        }
        if (useMultiQuery) {
            propertiesStep.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) {
            JanusGraphVertexStep vertexStep = new JanusGraphVertexStep((VertexStep) localStart);
            TraversalHelper.replaceStep(localStart, vertexStep, localTraversal);
            if (JanusGraphTraversalUtil.isEdgeReturnStep(vertexStep)) {
                HasStepFolder.foldInHasContainer(vertexStep, localTraversal);
                HasStepFolder.foldInOrder(vertexStep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(vertexStep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, vertexStep, useMultiQuery);
        }
        if (localStart instanceof PropertiesStep) {
            JanusGraphPropertiesStep propertiesStep = new JanusGraphPropertiesStep((PropertiesStep) localStart);
            TraversalHelper.replaceStep(localStart, propertiesStep, localTraversal);
            if (propertiesStep.getReturnType().forProperties()) {
                HasStepFolder.foldInHasContainer(propertiesStep, localTraversal);
                HasStepFolder.foldInOrder(propertiesStep, localTraversal, traversal, false);
            }
            HasStepFolder.foldInRange(propertiesStep, localTraversal);
            unfoldLocalTraversal(traversal, localStep, localTraversal, propertiesStep, 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) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Aggregations

StandardJanusGraphTx (org.janusgraph.graphdb.transaction.StandardJanusGraphTx)17 Test (org.junit.Test)4 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)3 RelationType (org.janusgraph.core.RelationType)3 WriteConfiguration (org.janusgraph.diskstorage.configuration.WriteConfiguration)3 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)3 Instant (java.time.Instant)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)2 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)2 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 Predicates (com.google.common.base.Predicates)1 com.google.common.cache (com.google.common.cache)1 com.google.common.collect (com.google.common.collect)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1