1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.graph.partition; |
20 | |
|
21 | |
import org.apache.giraph.graph.BspUtils; |
22 | |
import org.apache.giraph.graph.GiraphJob; |
23 | |
import org.apache.giraph.graph.Vertex; |
24 | |
import org.apache.hadoop.conf.Configuration; |
25 | |
import org.apache.hadoop.io.Writable; |
26 | |
import org.apache.hadoop.io.WritableComparable; |
27 | |
|
28 | |
import com.google.common.collect.Maps; |
29 | |
|
30 | |
import java.io.DataInput; |
31 | |
import java.io.DataOutput; |
32 | |
import java.io.IOException; |
33 | |
import java.util.Collection; |
34 | |
import java.util.concurrent.ConcurrentMap; |
35 | |
import java.util.concurrent.ConcurrentSkipListMap; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@SuppressWarnings("rawtypes") |
47 | |
public class Partition<I extends WritableComparable, |
48 | |
V extends Writable, E extends Writable, M extends Writable> |
49 | |
implements Writable { |
50 | |
|
51 | |
private final Configuration conf; |
52 | |
|
53 | |
private final int id; |
54 | |
|
55 | |
private final ConcurrentMap<I, Vertex<I, V, E, M>> vertexMap; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 72 | public Partition(Configuration conf, int id) { |
64 | 72 | this.conf = conf; |
65 | 72 | this.id = id; |
66 | 72 | if (conf.getBoolean(GiraphJob.USE_OUT_OF_CORE_MESSAGES, |
67 | |
GiraphJob.USE_OUT_OF_CORE_MESSAGES_DEFAULT)) { |
68 | 0 | vertexMap = new ConcurrentSkipListMap<I, Vertex<I, V, E, M>>(); |
69 | |
} else { |
70 | 72 | vertexMap = Maps.newConcurrentMap(); |
71 | |
} |
72 | 72 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Vertex<I, V, E, M> getVertex(I vertexIndex) { |
81 | 45 | return vertexMap.get(vertexIndex); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public Vertex<I, V, E, M> putVertex(Vertex<I, V, E, M> vertex) { |
91 | 480 | return vertexMap.put(vertex.getId(), vertex); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Vertex<I, V, E, M> removeVertex(I vertexIndex) { |
101 | 10 | return vertexMap.remove(vertexIndex); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public Collection<Vertex<I, V, E , M>> getVertices() { |
110 | 460 | return vertexMap.values(); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public void putVertices(Collection<Vertex<I, V, E , M>> vertices) { |
119 | 28 | for (Vertex<I, V, E , M> vertex : vertices) { |
120 | 437 | vertexMap.put(vertex.getId(), vertex); |
121 | |
} |
122 | 28 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public long getEdgeCount() { |
130 | 24 | long edges = 0; |
131 | 24 | for (Vertex<I, V, E, M> vertex : vertexMap.values()) { |
132 | 423 | edges += vertex.getNumEdges(); |
133 | |
} |
134 | 24 | return edges; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public int getId() { |
143 | 340 | return id; |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public String toString() { |
148 | 0 | return "(id=" + getId() + ",V=" + vertexMap.size() + |
149 | |
",E=" + getEdgeCount() + ")"; |
150 | |
} |
151 | |
|
152 | |
@Override |
153 | |
public void readFields(DataInput input) throws IOException { |
154 | 0 | int vertices = input.readInt(); |
155 | 0 | for (int i = 0; i < vertices; ++i) { |
156 | 0 | Vertex<I, V, E, M> vertex = |
157 | |
BspUtils.<I, V, E, M>createVertex(conf); |
158 | 0 | vertex.readFields(input); |
159 | 0 | if (vertexMap.put(vertex.getId(), |
160 | |
(Vertex<I, V, E, M>) vertex) != null) { |
161 | 0 | throw new IllegalStateException( |
162 | |
"readFields: " + this + |
163 | |
" already has same id " + vertex); |
164 | |
} |
165 | |
} |
166 | 0 | } |
167 | |
|
168 | |
@Override |
169 | |
public void write(DataOutput output) throws IOException { |
170 | 4 | output.writeInt(vertexMap.size()); |
171 | 4 | for (Vertex vertex : vertexMap.values()) { |
172 | 20 | vertex.write(output); |
173 | |
} |
174 | 4 | } |
175 | |
} |