Search in sources :

Example 1 with SqlgTraverser

use of org.umlg.sqlg.structure.SqlgTraverser 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 2 with SqlgTraverser

use of org.umlg.sqlg.structure.SqlgTraverser 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 3 with SqlgTraverser

use of org.umlg.sqlg.structure.SqlgTraverser 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 4 with SqlgTraverser

use of org.umlg.sqlg.structure.SqlgTraverser 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)

Example 5 with SqlgTraverser

use of org.umlg.sqlg.structure.SqlgTraverser 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)

Aggregations

Traverser (org.apache.tinkerpop.gremlin.process.traversal.Traverser)8 SqlgTraverser (org.umlg.sqlg.structure.SqlgTraverser)8 Element (org.apache.tinkerpop.gremlin.structure.Element)6 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 SqlgElement (org.umlg.sqlg.structure.SqlgElement)2 ArrayList (java.util.ArrayList)1