Coverage Report - org.apache.giraph.examples.SimpleShortestPathsVertex
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleShortestPathsVertex
90%
18/20
87%
14/16
4.5
 
 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.examples;
 20  
 
 21  
 import org.apache.giraph.graph.Edge;
 22  
 import org.apache.giraph.graph.EdgeListVertex;
 23  
 import org.apache.hadoop.io.DoubleWritable;
 24  
 import org.apache.hadoop.io.FloatWritable;
 25  
 import org.apache.hadoop.io.LongWritable;
 26  
 import org.apache.log4j.Logger;
 27  
 
 28  
 /**
 29  
  * Demonstrates the basic Pregel shortest paths implementation.
 30  
  */
 31  
 @Algorithm(
 32  
     name = "Shortest paths",
 33  
     description = "Finds all shortest paths from a selected vertex"
 34  
 )
 35  11
 public class SimpleShortestPathsVertex extends
 36  
     EdgeListVertex<LongWritable, DoubleWritable,
 37  
     FloatWritable, DoubleWritable> {
 38  
   /** The shortest paths id */
 39  
   public static final String SOURCE_ID = "SimpleShortestPathsVertex.sourceId";
 40  
   /** Default shortest paths id */
 41  
   public static final long SOURCE_ID_DEFAULT = 1;
 42  
   /** Class logger */
 43  1
   private static final Logger LOG =
 44  
       Logger.getLogger(SimpleShortestPathsVertex.class);
 45  
 
 46  
   /**
 47  
    * Is this vertex the source id?
 48  
    *
 49  
    * @return True if the source id
 50  
    */
 51  
   private boolean isSource() {
 52  21
     return getId().get() ==
 53  
         getContext().getConfiguration().getLong(SOURCE_ID,
 54  
             SOURCE_ID_DEFAULT);
 55  
   }
 56  
 
 57  
   @Override
 58  
   public void compute(Iterable<DoubleWritable> messages) {
 59  21
     if (getSuperstep() == 0) {
 60  9
       setValue(new DoubleWritable(Double.MAX_VALUE));
 61  
     }
 62  21
     double minDist = isSource() ? 0d : Double.MAX_VALUE;
 63  21
     for (DoubleWritable message : messages) {
 64  15
       minDist = Math.min(minDist, message.get());
 65  
     }
 66  21
     if (LOG.isDebugEnabled()) {
 67  0
       LOG.debug("Vertex " + getId() + " got minDist = " + minDist +
 68  
           " vertex value = " + getValue());
 69  
     }
 70  21
     if (minDist < getValue().get()) {
 71  12
       setValue(new DoubleWritable(minDist));
 72  12
       for (Edge<LongWritable, FloatWritable> edge : getEdges()) {
 73  13
         double distance = minDist + edge.getValue().get();
 74  13
         if (LOG.isDebugEnabled()) {
 75  0
           LOG.debug("Vertex " + getId() + " sent to " +
 76  
               edge.getTargetVertexId() + " = " + distance);
 77  
         }
 78  13
         sendMessage(edge.getTargetVertexId(), new DoubleWritable(distance));
 79  13
       }
 80  
     }
 81  21
     voteToHalt();
 82  21
   }
 83  
 }