Coverage Report - org.apache.giraph.io.formats.multi.InputFormatDescription
 
Classes in this File Line Coverage Branch Coverage Complexity
InputFormatDescription
0%
0/53
0%
0/10
2.3
 
 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.io.formats.multi;
 20  
 
 21  
 import org.apache.giraph.io.GiraphInputFormat;
 22  
 import org.apache.hadoop.conf.Configuration;
 23  
 import org.json.JSONArray;
 24  
 import org.json.JSONException;
 25  
 import org.json.JSONObject;
 26  
 
 27  
 import com.google.common.collect.Maps;
 28  
 
 29  
 import java.util.Iterator;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * Description of the input format - holds input format class and all
 34  
  * parameters specifically set for that input format.
 35  
  *
 36  
  * Used only with input formats which wrap several input formats into one
 37  
  * ({@link MultiVertexInputFormat} and {@link MultiEdgeInputFormat})
 38  
  *
 39  
  * @param <IF> Input format type
 40  
  */
 41  
 public abstract class InputFormatDescription<IF extends GiraphInputFormat> {
 42  
   /** Input format class */
 43  
   private Class<? extends IF> inputFormatClass;
 44  
   /** Parameters set specifically for this input format */
 45  0
   private final Map<String, String> parameters = Maps.newHashMap();
 46  
 
 47  
   /**
 48  
    * Constructor with input format class
 49  
    *
 50  
    * @param inputFormatClass Input format class
 51  
    */
 52  0
   public InputFormatDescription(Class<? extends IF> inputFormatClass) {
 53  0
     this.inputFormatClass = inputFormatClass;
 54  0
   }
 55  
 
 56  
   /**
 57  
    * Constructor with json string describing this input format
 58  
    *
 59  
    * @param description Json string describing this input format
 60  
    */
 61  
   @SuppressWarnings("unchecked")
 62  0
   public InputFormatDescription(String description) {
 63  
     try {
 64  0
       JSONArray jsonArray = new JSONArray(description);
 65  0
       inputFormatClass =
 66  0
           (Class<? extends IF>) Class.forName(jsonArray.getString(0));
 67  0
       if (jsonArray.length() > 1) {
 68  0
         addParameters(jsonArray.getJSONObject(1));
 69  
       }
 70  0
     } catch (JSONException e) {
 71  0
       throw new IllegalStateException(
 72  
           "Failed to parse JSON " + description, e);
 73  0
     } catch (ClassNotFoundException e) {
 74  0
       throw new IllegalStateException("Couldn't find " +
 75  
           "input format class from description " + description, e);
 76  0
     }
 77  0
   }
 78  
 
 79  
   /**
 80  
    * Add parameter to this input format description
 81  
    *
 82  
    * @param name  Parameter name
 83  
    * @param value Parameter value
 84  
    */
 85  
   public void addParameter(String name, String value) {
 86  0
     parameters.put(name, value);
 87  0
   }
 88  
 
 89  
   /**
 90  
    * Add all parameters from json object
 91  
    *
 92  
    * @param parametersJson Json object to read parameters from
 93  
    */
 94  
   public void addParameters(JSONObject parametersJson) {
 95  0
     Iterator<?> keys = parametersJson.keys();
 96  0
     while (keys.hasNext()) {
 97  0
       String key = (String) keys.next();
 98  
       try {
 99  0
         addParameter(key, parametersJson.getString(key));
 100  0
       } catch (JSONException e) {
 101  0
         throw new IllegalStateException("addParameters: Failed to parse " +
 102  
             parametersJson, e);
 103  0
       }
 104  0
     }
 105  0
   }
 106  
 
 107  
   /**
 108  
    * Convert input format description to json array
 109  
    *
 110  
    * @return Json array representing this input format description
 111  
    */
 112  
   public JSONArray toJsonArray() {
 113  0
     JSONArray jsonArray = new JSONArray();
 114  0
     jsonArray.put(inputFormatClass.getName());
 115  0
     JSONObject jsonParameters = new JSONObject();
 116  0
     for (Map.Entry<String, String> entry : parameters.entrySet()) {
 117  
       try {
 118  0
         jsonParameters.put(entry.getKey(), entry.getValue());
 119  0
       } catch (JSONException e) {
 120  0
         throw new IllegalStateException("toJsonArray: JSONException occurred " +
 121  0
             "while trying to process (" + entry.getKey() + ", " +
 122  0
             entry.getValue() + ")", e);
 123  0
       }
 124  0
     }
 125  0
     jsonArray.put(jsonParameters);
 126  0
     return jsonArray;
 127  
   }
 128  
 
 129  
   public Class<? extends IF> getInputFormatClass() {
 130  0
     return inputFormatClass;
 131  
   }
 132  
 
 133  
   public void setInputFormatClass(Class<? extends IF> inputFormatClass) {
 134  0
     this.inputFormatClass = inputFormatClass;
 135  0
   }
 136  
 
 137  
   /**
 138  
    * Put parameters from this input format description to configuration
 139  
    *
 140  
    * @param conf Configuration to put parameters to
 141  
    */
 142  
   public void putParametersToConfiguration(Configuration conf) {
 143  0
     for (Map.Entry<String, String> entry : parameters.entrySet()) {
 144  0
       conf.set(entry.getKey(), entry.getValue());
 145  0
     }
 146  0
   }
 147  
 
 148  
   @Override
 149  
   public String toString() {
 150  0
     return toJsonArray().toString();
 151  
   }
 152  
 
 153  
   /**
 154  
    * Create JSON string for the InputFormatDescriptions
 155  
    *
 156  
    * @param descriptions InputFormatDescriptions
 157  
    * @return JSON string describing these InputFormatDescriptions
 158  
    */
 159  
   public static String toJsonString(
 160  
       Iterable<? extends InputFormatDescription> descriptions) {
 161  0
     JSONArray jsonArray = new JSONArray();
 162  0
     for (InputFormatDescription description : descriptions) {
 163  0
       jsonArray.put(description.toJsonArray());
 164  0
     }
 165  0
     return jsonArray.toString();
 166  
   }
 167  
 }