1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import java.util.Collection; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
import java.util.Map.Entry; |
25 | |
|
26 | |
import com.google.common.base.MoreObjects; |
27 | |
import org.apache.giraph.conf.GiraphConfiguration; |
28 | |
import org.apache.giraph.conf.GiraphConstants; |
29 | |
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; |
30 | |
import org.apache.giraph.edge.CreateSourceVertexCallback; |
31 | |
import org.apache.giraph.edge.Edge; |
32 | |
import org.apache.giraph.edge.EdgeFactory; |
33 | |
import org.apache.giraph.graph.Vertex; |
34 | |
import org.apache.giraph.graph.VertexValueCombiner; |
35 | |
import org.apache.giraph.types.ops.collections.Basic2ObjectMap; |
36 | |
import org.apache.giraph.types.ops.collections.BasicCollectionsUtils; |
37 | |
import org.apache.hadoop.io.Writable; |
38 | |
import org.apache.hadoop.io.WritableComparable; |
39 | |
|
40 | |
import com.google.common.collect.Lists; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class TestGraph<I extends WritableComparable, |
50 | |
V extends Writable, |
51 | |
E extends Writable> |
52 | |
implements Iterable<Vertex<I, V, E>> { |
53 | |
|
54 | |
protected final VertexValueCombiner<V> vertexValueCombiner; |
55 | |
|
56 | |
protected Basic2ObjectMap<I, Vertex<I, V, E>> vertices; |
57 | |
|
58 | |
protected ImmutableClassesGiraphConfiguration<I, V, E> conf; |
59 | |
|
60 | |
private CreateSourceVertexCallback<I> createSourceVertexCallback; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | public TestGraph(GiraphConfiguration conf) { |
68 | 0 | this.conf = new ImmutableClassesGiraphConfiguration<>(conf); |
69 | 0 | createSourceVertexCallback = |
70 | |
GiraphConstants.CREATE_EDGE_SOURCE_VERTICES_CALLBACK |
71 | 0 | .newInstance(this.conf); |
72 | 0 | vertexValueCombiner = this.conf.createVertexValueCombiner(); |
73 | 0 | vertices = BasicCollectionsUtils.create2ObjectMap( |
74 | 0 | this.conf.getVertexIdClass() |
75 | |
); |
76 | 0 | } |
77 | |
|
78 | |
public Collection<Vertex<I, V, E>> getVertices() { |
79 | 0 | return vertices.values(); |
80 | |
} |
81 | |
|
82 | |
public int getVertexCount() { |
83 | 0 | return vertices.size(); |
84 | |
} |
85 | |
|
86 | |
public ImmutableClassesGiraphConfiguration<I, V, E> getConf() { |
87 | 0 | return conf; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void clear() { |
95 | 0 | vertices.clear(); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public TestGraph<I, V, E> addVertex(Vertex<I, V, E> vertex) { |
105 | 0 | Vertex<I, V, E> previousVertex = vertices.get(vertex.getId()); |
106 | 0 | if (previousVertex != null) { |
107 | 0 | vertexValueCombiner.combine(previousVertex.getValue(), vertex.getValue()); |
108 | 0 | for (Edge<I, E> edge : vertex.getEdges()) { |
109 | 0 | previousVertex.addEdge(edge); |
110 | 0 | } |
111 | |
} else { |
112 | 0 | vertices.put(vertex.getId(), vertex); |
113 | |
} |
114 | 0 | return this; |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public TestGraph<I, V, E> addVertex(I id, V value, |
126 | |
Entry<I, E>... edges) { |
127 | 0 | addVertex(makeVertex(id, value, edges)); |
128 | 0 | return this; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public TestGraph<I, V, E> setVertex(Vertex<I, V, E> vertex) { |
138 | 0 | vertices.put(vertex.getId(), vertex); |
139 | 0 | return this; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public TestGraph<I, V, E> setVertex(I id, V value, Entry<I, E>... edges) { |
151 | 0 | setVertex(makeVertex(id, value, edges)); |
152 | 0 | return this; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public TestGraph<I, V, E> addEdge(I vertexId, Entry<I, E> edgePair) { |
163 | 0 | return addEdge(vertexId, edgePair.getKey(), edgePair.getValue()); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public TestGraph<I, V, E> addEdge(I vertexId, I toVertex, E edgeValue) { |
175 | 0 | if (!vertices.containsKey(vertexId)) { |
176 | 0 | if (createSourceVertexCallback.shouldCreateSourceVertex(vertexId)) { |
177 | 0 | Vertex<I, V, E> v = conf.createVertex(); |
178 | 0 | v.initialize(vertexId, conf.createVertexValue()); |
179 | 0 | vertices.put(vertexId, v); |
180 | |
} |
181 | |
} |
182 | 0 | Vertex<I, V, E> v = vertices.get(vertexId); |
183 | 0 | if (v != null) { |
184 | 0 | v.addEdge(EdgeFactory.create(toVertex, edgeValue)); |
185 | |
} |
186 | 0 | return this; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
@Override |
195 | |
public Iterator<Vertex<I, V, E>> iterator() { |
196 | 0 | return vertices.valueIterator(); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public Vertex<I, V, E> getVertex(I id) { |
206 | 0 | return vertices.get(id); |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected Iterable<Edge<I, E>> |
216 | |
createEdges(Entry<I, E>... destEdgess) { |
217 | 0 | List<Edge<I, E>> edgesList = Lists.newArrayList(); |
218 | 0 | for (Entry<I, E> e: destEdgess) { |
219 | 0 | edgesList.add(EdgeFactory.create(e.getKey(), e.getValue())); |
220 | |
} |
221 | 0 | return edgesList; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
protected Vertex<I, V, E> makeVertex(I id, V value, |
233 | |
Entry<I, E>... edges) { |
234 | 0 | Vertex<I, V, E> vertex = conf.createVertex(); |
235 | 0 | vertex.initialize(id, value, createEdges(edges)); |
236 | 0 | return vertex; |
237 | |
} |
238 | |
|
239 | |
@Override |
240 | |
public String toString() { |
241 | 0 | return MoreObjects.toStringHelper(this).add( |
242 | 0 | "vertices", vertices).toString(); |
243 | |
} |
244 | |
} |