Coverage Report - org.apache.giraph.utils.ConfigurationObjectUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationObjectUtils
0%
0/23
0%
0/4
0
 
 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.utils;
 19  
 
 20  
 import org.apache.giraph.writable.kryo.KryoWritableWrapper;
 21  
 import org.apache.hadoop.conf.Configuration;
 22  
 
 23  
 /**
 24  
  * Utility methods for dealing with Hadoop configuration
 25  
  */
 26  
 public class ConfigurationObjectUtils {
 27  
   /** Hide constructor */
 28  0
   private ConfigurationObjectUtils() {
 29  0
   }
 30  
 
 31  
   /**
 32  
    * Encode bytes to a hex String
 33  
    *
 34  
    * @param bytes byte[]
 35  
    * @return encoded String
 36  
    */
 37  
   public static String encodeBytes(byte[] bytes) {
 38  0
     StringBuilder strBuf = new StringBuilder();
 39  0
     for (int i = 0; i < bytes.length; i++) {
 40  0
       strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ('a')));
 41  0
       strBuf.append((char) (((bytes[i]) & 0xF) + ('a')));
 42  
     }
 43  0
     return strBuf.toString();
 44  
   }
 45  
 
 46  
   /**
 47  
    * Decode bytes from a hex String
 48  
    *
 49  
    * @param str String to decode
 50  
    * @return decoded byte[]
 51  
    */
 52  
   public static byte[] decodeBytes(String str) {
 53  0
     byte[] bytes = new byte[str.length() / 2];
 54  0
     for (int i = 0; i < str.length(); i += 2) {
 55  0
       char c = str.charAt(i);
 56  0
       bytes[i / 2] = (byte) ((c - 'a') << 4);
 57  0
       c = str.charAt(i + 1);
 58  0
       bytes[i / 2] += c - 'a';
 59  
     }
 60  0
     return bytes;
 61  
   }
 62  
 
 63  
   /**
 64  
    * Set byte array to a conf option
 65  
    *
 66  
    * @param data Byte array
 67  
    * @param confOption Conf option
 68  
    * @param conf Configuration
 69  
    */
 70  
   public static void setByteArray(byte[] data, String confOption,
 71  
       Configuration conf) {
 72  0
     conf.set(confOption, encodeBytes(data));
 73  0
   }
 74  
 
 75  
   /**
 76  
    * Get byte array from a conf option
 77  
    *
 78  
    * @param confOption Conf option
 79  
    * @param conf Configuration
 80  
    * @return Byte array
 81  
    */
 82  
   public static byte[] getByteArray(String confOption,
 83  
       Configuration conf) {
 84  0
     return decodeBytes(conf.get(confOption));
 85  
   }
 86  
 
 87  
   /**
 88  
    * Set object in a conf option using kryo
 89  
    *
 90  
    * @param object Object to set
 91  
    * @param confOption Conf option
 92  
    * @param conf Configuration
 93  
    * @param <T> Type of the object
 94  
    */
 95  
   public static <T> void setObjectKryo(T object, String confOption,
 96  
       Configuration conf) {
 97  0
     setByteArray(WritableUtils.toByteArrayUnsafe(
 98  
             new KryoWritableWrapper<>(object)),
 99  
         confOption, conf);
 100  0
   }
 101  
 
 102  
   /**
 103  
    * Get object from a conf option using kryo
 104  
    *
 105  
    * @param confOption Conf option
 106  
    * @param conf Configuration
 107  
    * @return Object from conf
 108  
    * @param <T> Type of the object
 109  
    */
 110  
   public static <T> T getObjectKryo(String confOption,
 111  
       Configuration conf) {
 112  0
     KryoWritableWrapper<T> wrapper = new KryoWritableWrapper<>();
 113  0
     WritableUtils.fromByteArrayUnsafe(
 114  0
         getByteArray(confOption, conf), wrapper,
 115  
         new UnsafeReusableByteArrayInput());
 116  0
     return wrapper.get();
 117  
   }
 118  
 }