Search in sources :

Example 1 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project titan by thinkaurelius.

the class TitanGraphBaseTest method verifyElementOrder.

public static void verifyElementOrder(Iterator<? extends Element> elements, String key, Order order, int expectedCount) {
    Comparable previous = null;
    int count = 0;
    while (elements.hasNext()) {
        Element element = elements.next();
        Comparable current = element.value(key);
        if (previous != null) {
            int cmp = previous.compareTo(current);
            assertTrue(previous + " <> " + current + " @ " + count, order == Order.ASC ? cmp <= 0 : cmp >= 0);
        }
        previous = current;
        count++;
    }
    assertEquals(expectedCount, count);
}
Also used : Element(org.apache.tinkerpop.gremlin.structure.Element)

Example 2 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project grakn by graknlabs.

the class GraqlTraversal method getGraphTraversal.

/**
 * Get the {@code GraphTraversal} that this {@code GraqlTraversal} represents.
 */
// Because 'union' accepts an array, we can't use generics
@SuppressWarnings("unchecked")
public GraphTraversal<Vertex, Map<String, Element>> getGraphTraversal(EmbeddedGraknTx<?> tx, Set<Var> vars) {
    if (fragments().size() == 1) {
        // If there are no disjunctions, we don't need to union them and get a performance boost
        ImmutableList<Fragment> list = Iterables.getOnlyElement(fragments());
        return getConjunctionTraversal(tx, tx.getTinkerTraversal().V(), vars, list);
    } else {
        Traversal[] traversals = fragments().stream().map(list -> getConjunctionTraversal(tx, __.V(), vars, list)).toArray(Traversal[]::new);
        // This is a sneaky trick - we want to do a union but tinkerpop requires all traversals to start from
        // somewhere, so we start from a single arbitrary vertex.
        GraphTraversal traversal = tx.getTinkerTraversal().V().limit(1).union(traversals);
        return selectVars(traversal, vars);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) SelectStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Match(ai.grakn.graql.Match) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) Schema(ai.grakn.util.Schema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment)

Example 3 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class SqlgRepeatStepBarrier method test.

public <S, E> boolean test(final Traverser.Admin<S> traverser, final Traversal.Admin<S, E> traversal) {
    if (this.untilHasContainer != null) {
        Element e = (Element) traverser.get();
        return this.untilHasContainer.getPredicate().test(e.value(this.untilHasContainer.getKey()));
    }
    if (traversal.getSteps().size() == 1 && traversal.getSteps().get(0) instanceof HasStep) {
        if (this.untilHasContainers == null) {
            HasStep hasStep = (HasStep) traversal.getSteps().get(0);
            this.untilHasContainers = hasStep.getHasContainers();
            if (this.untilHasContainers.size() == 1) {
                this.untilHasContainer = this.untilHasContainers.get(0);
                Element e = (Element) traverser.get();
                return this.untilHasContainer.getPredicate().test(e.value(this.untilHasContainer.getKey()));
            }
        }
        for (HasContainer hasContainer : this.untilHasContainers) {
            if (!hasContainer.test((Element) traverser.get())) {
                return false;
            }
        }
        return true;
    } else {
        final Traverser.Admin<S> split = traverser.split();
        split.setSideEffects(traversal.getSideEffects());
        split.setBulk(1l);
        traversal.reset();
        traversal.addStart(split);
        // filter
        return traversal.hasNext();
    }
}
Also used : HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) Element(org.apache.tinkerpop.gremlin.structure.Element) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 4 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class SqlgTraversalFilterStepBarrier method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() throws NoSuchElementException {
    if (this.first) {
        this.first = false;
        Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
        while (this.starts.hasNext()) {
            Traverser.Admin<S> start = this.starts.next();
            // However the traverser might have been created from a step and strategy that does not care for TraverserRequirement.ONE_BULK so we force it here.
            if (start instanceof SqlgTraverser) {
                ((SqlgTraverser) start).setRequiresOneBulk(true);
            }
            List<Object> startObjects = start.path().objects();
            StringBuilder recordIdConcatenated = new StringBuilder();
            for (Object startObject : startObjects) {
                if (startObject instanceof Element) {
                    Element e = (Element) startObject;
                    recordIdConcatenated.append(e.id().toString());
                } else {
                    recordIdConcatenated.append(startObject.toString());
                }
            }
            startRecordIds.put(recordIdConcatenated.toString(), start);
            this.filterTraversal.addStart(start);
        }
        while (this.filterTraversal.hasNext()) {
            Traverser.Admin<?> filterTraverser = this.filterTraversal.nextTraverser();
            List<Object> filterTraverserObjects = filterTraverser.path().objects();
            String startId = "";
            for (Object filteredTraverserObject : filterTraverserObjects) {
                if (filteredTraverserObject instanceof Element) {
                    Element e = (Element) filteredTraverserObject;
                    startId += e.id().toString();
                } else {
                    startId += filteredTraverserObject.toString();
                }
                if (startRecordIds.containsKey(startId)) {
                    results.addAll(startRecordIds.get(startId));
                    startRecordIds.removeAll(startId);
                }
                if (startRecordIds.isEmpty()) {
                    break;
                }
            }
        }
        this.results.sort((o1, o2) -> {
            SqlgTraverser x = (SqlgTraverser) o1;
            SqlgTraverser y = (SqlgTraverser) o2;
            return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
        });
        this.resultIterator = this.results.iterator();
    }
    if (this.resultIterator.hasNext()) {
        Traverser.Admin<S> traverser = this.resultIterator.next();
        return traverser;
    } else {
        // The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
        reset();
        throw FastNoSuchElementException.instance();
    }
}
Also used : SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser) Element(org.apache.tinkerpop.gremlin.structure.Element)

Example 5 with Element

use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.

the class SqlgAndStepBarrier method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() {
    if (this.first) {
        this.first = false;
        Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
        while (this.starts.hasNext()) {
            Traverser.Admin<S> start = this.starts.next();
            if (start instanceof SqlgTraverser) {
                ((SqlgTraverser) start).setRequiresOneBulk(true);
            }
            List<Object> startObjects = start.path().objects();
            StringBuilder recordIdConcatenated = new StringBuilder();
            for (Object startObject : startObjects) {
                if (startObject instanceof Element) {
                    Element e = (Element) startObject;
                    recordIdConcatenated.append(e.id().toString());
                } else {
                    recordIdConcatenated.append(startObject.toString());
                }
            }
            startRecordIds.put(recordIdConcatenated.toString(), start);
        }
        boolean first = true;
        for (Traversal.Admin<S, ?> andTraversal : this.andTraversals) {
            for (Traverser.Admin<S> start : startRecordIds.values()) {
                andTraversal.addStart(start);
            }
            // Need to use a tmp map here to prevent duplicates.
            // If the and traversals yields multiple results the start must only be added once.
            Multimap<String, Traverser.Admin<S>> tmpStartRecordIds = LinkedListMultimap.create();
            tmpStartRecordIds.putAll(startRecordIds);
            List<Traverser.Admin<S>> tmpResult = new ArrayList<>();
            while (andTraversal.hasNext()) {
                Traverser.Admin<?> filterTraverser = andTraversal.nextTraverser();
                List<Object> filterTraverserObjects = filterTraverser.path().objects();
                String startId = "";
                for (Object filteredTraverserObject : filterTraverserObjects) {
                    if (filteredTraverserObject instanceof Element) {
                        Element e = (Element) filteredTraverserObject;
                        startId += e.id().toString();
                    } else {
                        startId += filteredTraverserObject.toString();
                    }
                    if (tmpStartRecordIds.containsKey(startId)) {
                        if (first) {
                            this.results.addAll(tmpStartRecordIds.get(startId));
                        } else {
                            tmpResult.addAll(tmpStartRecordIds.get(startId));
                        }
                        tmpStartRecordIds.removeAll(startId);
                    }
                }
            }
            if (!first) {
                this.results = new ArrayList<>(CollectionUtils.intersection(this.results, tmpResult));
            }
            first = false;
        }
        this.results.sort((o1, o2) -> {
            SqlgTraverser x = (SqlgTraverser) o1;
            SqlgTraverser y = (SqlgTraverser) o2;
            return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
        });
        this.resultIterator = this.results.iterator();
    }
    if (this.resultIterator.hasNext()) {
        Traverser.Admin<S> traverser = this.resultIterator.next();
        return traverser;
    } else {
        // The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
        reset();
        throw FastNoSuchElementException.instance();
    }
}
Also used : SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser) Element(org.apache.tinkerpop.gremlin.structure.Element) ArrayList(java.util.ArrayList) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser)

Aggregations

Element (org.apache.tinkerpop.gremlin.structure.Element)22 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)10 Traverser (org.apache.tinkerpop.gremlin.process.traversal.Traverser)8 SqlgTraverser (org.umlg.sqlg.structure.SqlgTraverser)6 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 Edge (org.apache.tinkerpop.gremlin.structure.Edge)4 Test (org.junit.Test)4 BaseTest (org.umlg.sqlg.test.BaseTest)4 Var (ai.grakn.graql.Var)2 IOException (java.io.IOException)2 java.util (java.util)2 HashMap (java.util.HashMap)2 Collectors (java.util.stream.Collectors)2 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)2 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)2 ConfigElement (org.janusgraph.diskstorage.configuration.ConfigElement)2 Concept (ai.grakn.concept.Concept)1 ConceptId (ai.grakn.concept.ConceptId)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Match (ai.grakn.graql.Match)1