public static void reIdSteps( final StepPosition stepPosition, final Traversal.Admin<?, ?> traversal) { stepPosition.x = 0; stepPosition.y = -1; stepPosition.z = -1; stepPosition.parentId = null; Traversal.Admin<?, ?> current = traversal; while (!(current instanceof EmptyTraversal)) { stepPosition.y++; final TraversalParent parent = current.getParent(); if (null == stepPosition.parentId && !(parent instanceof EmptyStep)) stepPosition.parentId = parent.asStep().getId(); if (-1 == stepPosition.z) { final int globalChildrenSize = parent.getGlobalChildren().size(); for (int i = 0; i < globalChildrenSize; i++) { if (parent.getGlobalChildren().get(i) == current) { stepPosition.z = i; } } for (int i = 0; i < parent.getLocalChildren().size(); i++) { if (parent.getLocalChildren().get(i) == current) { stepPosition.z = i + globalChildrenSize; } } } current = parent.asStep().getTraversal(); } if (-1 == stepPosition.z) stepPosition.z = 0; if (null == stepPosition.parentId) stepPosition.parentId = ""; for (final Step<?, ?> step : traversal.getSteps()) { step.setId(stepPosition.nextXId()); } }
public static Traversal.Admin<?, ?> getRootTraversal(Traversal.Admin<?, ?> traversal) { while (!((traversal.getParent()) instanceof EmptyStep)) { traversal = traversal.getParent().asStep().getTraversal(); } return traversal; }
@Override public void apply(final Traversal.Admin<?, ?> traversal) { final TraversalParent parent = traversal.getParent(); int size = traversal.getSteps().size(); Step prev = null; for (int i = 0; i < size; i++) { final Step curr = traversal.getSteps().get(i); if (curr instanceof CountGlobalStep && i < size - 1) { final Step next = traversal.getSteps().get(i + 1); if (next instanceof IsStep && !(prev instanceof RangeGlobalStep)) { // if a RangeStep was provided, assume that the user knows what // he's doing final IsStep isStep = (IsStep) next; final P isStepPredicate = isStep.getPredicate(); Long highRange = null; boolean useNotStep = false; for (P p : isStepPredicate instanceof ConnectiveP ? ((ConnectiveP<?>) isStepPredicate).getPredicates() : Collections.singletonList(isStepPredicate)) { final Object value = p.getValue(); final BiPredicate predicate = p.getBiPredicate(); if (value instanceof Number) { final long highRangeOffset = INCREASED_OFFSET_SCALAR_PREDICATES.contains(predicate) ? 1L : 0L; final Long highRangeCandidate = ((Number) value).longValue() + highRangeOffset; final boolean update = highRange == null || highRangeCandidate > highRange; if (update) { if (parent instanceof EmptyStep) { useNotStep = true; } else { if (parent instanceof RepeatStep) { final RepeatStep repeatStep = (RepeatStep) parent; useNotStep = Objects.equals(traversal, repeatStep.getUntilTraversal()) || Objects.equals(traversal, repeatStep.getEmitTraversal()); } else { useNotStep = parent instanceof FilterStep || parent instanceof SideEffectStep; } } highRange = highRangeCandidate; useNotStep &= curr.getLabels().isEmpty() && next.getLabels().isEmpty() && next.getNextStep() instanceof EmptyStep && ((highRange <= 1L && predicate.equals(Compare.lt)) || (highRange == 1L && (predicate.equals(Compare.eq) || predicate.equals(Compare.lte)))); } } else { final Long highRangeOffset = RANGE_PREDICATES.get(predicate); if (value instanceof Collection && highRangeOffset != null) { final Object high = Collections.max((Collection) value); if (high instanceof Number) { final Long highRangeCandidate = ((Number) high).longValue() + highRangeOffset; final boolean update = highRange == null || highRangeCandidate > highRange; if (update) highRange = highRangeCandidate; } } } } if (highRange != null) { if (useNotStep) { traversal.asAdmin().removeStep(next); // IsStep traversal.asAdmin().removeStep(curr); // CountStep size -= 2; if (prev != null) { final Traversal.Admin inner = __.start().asAdmin(); TraversalHelper.insertAfterStep(prev, inner.getStartStep(), inner); TraversalHelper.replaceStep(prev, new NotStep<>(traversal, inner), traversal); } else { traversal.asAdmin().addStep(new NotStep<>(traversal, __.identity())); } } else { TraversalHelper.insertBeforeStep( new RangeGlobalStep<>(traversal, 0L, highRange), curr, traversal); } i++; } } } prev = curr; } }