Coverage Report - org.apache.giraph.comm.SendPartitionMessagesRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
SendPartitionMessagesRequest
86%
32/37
90%
9/10
2
 
 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.comm;
 20  
 
 21  
 import org.apache.giraph.comm.RequestRegistry.Type;
 22  
 import org.apache.giraph.graph.BspUtils;
 23  
 import org.apache.hadoop.io.Writable;
 24  
 import org.apache.hadoop.io.WritableComparable;
 25  
 import org.apache.log4j.Logger;
 26  
 
 27  
 import com.google.common.collect.Lists;
 28  
 import com.google.common.collect.Maps;
 29  
 
 30  
 import java.io.DataInput;
 31  
 import java.io.DataOutput;
 32  
 import java.io.IOException;
 33  
 import java.util.Collection;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 import java.util.Map.Entry;
 37  
 
 38  
 /**
 39  
  * Send a collection of vertex messages for a partition.
 40  
  *
 41  
  * @param <I> Vertex id
 42  
  * @param <V> Vertex data
 43  
  * @param <E> Edge data
 44  
  * @param <M> Message data
 45  
  */
 46  
 @SuppressWarnings("rawtypes")
 47  
 public class SendPartitionMessagesRequest<I extends WritableComparable,
 48  
     V extends Writable, E extends Writable,
 49  
     M extends Writable> extends WritableRequest<I, V, E, M> {
 50  
   /** Class logger */
 51  1
   private static final Logger LOG =
 52  
       Logger.getLogger(SendPartitionMessagesRequest.class);
 53  
   /** Partition id */
 54  
   private int partitionId;
 55  
   /** Messages sent for a partition */
 56  
   private Map<I, Collection<M>> vertexIdMessages;
 57  
 
 58  
   /**
 59  
    * Constructor used for reflection only
 60  
    */
 61  19
   public SendPartitionMessagesRequest() { }
 62  
 
 63  
   /**
 64  
    * Constructor used to send request.
 65  
    *
 66  
    * @param partitionId Partition to send the request to
 67  
    * @param vertexIdMessages Map of messages to send
 68  
    */
 69  
   public SendPartitionMessagesRequest(int partitionId,
 70  1
                                       Map<I, Collection<M>> vertexIdMessages) {
 71  1
     this.partitionId = partitionId;
 72  1
     this.vertexIdMessages = vertexIdMessages;
 73  1
   }
 74  
 
 75  
   @Override
 76  
   public void readFieldsRequest(DataInput input) throws IOException {
 77  1
     partitionId = input.readInt();
 78  1
     int vertexIdMessagesSize = input.readInt();
 79  1
     vertexIdMessages = Maps.newHashMapWithExpectedSize(vertexIdMessagesSize);
 80  7
     for (int i = 0; i < vertexIdMessagesSize; ++i) {
 81  6
       I vertexId = BspUtils.<I>createVertexId(getConf());
 82  6
       vertexId.readFields(input);
 83  6
       int messageCount = input.readInt();
 84  6
       List<M> messageList = Lists.newArrayListWithCapacity(messageCount);
 85  27
       for (int j = 0; j < messageCount; ++j) {
 86  21
         M message = BspUtils.<M>createMessageValue(getConf());
 87  21
         message.readFields(input);
 88  21
         messageList.add(message);
 89  
       }
 90  6
       if (vertexIdMessages.put(vertexId, messageList) != null) {
 91  0
         throw new IllegalStateException(
 92  
             "readFields: Already has vertex id " + vertexId);
 93  
       }
 94  
     }
 95  1
   }
 96  
 
 97  
   @Override
 98  
   public void writeRequest(DataOutput output) throws IOException {
 99  1
     output.writeInt(partitionId);
 100  1
     output.writeInt(vertexIdMessages.size());
 101  1
     for (Entry<I, Collection<M>> entry : vertexIdMessages.entrySet()) {
 102  6
       entry.getKey().write(output);
 103  6
       output.writeInt(entry.getValue().size());
 104  6
       for (M message : entry.getValue()) {
 105  21
         message.write(output);
 106  
       }
 107  
     }
 108  1
   }
 109  
 
 110  
   @Override
 111  
   public Type getType() {
 112  10
     return Type.SEND_PARTITION_MESSAGES_REQUEST;
 113  
   }
 114  
 
 115  
   @Override
 116  
   public void doRequest(ServerData<I, V, E, M> serverData) {
 117  
     try {
 118  1
       serverData.getIncomingMessageStore().addPartitionMessages(
 119  
           vertexIdMessages, partitionId);
 120  0
     } catch (IOException e) {
 121  0
       throw new RuntimeException("doRequest: Got IOException ", e);
 122  1
     }
 123  1
   }
 124  
 
 125  
   /**
 126  
    * Get id of partition
 127  
    *
 128  
    * @return Partition id
 129  
    */
 130  
   public int getPartitionId() {
 131  0
     return partitionId;
 132  
   }
 133  
 
 134  
   /**
 135  
    * Get messages
 136  
    *
 137  
    * @return Messages map
 138  
    */
 139  
   public Map<I, Collection<M>> getVertexIdMessages() {
 140  0
     return vertexIdMessages;
 141  
   }
 142  
 }