Search in sources :

Example 1 with Traverser

use of org.apache.tinkerpop.gremlin.process.traversal.Traverser 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 2 with Traverser

use of org.apache.tinkerpop.gremlin.process.traversal.Traverser 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 3 with Traverser

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

Example 4 with Traverser

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

the class SqlgLocalStepBarrier method processNextStart.

@Override
protected Traverser.Admin<E> processNextStart() throws NoSuchElementException {
    if (this.first) {
        this.first = false;
        while (this.starts.hasNext()) {
            this.localTraversal.addStart(this.starts.next());
        }
        while (this.localTraversal.hasNext()) {
            this.results.add(this.localTraversal.nextTraverser());
        }
        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<E> traverser = this.resultIterator.next();
        return traverser;
    } else {
        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)

Example 5 with Traverser

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

the class SqlgNotStepBarrier 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);
            this.notTraversal.addStart(start);
        }
        while (this.notTraversal.hasNext()) {
            Traverser.Admin<?> orTraverser = this.notTraversal.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();
                }
                startRecordIds.removeAll(startId);
                if (startRecordIds.isEmpty()) {
                    break;
                }
            }
        }
        this.results.addAll(startRecordIds.values());
        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)

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