Coverage Report - org.apache.giraph.graph.DefaultVertex
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultVertex
0%
0/58
0%
0/22
1.75
DefaultVertex$1
0%
0/2
N/A
1.75
DefaultVertex$2
0%
0/2
N/A
1.75
DefaultVertex$2$1
0%
0/8
0%
0/4
1.75
 
 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  
 package org.apache.giraph.graph;
 19  
 
 20  
 import org.apache.giraph.conf.DefaultImmutableClassesGiraphConfigurable;
 21  
 import org.apache.giraph.edge.Edge;
 22  
 import org.apache.giraph.utils.Trimmable;
 23  
 import org.apache.giraph.edge.MultiRandomAccessOutEdges;
 24  
 import org.apache.giraph.edge.MutableEdge;
 25  
 import org.apache.giraph.edge.MutableEdgesIterable;
 26  
 import org.apache.giraph.edge.MutableEdgesWrapper;
 27  
 import org.apache.giraph.edge.MutableOutEdges;
 28  
 import org.apache.giraph.edge.OutEdges;
 29  
 import org.apache.giraph.edge.StrictRandomAccessOutEdges;
 30  
 import org.apache.hadoop.io.Writable;
 31  
 import org.apache.hadoop.io.WritableComparable;
 32  
 
 33  
 import com.google.common.collect.UnmodifiableIterator;
 34  
 
 35  
 import java.util.Iterator;
 36  
 
 37  
 /**
 38  
  * Class which holds vertex id, data and edges.
 39  
  *
 40  
  * @param <I> Vertex id
 41  
  * @param <V> Vertex data
 42  
  * @param <E> Edge data
 43  
  */
 44  0
 public class DefaultVertex<I extends WritableComparable,
 45  
     V extends Writable, E extends Writable>
 46  
     extends DefaultImmutableClassesGiraphConfigurable<I, V, E>
 47  
     implements Vertex<I, V, E>, Trimmable {
 48  
   /** Vertex id. */
 49  
   private I id;
 50  
   /** Vertex value. */
 51  
   private V value;
 52  
   /** Outgoing edges. */
 53  
   private OutEdges<I, E> edges;
 54  
   /** If true, do not do anymore computation on this vertex. */
 55  
   private boolean halt;
 56  
 
 57  
   @Override
 58  
   public void initialize(I id, V value, Iterable<Edge<I, E>> edges) {
 59  0
     this.id = id;
 60  0
     this.value = value;
 61  0
     setEdges(edges);
 62  0
   }
 63  
 
 64  
   @Override
 65  
   public void initialize(I id, V value) {
 66  0
     this.id = id;
 67  0
     this.value = value;
 68  0
     this.edges = getConf().createAndInitializeOutEdges(0);
 69  0
   }
 70  
 
 71  
   @Override
 72  
   public void setEdges(Iterable<Edge<I, E>> edges) {
 73  
     // If the iterable is actually an instance of OutEdges,
 74  
     // we simply take the reference.
 75  
     // Otherwise, we initialize a new OutEdges.
 76  0
     if (edges instanceof OutEdges) {
 77  0
       this.edges = (OutEdges<I, E>) edges;
 78  
     } else {
 79  0
       this.edges = getConf().createAndInitializeOutEdges(edges);
 80  
     }
 81  0
   }
 82  
 
 83  
   @Override
 84  
   public I getId() {
 85  0
     return id;
 86  
   }
 87  
 
 88  
   @Override
 89  
   public V getValue() {
 90  0
     return value;
 91  
   }
 92  
 
 93  
   @Override
 94  
   public void setValue(V value) {
 95  0
     this.value = value;
 96  0
   }
 97  
 
 98  
   @Override
 99  
   public Iterable<Edge<I, E>> getEdges() {
 100  0
     return edges;
 101  
   }
 102  
 
 103  
   @Override
 104  
   public Iterable<MutableEdge<I, E>> getMutableEdges() {
 105  
     // If the OutEdges implementation has a specialized mutable iterator,
 106  
     // we use that; otherwise, we build a new data structure as we iterate
 107  
     // over the current edges.
 108  0
     if (edges instanceof MutableOutEdges) {
 109  0
       return new Iterable<MutableEdge<I, E>>() {
 110  
         @Override
 111  
         public Iterator<MutableEdge<I, E>> iterator() {
 112  0
           return ((MutableOutEdges<I, E>) edges).mutableIterator();
 113  
         }
 114  
       };
 115  
     } else {
 116  0
       return new MutableEdgesIterable<I, E>(this);
 117  
     }
 118  
   }
 119  
 
 120  
   @Override
 121  
   public void unwrapMutableEdges() {
 122  0
     if (edges instanceof MutableEdgesWrapper) {
 123  0
       edges = ((MutableEdgesWrapper<I, E>) edges).unwrap();
 124  
     }
 125  0
   }
 126  
 
 127  
   @Override
 128  
   public int getNumEdges() {
 129  0
     return edges.size();
 130  
   }
 131  
 
 132  
   @Override
 133  
   public E getEdgeValue(I targetVertexId) {
 134  
     // If the OutEdges implementation has a specialized random-access
 135  
     // method, we use that; otherwise, we scan the edges.
 136  0
     if (edges instanceof StrictRandomAccessOutEdges) {
 137  0
       return ((StrictRandomAccessOutEdges<I, E>) edges)
 138  0
           .getEdgeValue(targetVertexId);
 139  
     } else {
 140  0
       for (Edge<I, E> edge : edges) {
 141  0
         if (edge.getTargetVertexId().equals(targetVertexId)) {
 142  0
           return edge.getValue();
 143  
         }
 144  0
       }
 145  0
       return null;
 146  
     }
 147  
   }
 148  
 
 149  
   @Override
 150  
   public void setEdgeValue(I targetVertexId, E edgeValue) {
 151  
     // If the OutEdges implementation has a specialized random-access
 152  
     // method, we use that; otherwise, we scan the edges.
 153  0
     if (edges instanceof StrictRandomAccessOutEdges) {
 154  0
       ((StrictRandomAccessOutEdges<I, E>) edges).setEdgeValue(
 155  
           targetVertexId, edgeValue);
 156  
     } else {
 157  0
       for (MutableEdge<I, E> edge : getMutableEdges()) {
 158  0
         if (edge.getTargetVertexId().equals(targetVertexId)) {
 159  0
           edge.setValue(edgeValue);
 160  
         }
 161  0
       }
 162  
     }
 163  0
   }
 164  
 
 165  
   @Override
 166  
   public Iterable<E> getAllEdgeValues(final I targetVertexId) {
 167  
     // If the OutEdges implementation has a specialized random-access
 168  
     // method, we use that; otherwise, we scan the edges.
 169  0
     if (edges instanceof MultiRandomAccessOutEdges) {
 170  0
       return ((MultiRandomAccessOutEdges<I, E>) edges)
 171  0
           .getAllEdgeValues(targetVertexId);
 172  
     } else {
 173  0
       return new Iterable<E>() {
 174  
         @Override
 175  
         public Iterator<E> iterator() {
 176  0
           return new UnmodifiableIterator<E>() {
 177  
             /** Iterator over all edges. */
 178  0
             private Iterator<Edge<I, E>> edgeIterator = edges.iterator();
 179  
             /** Last matching edge found. */
 180  
             private Edge<I, E> currentEdge;
 181  
 
 182  
             @Override
 183  
             public boolean hasNext() {
 184  0
               while (edgeIterator.hasNext()) {
 185  0
                 currentEdge = edgeIterator.next();
 186  0
                 if (currentEdge.getTargetVertexId().equals(targetVertexId)) {
 187  0
                   return true;
 188  
                 }
 189  
               }
 190  0
               return false;
 191  
             }
 192  
 
 193  
             @Override
 194  
             public E next() {
 195  0
               return currentEdge.getValue();
 196  
             }
 197  
           };
 198  
         }
 199  
       };
 200  
     }
 201  
   }
 202  
 
 203  
   @Override
 204  
   public void voteToHalt() {
 205  0
     halt = true;
 206  0
   }
 207  
 
 208  
   @Override
 209  
   public void wakeUp() {
 210  0
     halt = false;
 211  0
   }
 212  
 
 213  
   @Override
 214  
   public boolean isHalted() {
 215  0
     return halt;
 216  
   }
 217  
 
 218  
   @Override
 219  
   public void trim() {
 220  0
     if (edges instanceof Trimmable) {
 221  0
       ((Trimmable) edges).trim();
 222  
     }
 223  0
   }
 224  
 
 225  
   @Override
 226  
   public void addEdge(Edge<I, E> edge) {
 227  0
     edges.add(edge);
 228  0
   }
 229  
 
 230  
   @Override
 231  
   public void removeEdges(I targetVertexId) {
 232  0
     edges.remove(targetVertexId);
 233  0
   }
 234  
 
 235  
   @Override
 236  
   public String toString() {
 237  0
     return "Vertex(id=" + getId() + ",value=" + getValue() +
 238  0
         ",#edges=" + getNumEdges() + ")";
 239  
   }
 240  
 }