Coverage Report - org.apache.giraph.graph.EdgeListVertex
 
Classes in this File Line Coverage Branch Coverage Complexity
EdgeListVertex
95%
66/69
91%
22/24
2.273
 
 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.hadoop.io.Writable;
 22  
 import org.apache.hadoop.io.WritableComparable;
 23  
 import org.apache.log4j.Logger;
 24  
 
 25  
 import com.google.common.collect.Iterables;
 26  
 import com.google.common.collect.Lists;
 27  
 
 28  
 import java.io.DataInput;
 29  
 import java.io.DataOutput;
 30  
 import java.io.IOException;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * User applications can subclass {@link EdgeListVertex}, which stores
 37  
  * the outbound edges in an ArrayList (less memory as the cost of expensive
 38  
  * random-access lookup).  Good for static graphs.
 39  
  *
 40  
  * @param <I> Vertex index value
 41  
  * @param <V> Vertex value
 42  
  * @param <E> Edge value
 43  
  * @param <M> Message value
 44  
  */
 45  
 @SuppressWarnings("rawtypes")
 46  509
 public abstract class EdgeListVertex<I extends WritableComparable,
 47  
     V extends Writable, E extends Writable, M extends Writable>
 48  
     extends MutableVertex<I, V, E, M> {
 49  
   /** Class logger */
 50  1
   private static final Logger LOG = Logger.getLogger(EdgeListVertex.class);
 51  
   /** List of edges */
 52  509
   private List<Edge<I, E>> edgeList = Lists.newArrayList();
 53  
   /** List of incoming messages from the previous superstep */
 54  509
   private List<M> messageList = Lists.newArrayList();
 55  
 
 56  
   @Override
 57  
   public void initialize(I id, V value, Map<I, E> edges, Iterable<M> messages) {
 58  460
     super.initialize(id, value);
 59  460
     if (edges != null) {
 60  397
       for (Map.Entry<I, E> edge : edges.entrySet()) {
 61  3711
         edgeList.add(new Edge<I, E>(edge.getKey(), edge.getValue()));
 62  
       }
 63  
     }
 64  460
     if (messages != null) {
 65  1
       Iterables.<M>addAll(messageList, messages);
 66  
     }
 67  460
   }
 68  
 
 69  
   @Override
 70  
   public Iterable<Edge<I, E>> getEdges() {
 71  1867
     return edgeList;
 72  
   }
 73  
 
 74  
   @Override
 75  
   public final boolean addEdge(I targetVertexId, E value) {
 76  265
     for (Edge<I, E> edge : getEdges()) {
 77  254
       if (edge.getTargetVertexId().equals(targetVertexId)) {
 78  245
         LOG.warn("addEdge: Vertex=" + getId() +
 79  
             ": already added an edge value for target vertex id " +
 80  
             targetVertexId);
 81  245
         return false;
 82  
       }
 83  
     }
 84  20
     edgeList.add(new Edge<I, E>(targetVertexId, value));
 85  20
     return true;
 86  
   }
 87  
 
 88  
   @Override
 89  
   public int getNumEdges() {
 90  4466
     return edgeList.size();
 91  
   }
 92  
 
 93  
   @Override
 94  
   public E removeEdge(I targetVertexId) {
 95  16
     for (Iterator<Edge<I, E>> edges = edgeList.iterator(); edges.hasNext();) {
 96  1140
       Edge<I, E> edge = edges.next();
 97  1140
       if (edge.getTargetVertexId().equals(targetVertexId)) {
 98  16
         E edgeValue = edge.getValue();
 99  16
         edges.remove();
 100  16
         return edgeValue;
 101  
       }
 102  1124
     }
 103  0
     return null;
 104  
   }
 105  
 
 106  
   @Override
 107  
   void putMessages(Iterable<M> messages) {
 108  1192
     messageList.clear();
 109  1192
     Iterables.addAll(messageList, messages);
 110  1192
   }
 111  
 
 112  
   @Override
 113  
   public Iterable<M> getMessages() {
 114  3750
     return Iterables.unmodifiableIterable(messageList);
 115  
   }
 116  
 
 117  
   @Override
 118  
   public int getNumMessages() {
 119  0
     return messageList.size();
 120  
   }
 121  
 
 122  
   @Override
 123  
   public final void readFields(DataInput in) throws IOException {
 124  44
     I vertexId = BspUtils.<I>createVertexId(getConf());
 125  44
     vertexId.readFields(in);
 126  44
     V vertexValue = BspUtils.<V>createVertexValue(getConf());
 127  44
     vertexValue.readFields(in);
 128  44
     super.initialize(vertexId, vertexValue);
 129  
 
 130  44
     int numEdges = in.readInt();
 131  44
     edgeList = Lists.newArrayListWithCapacity(numEdges);
 132  1044
     for (int i = 0; i < numEdges; ++i) {
 133  1000
       I targetVertexId = BspUtils.<I>createVertexId(getConf());
 134  1000
       targetVertexId.readFields(in);
 135  1000
       E edgeValue = BspUtils.<E>createEdgeValue(getConf());
 136  1000
       edgeValue.readFields(in);
 137  1000
       edgeList.add(new Edge<I, E>(targetVertexId, edgeValue));
 138  
     }
 139  
 
 140  44
     int numMessages = in.readInt();
 141  44
     messageList = Lists.newArrayListWithCapacity(numMessages);
 142  46
     for (int i = 0; i < numMessages; ++i) {
 143  2
       M message = BspUtils.<M>createMessageValue(getConf());
 144  2
       message.readFields(in);
 145  2
       messageList.add(message);
 146  
     }
 147  
 
 148  44
     boolean halt = in.readBoolean();
 149  44
     if (halt) {
 150  0
       voteToHalt();
 151  
     } else {
 152  44
       wakeUp();
 153  
     }
 154  44
   }
 155  
 
 156  
   @Override
 157  
   public final void write(DataOutput out) throws IOException {
 158  64
     getId().write(out);
 159  64
     getValue().write(out);
 160  
 
 161  64
     out.writeInt(edgeList.size());
 162  64
     for (Edge<I, E> edge : edgeList) {
 163  1020
       edge.getTargetVertexId().write(out);
 164  1020
       edge.getValue().write(out);
 165  
     }
 166  
 
 167  64
     out.writeInt(messageList.size());
 168  64
     for (M message : messageList) {
 169  17
       message.write(out);
 170  
     }
 171  
 
 172  64
     out.writeBoolean(isHalted());
 173  64
   }
 174  
 
 175  
   @Override
 176  
   void releaseResources() {
 177  
     // Hint to GC to free the messages
 178  1874
     messageList.clear();
 179  1874
   }
 180  
 }
 181