Search in sources :

Example 6 with IdentityStep

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

the class AdjacentVertexFilterOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
        Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
        List<Step> steps = filterTraversal.getSteps();
        if (steps.size() == 2 && (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) && (steps.get(1) instanceof IsStep)) {
            Direction direction = null;
            if (steps.get(0) instanceof EdgeVertexStep) {
                EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
                if (evs.getDirection() != Direction.BOTH)
                    direction = evs.getDirection();
            } else {
                assert steps.get(0) instanceof EdgeOtherVertexStep;
                direction = Direction.BOTH;
            }
            P predicate = ((IsStep) steps.get(1)).getPredicate();
            if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
                TitanVertex vertex = TitanTraversalUtil.getTitanVertex((Vertex) predicate.getValue());
                Step<?, ?> currentStep = originalStep.getPreviousStep();
                while (true) {
                    if (currentStep instanceof HasStep || currentStep instanceof IdentityStep) {
                    } else
                        break;
                }
                if (currentStep instanceof VertexStep) {
                    VertexStep vstep = (VertexStep) currentStep;
                    if (vstep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vstep.getDirection().opposite()))) {
                        TraversalHelper.replaceStep(originalStep, new HasStep(traversal, HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal);
                    }
                }
            }
        }
    });
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.GraphStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) Direction(org.apache.tinkerpop.gremlin.structure.Direction) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep)

Example 7 with IdentityStep

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

the class HasStepFolder method foldInOrder.

static void foldInOrder(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal, boolean isVertexOrder) {
    Step<?, ?> currentStep = janusgraphStep.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(janusgraphStep::addLabel);
                traversal.removeStep(lastOrder);
            }
            lastOrder = (OrderGlobalStep) currentStep;
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof HasStep) && !(currentStep instanceof NoOpBarrierStep)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    if (lastOrder != null) {
        if (validJanusGraphOrder(lastOrder, rootTraversal, isVertexOrder)) {
            // Add orders to HasStepFolder
            for (Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>> comp : (List<Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>>>) ((OrderGlobalStep) lastOrder).getComparators()) {
                ElementValueTraversal evt = (ElementValueTraversal) comp.getValue0();
                janusgraphStep.orderBy(evt.getPropertyKey(), (Order) comp.getValue1());
            }
            lastOrder.getLabels().forEach(janusgraphStep::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) NoOpBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep) ArrayList(java.util.ArrayList) List(java.util.List) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) Comparator(java.util.Comparator)

Example 8 with IdentityStep

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

the class AdjacentVertexFilterOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
        // Check if this filter traversal matches the pattern: _.inV/outV/otherV.is(x)
        Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
        List<Step> steps = filterTraversal.getSteps();
        if (steps.size() == 2 && (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) && (steps.get(1) instanceof IsStep)) {
            // Get the direction in which we filter on the adjacent vertex (or null if not a valid adjacency filter)
            Direction direction = null;
            if (steps.get(0) instanceof EdgeVertexStep) {
                EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
                if (evs.getDirection() != Direction.BOTH)
                    direction = evs.getDirection();
            } else {
                assert steps.get(0) instanceof EdgeOtherVertexStep;
                direction = Direction.BOTH;
            }
            P predicate = ((IsStep) steps.get(1)).getPredicate();
            // Check that we have a valid direction and a valid vertex filter predicate
            if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
                JanusGraphVertex vertex = JanusGraphTraversalUtil.getJanusGraphVertex((Vertex) predicate.getValue());
                // Now, check that this step is preceded by VertexStep that returns edges
                Step<?, ?> currentStep = originalStep.getPreviousStep();
                while (true) {
                    if (!(currentStep instanceof HasStep) && !(currentStep instanceof IdentityStep)) {
                        break;
                    }
                // We can jump over other steps as we move backward
                }
                if (currentStep instanceof VertexStep) {
                    VertexStep vertexStep = (VertexStep) currentStep;
                    if (vertexStep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vertexStep.getDirection().opposite()))) {
                        // Now replace the step with a has condition
                        TraversalHelper.replaceStep(originalStep, new HasStep(traversal, HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal);
                    }
                }
            }
        }
    });
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) Direction(org.apache.tinkerpop.gremlin.structure.Direction) P(org.apache.tinkerpop.gremlin.process.traversal.P) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep)

Example 9 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 || 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 10 with IdentityStep

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

the class HasStepFolder method foldInOrder.

static Step<?, ?> foldInOrder(final HasStepFolder janusgraphStep, final Step<?, ?> tinkerpopStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal, boolean isVertexOrder, final List<HasContainer> hasContainers) {
    Step<?, ?> currentStep = tinkerpopStep;
    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(janusgraphStep::addLabel);
                traversal.removeStep(lastOrder);
            }
            lastOrder = (OrderGlobalStep) currentStep;
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof HasStep) && !(currentStep instanceof NoOpBarrierStep)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    if (lastOrder != null && validJanusGraphOrder(lastOrder, rootTraversal, isVertexOrder)) {
        // Add orders to HasStepFolder
        for (final Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>> comp : (List<Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>>>) ((OrderGlobalStep) lastOrder).getComparators()) {
            final String key;
            final Order order;
            if (comp.getValue0() instanceof ValueTraversal) {
                final ValueTraversal evt = (ValueTraversal) comp.getValue0();
                key = evt.getPropertyKey();
                order = (Order) comp.getValue1();
            } else {
                final ElementValueComparator evc = (ElementValueComparator) comp.getValue1();
                key = evc.getPropertyKey();
                order = (Order) evc.getValueComparator();
            }
            if (hasContainers == null) {
                janusgraphStep.orderBy(key, order);
            } else {
                janusgraphStep.localOrderBy(traversal.getParent(), hasContainers, key, order);
            }
        }
        lastOrder.getLabels().forEach(janusgraphStep::addLabel);
        traversal.removeStep(lastOrder);
    }
    return currentStep;
}
Also used : Order(org.apache.tinkerpop.gremlin.process.traversal.Order) ValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ValueTraversal) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) NoOpBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) Comparator(java.util.Comparator) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) ArrayList(java.util.ArrayList) List(java.util.List)

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