Coverage Report - org.apache.giraph.graph.VertexResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
VertexResolver
94%
33/35
86%
26/30
4
 
 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.conf.Configurable;
 22  
 import org.apache.hadoop.conf.Configuration;
 23  
 import org.apache.hadoop.io.Writable;
 24  
 import org.apache.hadoop.io.WritableComparable;
 25  
 import org.apache.log4j.Logger;
 26  
 
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * Default implementation of how to resolve vertex creation/removal, messages
 31  
  * to nonexistent vertices, etc.
 32  
  *
 33  
  * @param <I> Vertex id
 34  
  * @param <V> Vertex data
 35  
  * @param <E> Edge data
 36  
  * @param <M> Message data
 37  
  */
 38  
 @SuppressWarnings("rawtypes")
 39  45
 public class VertexResolver<I extends WritableComparable, V extends Writable,
 40  
     E extends Writable, M extends Writable>
 41  
     implements BasicVertexResolver<I, V, E, M>, Configurable {
 42  
   /** Class logger */
 43  1
   private static final Logger LOG = Logger.getLogger(VertexResolver.class);
 44  
   /** Configuration */
 45  45
   private Configuration conf = null;
 46  
   /** Stored graph state */
 47  
   private GraphState<I, V, E, M> graphState;
 48  
 
 49  
   @Override
 50  
   public Vertex<I, V, E, M> resolve(
 51  
       I vertexId,
 52  
       Vertex<I, V, E, M> vertex,
 53  
       VertexChanges<I, V, E, M> vertexChanges,
 54  
       boolean hasMessages) {
 55  
     // Default algorithm:
 56  
     // 1. If the vertex exists, first prune the edges
 57  
     // 2. If vertex removal desired, remove the vertex.
 58  
     // 3. If creation of vertex desired, pick first vertex
 59  
     // 4. If vertex doesn't exist, but got messages, create
 60  
     // 5. If edge addition, add the edges
 61  45
     if (vertex != null) {
 62  20
       if (vertexChanges != null) {
 63  20
         List<I> removedEdgeList = vertexChanges.getRemovedEdgeList();
 64  20
         for (I removedDestVertex : removedEdgeList) {
 65  10
           E removeEdge =
 66  
               ((MutableVertex<I, V, E, M>) vertex).removeEdge(
 67  
                   removedDestVertex);
 68  10
           if (removeEdge == null) {
 69  0
             LOG.warn("resolve: Failed to remove edge with " +
 70  
                 "destination " + removedDestVertex + "on " +
 71  
                 vertex + " since it doesn't exist.");
 72  
           }
 73  10
         }
 74  20
         if (vertexChanges.getRemovedVertexCount() > 0) {
 75  10
           vertex = null;
 76  
         }
 77  
       }
 78  
     }
 79  
 
 80  45
     if (vertex == null) {
 81  35
       if (vertexChanges != null) {
 82  30
         if (!vertexChanges.getAddedVertexList().isEmpty()) {
 83  10
           vertex = vertexChanges.getAddedVertexList().get(0);
 84  
         }
 85  
       }
 86  35
       if (vertex == null && hasMessages) {
 87  5
         vertex = instantiateVertex();
 88  5
         vertex.initialize(vertexId,
 89  
             BspUtils.<V>createVertexValue(getConf()),
 90  
             null,
 91  
             null);
 92  
       }
 93  
     } else {
 94  10
       if ((vertexChanges != null) &&
 95  
           (!vertexChanges.getAddedVertexList().isEmpty())) {
 96  0
         LOG.warn("resolve: Tried to add a vertex with id = " +
 97  
             vertex.getId() + " when one already " +
 98  
             "exists.  Ignoring the add vertex request.");
 99  
       }
 100  
     }
 101  
 
 102  45
     if (vertexChanges != null &&
 103  
         !vertexChanges.getAddedEdgeList().isEmpty()) {
 104  10
       MutableVertex<I, V, E, M> mutableVertex =
 105  
           (MutableVertex<I, V, E, M>) vertex;
 106  10
       for (Edge<I, E> edge : vertexChanges.getAddedEdgeList()) {
 107  10
         mutableVertex.addEdge(edge.getTargetVertexId(),
 108  
             edge.getValue());
 109  
       }
 110  
     }
 111  
 
 112  45
     return vertex;
 113  
   }
 114  
 
 115  
   @Override
 116  
   public Vertex<I, V, E, M> instantiateVertex() {
 117  5
     Vertex<I, V, E, M> vertex =
 118  
         BspUtils.<I, V, E, M>createVertex(getConf());
 119  5
     vertex.setGraphState(graphState);
 120  5
     return vertex;
 121  
   }
 122  
 
 123  
   @Override
 124  
   public Configuration getConf() {
 125  10
     return conf;
 126  
   }
 127  
 
 128  
   @Override
 129  
   public void setConf(Configuration conf) {
 130  45
     this.conf = conf;
 131  45
   }
 132  
 
 133  
   /**
 134  
    * Set the graph state.
 135  
    *
 136  
    * @param graphState Graph state saved.
 137  
    */
 138  
   public void setGraphState(GraphState<I, V, E, M> graphState) {
 139  45
     this.graphState = graphState;
 140  45
   }
 141  
 }