spmallette commented on a change in pull request #1555: URL: https://github.com/apache/tinkerpop/pull/1555#discussion_r797687584
########## File path: gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MergeEdgeStep.java ########## @@ -0,0 +1,340 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tinkerpop.gremlin.process.traversal.step.map; + +import org.apache.tinkerpop.gremlin.process.traversal.Merge; +import org.apache.tinkerpop.gremlin.process.traversal.Step; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.Traverser; +import org.apache.tinkerpop.gremlin.process.traversal.TraverserGenerator; +import org.apache.tinkerpop.gremlin.process.traversal.lambda.ConstantTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.step.Mutating; +import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalOptionParent; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.event.CallbackRegistry; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.event.Event; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.event.ListCallbackRegistry; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement; +import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalUtil; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.apache.tinkerpop.gremlin.structure.T; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.apache.tinkerpop.gremlin.structure.util.Attachable; +import org.apache.tinkerpop.gremlin.structure.util.StringFactory; +import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph; +import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Stream; + +/** + * Implementation for the {@code mergeE()} step covering both the start step version and the one used mid-traversal. + */ +public class MergeEdgeStep<S> extends FlatMapStep<S, Edge> implements Mutating<Event>, + TraversalOptionParent<Merge, S, Edge> { + + private final boolean isStart; + private boolean first = true; + private Traversal.Admin<S,Map<Object, Object>> searchCreateTraversal; + private Traversal.Admin<S, Map<Object, Object>> onCreateTraversal = null; + private Traversal.Admin<S, Map<String, Object>> onMatchTraversal = null; + + protected CallbackRegistry<Event> callbackRegistry; + + public MergeEdgeStep(final Traversal.Admin traversal, final boolean isStart) { + this(traversal, isStart, new IdentityTraversal<>()); + } + + public MergeEdgeStep(final Traversal.Admin traversal, final boolean isStart, final Map<Object, Object> searchCreate) { + this(traversal, isStart, new ConstantTraversal<>(searchCreate)); + } + + public MergeEdgeStep(final Traversal.Admin traversal, final boolean isStart, final Traversal.Admin<?,Map<Object, Object>> searchCreateTraversal) { + super(traversal); + this.isStart = isStart; + this.searchCreateTraversal = integrateChild(searchCreateTraversal); + } + + public Traversal.Admin<S, Map<Object, Object>> getSearchCreateTraversal() { + return searchCreateTraversal; + } + + public Traversal.Admin<S, Map<Object, Object>> getOnCreateTraversal() { + return onCreateTraversal; + } + + public Traversal.Admin<S, Map<String, Object>> getOnMatchTraversal() { + return onMatchTraversal; + } + + /** + * Determines if this is a start step. + */ + public boolean isStart() { + return isStart; + } + + public boolean isFirst() { + return first; + } + + public CallbackRegistry<Event> getCallbackRegistry() { + return callbackRegistry; + } + + @Override + public void addChildOption(final Merge token, final Traversal.Admin<S, Edge> traversalOption) { + if (token == Merge.onCreate) { + this.onCreateTraversal = this.integrateChild(traversalOption); + } else if (token == Merge.onMatch) { + this.onMatchTraversal = this.integrateChild(traversalOption); + } else { + throw new UnsupportedOperationException(String.format("Option %s for Merge is not supported", token.name())); + } + } + + @Override + public <S, E> List<Traversal.Admin<S, E>> getLocalChildren() { + final List<Traversal.Admin<S, E>> children = new ArrayList<>(); + if (searchCreateTraversal != null) children.add((Traversal.Admin<S, E>) searchCreateTraversal); + if (onMatchTraversal != null) children.add((Traversal.Admin<S, E>) onMatchTraversal); + if (onCreateTraversal != null) children.add((Traversal.Admin<S, E>) onCreateTraversal); + return children; + } + + @Override + public void configure(final Object... keyValues) { + // this is a Mutating step but property() should not be folded into this step. this exception should not + // end up visible to users really + } + + @Override + public Parameters getParameters() { + // merge doesn't take fold ups of property() calls. those need to get treated as regular old PropertyStep + // instances. not sure if this should support with() though.....none of the other Mutating steps do. + return null; + } + + @Override + protected Traverser.Admin<Edge> processNextStart() { + // when it's a start step a traverser needs to be created to kick off the traversal. + if (isStart && first) { + first = false; + final TraverserGenerator generator = this.getTraversal().getTraverserGenerator(); + this.addStart(generator.generate(false, (Step) this, 1L)); + } + return super.processNextStart(); + } + + /** + * Use the {@code Map} of search criteria to most efficiently return a {@code Stream<Edge>} of matching elements. + * Providers might override this method when extending this step to provide their own optimized mechanisms for + * matching the list of edges. This implementation is only optimized for the {@link T#id} so any other usage + * will simply be in-memory filtering which could be slow. + */ + protected Stream<Edge> createSearchStream(final Map<Object,Object> search) { Review comment: Might not be a bad idea, though a lot of providers are just extending steps or developing their own implementations wholly. My impression is that the Graph API has become a bit less used in favor of customizations by way of strategies as I did for TinkerGraph. I'd be ok leaving that as an open issue for a future change. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
