1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.graph; |
20 | |
|
21 | |
import org.apache.hadoop.conf.Configurable; |
22 | |
import org.apache.hadoop.conf.Configuration; |
23 | |
import org.apache.hadoop.io.Writable; |
24 | |
import org.apache.hadoop.io.WritableComparable; |
25 | |
import org.apache.log4j.Logger; |
26 | |
|
27 | |
import java.util.List; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
@SuppressWarnings("rawtypes") |
39 | 45 | public class VertexResolver<I extends WritableComparable, V extends Writable, |
40 | |
E extends Writable, M extends Writable> |
41 | |
implements BasicVertexResolver<I, V, E, M>, Configurable { |
42 | |
|
43 | 1 | private static final Logger LOG = Logger.getLogger(VertexResolver.class); |
44 | |
|
45 | 45 | private Configuration conf = null; |
46 | |
|
47 | |
private GraphState<I, V, E, M> graphState; |
48 | |
|
49 | |
@Override |
50 | |
public Vertex<I, V, E, M> resolve( |
51 | |
I vertexId, |
52 | |
Vertex<I, V, E, M> vertex, |
53 | |
VertexChanges<I, V, E, M> vertexChanges, |
54 | |
boolean hasMessages) { |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 45 | if (vertex != null) { |
62 | 20 | if (vertexChanges != null) { |
63 | 20 | List<I> removedEdgeList = vertexChanges.getRemovedEdgeList(); |
64 | 20 | for (I removedDestVertex : removedEdgeList) { |
65 | 10 | E removeEdge = |
66 | |
((MutableVertex<I, V, E, M>) vertex).removeEdge( |
67 | |
removedDestVertex); |
68 | 10 | if (removeEdge == null) { |
69 | 0 | LOG.warn("resolve: Failed to remove edge with " + |
70 | |
"destination " + removedDestVertex + "on " + |
71 | |
vertex + " since it doesn't exist."); |
72 | |
} |
73 | 10 | } |
74 | 20 | if (vertexChanges.getRemovedVertexCount() > 0) { |
75 | 10 | vertex = null; |
76 | |
} |
77 | |
} |
78 | |
} |
79 | |
|
80 | 45 | if (vertex == null) { |
81 | 35 | if (vertexChanges != null) { |
82 | 30 | if (!vertexChanges.getAddedVertexList().isEmpty()) { |
83 | 10 | vertex = vertexChanges.getAddedVertexList().get(0); |
84 | |
} |
85 | |
} |
86 | 35 | if (vertex == null && hasMessages) { |
87 | 5 | vertex = instantiateVertex(); |
88 | 5 | vertex.initialize(vertexId, |
89 | |
BspUtils.<V>createVertexValue(getConf()), |
90 | |
null, |
91 | |
null); |
92 | |
} |
93 | |
} else { |
94 | 10 | if ((vertexChanges != null) && |
95 | |
(!vertexChanges.getAddedVertexList().isEmpty())) { |
96 | 0 | LOG.warn("resolve: Tried to add a vertex with id = " + |
97 | |
vertex.getId() + " when one already " + |
98 | |
"exists. Ignoring the add vertex request."); |
99 | |
} |
100 | |
} |
101 | |
|
102 | 45 | if (vertexChanges != null && |
103 | |
!vertexChanges.getAddedEdgeList().isEmpty()) { |
104 | 10 | MutableVertex<I, V, E, M> mutableVertex = |
105 | |
(MutableVertex<I, V, E, M>) vertex; |
106 | 10 | for (Edge<I, E> edge : vertexChanges.getAddedEdgeList()) { |
107 | 10 | mutableVertex.addEdge(edge.getTargetVertexId(), |
108 | |
edge.getValue()); |
109 | |
} |
110 | |
} |
111 | |
|
112 | 45 | return vertex; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public Vertex<I, V, E, M> instantiateVertex() { |
117 | 5 | Vertex<I, V, E, M> vertex = |
118 | |
BspUtils.<I, V, E, M>createVertex(getConf()); |
119 | 5 | vertex.setGraphState(graphState); |
120 | 5 | return vertex; |
121 | |
} |
122 | |
|
123 | |
@Override |
124 | |
public Configuration getConf() { |
125 | 10 | return conf; |
126 | |
} |
127 | |
|
128 | |
@Override |
129 | |
public void setConf(Configuration conf) { |
130 | 45 | this.conf = conf; |
131 | 45 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public void setGraphState(GraphState<I, V, E, M> graphState) { |
139 | 45 | this.graphState = graphState; |
140 | 45 | } |
141 | |
} |