Coverage Report - org.apache.giraph.conf.GiraphTypes
 
Classes in this File Line Coverage Branch Coverage Complexity
GiraphTypes
0%
0/52
0%
0/14
1.294
 
 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.conf;
 19  
 
 20  
 import static org.apache.giraph.conf.GiraphConstants.EDGE_VALUE_CLASS;
 21  
 import static org.apache.giraph.conf.GiraphConstants.OUTGOING_MESSAGE_VALUE_CLASS;
 22  
 import static org.apache.giraph.conf.GiraphConstants.VERTEX_CLASS;
 23  
 import static org.apache.giraph.conf.GiraphConstants.VERTEX_ID_CLASS;
 24  
 import static org.apache.giraph.conf.GiraphConstants.VERTEX_VALUE_CLASS;
 25  
 import static org.apache.giraph.utils.ConfigurationUtils.getTypesHolderClass;
 26  
 import static org.apache.giraph.utils.ReflectionUtils.getTypeArguments;
 27  
 
 28  
 import org.apache.giraph.graph.DefaultVertex;
 29  
 import org.apache.giraph.graph.Vertex;
 30  
 import org.apache.hadoop.conf.Configuration;
 31  
 import org.apache.hadoop.io.Writable;
 32  
 import org.apache.hadoop.io.WritableComparable;
 33  
 
 34  
 import com.google.common.base.Preconditions;
 35  
 
 36  
 /**
 37  
  * Holder for the generic types that describe user's graph.
 38  
  *
 39  
  * @param <I> Vertex ID class
 40  
  * @param <V> Vertex Value class
 41  
  * @param <E> Edge class
 42  
  */
 43  
 public class GiraphTypes<I extends WritableComparable, V extends Writable,
 44  
     E extends Writable> {
 45  
   /** Vertex id class */
 46  
   private Class<I> vertexIdClass;
 47  
   /** Vertex value class */
 48  
   private Class<V> vertexValueClass;
 49  
   /** Edge value class */
 50  
   private Class<E> edgeValueClass;
 51  
   /** Outgoing message value class */
 52  
   private Class<? extends Writable> outgoingMessageValueClass;
 53  
   /** Vertex implementation class */
 54  0
   private Class<? extends Vertex> vertexClass = DefaultVertex.class;
 55  
 
 56  
 
 57  
   /**
 58  
    * Empty Constructor
 59  
    */
 60  0
   public GiraphTypes() { }
 61  
 
 62  
   /**
 63  
    * Constructor taking values
 64  
    *
 65  
    * @param vertexIdClass vertex id class
 66  
    * @param vertexValueClass vertex value class
 67  
    * @param edgeValueClass edge value class
 68  
    * @param incomingMessageValueClass incoming message class
 69  
    * @param outgoingMessageValueClass outgoing message class
 70  
    */
 71  
   public GiraphTypes(Class<I> vertexIdClass,
 72  
       Class<V> vertexValueClass,
 73  
       Class<E> edgeValueClass,
 74  
       Class<? extends Writable> incomingMessageValueClass,
 75  0
       Class<? extends Writable> outgoingMessageValueClass) {
 76  0
     this.edgeValueClass = edgeValueClass;
 77  0
     this.outgoingMessageValueClass = outgoingMessageValueClass;
 78  0
     this.vertexIdClass = vertexIdClass;
 79  0
     this.vertexValueClass = vertexValueClass;
 80  0
   }
 81  
 
 82  
   /**
 83  
    * Read types from a {@link Configuration}.
 84  
    * First tries to read them directly from the configuration options.
 85  
    * If that doesn't work, tries to infer from {@link TypesHolder}.
 86  
    *
 87  
    * @param conf Configuration
 88  
    * @param <IX> vertex id
 89  
    * @param <VX> vertex value
 90  
    * @param <EX> edge value
 91  
    * @return GiraphTypes
 92  
    */
 93  
   public static <IX extends WritableComparable, VX extends Writable,
 94  
         EX extends Writable> GiraphTypes<IX, VX, EX> readFrom(
 95  
       Configuration conf) {
 96  0
     GiraphTypes<IX, VX, EX> types = new GiraphTypes<IX, VX, EX>();
 97  0
     types.readDirect(conf);
 98  0
     if (!types.hasData()) {
 99  0
       Class<? extends TypesHolder> klass = getTypesHolderClass(conf);
 100  0
       if (klass != null) {
 101  0
         types.inferFrom(klass);
 102  
       }
 103  
     }
 104  0
     return types;
 105  
   }
 106  
 
 107  
   /**
 108  
    * Infer types from Computation class
 109  
    *
 110  
    * @param klass Computation class
 111  
    */
 112  
   public void inferFrom(Class<? extends TypesHolder> klass) {
 113  0
     Class<?>[] classList = getTypeArguments(TypesHolder.class, klass);
 114  0
     Preconditions.checkArgument(classList.length == 5);
 115  0
     vertexIdClass = (Class<I>) classList[0];
 116  0
     vertexValueClass = (Class<V>) classList[1];
 117  0
     edgeValueClass = (Class<E>) classList[2];
 118  0
     outgoingMessageValueClass = (Class<? extends Writable>) classList[4];
 119  0
   }
 120  
 
 121  
   /**
 122  
    * Read types directly from Configuration
 123  
    *
 124  
    * @param conf Configuration
 125  
    */
 126  
   private void readDirect(Configuration conf) {
 127  0
     vertexIdClass = (Class<I>) VERTEX_ID_CLASS.get(conf);
 128  0
     vertexValueClass = (Class<V>) VERTEX_VALUE_CLASS.get(conf);
 129  0
     edgeValueClass = (Class<E>) EDGE_VALUE_CLASS.get(conf);
 130  0
     outgoingMessageValueClass = OUTGOING_MESSAGE_VALUE_CLASS.get(conf);
 131  0
     vertexClass = VERTEX_CLASS.get(conf);
 132  0
   }
 133  
 
 134  
   /**
 135  
    * Check if types are set
 136  
    *
 137  
    * @return true if types are set
 138  
    */
 139  
   public boolean hasData() {
 140  0
     return vertexIdClass != null &&
 141  
         vertexValueClass != null &&
 142  
         edgeValueClass != null &&
 143  
         outgoingMessageValueClass != null;
 144  
   }
 145  
 
 146  
   /**
 147  
    * Write types to Configuration
 148  
    *
 149  
    * @param conf Configuration
 150  
    */
 151  
   public void writeTo(Configuration conf) {
 152  0
     VERTEX_ID_CLASS.set(conf, vertexIdClass);
 153  0
     VERTEX_VALUE_CLASS.set(conf, vertexValueClass);
 154  0
     EDGE_VALUE_CLASS.set(conf, edgeValueClass);
 155  0
     OUTGOING_MESSAGE_VALUE_CLASS.set(conf, outgoingMessageValueClass);
 156  0
   }
 157  
 
 158  
   /**
 159  
    * Write types to Configuration if not already set
 160  
    *
 161  
    * @param conf Configuration
 162  
    */
 163  
   public void writeIfUnset(Configuration conf) {
 164  0
     VERTEX_ID_CLASS.setIfUnset(conf, vertexIdClass);
 165  0
     VERTEX_VALUE_CLASS.setIfUnset(conf, vertexValueClass);
 166  0
     EDGE_VALUE_CLASS.setIfUnset(conf, edgeValueClass);
 167  0
     OUTGOING_MESSAGE_VALUE_CLASS.setIfUnset(conf, outgoingMessageValueClass);
 168  0
   }
 169  
 
 170  
   public Class<E> getEdgeValueClass() {
 171  0
     return edgeValueClass;
 172  
   }
 173  
 
 174  
   Class<? extends Writable> getInitialOutgoingMessageValueClass() {
 175  0
     return outgoingMessageValueClass;
 176  
   }
 177  
 
 178  
   public Class<I> getVertexIdClass() {
 179  0
     return vertexIdClass;
 180  
   }
 181  
 
 182  
   public Class<V> getVertexValueClass() {
 183  0
     return vertexValueClass;
 184  
   }
 185  
 
 186  
   public Class<? extends Vertex> getVertexClass() {
 187  0
     return vertexClass;
 188  
   }
 189  
 
 190  
   public void setEdgeValueClass(Class<E> edgeValueClass) {
 191  0
     this.edgeValueClass = edgeValueClass;
 192  0
   }
 193  
 
 194  
   public void setVertexIdClass(Class<I> vertexIdClass) {
 195  0
     this.vertexIdClass = vertexIdClass;
 196  0
   }
 197  
 
 198  
   public void setVertexValueClass(Class<V> vertexValueClass) {
 199  0
     this.vertexValueClass = vertexValueClass;
 200  0
   }
 201  
 
 202  
   public void setOutgoingMessageValueClass(
 203  
       Class<? extends Writable> outgoingMessageValueClass) {
 204  0
     this.outgoingMessageValueClass = outgoingMessageValueClass;
 205  0
   }
 206  
 }