TINKERPOP-1862 Fix Messenger implementations for Spark/Giraph handling BOTH
[tinkerpop.git] / spark-gremlin / src / main / java / org / apache / tinkerpop / gremlin / spark / process / computer / SparkMessenger.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.tinkerpop.gremlin.spark.process.computer;
20
21 import org.apache.tinkerpop.gremlin.process.computer.MessageScope;
22 import org.apache.tinkerpop.gremlin.process.computer.Messenger;
23 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
24 import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
25 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.apache.tinkerpop.gremlin.structure.Edge;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
30 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
31 import scala.Tuple2;
32
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36
37 /**
38 * @author Marko A. Rodriguez (http://markorodriguez.com)
39 */
40 public final class SparkMessenger<M> implements Messenger<M> {
41
42 private Vertex vertex;
43 private Iterable<M> incomingMessages;
44 private List<Tuple2<Object, M>> outgoingMessages = new ArrayList<>();
45
46 public void setVertexAndIncomingMessages(final Vertex vertex, final Iterable<M> incomingMessages) {
47 this.vertex = vertex;
48 this.incomingMessages = incomingMessages;
49 this.outgoingMessages = new ArrayList<>();
50 }
51
52 public List<Tuple2<Object, M>> getOutgoingMessages() {
53 return this.outgoingMessages;
54 }
55
56 @Override
57 public Iterator<M> receiveMessages() {
58 return IteratorUtils.removeOnNext(this.incomingMessages.iterator());
59 }
60
61 @Override
62 public void sendMessage(final MessageScope messageScope, final M message) {
63 if (messageScope instanceof MessageScope.Local) {
64 final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
65 final Traversal.Admin<Vertex, Edge> incidentTraversal = SparkMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
66 final Direction direction = SparkMessenger.getOppositeDirection(incidentTraversal);
67
68 // handle processing for BOTH given TINKERPOP-1862 where the target of the message is the one opposite
69 // the current vertex
70 incidentTraversal.forEachRemaining(edge -> {
71 if (direction.equals(Direction.IN) || direction.equals(Direction.OUT))
72 this.outgoingMessages.add(new Tuple2<>(edge.vertices(direction).next().id(), localMessageScope.getEdgeFunction().apply(message, edge)));
73 else
74 this.outgoingMessages.add(new Tuple2<>(edge instanceof StarGraph.StarOutEdge ? edge.inVertex().id() : edge.outVertex().id(), localMessageScope.getEdgeFunction().apply(message, edge)));
75
76 });
77 } else {
78 ((MessageScope.Global) messageScope).vertices().forEach(v -> this.outgoingMessages.add(new Tuple2<>(v.id(), message)));
79 }
80 }
81
82 ///////////
83
84 private static <T extends Traversal.Admin<Vertex, Edge>> T setVertexStart(final Traversal.Admin<Vertex, Edge> incidentTraversal, final Vertex vertex) {
85 incidentTraversal.asAdmin().addStart(incidentTraversal.getTraverserGenerator().generate(vertex, incidentTraversal.asAdmin().getStartStep(), 1l));
86 return (T) incidentTraversal;
87 }
88
89 private static Direction getOppositeDirection(final Traversal.Admin<Vertex, Edge> incidentTraversal) {
90 final VertexStep step = TraversalHelper.getLastStepOfAssignableClass(VertexStep.class, incidentTraversal).get();
91 return step.getDirection().opposite();
92 }
93 }