Search in sources :

Example 46 with HasContainer

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

the class BaseStrategy method optimizeLabelHas.

private List<HasContainer> optimizeLabelHas(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainer.getKey().equals(T.label.getAccessor()) && SUPPORTED_LABEL_BI_PREDICATE.contains(hasContainer.getBiPredicate())) {
            replacedStep.addLabelHasContainer(hasContainer);
            result.add(hasContainer);
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 47 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer 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 48 with HasContainer

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

the class BaseStrategy method optimizeOutside.

private List<HasContainer> optimizeOutside(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof OrP) {
            OrP<?> orP = (OrP) hasContainer.getPredicate();
            List<? extends P<?>> predicates = orP.getPredicates();
            if (predicates.size() == 2) {
                if (predicates.get(0).getBiPredicate() == Compare.lt && predicates.get(1).getBiPredicate() == Compare.gt) {
                    replacedStep.addHasContainer(hasContainer);
                    result.add(hasContainer);
                }
            }
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP)

Example 49 with HasContainer

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

the class BaseStrategy method optimizeBetween.

private List<HasContainer> optimizeBetween(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof AndP) {
            AndP<?> andP = (AndP) hasContainer.getPredicate();
            List<? extends P<?>> predicates = andP.getPredicates();
            if (predicates.size() == 2) {
                if (predicates.get(0).getBiPredicate() == Compare.gte && predicates.get(1).getBiPredicate() == Compare.lt) {
                    replacedStep.addHasContainer(hasContainer);
                    result.add(hasContainer);
                }
            }
        }
    }
    return result;
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 50 with HasContainer

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

the class BaseStrategy method optimizeIdHas.

private List<HasContainer> optimizeIdHas(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainer.getKey().equals(T.id.getAccessor()) && SUPPORTED_ID_BI_PREDICATE.contains(hasContainer.getBiPredicate())) {
            replacedStep.addIdHasContainer(hasContainer);
            result.add(hasContainer);
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Aggregations

HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)56 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)13 P (org.apache.tinkerpop.gremlin.process.traversal.P)9 Collectors (java.util.stream.Collectors)7 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)6 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)6 FullText (org.umlg.sqlg.predicate.FullText)6 PredicatesHolder (org.unipop.query.predicates.PredicatesHolder)6 java.util (java.util)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 BiPredicate (java.util.function.BiPredicate)5 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)5 JSONObject (org.json.JSONObject)5 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)4 BasicVertexCentricQueryBuilder (org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)3 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 ConnectiveP (org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP)3