static void localFoldInHasContainer(final HasStepFolder janusgraphStep, final Step<?, ?> tinkerpopStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal){ Step<?, ?> currentStep = tinkerpopStep; while (true) { if (currentStep instanceof HasContainerHolder) { final Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList()); final List<HasContainer> hasContainers = janusgraphStep.addLocalAll(containers); currentStep.getLabels().forEach(janusgraphStep::addLabel); traversal.removeStep(currentStep); currentStep = foldInOrder(janusgraphStep, currentStep, traversal, rootTraversal, janusgraphStep instanceof JanusGraphStep && ((JanusGraphStep)janusgraphStep).returnsVertex(), hasContainers); foldInRange(janusgraphStep, currentStep, traversal, hasContainers); } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep)) { break; } currentStep = currentStep.getNextStep(); } }
final Set<String> stepLabels = currentStep.getLabels(); hasContainerHolder.getHasContainers().forEach(hasContainer -> { if (GraphStep.processHasContainerIds(graphStep, hasContainer)) {
public static void foldInHasContainer(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal) { Step<?, ?> currentStep = titanStep.getNextStep(); while (true) { if (currentStep instanceof HasContainerHolder) { Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers(); if (validTitanHas(containers)) { titanStep.addAll(containers); currentStep.getLabels().forEach(titanStep::addLabel); traversal.removeStep(currentStep); } } else if (currentStep instanceof IdentityStep) { // do nothing, has no impact } else { break; } currentStep = currentStep.getNextStep(); } }
public static <E extends Ranging> void foldInRange(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal) { Step<?, ?> nextStep = TitanTraversalUtil.getNextNonIdentityStep(titanStep); if (nextStep instanceof RangeGlobalStep) { RangeGlobalStep range = (RangeGlobalStep) nextStep; int limit = QueryUtil.convertLimit(range.getHighRange()); titanStep.setLimit(QueryUtil.mergeLimits(limit, titanStep.getLimit())); if (range.getLowRange() == 0) { //Range can be removed since there is no offset nextStep.getLabels().forEach(titanStep::addLabel); traversal.removeStep(nextStep); } } }
static void foldInHasContainer(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal) { Step<?, ?> currentStep = janusgraphStep.getNextStep(); while (true) { if (currentStep instanceof OrStep && janusgraphStep instanceof JanusGraphStep) { for (final Traversal.Admin<?, ?> child : ((OrStep<?>) currentStep).getLocalChildren()) { if (!validFoldInHasContainer(child.getStartStep(), false)){ return; } } ((OrStep<?>) currentStep).getLocalChildren().forEach(t ->localFoldInHasContainer(janusgraphStep, t.getStartStep(), t, rootTraversal)); traversal.removeStep(currentStep); } else if (currentStep instanceof HasContainerHolder){ final Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList()); if (validFoldInHasContainer(currentStep, true)) { janusgraphStep.addAll(containers); currentStep.getLabels().forEach(janusgraphStep::addLabel); traversal.removeStep(currentStep); } } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof HasContainerHolder)) { break; } currentStep = currentStep.getNextStep(); } }
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(hasContainers)); } int high = QueryUtil.convertLimit(range.getHighRange()); high = QueryUtil.mergeHighLimits(high, hasContainers == null ? janusgraphStep.getHighLimit(): janusgraphStep.getLocalHighLimit(hasContainers)); if (hasContainers == null) { janusgraphStep.setLimit(low, high); } else { janusgraphStep.setLocalLimit(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); } } } }
public static void copyLabels(final Step<?, ?> fromStep, final Step<?, ?> toStep, final boolean moveLabels) { if (!fromStep.getLabels().isEmpty()) { for (final String label : moveLabels ? new LinkedHashSet<>(fromStep.getLabels()) : fromStep.getLabels()) { toStep.addLabel(label); if (moveLabels) fromStep.removeLabel(label); } } }
public LP_O_OB_S_SE_SL_Traverser(final T t, final Step<T, ?> step) { super(t, step); this.path = ImmutablePath.make(); final Set<String> labels = step.getLabels(); if (!labels.isEmpty()) this.path = this.path.extend(t, labels); }
public B_LP_O_S_SE_SL_Traverser(final T t, final Step<T, ?> step, final long initialBulk) { super(t, step, initialBulk); this.path = ImmutablePath.make(); final Set<String> labels = step.getLabels(); if (!labels.isEmpty()) this.path = this.path.extend(t, labels); }
private static int labelCount(final String label, final Traversal.Admin<?, ?> traversal) { int count = 0; for (final Step step : traversal.getSteps()) { if (step.getLabels().contains(label)) count++; if (step instanceof TraversalParent) { count = count + ((TraversalParent) step).getLocalChildren().stream().map(t -> labelCount(label, t)).reduce(0, (a, b) -> a + b); count = count + ((TraversalParent) step).getGlobalChildren().stream().map(t -> labelCount(label, t)).reduce(0, (a, b) -> a + b); } } return count; } }
public LP_O_OB_P_S_SE_SL_Traverser(final T t, final Step<T, ?> step) { super(t, step); this.path = ImmutablePath.make().extend(t, step.getLabels()); }
public B_LP_O_P_S_SE_SL_Traverser(final T t, final Step<T, ?> step, final long initialBulk) { super(t, step, initialBulk); this.path = ImmutablePath.make().extend(t, step.getLabels()); }
@Override public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) { final LP_O_OB_S_SE_SL_Traverser<R> clone = (LP_O_OB_S_SE_SL_Traverser<R>) super.split(r, step); clone.path = clone.path.clone(); final Set<String> labels = step.getLabels(); if (!labels.isEmpty()) clone.path = clone.path.extend(r, labels); return clone; }
@Override public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) { final B_LP_O_S_SE_SL_Traverser<R> clone = (B_LP_O_S_SE_SL_Traverser<R>) super.split(r, step); clone.path = clone.path.clone(); final Set<String> labels = step.getLabels(); if (!labels.isEmpty()) clone.path = clone.path.extend(r, labels); return clone; }
private static GraphTraversal<?, ?> transform(final E_Exists expression) { final OpBGP opBGP = (OpBGP) expression.getGraphPattern(); final List<Triple> triples = opBGP.getPattern().getList(); if (triples.size() != 1) throw new IllegalStateException("Unhandled EXISTS pattern"); final GraphTraversal<?, ?> traversal = TraversalBuilder.transform(triples.get(0)); final Step endStep = traversal.asAdmin().getEndStep(); final String label = (String) endStep.getLabels().iterator().next(); endStep.removeLabel(label); return traversal; }
private static GraphTraversal<?, ?> transform(final E_NotExists expression) { final OpBGP opBGP = (OpBGP) expression.getGraphPattern(); final List<Triple> triples = opBGP.getPattern().getList(); if (triples.size() != 1) throw new IllegalStateException("Unhandled NOT EXISTS pattern"); final GraphTraversal<?, ?> traversal = TraversalBuilder.transform(triples.get(0)); final Step endStep = traversal.asAdmin().getEndStep(); final String label = (String) endStep.getLabels().iterator().next(); endStep.removeLabel(label); return __.not(traversal); } }
@Override public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) { final LP_O_OB_P_S_SE_SL_Traverser<R> clone = (LP_O_OB_P_S_SE_SL_Traverser<R>) super.split(r, step); clone.path = clone.path.clone().extend(r, step.getLabels()); return clone; }
@Override public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) { final B_LP_O_P_S_SE_SL_Traverser<R> clone = (B_LP_O_P_S_SE_SL_Traverser<R>) super.split(r, step); clone.path = clone.path.clone().extend(r, step.getLabels()); return clone; }
private boolean doStrategy(final Step step) { if (!(step instanceof CountGlobalStep) || !(step.getNextStep() instanceof IsStep) || step.getPreviousStep() instanceof RangeGlobalStep) // if a RangeStep was provided, assume that the user knows what he's doing return false; final Step parent = step.getTraversal().getParent().asStep(); return (parent instanceof FilterStep || parent.getLabels().isEmpty()) && // if the parent is labeled, then the count matters !(parent.getNextStep() instanceof MatchStep.MatchEndStep && // if this is in a pattern match, then don't do it. ((MatchStep.MatchEndStep) parent.getNextStep()).getMatchKey().isPresent()); } }
/** * Checks whether a given step is optimizable or not. * * @param step the step to check * @return <code>true</code> if the step is optimizable, otherwise <code>false</code> */ private static boolean isOptimizable(final Step step) { return ((step instanceof VertexStep && ((VertexStep) step).returnsVertex()) || (step instanceof PropertiesStep && PropertyType.VALUE.equals(((PropertiesStep) step).getReturnType()))) && (step.getTraversal().getEndStep().getLabels().isEmpty() || step.getNextStep() instanceof CountGlobalStep); }