This project has retired. For details please refer to its Attic page.
VertexMutations xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Structure to hold all the possible graph mutations that can occur during a
39   * superstep.
40   *
41   * @param <I> Vertex index value
42   * @param <V> Vertex value
43   * @param <E> Edge value
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    /** List of added vertices during the last superstep */
50    private final List<Vertex<I, V, E>> addedVertexList =
51        Lists.newArrayList();
52    /** Count of remove vertex requests */
53    private int removedVertexCount = 0;
54    /** List of added edges */
55    private final List<Edge<I, E>> addedEdgeList = Lists.newArrayList();
56    /** List of removed edges */
57    private final List<I> removedEdgeList = Lists.newArrayList();
58    /** Configuration */
59    private ImmutableClassesGiraphConfiguration<I, V, E> conf;
60  
61    /**
62     * Copy the vertex mutations.
63     *
64     * @return Copied vertex mutations
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    * Add a vertex mutation
128    *
129    * @param vertex Vertex to be added
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    * Removed a vertex mutation (increments a count)
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    * Add an edge to this vertex
154    *
155    * @param edge Edge to be added
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    * Remove an edge on this vertex
168    *
169    * @param destinationVertexId Vertex index of the destination of the edge
170    */
171   public void removeEdge(I destinationVertexId) {
172     removedEdgeList.add(destinationVertexId);
173   }
174 
175   /**
176    * Add one vertex mutations to another
177    *
178    * @param vertexMutations Object to be added
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 }