use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project sqlg by pietermartin.
the class TestUnoptimizedRepeatStep method testRepeatStepWithLimit.
@Test
public void testRepeatStepWithLimit() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
a1.addEdge("ab", b1);
a2.addEdge("ab", b2);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").until(__.has(T.label, "B")).repeat(__.out()).limit(1);
Assert.assertEquals(4, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(2) instanceof RepeatStep);
Assert.assertTrue(traversal.getSteps().get(3) instanceof RangeGlobalStep);
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(2, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgRepeatStepBarrier);
Assert.assertEquals(1, vertices.size());
Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));
vertices = this.sqlgGraph.traversal().V().hasLabel("A").repeat(__.out()).until(__.has(T.label, "B")).limit(1).toList();
Assert.assertEquals(1, vertices.size());
Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project unipop by unipop-graph.
the class UniGraphUnionStepStrategy method apply.
@Override
public void apply(Traversal.Admin<?, ?> traversal) {
Graph graph = traversal.getGraph().get();
if (!(graph instanceof UniGraph)) {
return;
}
UniGraph uniGraph = (UniGraph) graph;
TraversalHelper.getStepsOfClass(UnionStep.class, traversal).forEach(unionStep -> {
Traversal.Admin[] traversals = (Traversal.Admin[]) unionStep.getGlobalChildren().toArray(new Traversal.Admin[0]);
for (Traversal.Admin admin : traversals) {
if (TraversalHelper.getLastStepOfAssignableClass(ReducingBarrierStep.class, admin).isPresent() || traversal.getParent() instanceof RepeatStep)
return;
}
UniGraphUnionStep uniGraphUnionStep = new UniGraphUnionStep(traversal, uniGraph, traversals);
if (TraversalHelper.stepIndex(unionStep, traversal) != -1) {
TraversalHelper.replaceStep(unionStep, uniGraphUnionStep, traversal);
} else {
TraversalHelper.getStepsOfAssignableClass(TraversalParent.class, traversal).forEach(traversalParent -> {
traversalParent.getLocalChildren().forEach(child -> {
if (TraversalHelper.stepIndex(unionStep, child) != -1) {
TraversalHelper.replaceStep(unionStep, uniGraphUnionStep, child);
}
});
traversalParent.getGlobalChildren().forEach(child -> {
if (TraversalHelper.stepIndex(unionStep, child) != -1) {
TraversalHelper.replaceStep(unionStep, uniGraphUnionStep, child);
}
});
});
}
});
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project unipop by unipop-graph.
the class UniGraphRepeatStepStrategy method apply.
@Override
public void apply(Traversal.Admin<?, ?> traversal) {
if (TraversalHelper.onGraphComputer(traversal))
return;
Graph graph = traversal.getGraph().get();
if (!(graph instanceof UniGraph)) {
return;
}
UniGraph uniGraph = (UniGraph) graph;
TraversalHelper.getStepsOfClass(RepeatStep.class, traversal).forEach(repeatStep -> {
if (TraversalHelper.hasStepOfClass(UnionStep.class, (Traversal.Admin) repeatStep.getGlobalChildren().get(0))) {
return;
}
UniGraphRepeatStep uniGraphRepeatStep = new UniGraphRepeatStep(repeatStep, traversal.asAdmin(), uniGraph);
if (repeatStep.getUntilTraversal() != null && TraversalHelper.getFirstStepOfAssignableClass(ReducingBarrierStep.class, repeatStep.getUntilTraversal()).isPresent())
return;
Traversal.Admin<?, ?> repeatTraversal = uniGraphRepeatStep.getRepeatTraversal();
TraversalHelper.replaceStep(repeatStep, uniGraphRepeatStep, traversal);
TraversalHelper.getStepsOfClass(RepeatStep.RepeatEndStep.class, repeatTraversal).forEach(repeatEndStep -> {
UniGraphRepeatStep.RepeatEndStep uniGraphRepeatEndStep = new UniGraphRepeatStep.RepeatEndStep(repeatTraversal, uniGraphRepeatStep);
TraversalHelper.replaceStep(repeatEndStep, uniGraphRepeatEndStep, repeatTraversal);
});
});
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project sqlg by pietermartin.
the class BaseStrategy method unoptimizableRepeatStep.
private boolean unoptimizableRepeatStep() {
List<RepeatStep> repeatSteps = TraversalHelper.getStepsOfAssignableClassRecursively(RepeatStep.class, this.traversal);
boolean hasUntil = repeatSteps.stream().filter(s -> s.getClass().equals(RepeatStep.class)).allMatch(repeatStep -> repeatStep.getUntilTraversal() != null);
boolean hasUnoptimizableUntil = false;
if (hasUntil) {
hasUnoptimizableUntil = repeatSteps.stream().filter(s -> s.getClass().equals(RepeatStep.class)).allMatch(repeatStep -> !(repeatStep.getUntilTraversal() instanceof LoopTraversal));
}
boolean badRepeat = !hasUntil || hasUnoptimizableUntil;
// Check if the repeat step only contains optimizable steps
if (!badRepeat) {
List<Step> collectedRepeatInternalSteps = new ArrayList<>();
for (Step step : repeatSteps) {
RepeatStep repeatStep = (RepeatStep) step;
List<Traversal.Admin> repeatTraversals = repeatStep.<Traversal.Admin>getGlobalChildren();
Traversal.Admin admin = repeatTraversals.get(0);
List<Step> repeatInternalSteps = admin.getSteps();
collectedRepeatInternalSteps.addAll(repeatInternalSteps);
}
return !collectedRepeatInternalSteps.stream().filter(s -> !s.getClass().equals(RepeatStep.RepeatEndStep.class)).allMatch((s) -> isReplaceableStep(s.getClass()));
} else {
return true;
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep in project sqlg by pietermartin.
the class BaseStrategy method handleRepeatStep.
private void handleRepeatStep(RepeatStep<?> repeatStep, MutableInt pathCount) {
List<? extends Traversal.Admin<?, ?>> repeatTraversals = repeatStep.getGlobalChildren();
Traversal.Admin admin = repeatTraversals.get(0);
LoopTraversal loopTraversal;
long numberOfLoops;
loopTraversal = (LoopTraversal) repeatStep.getUntilTraversal();
numberOfLoops = loopTraversal.getMaxLoops();
for (int i = 0; i < numberOfLoops; i++) {
ListIterator<Step<?, ?>> repeatStepIterator = admin.getSteps().listIterator();
while (repeatStepIterator.hasNext()) {
Step internalRepeatStep = repeatStepIterator.next();
if (internalRepeatStep instanceof RepeatStep.RepeatEndStep) {
break;
} else if (internalRepeatStep instanceof VertexStep || internalRepeatStep instanceof EdgeVertexStep || internalRepeatStep instanceof EdgeOtherVertexStep) {
ReplacedStep<?, ?> replacedStepToEmit;
// i.e. the currentReplacedStep before running handleVertexStep needs to be emitted.
if (repeatStep.emitFirst) {
replacedStepToEmit = this.currentReplacedStep;
pathCount.decrement();
// noinspection ConstantConditions
replacedStepToEmit.setEmit(repeatStep.getEmitTraversal() != null);
replacedStepToEmit.setUntilFirst(repeatStep.untilFirst);
if (repeatStep.getLabels().isEmpty()) {
replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
} else {
for (String label : repeatStep.getLabels()) {
replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + label);
}
}
pathCount.increment();
}
handleVertexStep(repeatStepIterator, (AbstractStep<?, ?>) internalRepeatStep, pathCount);
pathCount.decrement();
if (!repeatStep.emitFirst) {
replacedStepToEmit = this.currentReplacedStep;
// noinspection ConstantConditions
replacedStepToEmit.setEmit(repeatStep.getEmitTraversal() != null);
replacedStepToEmit.setUntilFirst(repeatStep.untilFirst);
if (repeatStep.getLabels().isEmpty()) {
replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
} else {
for (String label : repeatStep.getLabels()) {
replacedStepToEmit.addLabel(pathCount + BaseStrategy.EMIT_LABEL_SUFFIX + label);
}
}
}
pathCount.increment();
// If there is an emit we can not continue the optimization.
this.reset = repeatStep.getEmitTraversal() != null;
} else {
throw new IllegalStateException("Unhandled step nested in RepeatStep " + internalRepeatStep.getClass().getName());
}
}
}
this.traversal.removeStep(repeatStep);
}
Aggregations