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