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