Search in sources :

Example 6 with Traverser

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

the class SqlgOrStepBarrier 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);
        }
        for (Traversal.Admin<S, ?> orTraversal : this.orTraversals) {
            for (Traverser.Admin<S> start : startRecordIds.values()) {
                orTraversal.addStart(start);
            }
            while (orTraversal.hasNext()) {
                Traverser.Admin<?> orTraverser = orTraversal.nextTraverser();
                List<Object> filterTraverserObjects = orTraverser.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)) {
                        this.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) Element(org.apache.tinkerpop.gremlin.structure.Element) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) SqlgTraverser(org.umlg.sqlg.structure.SqlgTraverser)

Example 7 with Traverser

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

the class UniGraphCoalesceStep method process.

@Override
protected Iterator<Traverser.Admin<E>> process(List<Traverser.Admin<S>> traversers) {
    List<Traverser.Admin<E>> coalesce = new ArrayList<>();
    List<Traverser.Admin<S>> traversersList = Lists.newArrayList(traversers);
    traversersList.forEach(t -> {
        t.setSideEffects(new DefaultTraversalSideEffects() {

            {
                register(t.toString(), () -> t, BinaryOperator.maxBy((o1, o2) -> 1));
                set(t.toString(), t);
            }
        });
    });
    coalesceTraversals.forEach(t -> {
        traversersList.forEach(t::addStart);
        while (t.hasNext()) {
            Traverser<E> item = (Traverser<E>) t.next();
            ((B_O_S_SE_SL_Traverser) item).getSideEffects().forEach((key, value) -> {
                if (value != null && value instanceof Traverser)
                    traversersList.remove(value);
            });
            coalesce.add((item.asAdmin()));
        }
    });
    return coalesce.iterator();
}
Also used : TraversalParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent) TraverserRequirement(org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement) DefaultTraversalSideEffects(org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalSideEffects) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) B_O_S_SE_SL_Traverser(org.apache.tinkerpop.gremlin.process.traversal.traverser.B_O_S_SE_SL_Traverser)

Example 8 with Traverser

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

the class UniGraphWhereTraversalStep method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() throws NoSuchElementException {
    if (this.starts.hasNext()) {
        this.starts.forEachRemaining(start -> {
            start.setSideEffects(new DefaultTraversalSideEffects() {

                {
                    register("_whereStep", () -> start, (sAdmin, sAdmin2) -> sAdmin);
                }
            });
            whereTraversal.asAdmin().addStart(start);
            originals.add(start);
            if (whereTraversal.asAdmin().getStartStep() instanceof UniGraphWhereStartStep) {
                ((UniGraphWhereStartStep) whereTraversal.asAdmin().getStartStep()).addOriginal(start);
            }
        });
        had = true;
    }
    if (had && !results.hasNext()) {
        HashSet<Traverser.Admin<S>> resultsList = new HashSet<>();
        while (whereTraversal.hasNext()) {
            had = false;
            B_O_S_SE_SL_Traverser next = (B_O_S_SE_SL_Traverser) whereTraversal.next();
            Object whereStep = next.getSideEffects().get("_whereStep");
            if (whereStep instanceof Traverser)
                resultsList.add((Traverser.Admin<S>) whereStep);
            else {
                results = ((ArrayList<Traverser.Admin<S>>) whereStep).iterator();
            }
        }
        if (resultsList.size() > 0)
            results = resultsList.iterator();
    }
    return results.next();
}
Also used : java.util(java.util) Pop(org.apache.tinkerpop.gremlin.process.traversal.Pop) EmptyIterator(org.apache.tinkerpop.gremlin.util.iterator.EmptyIterator) UniGraphTraverserStep(org.unipop.process.traverser.UniGraphTraverserStep) AbstractStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep) Scoping(org.apache.tinkerpop.gremlin.process.traversal.step.Scoping) Element(org.apache.tinkerpop.gremlin.structure.Element) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) TraversalParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent) DefaultTraversalSideEffects(org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalSideEffects) TraverserRequirement(org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement) StringFactory(org.apache.tinkerpop.gremlin.structure.util.StringFactory) B_O_S_SE_SL_Traverser(org.apache.tinkerpop.gremlin.process.traversal.traverser.B_O_S_SE_SL_Traverser) FastNoSuchElementException(org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException) DefaultTraversalSideEffects(org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalSideEffects) B_O_S_SE_SL_Traverser(org.apache.tinkerpop.gremlin.process.traversal.traverser.B_O_S_SE_SL_Traverser) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) B_O_S_SE_SL_Traverser(org.apache.tinkerpop.gremlin.process.traversal.traverser.B_O_S_SE_SL_Traverser)

Example 9 with Traverser

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

the class UniGraphEdgeOtherVertexStep method process.

@Override
protected Iterator<Traverser.Admin<Vertex>> process(List<Traverser.Admin<Edge>> traversers) {
    List<Traverser.Admin<Vertex>> vertices = new ArrayList<>();
    traversers.forEach(traverser -> {
        final List<Object> objects = traverser.path().objects();
        if (objects.get(objects.size() - 2) instanceof Vertex) {
            Vertex vertex = ElementHelper.areEqual((Vertex) objects.get(objects.size() - 2), traverser.get().outVertex()) ? traverser.get().inVertex() : traverser.get().outVertex();
            vertices.add(traverser.split(vertex, this));
        }
    });
    if (propertyKeys == null || propertyKeys.size() > 0) {
        List<DeferredVertex> v = vertices.stream().map(Attachable::get).filter(vertex -> vertex instanceof DeferredVertex).map(vertex -> ((DeferredVertex) vertex)).filter(DeferredVertex::isDeferred).collect(Collectors.toList());
        if (v.size() > 0) {
            DeferredVertexQuery query = new DeferredVertexQuery(v, propertyKeys, orders, stepDescriptor, traversal);
            deferredVertexControllers.forEach(deferredVertexController -> deferredVertexController.fetchProperties(query));
        }
    }
    return vertices.iterator();
}
Also used : DeferredVertex(org.unipop.schema.reference.DeferredVertex) java.util(java.util) Attachable(org.apache.tinkerpop.gremlin.structure.util.Attachable) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) Collectors(java.util.stream.Collectors) Pair(org.javatuples.Pair) StepDescriptor(org.unipop.query.StepDescriptor) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Orderable(org.unipop.process.order.Orderable) Profiling(org.apache.tinkerpop.gremlin.process.traversal.step.Profiling) TraverserRequirement(org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement) MutableMetrics(org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics) ControllerManager(org.unipop.query.controller.ControllerManager) UniGraph(org.unipop.structure.UniGraph) DeferredVertexQuery(org.unipop.query.search.DeferredVertexQuery) UniPredicatesStep(org.unipop.process.UniPredicatesStep) Edge(org.apache.tinkerpop.gremlin.structure.Edge) DeferredVertex(org.unipop.schema.reference.DeferredVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DeferredVertex(org.unipop.schema.reference.DeferredVertex) DeferredVertexQuery(org.unipop.query.search.DeferredVertexQuery) Attachable(org.apache.tinkerpop.gremlin.structure.util.Attachable)

Example 10 with Traverser

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

the class SqlgWhereTraversalStepBarrier method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() throws NoSuchElementException {
    if (this.first) {
        this.first = false;
        while (this.starts.hasNext()) {
            Traverser.Admin 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());
                }
            }
            this.startRecordIds.put(recordIdConcatenated.toString(), start);
            this.whereTraversal.addStart(start);
        }
        while (this.whereTraversal.hasNext()) {
            Traverser.Admin<?> filterTraverser = this.whereTraversal.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)) {
                    this.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 PathProcessor.processTraverserPathLabels(traverser, this.keepLabels);
    } 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)

Aggregations

Traverser (org.apache.tinkerpop.gremlin.process.traversal.Traverser)10 Element (org.apache.tinkerpop.gremlin.structure.Element)7 SqlgTraverser (org.umlg.sqlg.structure.SqlgTraverser)6 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 TraverserRequirement (org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement)3 java.util (java.util)2 TraversalParent (org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent)2 B_O_S_SE_SL_Traverser (org.apache.tinkerpop.gremlin.process.traversal.traverser.B_O_S_SE_SL_Traverser)2 DefaultTraversalSideEffects (org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalSideEffects)2 ArrayList (java.util.ArrayList)1 Collectors (java.util.stream.Collectors)1 Order (org.apache.tinkerpop.gremlin.process.traversal.Order)1 Pop (org.apache.tinkerpop.gremlin.process.traversal.Pop)1 Profiling (org.apache.tinkerpop.gremlin.process.traversal.step.Profiling)1 Scoping (org.apache.tinkerpop.gremlin.process.traversal.step.Scoping)1 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)1 AbstractStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep)1 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)1 FastNoSuchElementException (org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException)1 MutableMetrics (org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics)1