Coverage Report - org.apache.giraph.graph.IntIntNullIntVertex
 
Classes in this File Line Coverage Branch Coverage Complexity
IntIntNullIntVertex
90%
49/54
85%
24/28
2.25
IntIntNullIntVertex$1
100%
2/2
N/A
2.25
IntIntNullIntVertex$2
100%
2/2
N/A
2.25
 
 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.utils.UnmodifiableIntArrayIterator;
 22  
 import org.apache.hadoop.io.IntWritable;
 23  
 import org.apache.hadoop.io.NullWritable;
 24  
 
 25  
 import com.google.common.collect.Iterables;
 26  
 
 27  
 import java.io.DataInput;
 28  
 import java.io.DataOutput;
 29  
 import java.io.IOException;
 30  
 import java.util.Iterator;
 31  
 import java.util.Map;
 32  
 
 33  
 /**
 34  
  * Simple implementation of {@link Vertex} using an int as id, value and
 35  
  * message.  Edges are immutable and unweighted. This class aims to be as
 36  
  * memory efficient as possible.
 37  
  */
 38  327
 public abstract class IntIntNullIntVertex extends
 39  
     SimpleVertex<IntWritable, IntWritable, IntWritable> {
 40  
   /** Int array of neighbor vertex ids */
 41  
   private int[] neighbors;
 42  
   /** Int array of messages */
 43  
   private int[] messages;
 44  
 
 45  
   @Override
 46  
   public void initialize(IntWritable id, IntWritable value,
 47  
                          Map<IntWritable, NullWritable> edges,
 48  
                          Iterable<IntWritable> messages) {
 49  48
     super.initialize(id, value);
 50  48
     this.neighbors = new int[(edges != null) ? edges.size() : 0];
 51  48
     int n = 0;
 52  48
     if (edges != null) {
 53  27
       for (Map.Entry<IntWritable, NullWritable> edge : edges.entrySet()) {
 54  66
         this.neighbors[n++] = edge.getKey().get();
 55  
       }
 56  
     }
 57  48
     this.messages = new int[(messages != null) ? Iterables.size(messages) : 0];
 58  48
     if (messages != null) {
 59  27
       n = 0;
 60  27
       for (IntWritable message : messages) {
 61  2
         this.messages[n++] = message.get();
 62  
       }
 63  
     }
 64  48
   }
 65  
 
 66  
   @Override
 67  
   public Iterable<IntWritable> getNeighbors() {
 68  62
     return new Iterable<IntWritable>() {
 69  
       @Override
 70  
       public Iterator<IntWritable> iterator() {
 71  62
         return new UnmodifiableIntArrayIterator(neighbors);
 72  
       }
 73  
     };
 74  
   }
 75  
 
 76  
   @Override
 77  
   public boolean hasEdge(IntWritable targetVertexId) {
 78  0
     for (int neighbor : neighbors) {
 79  0
       if (neighbor == targetVertexId.get()) {
 80  0
         return true;
 81  
       }
 82  
     }
 83  0
     return false;
 84  
   }
 85  
 
 86  
   @Override
 87  
   public int getNumEdges() {
 88  209
     return neighbors.length;
 89  
   }
 90  
 
 91  
   @Override
 92  
   public Iterable<IntWritable> getMessages() {
 93  175
     return new Iterable<IntWritable>() {
 94  
       @Override
 95  
       public Iterator<IntWritable> iterator() {
 96  149
         return new UnmodifiableIntArrayIterator(messages);
 97  
       }
 98  
     };
 99  
   }
 100  
 
 101  
   @Override
 102  
   public void putMessages(Iterable<IntWritable> newMessages) {
 103  44
     messages = new int[Iterables.size(newMessages)];
 104  44
     int n = 0;
 105  44
     for (IntWritable message : newMessages) {
 106  44
       messages[n++] = message.get();
 107  
     }
 108  44
   }
 109  
 
 110  
   @Override
 111  
   public int getNumMessages() {
 112  0
     return messages.length;
 113  
   }
 114  
 
 115  
   @Override
 116  
   void releaseResources() {
 117  70
     messages = new int[0];
 118  70
   }
 119  
 
 120  
   @Override
 121  
   public void write(final DataOutput out) throws IOException {
 122  28
     out.writeInt(getId().get());
 123  28
     out.writeInt(getValue().get());
 124  28
     out.writeInt(neighbors.length);
 125  30
     for (int n = 0; n < neighbors.length; n++) {
 126  2
       out.writeInt(neighbors[n]);
 127  
     }
 128  28
     out.writeInt(messages.length);
 129  30
     for (int n = 0; n < messages.length; n++) {
 130  2
       out.writeInt(messages[n]);
 131  
     }
 132  28
     out.writeBoolean(isHalted());
 133  28
   }
 134  
 
 135  
   @Override
 136  
   public void readFields(DataInput in) throws IOException {
 137  21
     int id = in.readInt();
 138  21
     int value = in.readInt();
 139  21
     super.initialize(new IntWritable(id), new IntWritable(value));
 140  21
     int numEdges = in.readInt();
 141  21
     neighbors = new int[numEdges];
 142  23
     for (int n = 0; n < numEdges; n++) {
 143  2
       neighbors[n] = in.readInt();
 144  
     }
 145  21
     int numMessages = in.readInt();
 146  21
     messages = new int[numMessages];
 147  23
     for (int n = 0; n < numMessages; n++) {
 148  2
       messages[n] = in.readInt();
 149  
     }
 150  21
     boolean halt = in.readBoolean();
 151  21
     if (halt) {
 152  1
       voteToHalt();
 153  
     } else {
 154  20
       wakeUp();
 155  
     }
 156  21
   }
 157  
 }