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 java.io.DataInput; |
22 | |
import java.io.DataOutput; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.hadoop.conf.Configurable; |
28 | |
import org.apache.hadoop.conf.Configuration; |
29 | |
import org.apache.hadoop.io.Writable; |
30 | |
import org.apache.hadoop.io.WritableComparable; |
31 | |
import org.json.JSONException; |
32 | |
import org.json.JSONObject; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@SuppressWarnings("rawtypes") |
44 | 62 | public class VertexMutations<I extends WritableComparable, |
45 | |
V extends Writable, E extends Writable, |
46 | |
M extends Writable> implements VertexChanges<I, V, E, M>, |
47 | |
Writable, Configurable { |
48 | |
|
49 | 62 | private final List<Vertex<I, V, E, M>> addedVertexList = |
50 | |
new ArrayList<Vertex<I, V, E, M>>(); |
51 | |
|
52 | 62 | private int removedVertexCount = 0; |
53 | |
|
54 | 62 | private final List<Edge<I, E>> addedEdgeList = new ArrayList<Edge<I, E>>(); |
55 | |
|
56 | 62 | private final List<I> removedEdgeList = new ArrayList<I>(); |
57 | |
|
58 | |
private Configuration conf; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public VertexMutations<I, V, E, M> copy() { |
66 | 0 | VertexMutations<I, V, E, M> copied = new VertexMutations<I, V, E, M>(); |
67 | 0 | copied.addedVertexList.addAll(this.addedVertexList); |
68 | 0 | copied.removedVertexCount = this.removedVertexCount; |
69 | 0 | copied.addedEdgeList.addAll(this.addedEdgeList); |
70 | 0 | copied.removedEdgeList.addAll(this.removedEdgeList); |
71 | 0 | copied.conf = this.conf; |
72 | 0 | return copied; |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
public List<Vertex<I, V, E, M>> getAddedVertexList() { |
77 | 61 | return addedVertexList; |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public void readFields(DataInput input) throws IOException { |
82 | 11 | addedVertexList.clear(); |
83 | 11 | addedEdgeList.clear(); |
84 | 11 | removedEdgeList.clear(); |
85 | |
|
86 | 11 | int addedVertexListSize = input.readInt(); |
87 | 44 | for (int i = 0; i < addedVertexListSize; ++i) { |
88 | 33 | Vertex<I, V, E, M> vertex = BspUtils.createVertex(conf); |
89 | 33 | vertex.readFields(input); |
90 | 33 | addedVertexList.add(vertex); |
91 | |
} |
92 | 11 | removedVertexCount = input.readInt(); |
93 | 11 | int addedEdgeListSize = input.readInt(); |
94 | 66 | for (int i = 0; i < addedEdgeListSize; ++i) { |
95 | 55 | I destVertex = BspUtils.<I>createVertexId(conf); |
96 | 55 | destVertex.readFields(input); |
97 | 55 | E edgeValue = BspUtils.<E>createEdgeValue(conf); |
98 | 55 | edgeValue.readFields(input); |
99 | 55 | addedEdgeList.add(new Edge<I, E>(destVertex, edgeValue)); |
100 | |
} |
101 | 11 | int removedEdgeListSize = input.readInt(); |
102 | 88 | for (int i = 0; i < removedEdgeListSize; ++i) { |
103 | 77 | I removedEdge = BspUtils.<I>createVertexId(conf); |
104 | 77 | removedEdge.readFields(input); |
105 | 77 | removedEdgeList.add(removedEdge); |
106 | |
} |
107 | 11 | } |
108 | |
|
109 | |
@Override |
110 | |
public void write(DataOutput output) throws IOException { |
111 | 11 | output.writeInt(addedVertexList.size()); |
112 | 11 | for (Vertex<I, V, E, M> vertex : addedVertexList) { |
113 | 33 | vertex.write(output); |
114 | |
} |
115 | 11 | output.writeInt(removedVertexCount); |
116 | 11 | output.writeInt(addedEdgeList.size()); |
117 | 11 | for (Edge<I, E> edge : addedEdgeList) { |
118 | 55 | edge.getTargetVertexId().write(output); |
119 | 55 | edge.getValue().write(output); |
120 | |
} |
121 | 11 | output.writeInt(removedEdgeList.size()); |
122 | 11 | for (I removedEdge : removedEdgeList) { |
123 | 77 | removedEdge.write(output); |
124 | |
} |
125 | 11 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public void addVertex(Vertex<I, V, E, M> vertex) { |
133 | 43 | addedVertexList.add(vertex); |
134 | 43 | } |
135 | |
|
136 | |
@Override |
137 | |
public int getRemovedVertexCount() { |
138 | 31 | return removedVertexCount; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void removeVertex() { |
145 | 32 | ++removedVertexCount; |
146 | 32 | } |
147 | |
|
148 | |
@Override |
149 | |
public List<Edge<I, E>> getAddedEdgeList() { |
150 | 61 | return addedEdgeList; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void addEdge(Edge<I, E> edge) { |
159 | 65 | addedEdgeList.add(edge); |
160 | 65 | } |
161 | |
|
162 | |
@Override |
163 | |
public List<I> getRemovedEdgeList() { |
164 | 20 | return removedEdgeList; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public void removeEdge(I destinationVertexId) { |
173 | 97 | removedEdgeList.add(destinationVertexId); |
174 | 97 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public void addVertexMutations(VertexMutations<I, V, E, M> vertexMutations) { |
182 | 0 | addedVertexList.addAll(vertexMutations.getAddedVertexList()); |
183 | 0 | removedVertexCount += vertexMutations.getRemovedVertexCount(); |
184 | 0 | addedEdgeList.addAll(vertexMutations.getAddedEdgeList()); |
185 | 0 | removedEdgeList.addAll(vertexMutations.getRemovedEdgeList()); |
186 | 0 | } |
187 | |
|
188 | |
@Override |
189 | |
public String toString() { |
190 | 0 | JSONObject jsonObject = new JSONObject(); |
191 | |
try { |
192 | 0 | jsonObject.put("added vertices", getAddedVertexList().toString()); |
193 | 0 | jsonObject.put("added edges", getAddedEdgeList().toString()); |
194 | 0 | jsonObject.put("removed vertex count", getRemovedVertexCount()); |
195 | 0 | jsonObject.put("removed edges", getRemovedEdgeList().toString()); |
196 | 0 | return jsonObject.toString(); |
197 | 0 | } catch (JSONException e) { |
198 | 0 | throw new IllegalStateException("toString: Got a JSON exception", |
199 | |
e); |
200 | |
} |
201 | |
} |
202 | |
|
203 | |
@Override |
204 | |
public Configuration getConf() { |
205 | 0 | return conf; |
206 | |
} |
207 | |
|
208 | |
@Override |
209 | |
public void setConf(Configuration conf) { |
210 | 11 | this.conf = conf; |
211 | 11 | } |
212 | |
} |