Search in sources :

Example 1 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project janusgraph by JanusGraph.

the class HasStepFolder method foldInIds.

static void foldInIds(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal) {
    Step<?, ?> currentStep = janusgraphStep.getNextStep();
    while (true) {
        if (currentStep instanceof HasContainerHolder) {
            final HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
            final GraphStep graphStep = (GraphStep) janusgraphStep;
            // HasContainer collection that we get back is immutable so we keep track of which containers
            // need to be deleted after they've been folded into the JanusGraphStep and then remove them from their
            // step using HasContainer.removeHasContainer
            final List<HasContainer> removableHasContainers = new ArrayList<>();
            final Set<String> stepLabels = currentStep.getLabels();
            hasContainerHolder.getHasContainers().forEach(hasContainer -> {
                if (GraphStep.processHasContainerIds(graphStep, hasContainer)) {
                    stepLabels.forEach(janusgraphStep::addLabel);
                    // this has container is no longer needed because its ids will be folded into the JanusGraphStep
                    removableHasContainers.add(hasContainer);
                }
            });
            if (!removableHasContainers.isEmpty()) {
                removableHasContainers.forEach(hasContainerHolder::removeHasContainer);
            }
            // if all has containers have been removed, the current step can be removed
            if (hasContainerHolder.getHasContainers().isEmpty()) {
                traversal.removeStep(currentStep);
            }
        } else if (currentStep instanceof IdentityStep) {
        // do nothing, has no impact
        } else if (currentStep instanceof NoOpBarrierStep) {
        // do nothing, has no impact
        } else {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
Also used : IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ArrayList(java.util.ArrayList) NoOpBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)

Example 2 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.

the class BaseStrategy method handleChooseStep.

private void handleChooseStep(int chooseStepNestedCount, ChooseStep<?, ?, ?> chooseStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    // The currentTreeNode here is the node that will need the left join in the sql generation
    this.chooseStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.chooseStepStack.size() == chooseStepNestedCount);
    List<? extends Traversal.Admin<?, ?>> globalChildren = chooseStep.getGlobalChildren();
    Preconditions.checkState(globalChildren.size() == 2, "ChooseStep's globalChildren must have size 2, one for true and one for false");
    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);
    Traversal.Admin<?, ?> trueTraversal;
    Traversal.Admin<?, ?> a = globalChildren.get(0);
    Traversal.Admin<?, ?> b = globalChildren.get(1);
    if (a.getSteps().stream().anyMatch(s -> s instanceof IdentityStep<?>)) {
        trueTraversal = b;
    } else {
        trueTraversal = a;
    }
    List<Step<?, ?>> trueTraversalSteps = new ArrayList(trueTraversal.getSteps());
    ListIterator<Step<?, ?>> trueTraversalStepsIterator = trueTraversalSteps.listIterator();
    while (trueTraversalStepsIterator.hasNext()) {
        Step internalChooseStep = trueTraversalStepsIterator.next();
        if (internalChooseStep instanceof VertexStep || internalChooseStep instanceof EdgeVertexStep || internalChooseStep instanceof EdgeOtherVertexStep) {
            handleVertexStep(trueTraversalStepsIterator, (AbstractStep<?, ?>) internalChooseStep, pathCount);
            // I.e. there was no results for the chooseSteps traversal.
            for (int i = chooseStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalChooseStep instanceof ChooseStep) {
            handleChooseStep(chooseStepNestedCount + 1, (ChooseStep) internalChooseStep, traversal, pathCount);
        } else if (internalChooseStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else {
            throw new IllegalStateException("Unhandled step nested in ChooseStep " + internalChooseStep.getClass().getName());
        }
    }
    // In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(chooseStep)) {
        traversal.removeStep(chooseStep);
    }
}
Also used : LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) SackValueStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) TreeSideEffectStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree)

Example 3 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.

the class TestOptionalWithOrder method testOptionalWithOrder2.

@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").as("a").optional(__.outE().as("e").otherV().as("v")).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, true);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof OrderGlobalStep);
    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 4 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project janusgraph by JanusGraph.

the class HasStepFolder method foldInRange.

static void foldInRange(final HasStepFolder janusgraphStep, final Step<?, ?> tinkerpopStep, final Traversal.Admin<?, ?> traversal, final List<HasContainer> hasContainers) {
    final Step<?, ?> nextStep = tinkerpopStep instanceof IdentityStep ? JanusGraphTraversalUtil.getNextNonIdentityStep(tinkerpopStep) : tinkerpopStep;
    if (nextStep instanceof RangeGlobalStep) {
        final RangeGlobalStep range = (RangeGlobalStep) nextStep;
        int low = 0;
        if (janusgraphStep instanceof JanusGraphStep) {
            low = QueryUtil.convertLimit(range.getLowRange());
            low = QueryUtil.mergeLowLimits(low, hasContainers == null ? janusgraphStep.getLowLimit() : janusgraphStep.getLocalLowLimit(traversal.getParent(), hasContainers));
        }
        int high = QueryUtil.convertLimit(range.getHighRange());
        high = QueryUtil.mergeHighLimits(high, hasContainers == null ? janusgraphStep.getHighLimit() : janusgraphStep.getLocalHighLimit(traversal.getParent(), hasContainers));
        if (hasContainers == null) {
            janusgraphStep.setLimit(low, high);
        } else {
            janusgraphStep.setLocalLimit(traversal.getParent(), hasContainers, low, high);
        }
        if (janusgraphStep instanceof JanusGraphStep || range.getLowRange() == 0) {
            // Range can be removed since there is JanusGraphStep or no offset
            nextStep.getLabels().forEach(janusgraphStep::addLabel);
            traversal.removeStep(nextStep);
        }
    }
}
Also used : IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep)

Example 5 with IdentityStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project titan by thinkaurelius.

the class HasStepFolder method foldInOrder.

//    public static boolean addLabeledStepAsIdentity(Step<?,?> currentStep, final Traversal.Admin<?, ?> traversal) {
//        if (currentStep.getLabel().isPresent()) {
//            final IdentityStep identityStep = new IdentityStep<>(traversal);
//            identityStep.setLabel(currentStep.getLabel().get());
//            TraversalHelper.insertAfterStep(identityStep, currentStep, traversal);
//            return true;
//        } else return false;
//    }
public static void foldInOrder(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal, boolean isVertexOrder) {
    Step<?, ?> currentStep = titanStep.getNextStep();
    OrderGlobalStep<?> lastOrder = null;
    while (true) {
        if (currentStep instanceof OrderGlobalStep) {
            if (lastOrder != null) {
                //Previous orders are rendered irrelevant by next order (since re-ordered)
                lastOrder.getLabels().forEach(titanStep::addLabel);
                traversal.removeStep(lastOrder);
            }
            lastOrder = (OrderGlobalStep) currentStep;
        } else if (currentStep instanceof IdentityStep) {
        // do nothing, can be skipped
        } else if (currentStep instanceof HasStep) {
        // do nothing, can be skipped
        } else {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    if (lastOrder != null && lastOrder instanceof OrderGlobalStep) {
        if (validTitanOrder(lastOrder, rootTraversal, isVertexOrder)) {
            //Add orders to HasStepFolder
            for (Comparator comp : (List<Comparator>) ((OrderGlobalStep) lastOrder).getComparators()) {
                ElementValueComparator evc = (ElementValueComparator) comp;
                titanStep.orderBy(evc.getPropertyKey(), (Order) evc.getValueComparator());
            }
            lastOrder.getLabels().forEach(titanStep::addLabel);
            traversal.removeStep(lastOrder);
        }
    }
}
Also used : IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) ArrayList(java.util.ArrayList) List(java.util.List) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) Comparator(java.util.Comparator)

Aggregations

IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)13 ArrayList (java.util.ArrayList)5 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)5 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)5 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)4 NoOpBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)4 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)4 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)4 Comparator (java.util.Comparator)3 List (java.util.List)3 LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)3 ChooseStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep)3 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)3 OptionalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep)3 RepeatStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep)3 SackValueStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SackValueStep)3 TreeSideEffectStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TreeSideEffectStep)3 AbstractStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep)3