Search in sources :

Example 1 with ValueTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.lambda.ValueTraversal in project janusgraph by JanusGraph.

the class HasStepFolder method validJanusGraphOrder.

static boolean validJanusGraphOrder(OrderGlobalStep orderGlobalStep, Traversal rootTraversal, boolean isVertexOrder) {
    final List<Pair<Traversal.Admin, Object>> comparators = orderGlobalStep.getComparators();
    for (final Pair<Traversal.Admin, Object> comp : comparators) {
        final String key;
        if (comp.getValue0() instanceof ValueTraversal && comp.getValue1() instanceof Order) {
            key = ((ValueTraversal) comp.getValue0()).getPropertyKey();
        } else if (comp.getValue1() instanceof ElementValueComparator) {
            final ElementValueComparator evc = (ElementValueComparator) comp.getValue1();
            if (!(evc.getValueComparator() instanceof Order))
                return false;
            key = evc.getPropertyKey();
        } else {
            // do not fold comparators that include nested traversals that are not simple ElementValues
            return false;
        }
        final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin());
        final PropertyKey pKey = tx.getPropertyKey(key);
        if (pKey == null || !(Comparable.class.isAssignableFrom(pKey.dataType())) || (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) {
            return false;
        }
    }
    return true;
}
Also used : Order(org.apache.tinkerpop.gremlin.process.traversal.Order) ValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ValueTraversal) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ValueTraversal) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) PropertyKey(org.janusgraph.core.PropertyKey) Pair(org.javatuples.Pair)

Example 2 with ValueTraversal

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

Order (org.apache.tinkerpop.gremlin.process.traversal.Order)2 ValueTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ValueTraversal)2 ElementValueComparator (org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator)2 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)1 NoOpBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep)1 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)1 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)1 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)1 PropertyKey (org.janusgraph.core.PropertyKey)1 Pair (org.javatuples.Pair)1