You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ha...@apache.org on 2017/07/12 00:52:43 UTC

hive git commit: HIVE-16955 : General Improvements To org.apache.hadoop.hive.metastore.MetaStoreUtils (Beluga Behr via Ashutosh Chauhan)

Repository: hive
Updated Branches:
  refs/heads/master 7fc723678 -> 607077a90


HIVE-16955 : General Improvements To org.apache.hadoop.hive.metastore.MetaStoreUtils (Beluga Behr via Ashutosh Chauhan)

Signed-off-by: Ashutosh Chauhan <ha...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/607077a9
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/607077a9
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/607077a9

Branch: refs/heads/master
Commit: 607077a906fa5157538ac74dd4be38c3ece33aa0
Parents: 7fc7236
Author: BELUGA BEHR <da...@gmail.com>
Authored: Sat Jun 24 17:02:00 2017 -0700
Committer: Ashutosh Chauhan <ha...@apache.org>
Committed: Tue Jul 11 17:51:54 2017 -0700

----------------------------------------------------------------------
 .../hadoop/hive/metastore/MetaStoreUtils.java   | 116 +++++++------------
 1 file changed, 42 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/607077a9/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java
----------------------------------------------------------------------
diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java
index 1aaba4c..b52c94c 100644
--- a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java
+++ b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java
@@ -43,6 +43,8 @@ import java.util.regex.Pattern;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Maps;
 
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.ListUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -129,7 +131,7 @@ public class MetaStoreUtils {
     serdeInfo.getParameters().put(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_FORMAT,
         DEFAULT_SERIALIZATION_FORMAT);
 
-    List<FieldSchema> fields = new ArrayList<FieldSchema>();
+    List<FieldSchema> fields = new ArrayList<FieldSchema>(columns.size());
     sd.setCols(fields);
     for (String col : columns) {
       FieldSchema field = new FieldSchema(col,
@@ -554,12 +556,9 @@ public class MetaStoreUtils {
    */
   public static List<String> getPvals(List<FieldSchema> partCols,
       Map<String, String> partSpec) {
-    List<String> pvals = new ArrayList<String>();
+    List<String> pvals = new ArrayList<String>(partCols.size());
     for (FieldSchema field : partCols) {
-      String val = partSpec.get(field.getName());
-      if (val == null) {
-        val = "";
-      }
+      String val = StringUtils.defaultString(partSpec.get(field.getName()));
       pvals.add(val);
     }
     return pvals;
@@ -590,10 +589,7 @@ public class MetaStoreUtils {
     }
     tpat = Pattern.compile("[" + allowedCharacters + "]+");
     Matcher m = tpat.matcher(name);
-    if (m.matches()) {
-      return true;
-    }
-    return false;
+    return m.matches();
   }
 
   /*
@@ -637,18 +633,7 @@ public class MetaStoreUtils {
   }
 
   static boolean areSameColumns(List<FieldSchema> oldCols, List<FieldSchema> newCols) {
-    if (oldCols.size() != newCols.size()) {
-      return false;
-    } else {
-      for (int i = 0; i < oldCols.size(); i++) {
-        FieldSchema oldCol = oldCols.get(i);
-        FieldSchema newCol = newCols.get(i);
-        if(!oldCol.equals(newCol)) {
-          return false;
-        }
-      }
-    }
-    return true;
+    return ListUtils.isEqualList(oldCols, newCols);
   }
 
   /*
@@ -725,7 +710,7 @@ public class MetaStoreUtils {
   }
 
   public static String validateSkewedColNames(List<String> cols) {
-    if (null == cols) {
+    if (CollectionUtils.isEmpty(cols)) {
       return null;
     }
     for (String col : cols) {
@@ -738,10 +723,10 @@ public class MetaStoreUtils {
 
   public static String validateSkewedColNamesSubsetCol(List<String> skewedColNames,
       List<FieldSchema> cols) {
-    if (null == skewedColNames) {
+    if (CollectionUtils.isEmpty(skewedColNames)) {
       return null;
     }
-    List<String> colNames = new ArrayList<String>();
+    List<String> colNames = new ArrayList<String>(cols.size());
     for (FieldSchema fieldSchema : cols) {
       colNames.add(fieldSchema.getName());
     }
@@ -905,7 +890,7 @@ public class MetaStoreUtils {
     }
     ddl.append("}");
 
-    LOG.debug("DDL: " + ddl);
+    LOG.trace("DDL: {}", ddl);
     return ddl.toString();
   }
 
@@ -1021,7 +1006,7 @@ public class MetaStoreUtils {
             (key.equals(cols) || key.equals(colTypes) || key.equals(parts))) {
           continue;
         }
-        schema.put(key, (param.getValue() != null) ? param.getValue() : "");
+        schema.put(key, (param.getValue() != null) ? param.getValue() : StringUtils.EMPTY);
       }
 
       if (sd.getSerdeInfo().getSerializationLib() != null) {
@@ -1058,7 +1043,7 @@ public class MetaStoreUtils {
       }
       colNameBuf.append(col.getName());
       colTypeBuf.append(col.getType());
-      colComment.append((null != col.getComment()) ? col.getComment() : "");
+      colComment.append((null != col.getComment()) ? col.getComment() : StringUtils.EMPTY);
       first = false;
     }
     schema.setProperty(
@@ -1116,7 +1101,7 @@ public class MetaStoreUtils {
     }
     if (sd.getSerdeInfo() != null) {
       for (Map.Entry<String,String> param : sd.getSerdeInfo().getParameters().entrySet()) {
-        schema.put(param.getKey(), (param.getValue() != null) ? param.getValue() : "");
+        schema.put(param.getKey(), (param.getValue() != null) ? param.getValue() : StringUtils.EMPTY);
       }
 
       if (sd.getSerdeInfo().getSerializationLib() != null) {
@@ -1132,10 +1117,10 @@ public class MetaStoreUtils {
           getDDLFromFieldSchema(tableName, sd.getCols()));
     }
 
-    String partString = "";
-    String partStringSep = "";
-    String partTypesString = "";
-    String partTypesStringSep = "";
+    String partString = StringUtils.EMPTY;
+    String partStringSep = StringUtils.EMPTY;
+    String partTypesString = StringUtils.EMPTY;
+    String partTypesStringSep = StringUtils.EMPTY;
     for (FieldSchema partKey : partitionKeys) {
       partString = partString.concat(partStringSep);
       partString = partString.concat(partKey.getName());
@@ -1325,7 +1310,7 @@ public class MetaStoreUtils {
     for (Map.Entry<Thread, StackTraceElement[]> entry : threadStacks.entrySet()) {
       Thread t = entry.getKey();
       sb.append(System.lineSeparator());
-      sb.append("Name: ").append(t.getName()).append(" State: " + t.getState());
+      sb.append("Name: ").append(t.getName()).append(" State: ").append(t.getState());
       addStackString(entry.getValue(), sb);
     }
     return sb.toString();
@@ -1510,11 +1495,7 @@ public class MetaStoreUtils {
   public static boolean isArchived(
       org.apache.hadoop.hive.metastore.api.Partition part) {
     Map<String, String> params = part.getParameters();
-    if ("true".equalsIgnoreCase(params.get(hive_metastoreConstants.IS_ARCHIVED))) {
-      return true;
-    } else {
-      return false;
-    }
+    return "TRUE".equalsIgnoreCase(params.get(hive_metastoreConstants.IS_ARCHIVED));
   }
 
   public static Path getOriginalLocation(
@@ -1643,10 +1624,9 @@ public class MetaStoreUtils {
    */
   static <T> List<T> getMetaStoreListeners(Class<T> clazz,
       HiveConf conf, String listenerImplList) throws MetaException {
-
     List<T> listeners = new ArrayList<T>();
-    listenerImplList = listenerImplList.trim();
-    if (listenerImplList.equals("")) {
+
+    if (StringUtils.isBlank(listenerImplList)) {
       return listeners;
     }
 
@@ -1740,23 +1720,15 @@ public class MetaStoreUtils {
     if (schema1.size() != schema2.size()) {
       return false;
     }
-    for (int i = 0; i < schema1.size(); i++) {
-      FieldSchema f1 = schema1.get(i);
-      FieldSchema f2 = schema2.get(i);
+    Iterator<FieldSchema> its1 = schema1.iterator();
+    Iterator<FieldSchema> its2 = schema2.iterator();
+    while (its1.hasNext()) {
+      FieldSchema f1 = its1.next();
+      FieldSchema f2 = its2.next();
       // The default equals provided by thrift compares the comments too for
       // equality, thus we need to compare the relevant fields here.
-      if (f1.getName() == null) {
-        if (f2.getName() != null) {
-          return false;
-        }
-      } else if (!f1.getName().equals(f2.getName())) {
-        return false;
-      }
-      if (f1.getType() == null) {
-        if (f2.getType() != null) {
-          return false;
-        }
-      } else if (!f1.getType().equals(f2.getType())) {
+      if (!StringUtils.equals(f1.getName(), f2.getName()) ||
+          !StringUtils.equals(f1.getType(), f2.getType())) {
         return false;
       }
     }
@@ -1797,9 +1769,9 @@ public class MetaStoreUtils {
     String lv = part.getParameters().get(ARCHIVING_LEVEL);
     if (lv != null) {
       return Integer.parseInt(lv);
-    } else {  // partitions archived before introducing multiple archiving
-      return part.getValues().size();
     }
+     // partitions archived before introducing multiple archiving
+    return part.getValues().size();
   }
 
   public static String[] getQualifiedName(String defaultDbName, String tableName) {
@@ -1807,7 +1779,7 @@ public class MetaStoreUtils {
     if (names.length == 1) {
       return new String[] { defaultDbName, tableName};
     }
-    return new String[] {names[0], names[1]};
+    return names;
   }
 
   /**
@@ -1817,11 +1789,7 @@ public class MetaStoreUtils {
       = new com.google.common.base.Function<String, String>() {
     @Override
     public java.lang.String apply(@Nullable java.lang.String string) {
-      if (string == null){
-        return "";
-      } else {
-        return string;
-      }
+      return StringUtils.defaultString(string);
     }
   };
 
@@ -1859,7 +1827,7 @@ public class MetaStoreUtils {
   private static URL urlFromPathString(String onestr) {
     URL oneurl = null;
     try {
-      if (StringUtils.indexOf(onestr, "file:/") == 0) {
+      if (onestr.startsWith("file:/")) {
         oneurl = new URL(onestr);
       } else {
         oneurl = new File(onestr).toURL();
@@ -1879,7 +1847,7 @@ public class MetaStoreUtils {
   public static ClassLoader addToClassPath(ClassLoader cloader, String[] newPaths) throws Exception {
     URLClassLoader loader = (URLClassLoader) cloader;
     List<URL> curPath = Arrays.asList(loader.getURLs());
-    ArrayList<URL> newPath = new ArrayList<URL>();
+    ArrayList<URL> newPath = new ArrayList<URL>(curPath.size());
 
     // get a list with the current classpath components
     for (URL onePath : curPath) {
@@ -1902,15 +1870,15 @@ public class MetaStoreUtils {
     // all the special characters with the corresponding number in ASCII.
     // Note that unicode is not supported in table names. And we have explicit
     // checks for it.
-    String ret = "";
+    StringBuilder sb = new StringBuilder();
     for (char ch : name.toCharArray()) {
       if (Character.isLetterOrDigit(ch) || ch == '_') {
-        ret += ch;
+        sb.append(ch);
       } else {
-        ret += "-" + (int) ch + "-";
+        sb.append('-').append((int) ch).append('-');
       }
     }
-    return ret;
+    return sb.toString();
   }
 
   // this function will merge csOld into csNew.
@@ -1923,8 +1891,8 @@ public class MetaStoreUtils {
       // present in both, overwrite stats for columns absent in metastore and
       // leave alone columns stats missing from stats task. This last case may
       // leave stats in stale state. This will be addressed later.
-      LOG.debug("New ColumnStats size is " + csNew.getStatsObj().size()
-          + ". But old ColumnStats size is " + csOld.getStatsObjSize());
+      LOG.debug("New ColumnStats size is {}, but old ColumnStats size is {}", 
+          csNew.getStatsObj().size(), csOld.getStatsObjSize());
     }
     // In this case, we have to find out which columns can be merged.
     Map<String, ColumnStatisticsObj> map = new HashMap<>();
@@ -1970,7 +1938,7 @@ public class MetaStoreUtils {
   }
   
   public static List<String> getColumnNames(List<FieldSchema> schema) {
-    List<String> cols = new ArrayList<>();
+    List<String> cols = new ArrayList<>(schema.size());
     for (FieldSchema fs : schema) {
       cols.add(fs.getName());
     }