use of org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep in project sqlg by pietermartin.
the class BaseStrategy method handleChooseStep.
private void handleChooseStep(int chooseStepNestedCount, ChooseStep<?, ?, ?> chooseStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
// The currentTreeNode here is the node that will need the left join in the sql generation
this.chooseStepStack.add(this.currentTreeNodeNode);
Preconditions.checkState(this.chooseStepStack.size() == chooseStepNestedCount);
List<? extends Traversal.Admin<?, ?>> globalChildren = chooseStep.getGlobalChildren();
Preconditions.checkState(globalChildren.size() == 2, "ChooseStep's globalChildren must have size 2, one for true and one for false");
ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
previousReplacedStep.setLeftJoin(true);
Traversal.Admin<?, ?> trueTraversal;
Traversal.Admin<?, ?> a = globalChildren.get(0);
Traversal.Admin<?, ?> b = globalChildren.get(1);
if (a.getSteps().stream().anyMatch(s -> s instanceof IdentityStep<?>)) {
trueTraversal = b;
} else {
trueTraversal = a;
}
List<Step<?, ?>> trueTraversalSteps = new ArrayList(trueTraversal.getSteps());
ListIterator<Step<?, ?>> trueTraversalStepsIterator = trueTraversalSteps.listIterator();
while (trueTraversalStepsIterator.hasNext()) {
Step internalChooseStep = trueTraversalStepsIterator.next();
if (internalChooseStep instanceof VertexStep || internalChooseStep instanceof EdgeVertexStep || internalChooseStep instanceof EdgeOtherVertexStep) {
handleVertexStep(trueTraversalStepsIterator, (AbstractStep<?, ?>) internalChooseStep, pathCount);
// I.e. there was no results for the chooseSteps traversal.
for (int i = chooseStepNestedCount; i < this.chooseStepStack.size(); i++) {
ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
this.currentReplacedStep.markAsJoinToLeftJoin();
treeNode.addReplacedStep(this.currentReplacedStep);
}
} else if (internalChooseStep instanceof ChooseStep) {
handleChooseStep(chooseStepNestedCount + 1, (ChooseStep) internalChooseStep, traversal, pathCount);
} else if (internalChooseStep instanceof ComputerAwareStep.EndStep) {
break;
} else {
throw new IllegalStateException("Unhandled step nested in ChooseStep " + internalChooseStep.getClass().getName());
}
}
// In that case it will not be a direct step of the traversal.
if (traversal.getSteps().contains(chooseStep)) {
traversal.removeStep(chooseStep);
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep in project sqlg by pietermartin.
the class BaseStrategy method handleOptionalStep.
private void handleOptionalStep(int optionalStepNestedCount, OptionalStep<?> optionalStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
// The currentTreeNode here is the node that will need the left join in the sql generation
this.optionalStepStack.add(this.currentTreeNodeNode);
Preconditions.checkState(this.optionalStepStack.size() == optionalStepNestedCount);
Traversal.Admin<?, ?> optionalTraversal = optionalStep.getLocalChildren().get(0);
ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
previousReplacedStep.setLeftJoin(true);
List<Step<?, ?>> optionalTraversalSteps = new ArrayList(optionalTraversal.getSteps());
ListIterator<Step<?, ?>> optionalStepsIterator = optionalTraversalSteps.listIterator();
while (optionalStepsIterator.hasNext()) {
Step internalOptionalStep = optionalStepsIterator.next();
if (internalOptionalStep instanceof VertexStep || internalOptionalStep instanceof EdgeVertexStep || internalOptionalStep instanceof EdgeOtherVertexStep) {
handleVertexStep(optionalStepsIterator, (AbstractStep<?, ?>) internalOptionalStep, pathCount);
// I.e. there was no results for the chooseSteps traversal.
for (int i = optionalStepNestedCount; i < this.chooseStepStack.size(); i++) {
ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
this.currentReplacedStep.markAsJoinToLeftJoin();
treeNode.addReplacedStep(this.currentReplacedStep);
}
} else if (internalOptionalStep instanceof OptionalStep) {
handleOptionalStep(optionalStepNestedCount + 1, (OptionalStep) internalOptionalStep, traversal, pathCount);
} else if (internalOptionalStep instanceof ComputerAwareStep.EndStep) {
break;
} else if (internalOptionalStep instanceof HasStep) {
handleHasSteps(optionalStepsIterator, pathCount.getValue());
} else {
throw new IllegalStateException("Unhandled step nested in OptionalStep " + internalOptionalStep.getClass().getName());
}
}
// In that case it will not be a direct step of the traversal.
if (traversal.getSteps().contains(optionalStep)) {
traversal.removeStep(optionalStep);
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep 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