Search in sources :

Example 1 with HasContainerHolder

use of org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder 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 HasContainerHolder

use of org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder in project unipop by unipop-graph.

the class PredicatesUtil method collectPredicates.

public static void collectPredicates(ReceivesPredicatesHolder step, Traversal.Admin traversal) {
    Step nextStep = step.getNextStep();
    Set<PredicatesHolder> predicates = new HashSet<>();
    while (true) {
        if (nextStep instanceof HasContainerHolder) {
            HasContainerHolder hasContainerHolder = (HasContainerHolder) nextStep;
            hasContainerHolder.getHasContainers().stream().map(PredicatesHolderFactory::predicate).forEach(predicates::add);
            traversal.removeStep(nextStep);
            if (collectLabels(nextStep, step))
                break;
        } else // }
        if (nextStep instanceof RangeGlobalStep) {
            RangeGlobalStep rangeGlobalStep = (RangeGlobalStep) nextStep;
            int limit = rangeGlobalStep.getHighRange() > Integer.MAX_VALUE ? -1 : (int) rangeGlobalStep.getHighRange();
            step.setLimit(limit);
            collectLabels(nextStep, step);
            break;
        } else {
            break;
        }
        nextStep = nextStep.getNextStep();
    }
    PredicatesHolder predicate = PredicatesHolderFactory.and(predicates);
    step.addPredicate(predicate);
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) HashSet(java.util.HashSet)

Example 3 with HasContainerHolder

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

the class BaseStrategy method handleHasSteps.

protected void handleHasSteps(ListIterator<Step<?, ?>> iterator, int pathCount) {
    // Collect the hasSteps
    int countToGoPrevious = 0;
    while (iterator.hasNext()) {
        Step<?, ?> currentStep = iterator.next();
        countToGoPrevious++;
        String notNullKey;
        String nullKey;
        if (currentStep instanceof HasContainerHolder) {
            HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
            List<HasContainer> hasContainers = hasContainerHolder.getHasContainers();
            List<HasContainer> toRemoveHasContainers = new ArrayList<>();
            if (isNotWithMultipleColumnValue(hasContainerHolder)) {
                toRemoveHasContainers.addAll(optimizeLabelHas(this.currentReplacedStep, hasContainers));
                // important to do optimizeIdHas after optimizeLabelHas as it might add its labels to the previous labelHasContainers labels.
                // i.e. for neq and without 'or' logic
                toRemoveHasContainers.addAll(optimizeIdHas(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeHas(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeWithInOut(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeBetween(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeInside(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeOutside(this.currentReplacedStep, hasContainers));
                toRemoveHasContainers.addAll(optimizeTextContains(this.currentReplacedStep, hasContainers));
                if (toRemoveHasContainers.size() == hasContainers.size()) {
                    if (!currentStep.getLabels().isEmpty()) {
                        final IdentityStep identityStep = new IdentityStep<>(this.traversal);
                        currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
                        TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
                    }
                    if (this.traversal.getSteps().contains(currentStep)) {
                        this.traversal.removeStep(currentStep);
                    }
                    iterator.remove();
                    countToGoPrevious--;
                }
            }
        } else if ((notNullKey = isNotNullStep(currentStep)) != null) {
            this.currentReplacedStep.addHasContainer(new HasContainer(notNullKey, new P<>(Existence.NOTNULL, null)));
            if (!currentStep.getLabels().isEmpty()) {
                final IdentityStep identityStep = new IdentityStep<>(this.traversal);
                currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
                TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
            }
            if (this.traversal.getSteps().contains(currentStep)) {
                this.traversal.removeStep(currentStep);
            }
            iterator.remove();
            countToGoPrevious--;
        } else if ((nullKey = isNullStep(currentStep)) != null) {
            this.currentReplacedStep.addHasContainer(new HasContainer(nullKey, new P<>(Existence.NULL, null)));
            if (!currentStep.getLabels().isEmpty()) {
                final IdentityStep identityStep = new IdentityStep<>(this.traversal);
                currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
                TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
            }
            if (this.traversal.getSteps().contains(currentStep)) {
                this.traversal.removeStep(currentStep);
            }
            iterator.remove();
            countToGoPrevious--;
        } else if (currentStep instanceof IdentityStep) {
        // do nothing
        } else {
            for (int i = 0; i < countToGoPrevious; i++) {
                iterator.previous();
            }
            break;
        }
    }
}
Also used : org.apache.tinkerpop.gremlin.process.traversal(org.apache.tinkerpop.gremlin.process.traversal) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) java.util(java.util) MutableInt(org.apache.commons.lang3.mutable.MutableInt) SqlgUtil(org.umlg.sqlg.util.SqlgUtil) Graph(org.apache.tinkerpop.gremlin.structure.Graph) FullText(org.umlg.sqlg.predicate.FullText) ZonedDateTime(java.time.ZonedDateTime) Text(org.umlg.sqlg.predicate.Text) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) 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) BiPredicate(java.util.function.BiPredicate) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) SqlgTraversalUtil(org.umlg.sqlg.util.SqlgTraversalUtil) Duration(java.time.Duration) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) ComputerAwareStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ComputerAwareStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) Range(org.apache.commons.lang3.Range) EventStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy) Period(java.time.Period) org.apache.tinkerpop.gremlin.process.traversal.step.map(org.apache.tinkerpop.gremlin.process.traversal.step.map) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ReplacedStepTree(org.umlg.sqlg.sql.parse.ReplacedStepTree) org.apache.tinkerpop.gremlin.process.traversal.step.filter(org.apache.tinkerpop.gremlin.process.traversal.step.filter) Predicate(java.util.function.Predicate) RepeatStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep) T(org.apache.tinkerpop.gremlin.structure.T) Collectors(java.util.stream.Collectors) Pair(org.javatuples.Pair) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) 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) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) Existence(org.umlg.sqlg.predicate.Existence) Preconditions(com.google.common.base.Preconditions) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 4 with HasContainerHolder

use of org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder 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)

Aggregations

HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)4 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)3 ArrayList (java.util.ArrayList)2 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)2 NoOpBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)2 Preconditions (com.google.common.base.Preconditions)1 Duration (java.time.Duration)1 Period (java.time.Period)1 ZonedDateTime (java.time.ZonedDateTime)1 java.util (java.util)1 HashSet (java.util.HashSet)1 BiPredicate (java.util.function.BiPredicate)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Range (org.apache.commons.lang3.Range)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1 org.apache.tinkerpop.gremlin.process.traversal (org.apache.tinkerpop.gremlin.process.traversal)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)1