Coverage Report - org.apache.giraph.graph.VertexMutations
 
Classes in this File Line Coverage Branch Coverage Complexity
VertexMutations
71%
53/74
100%
12/12
1.6
 
 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 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  
  * Structure to hold all the possible graph mutations that can occur during a
 36  
  * superstep.
 37  
  *
 38  
  * @param <I> Vertex index value
 39  
  * @param <V> Vertex value
 40  
  * @param <E> Edge value
 41  
  * @param <M> Message value
 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  
   /** List of added vertices during the last superstep */
 49  62
   private final List<Vertex<I, V, E, M>> addedVertexList =
 50  
       new ArrayList<Vertex<I, V, E, M>>();
 51  
   /** Count of remove vertex requests */
 52  62
   private int removedVertexCount = 0;
 53  
   /** List of added edges */
 54  62
   private final List<Edge<I, E>> addedEdgeList = new ArrayList<Edge<I, E>>();
 55  
   /** List of removed edges */
 56  62
   private final List<I> removedEdgeList = new ArrayList<I>();
 57  
   /** Configuration */
 58  
   private Configuration conf;
 59  
 
 60  
   /**
 61  
    * Copy the vertex mutations.
 62  
    *
 63  
    * @return Copied vertex mutations
 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  
    * Add a vertex mutation
 129  
    *
 130  
    * @param vertex Vertex to be added
 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  
    * Removed a vertex mutation (increments a count)
 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  
    * Add an edge to this vertex
 155  
    *
 156  
    * @param edge Edge to be added
 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  
    * Remove an edge on this vertex
 169  
    *
 170  
    * @param destinationVertexId Vertex index of the destination of the edge
 171  
    */
 172  
   public void removeEdge(I destinationVertexId) {
 173  97
     removedEdgeList.add(destinationVertexId);
 174  97
   }
 175  
 
 176  
   /**
 177  
    * Add one vertex mutations to another
 178  
    *
 179  
    * @param vertexMutations Object to be added
 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  
 }