use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project janusgraph by JanusGraph.
the class HasStepFolder method foldInIds.
static void foldInIds(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal) {
Step<?, ?> currentStep = janusgraphStep.getNextStep();
while (true) {
if (currentStep instanceof HasContainerHolder) {
final HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
final GraphStep graphStep = (GraphStep) janusgraphStep;
// HasContainer collection that we get back is immutable so we keep track of which containers
// need to be deleted after they've been folded into the JanusGraphStep and then remove them from their
// step using HasContainer.removeHasContainer
final List<HasContainer> removableHasContainers = new ArrayList<>();
final Set<String> stepLabels = currentStep.getLabels();
hasContainerHolder.getHasContainers().forEach(hasContainer -> {
if (GraphStep.processHasContainerIds(graphStep, hasContainer)) {
stepLabels.forEach(janusgraphStep::addLabel);
// this has container is no longer needed because its ids will be folded into the JanusGraphStep
removableHasContainers.add(hasContainer);
}
});
if (!removableHasContainers.isEmpty()) {
removableHasContainers.forEach(hasContainerHolder::removeHasContainer);
}
// if all has containers have been removed, the current step can be removed
if (hasContainerHolder.getHasContainers().isEmpty()) {
traversal.removeStep(currentStep);
}
} else if (currentStep instanceof IdentityStep) {
// do nothing, has no impact
} else if (currentStep instanceof NoOpBarrierStep) {
// do nothing, has no impact
} else {
break;
}
currentStep = currentStep.getNextStep();
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep 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.sideEffect.IdentityStep in project sqlg by pietermartin.
the class TestOptionalWithOrder 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").optional(__.outE().as("e").otherV().as("v")).order().by("order");
Assert.assertEquals(4, traversal.getSteps().size());
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(3, traversal.getSteps().size());
assertStep(traversal.getSteps().get(0), true, false, false, true);
Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
Assert.assertTrue(traversal.getSteps().get(2) instanceof OrderGlobalStep);
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));
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project janusgraph by JanusGraph.
the class HasStepFolder method foldInRange.
static void foldInRange(final HasStepFolder janusgraphStep, final Step<?, ?> tinkerpopStep, final Traversal.Admin<?, ?> traversal, final List<HasContainer> hasContainers) {
final Step<?, ?> nextStep = tinkerpopStep instanceof IdentityStep ? JanusGraphTraversalUtil.getNextNonIdentityStep(tinkerpopStep) : tinkerpopStep;
if (nextStep instanceof RangeGlobalStep) {
final RangeGlobalStep range = (RangeGlobalStep) nextStep;
int low = 0;
if (janusgraphStep instanceof JanusGraphStep) {
low = QueryUtil.convertLimit(range.getLowRange());
low = QueryUtil.mergeLowLimits(low, hasContainers == null ? janusgraphStep.getLowLimit() : janusgraphStep.getLocalLowLimit(traversal.getParent(), hasContainers));
}
int high = QueryUtil.convertLimit(range.getHighRange());
high = QueryUtil.mergeHighLimits(high, hasContainers == null ? janusgraphStep.getHighLimit() : janusgraphStep.getLocalHighLimit(traversal.getParent(), hasContainers));
if (hasContainers == null) {
janusgraphStep.setLimit(low, high);
} else {
janusgraphStep.setLocalLimit(traversal.getParent(), hasContainers, low, high);
}
if (janusgraphStep instanceof JanusGraphStep || range.getLowRange() == 0) {
// Range can be removed since there is JanusGraphStep or no offset
nextStep.getLabels().forEach(janusgraphStep::addLabel);
traversal.removeStep(nextStep);
}
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep in project titan by thinkaurelius.
the class HasStepFolder method foldInOrder.
// public static boolean addLabeledStepAsIdentity(Step<?,?> currentStep, final Traversal.Admin<?, ?> traversal) {
// if (currentStep.getLabel().isPresent()) {
// final IdentityStep identityStep = new IdentityStep<>(traversal);
// identityStep.setLabel(currentStep.getLabel().get());
// TraversalHelper.insertAfterStep(identityStep, currentStep, traversal);
// return true;
// } else return false;
// }
public static void foldInOrder(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal, boolean isVertexOrder) {
Step<?, ?> currentStep = titanStep.getNextStep();
OrderGlobalStep<?> lastOrder = null;
while (true) {
if (currentStep instanceof OrderGlobalStep) {
if (lastOrder != null) {
//Previous orders are rendered irrelevant by next order (since re-ordered)
lastOrder.getLabels().forEach(titanStep::addLabel);
traversal.removeStep(lastOrder);
}
lastOrder = (OrderGlobalStep) currentStep;
} else if (currentStep instanceof IdentityStep) {
// do nothing, can be skipped
} else if (currentStep instanceof HasStep) {
// do nothing, can be skipped
} else {
break;
}
currentStep = currentStep.getNextStep();
}
if (lastOrder != null && lastOrder instanceof OrderGlobalStep) {
if (validTitanOrder(lastOrder, rootTraversal, isVertexOrder)) {
//Add orders to HasStepFolder
for (Comparator comp : (List<Comparator>) ((OrderGlobalStep) lastOrder).getComparators()) {
ElementValueComparator evc = (ElementValueComparator) comp;
titanStep.orderBy(evc.getPropertyKey(), (Order) evc.getValueComparator());
}
lastOrder.getLabels().forEach(titanStep::addLabel);
traversal.removeStep(lastOrder);
}
}
}
Aggregations