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