Coverage Report - org.apache.giraph.graph.VertexMutations
 
Classes in this File Line Coverage Branch Coverage Complexity
VertexMutations
0%
0/76
0%
0/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 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  0
 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  0
   private final List<Vertex<I, V, E>> addedVertexList =
 51  0
       Lists.newArrayList();
 52  
   /** Count of remove vertex requests */
 53  0
   private int removedVertexCount = 0;
 54  
   /** List of added edges */
 55  0
   private final List<Edge<I, E>> addedEdgeList = Lists.newArrayList();
 56  
   /** List of removed edges */
 57  0
   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  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  
    * Add a vertex mutation
 128  
    *
 129  
    * @param vertex Vertex to be added
 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  
    * Removed a vertex mutation (increments a count)
 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  
    * Add an edge to this vertex
 154  
    *
 155  
    * @param edge Edge to be added
 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  
    * 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  0
     removedEdgeList.add(destinationVertexId);
 173  0
   }
 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  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  
 }