use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.
the class TestHasLabelAndId method testConsecutiveEqHasWithoutIdAndLabels.
@Test
public void testConsecutiveEqHasWithoutIdAndLabels() {
Vertex a = this.sqlgGraph.addVertex(T.label, "A");
Vertex b = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c = this.sqlgGraph.addVertex(T.label, "C");
Vertex d = this.sqlgGraph.addVertex(T.label, "D");
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A", "B", "C", "D").has(T.id, P.without(b.id(), b2.id()));
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(3, vertices.size());
Assert.assertTrue(vertices.contains(a) && vertices.contains(c) && vertices.contains(d));
Assert.assertEquals(1, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
Assert.assertEquals(1, sqlgGraphStep.getReplacedSteps().size());
ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
// without's labels are merged into the previous labelHasContainers
// this is because without has 'or' logic rather than 'and'
Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
}
use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.
the class TestHasLabelAndId method testInWithHasLabelAndHasWithoutId2.
@Test
public void testInWithHasLabelAndHasWithoutId2() {
Vertex a = this.sqlgGraph.addVertex(T.label, "A");
Vertex b = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
Vertex c = this.sqlgGraph.addVertex(T.label, "C");
Vertex d = this.sqlgGraph.addVertex(T.label, "D");
b.addEdge("ab", a);
b2.addEdge("ab", a);
c.addEdge("ac", a);
d.addEdge("ad", a);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").in().hasLabel("B", "C", "D").has(T.id, P.without(b.id(), c.id()));
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(2, vertices.size());
Assert.assertTrue(vertices.contains(b2) && vertices.contains(d));
Assert.assertEquals(1, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
Assert.assertEquals(2, sqlgGraphStep.getReplacedSteps().size());
ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(1);
Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
}
use of org.umlg.sqlg.sql.parse.ReplacedStep in project sqlg by pietermartin.
the class BaseStrategy method handleOrderGlobalSteps.
protected void handleOrderGlobalSteps(ListIterator<Step<?, ?>> iterator, MutableInt pathCount) {
// Collect the OrderGlobalSteps
while (iterator.hasNext()) {
Step<?, ?> step = iterator.next();
if (step instanceof OrderGlobalStep) {
if (optimizable((OrderGlobalStep) step)) {
// add the label if any
for (String label : step.getLabels()) {
this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + label);
}
// The step might not be here. For instance if it was nested in a chooseStep where the chooseStep logic already removed the step.
if (this.traversal.getSteps().contains(step)) {
this.traversal.removeStep(step);
}
iterator.previous();
Step previousStep = iterator.previous();
if (previousStep instanceof SelectOneStep) {
SelectOneStep selectOneStep = (SelectOneStep) previousStep;
String key = (String) selectOneStep.getScopeKeys().iterator().next();
this.currentReplacedStep.getSqlgComparatorHolder().setPrecedingSelectOneLabel(key);
List<Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparators = ((OrderGlobalStep) step).getComparators();
// get the step for the label
Optional<ReplacedStep<?, ?>> labeledReplacedStep = this.sqlgStep.getReplacedSteps().stream().filter(r -> {
Set<String> labels = r.getLabels();
for (String label : labels) {
String stepLabel = SqlgUtil.originalLabel(label);
if (stepLabel.equals(key)) {
return true;
} else {
return false;
}
}
return false;
}).findAny();
Preconditions.checkState(labeledReplacedStep.isPresent());
ReplacedStep<?, ?> replacedStep = labeledReplacedStep.get();
replacedStep.getSqlgComparatorHolder().setComparators(comparators);
// add a label if the step does not yet have one and is not a leaf node
if (replacedStep.getLabels().isEmpty()) {
replacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL);
}
} else if (previousStep instanceof OptionalStep) {
throw new RuntimeException("not yet implemented");
} else if (previousStep instanceof ChooseStep) {
// The order applies to the current replaced step and the previous ChooseStep
List<Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparators = ((OrderGlobalStep) step).getComparators();
this.currentReplacedStep.getSqlgComparatorHolder().setComparators(comparators);
// add a label if the step does not yet have one and is not a leaf node
if (this.currentReplacedStep.getLabels().isEmpty()) {
this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL);
}
} else {
List<Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparators = ((OrderGlobalStep) step).getComparators();
this.currentReplacedStep.getSqlgComparatorHolder().setComparators(comparators);
// add a label if the step does not yet have one and is not a leaf node
if (this.currentReplacedStep.getLabels().isEmpty()) {
this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL);
}
}
iterator.next();
iterator.next();
} else {
return;
}
} else {
// break on the first step that is not a OrderGlobalStep
iterator.previous();
break;
}
}
}
use of org.umlg.sqlg.sql.parse.ReplacedStep 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