Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#replaceStep()
The following examples show how to use
org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#replaceStep() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HugeVertexStepStrategy.java From hugegraph with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings({ "rawtypes", "unchecked" }) public void apply(final Traversal.Admin<?, ?> traversal) { TraversalUtil.convAllHasSteps(traversal); List<VertexStep> steps = TraversalHelper.getStepsOfClass( VertexStep.class, traversal); for (VertexStep originStep : steps) { HugeVertexStep<?> newStep = new HugeVertexStep<>(originStep); TraversalHelper.replaceStep(originStep, newStep, traversal); TraversalUtil.extractHasContainer(newStep, traversal); // TODO: support order-by optimize // TraversalUtil.extractOrder(newStep, traversal); TraversalUtil.extractRange(newStep, traversal, true); TraversalUtil.extractCount(newStep, traversal); } }
Example 2
Source File: TinkerGraphStepStrategy.java From tinkergraph-gremlin with Apache License 2.0 | 6 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { if (TraversalHelper.onGraphComputer(traversal)) return; for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) { final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep); TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal); Step<?, ?> currentStep = tinkerGraphStep.getNextStep(); while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) { if (currentStep instanceof HasStep) { for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) { if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer)) tinkerGraphStep.addHasContainer(hasContainer); } TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false); traversal.removeStep(currentStep); } currentStep = currentStep.getNextStep(); } } }
Example 3
Source File: BitsyTraversalStrategy.java From bitsy with Apache License 2.0 | 6 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) { final BitsyGraphStep<?, ?> bitsyGraphStep = new BitsyGraphStep<>(originalGraphStep); TraversalHelper.replaceStep(originalGraphStep, bitsyGraphStep, traversal); Step<?, ?> currentStep = bitsyGraphStep.getNextStep(); while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) { if (currentStep instanceof HasStep) { for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) { if (!GraphStep.processHasContainerIds(bitsyGraphStep, hasContainer)) bitsyGraphStep.addHasContainer(hasContainer); } TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false); traversal.removeStep(currentStep); } currentStep = currentStep.getNextStep(); } } }
Example 4
Source File: Neo4jGraphStepStrategy.java From tinkerpop with Apache License 2.0 | 6 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) { final Neo4jGraphStep<?, ?> neo4jGraphStep = new Neo4jGraphStep<>(originalGraphStep); TraversalHelper.replaceStep(originalGraphStep, neo4jGraphStep, traversal); Step<?, ?> currentStep = neo4jGraphStep.getNextStep(); while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) { if (currentStep instanceof HasStep) { for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) { if (!GraphStep.processHasContainerIds(neo4jGraphStep, hasContainer)) neo4jGraphStep.addHasContainer(hasContainer); } TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false); traversal.removeStep(currentStep); } currentStep = currentStep.getNextStep(); } } }
Example 5
Source File: HBaseVertexStepStrategy.java From hgraphdb with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public void apply(final Traversal.Admin<?, ?> traversal) { for (final VertexStep originalVertexStep : TraversalHelper.getStepsOfClass(VertexStep.class, traversal)) { final HBaseVertexStep<?> hbaseVertexStep = new HBaseVertexStep<>(originalVertexStep); TraversalHelper.replaceStep(originalVertexStep, hbaseVertexStep, traversal); Step<?, ?> currentStep = hbaseVertexStep.getNextStep(); while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) { if (currentStep instanceof HasStep) { for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) { hbaseVertexStep.addHasContainer(hasContainer); } TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false); traversal.removeStep(currentStep); } currentStep = currentStep.getNextStep(); } } }
Example 6
Source File: HBaseGraphStepStrategy.java From hgraphdb with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public void apply(final Traversal.Admin<?, ?> traversal) { for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) { final HBaseGraphStep<?, ?> hbaseGraphStep = new HBaseGraphStep<>(originalGraphStep); TraversalHelper.replaceStep(originalGraphStep, hbaseGraphStep, traversal); Step<?, ?> currentStep = hbaseGraphStep.getNextStep(); while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) { if (currentStep instanceof HasStep) { for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) { if (!GraphStep.processHasContainerIds(hbaseGraphStep, hasContainer)) hbaseGraphStep.addHasContainer(hasContainer); } TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false); traversal.removeStep(currentStep); } currentStep = currentStep.getNextStep(); } } }
Example 7
Source File: HugeGraphStepStrategy.java From hugegraph with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings({ "rawtypes", "unchecked" }) public void apply(Traversal.Admin<?, ?> traversal) { TraversalUtil.convAllHasSteps(traversal); // Extract conditions in GraphStep List<GraphStep> steps = TraversalHelper.getStepsOfClass( GraphStep.class, traversal); for (GraphStep originStep : steps) { HugeGraphStep<?, ?> newStep = new HugeGraphStep<>(originStep); TraversalHelper.replaceStep(originStep, newStep, traversal); TraversalUtil.extractHasContainer(newStep, traversal); // TODO: support order-by optimize // TraversalUtil.extractOrder(newStep, traversal); TraversalUtil.extractRange(newStep, traversal, false); TraversalUtil.extractCount(newStep, traversal); } }
Example 8
Source File: TinkerGraphStepStrategy.java From tinkerpop with Apache License 2.0 | 6 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { if (TraversalHelper.onGraphComputer(traversal)) return; for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) { final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep); TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal); Step<?, ?> currentStep = tinkerGraphStep.getNextStep(); while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) { if (currentStep instanceof HasStep) { for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) { if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer)) tinkerGraphStep.addHasContainer(hasContainer); } TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false); traversal.removeStep(currentStep); } currentStep = currentStep.getNextStep(); } } }
Example 9
Source File: SqlgTraversalFilterStepStrategy.java From sqlg with MIT License | 5 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { //Only optimize SqlgGraph. StarGraph also passes through here. if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) { return; } if (!SqlgTraversalUtil.mayOptimize(traversal)) { return; } List<TraversalFilterStep> traversalFilterSteps = TraversalHelper.getStepsOfAssignableClass(TraversalFilterStep.class, traversal); for (@SuppressWarnings("unchecked") TraversalFilterStep<S> traversalFilterStep : traversalFilterSteps) { List<Traversal.Admin<S, ?>> filterTraversals = traversalFilterStep.getLocalChildren(); Preconditions.checkState(filterTraversals.size() == 1); Traversal.Admin<S, ?> filterTraversal = filterTraversals.get(0); //reducing barrier steps like count does not work with Sqlg's barrier optimizations List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, filterTraversal); if (!reducingBarrierSteps.isEmpty()) { continue; } SqlgTraversalFilterStepBarrier sqlgTraversalFilterStepBarrier = new SqlgTraversalFilterStepBarrier<>( traversal, filterTraversal ); for (String label : traversalFilterStep.getLabels()) { sqlgTraversalFilterStepBarrier.addLabel(label); } //noinspection unchecked TraversalHelper.replaceStep( traversalFilterStep, sqlgTraversalFilterStepBarrier, traversalFilterStep.getTraversal() ); } }
Example 10
Source File: VertexStrategy.java From sqlg with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Override protected void replaceStepInTraversal(Step stepToReplace, SqlgStep sqlgStep) { if (this.traversal.getSteps().contains(stepToReplace)) { TraversalHelper.replaceStep(stepToReplace, sqlgStep, this.traversal); } else { TraversalHelper.insertAfterStep(sqlgStep, stepToReplace.getPreviousStep(), this.traversal); } }
Example 11
Source File: SqlgHasStepStrategy.java From sqlg with MIT License | 5 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { //Only optimize SqlgGraph. StarGraph also passes through here. if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) { return; } if (!SqlgTraversalUtil.mayOptimize(traversal)) { return; } List<HasStep> hasSteps = TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal); for (HasStep<?> hasStep : hasSteps) { List<HasContainer> hasContainers = new ArrayList<>(hasStep.getHasContainers()); SqlgHasStep sqlgHasStep = new SqlgHasStep( hasStep.getTraversal(), hasContainers.toArray(new HasContainer[]{}) ); for (String label : hasStep.getLabels()) { sqlgHasStep.addLabel(label); } //noinspection unchecked TraversalHelper.replaceStep( hasStep, sqlgHasStep, hasStep.getTraversal() ); } }
Example 12
Source File: SqlgAddVertexStartStepStrategy.java From sqlg with MIT License | 5 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { //Only optimize SqlgGraph. StarGraph also passes through here. if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) { return; } if (!SqlgTraversalUtil.mayOptimize(traversal)) { return; } List<AddVertexStartStep> addVertexStartSteps = TraversalHelper.getStepsOfAssignableClassRecursively(AddVertexStartStep.class, traversal); for (AddVertexStartStep addVertexStartStep : addVertexStartSteps) { SqlgAddVertexStartStep sqlgAddVertexStartStep = new SqlgAddVertexStartStep( addVertexStartStep.getTraversal(), addVertexStartStep.getParameters() ); for (EventCallback<Event.VertexAddedEvent> callback : addVertexStartStep.getMutatingCallbackRegistry().getCallbacks()) { sqlgAddVertexStartStep.getMutatingCallbackRegistry().addCallback(callback); } for (String label : addVertexStartStep.getLabels()) { sqlgAddVertexStartStep.addLabel(label); } //noinspection unchecked TraversalHelper.replaceStep( addVertexStartStep, sqlgAddVertexStartStep, addVertexStartStep.getTraversal() ); } }
Example 13
Source File: SqlgWhereTraversalStepStrategy.java From sqlg with MIT License | 5 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { //Only optimize SqlgGraph. StarGraph also passes through here. if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) { return; } if (!SqlgTraversalUtil.mayOptimize(traversal)) { return; } List<WhereTraversalStep> whereTraversalSteps = TraversalHelper.getStepsOfAssignableClass(WhereTraversalStep.class, traversal); for (@SuppressWarnings("unchecked") WhereTraversalStep<S> whereTraversalStep : whereTraversalSteps) { List<Traversal.Admin<?, ?>> whereTraversals = whereTraversalStep.getLocalChildren(); Preconditions.checkState(whereTraversals.size() == 1); Traversal.Admin<?, ?> whereTraversal = whereTraversals.get(0); //reducing barrier steps like count does not work with Sqlg's barrier optimizations List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, whereTraversal); if (!reducingBarrierSteps.isEmpty()) { continue; } SqlgWhereTraversalStepBarrier sqlgTraversalFilterStepBarrier = new SqlgWhereTraversalStepBarrier<>( traversal, whereTraversalStep ); for (String label : whereTraversalStep.getLabels()) { sqlgTraversalFilterStepBarrier.addLabel(label); } //noinspection unchecked TraversalHelper.replaceStep( whereTraversalStep, sqlgTraversalFilterStepBarrier, whereTraversalStep.getTraversal() ); } }
Example 14
Source File: TraversalHelperTest.java From tinkerpop with Apache License 2.0 | 5 votes |
@Test public void shouldSetPreviousStepToEmptyStep() { final Traversal.Admin<?, ?> traversal = __.V().out().asAdmin(); //transform the traversal to __.V().not(out()) //the VertexStep's previousStep should be the EmptyStep Optional<VertexStep> vertexStepOpt = TraversalHelper.getFirstStepOfAssignableClass(VertexStep.class, traversal); assertTrue(vertexStepOpt.isPresent()); Traversal.Admin<?,?> inner = __.start().asAdmin(); inner.addStep(0, vertexStepOpt.get()); TraversalHelper.replaceStep(vertexStepOpt.get(), new NotStep<>(__.identity().asAdmin(), inner), traversal); List<VertexStep> vertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(VertexStep.class, traversal); assertEquals(1, vertexSteps.size()); VertexStep vertexStep = vertexSteps.get(0); assertTrue("Expected the previousStep to be an EmptyStep, found instead " + vertexStep.getPreviousStep().toString(),vertexStep.getPreviousStep() == EmptyStep.instance()); }
Example 15
Source File: SqlgDropStepStrategy.java From sqlg with MIT License | 5 votes |
@Override public void apply(Traversal.Admin<?, ?> traversal) { if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) { return; } if (!SqlgTraversalUtil.mayOptimize(traversal)) { return; } Optional<DropStep> dropStepOptional = TraversalHelper.getLastStepOfAssignableClass(DropStep.class, traversal); if (dropStepOptional.isPresent()) { DropStep<?> dropStep = dropStepOptional.get(); TraversalHelper.replaceStep(dropStep, new SqlgDropStepBarrier<>(traversal, dropStep.getMutatingCallbackRegistry()), traversal); } }
Example 16
Source File: JanusGraphLocalQueryOptimizerStrategy.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
/** * If this step is followed by a subsequent has step then the properties will need to be * known when that has step is executed. The batch property pre-fetching optimisation * loads those properties into the vertex cache with a multiQuery preventing the need to * go back to the storage back-end for each vertex to fetch the properties. * * @param traversal The traversal containing the step * @param vertexStep The step to potentially apply the optimisation to * @param nextStep The next step in the traversal * @param txVertexCacheSize The size of the vertex cache */ private void applyBatchPropertyPrefetching(Admin<?, ?> traversal, JanusGraphVertexStep vertexStep, Step nextStep, int txVertexCacheSize) { if (Vertex.class.isAssignableFrom(vertexStep.getReturnClass())) { if (HasStepFolder.foldableHasContainerNoLimit(vertexStep)) { vertexStep.setBatchPropertyPrefetching(true); vertexStep.setTxVertexCacheSize(txVertexCacheSize); } } else if (nextStep instanceof EdgeVertexStep) { EdgeVertexStep edgeVertexStep = (EdgeVertexStep) nextStep; if (HasStepFolder.foldableHasContainerNoLimit(edgeVertexStep)) { JanusGraphEdgeVertexStep estep = new JanusGraphEdgeVertexStep(edgeVertexStep, txVertexCacheSize); TraversalHelper.replaceStep(nextStep, estep, traversal); } } }
Example 17
Source File: JanusPreviousPropertyStepStrategy.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
/** * Replace the {@code graphStep} and {@code filterStep} with a new JanusPreviousPropertyStep in the given * {@code traversal}. */ private void executeStrategy( Traversal.Admin<?, ?> traversal, GraphStep<?, ?> graphStep, TraversalFilterStep<Vertex> filterStep, String propertyKey, String label) { JanusPreviousPropertyStep newStep = new JanusPreviousPropertyStep(traversal, propertyKey, label); traversal.removeStep(filterStep); TraversalHelper.replaceStep(graphStep, newStep, traversal); }
Example 18
Source File: TraversalUtil.java From hugegraph with Apache License 2.0 | 5 votes |
public static void extractRange(Step<?, ?> newStep, Traversal.Admin<?, ?> traversal, boolean extractOnlyLimit) { QueryHolder holder = (QueryHolder) newStep; Step<?, ?> step = newStep; do { step = step.getNextStep(); if (step instanceof RangeGlobalStep) { @SuppressWarnings("unchecked") RangeGlobalStep<Object> range = (RangeGlobalStep<Object>) step; /* * NOTE: keep the step to limit results after query from DB * due to `limit`(in DB) may not be implemented accurately. * but the backend driver should ensure `offset` accurately. */ // TraversalHelper.copyLabels(step, newStep, false); // traversal.removeStep(step); if (extractOnlyLimit) { // May need to retain offset for multiple sub-queries holder.setRange(0, range.getHighRange()); } else { long limit = holder.setRange(range.getLowRange(), range.getHighRange()); RangeGlobalStep<Object> newRange = new RangeGlobalStep<>( traversal, 0, limit); TraversalHelper.replaceStep(range, newRange, traversal); } } } while (step instanceof RangeGlobalStep || step instanceof IdentityStep || step instanceof NoOpBarrierStep); }
Example 19
Source File: InlineFilterStrategy.java From tinkerpop with Apache License 2.0 | 4 votes |
private static final boolean processOrStep(final OrStep<?> step, final Traversal.Admin<?, ?> traversal) { boolean process = true; String key = null; P predicate = null; final List<String> labels = new ArrayList<>(); for (final Traversal.Admin<?, ?> childTraversal : step.getLocalChildren()) { InlineFilterStrategy.instance().apply(childTraversal); // todo: this may be a bad idea, but I can't seem to find a test case to break it for (final Step<?, ?> childStep : childTraversal.getSteps()) { if (childStep instanceof HasStep) { P p = null; for (final HasContainer hasContainer : ((HasStep<?>) childStep).getHasContainers()) { if (null == key) key = hasContainer.getKey(); else if (!hasContainer.getKey().equals(key)) { process = false; break; } p = null == p ? hasContainer.getPredicate() : p.and(hasContainer.getPredicate()); } if (process) { predicate = null == predicate ? p : predicate.or(p); } labels.addAll(childStep.getLabels()); } else { process = false; break; } } if (!process) break; } if (process) { final HasStep hasStep = new HasStep<>(traversal, new HasContainer(key, predicate)); TraversalHelper.replaceStep(step, hasStep, traversal); TraversalHelper.copyLabels(step, hasStep, false); for (final String label : labels) { hasStep.addLabel(label); } return true; } return false; }
Example 20
Source File: SqlgChooseStepStrategy.java From sqlg with MIT License | 4 votes |
@SuppressWarnings("unchecked") @Override public void apply(final Traversal.Admin<?, ?> traversal) { //Only optimize SqlgGraph. StarGraph also passes through here. //noinspection OptionalGetWithoutIsPresent if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) { return; } if (!SqlgTraversalUtil.mayOptimize(traversal)) { return; } List<ChooseStep> chooseSteps = TraversalHelper.getStepsOfAssignableClass(ChooseStep.class, traversal); for (ChooseStep<S, E, M> chooseStep : chooseSteps) { Traversal.Admin<S, M> predicateTraversal = chooseStep.getLocalChildren().get(0); //The predicate branch step is a local traversal. //As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work. List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, predicateTraversal); if (!reducingBarrierSteps.isEmpty()) { continue; } if (predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1) instanceof HasNextStep) { predicateTraversal.removeStep(predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1)); } SqlgBranchStepBarrier sqlgBranchStepBarrier = new SqlgChooseStepBarrier<>( traversal, predicateTraversal ); for (String label : chooseStep.getLabels()) { sqlgBranchStepBarrier.addLabel(label); } try { Field traversalOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalOptions"); traversalOptionsField.setAccessible(true); Map<M, List<Traversal.Admin<S, E>>> traversalOptions = (Map<M, List<Traversal.Admin<S, E>>>) traversalOptionsField.get(chooseStep); for (Map.Entry<M, List<Traversal.Admin<S, E>>> entry : traversalOptions.entrySet()) { for (Traversal.Admin<S, E> admin : entry.getValue()) { sqlgBranchStepBarrier.addGlobalChildOption(entry.getKey(), admin); } } } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException(e); } TraversalHelper.replaceStep( chooseStep, sqlgBranchStepBarrier, chooseStep.getTraversal() ); } }