use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.
the class BaseStrategy method handleHasSteps.
protected void handleHasSteps(ListIterator<Step<?, ?>> iterator, int pathCount) {
// Collect the hasSteps
int countToGoPrevious = 0;
while (iterator.hasNext()) {
Step<?, ?> currentStep = iterator.next();
countToGoPrevious++;
String notNullKey;
String nullKey;
if (currentStep instanceof HasContainerHolder) {
HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
List<HasContainer> hasContainers = hasContainerHolder.getHasContainers();
List<HasContainer> toRemoveHasContainers = new ArrayList<>();
if (isNotWithMultipleColumnValue(hasContainerHolder)) {
toRemoveHasContainers.addAll(optimizeLabelHas(this.currentReplacedStep, hasContainers));
// important to do optimizeIdHas after optimizeLabelHas as it might add its labels to the previous labelHasContainers labels.
// i.e. for neq and without 'or' logic
toRemoveHasContainers.addAll(optimizeIdHas(this.currentReplacedStep, hasContainers));
toRemoveHasContainers.addAll(optimizeHas(this.currentReplacedStep, hasContainers));
toRemoveHasContainers.addAll(optimizeWithInOut(this.currentReplacedStep, hasContainers));
toRemoveHasContainers.addAll(optimizeBetween(this.currentReplacedStep, hasContainers));
toRemoveHasContainers.addAll(optimizeInside(this.currentReplacedStep, hasContainers));
toRemoveHasContainers.addAll(optimizeOutside(this.currentReplacedStep, hasContainers));
toRemoveHasContainers.addAll(optimizeTextContains(this.currentReplacedStep, hasContainers));
if (toRemoveHasContainers.size() == hasContainers.size()) {
if (!currentStep.getLabels().isEmpty()) {
final IdentityStep identityStep = new IdentityStep<>(this.traversal);
currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
}
if (this.traversal.getSteps().contains(currentStep)) {
this.traversal.removeStep(currentStep);
}
iterator.remove();
countToGoPrevious--;
}
}
} else if ((notNullKey = isNotNullStep(currentStep)) != null) {
this.currentReplacedStep.addHasContainer(new HasContainer(notNullKey, new P<>(Existence.NOTNULL, null)));
if (!currentStep.getLabels().isEmpty()) {
final IdentityStep identityStep = new IdentityStep<>(this.traversal);
currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
}
if (this.traversal.getSteps().contains(currentStep)) {
this.traversal.removeStep(currentStep);
}
iterator.remove();
countToGoPrevious--;
} else if ((nullKey = isNullStep(currentStep)) != null) {
this.currentReplacedStep.addHasContainer(new HasContainer(nullKey, new P<>(Existence.NULL, null)));
if (!currentStep.getLabels().isEmpty()) {
final IdentityStep identityStep = new IdentityStep<>(this.traversal);
currentStep.getLabels().forEach(label -> this.currentReplacedStep.addLabel(pathCount + BaseStrategy.PATH_LABEL_SUFFIX + label));
TraversalHelper.insertAfterStep(identityStep, currentStep, this.traversal);
}
if (this.traversal.getSteps().contains(currentStep)) {
this.traversal.removeStep(currentStep);
}
iterator.remove();
countToGoPrevious--;
} else if (currentStep instanceof IdentityStep) {
// do nothing
} else {
for (int i = 0; i < countToGoPrevious; i++) {
iterator.previous();
}
break;
}
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.
the class BaseStrategy method unoptimizableChooseStep.
boolean unoptimizableChooseStep(ChooseStep<?, ?, ?> chooseStep) {
if (!this.chooseStepStack.isEmpty()) {
return true;
}
List<? extends Traversal.Admin<?, ?>> traversalAdmins = chooseStep.getGlobalChildren();
if (traversalAdmins.size() != 2) {
return true;
}
Traversal.Admin<?, ?> predicate = chooseStep.getLocalChildren().get(0);
List<Step> predicateSteps = new ArrayList<>(predicate.getSteps());
if (!(predicate.getSteps().get(predicate.getSteps().size() - 1) instanceof HasNextStep)) {
return true;
}
// Remove the HasNextStep
predicateSteps.remove(predicate.getSteps().size() - 1);
Traversal.Admin<?, ?> globalChildOne = chooseStep.getGlobalChildren().get(0);
List<Step> globalChildOneSteps = new ArrayList<>(globalChildOne.getSteps());
globalChildOneSteps.remove(globalChildOneSteps.size() - 1);
Traversal.Admin<?, ?> globalChildTwo = chooseStep.getGlobalChildren().get(1);
List<Step> globalChildTwoSteps = new ArrayList<>(globalChildTwo.getSteps());
globalChildTwoSteps.remove(globalChildTwoSteps.size() - 1);
boolean hasIdentity = globalChildOne.getSteps().stream().anyMatch(s -> s instanceof IdentityStep);
if (!hasIdentity) {
hasIdentity = globalChildTwo.getSteps().stream().anyMatch(s -> s instanceof IdentityStep);
if (hasIdentity) {
// Identity found check predicate and true are the same
if (!predicateSteps.equals(globalChildOneSteps)) {
return true;
}
} else {
// Identity not found
return true;
}
} else {
// Identity found check predicate and true are the same
if (!predicateSteps.equals(globalChildTwoSteps)) {
return true;
}
}
List<Step> localSteps = predicateSteps.stream().filter(p -> p.getClass().equals(LocalStep.class)).collect(Collectors.toList());
if (!localSteps.isEmpty()) {
return true;
}
List<Step> rangeGlobalSteps = predicateSteps.stream().filter(p -> p.getClass().equals(RangeGlobalStep.class)).collect(Collectors.toList());
if (rangeGlobalSteps.size() > 1) {
return true;
}
if (rangeGlobalSteps.size() > 0) {
Step rangeGlobalStep = rangeGlobalSteps.get(0);
// Only if the rangeGlobalStep is the last step can it be optimized
if (predicateSteps.get(predicateSteps.size() - 1) != rangeGlobalStep) {
return true;
}
}
Traversal.Admin<?, ?> trueTraversal;
Traversal.Admin<?, ?> a = globalChildOne;
Traversal.Admin<?, ?> b = globalChildTwo;
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 || internalChooseStep instanceof ComputerAwareStep.EndStep || internalChooseStep instanceof ChooseStep || internalChooseStep instanceof HasStep || internalChooseStep instanceof OrderGlobalStep || internalChooseStep instanceof RangeGlobalStep)) {
return true;
}
}
List<Step> chooseSteps = globalChildTwo.getSteps().stream().filter(p -> p.getClass().equals(ChooseStep.class)).collect(Collectors.toList());
for (Step step : chooseSteps) {
if (unoptimizableChooseStep((ChooseStep<?, ?, ?>) step)) {
return true;
}
}
return false;
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project sqlg by pietermartin.
the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrder2.
@Test
public void testOptionalWithOrder2() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
a1.addEdge("ab", b1);
a1.addEdge("ab", b2);
a1.addEdge("ab", b3);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").as("a").local(__.optional(__.outE().as("e").otherV().as("v"))).order().by("order");
Assert.assertEquals(4, traversal.getSteps().size());
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(4, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
Assert.assertTrue(traversal.getSteps().get(3) instanceof OrderGlobalStep);
SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
Assert.assertEquals(1, sqlgVertexSteps.size());
Assert.assertEquals(5, vertices.size());
Assert.assertEquals(b3, vertices.get(0));
Assert.assertEquals(b2, vertices.get(1));
Assert.assertEquals(b1, vertices.get(2));
Assert.assertEquals(a3, vertices.get(3));
Assert.assertEquals(a2, vertices.get(4));
}
Aggregations