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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations