.../hadoop/hive/metastore/MetaStoreUtils.java | 116 ++++++++------------- 1 file changed, 42 insertions(+), 74 deletions(-) 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 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 static Table createColumnsetSchema(String name, List columns, serdeInfo.getParameters().put(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_FORMAT, DEFAULT_SERIALIZATION_FORMAT); - List fields = new ArrayList(); + List fields = new ArrayList(columns.size()); sd.setCols(fields); for (String col : columns) { FieldSchema field = new FieldSchema(col, @@ -554,12 +556,9 @@ static public void deleteWHDirectory(Path path, Configuration conf, */ public static List getPvals(List partCols, Map partSpec) { - List pvals = new ArrayList(); + List pvals = new ArrayList(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 @@ static public boolean validateName(String name, Configuration conf) { } tpat = Pattern.compile("[" + allowedCharacters + "]+"); Matcher m = tpat.matcher(name); - if (m.matches()) { - return true; - } - return false; + return m.matches(); } /* @@ -637,18 +633,7 @@ static void throwExceptionIfIncompatibleColTypeChange( } static boolean areSameColumns(List oldCols, List 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 @@ private static boolean isValidTypeChar(char c) { } public static String validateSkewedColNames(List cols) { - if (null == cols) { + if (CollectionUtils.isEmpty(cols)) { return null; } for (String col : cols) { @@ -738,10 +723,10 @@ public static String validateSkewedColNames(List cols) { public static String validateSkewedColNamesSubsetCol(List skewedColNames, List cols) { - if (null == skewedColNames) { + if (CollectionUtils.isEmpty(skewedColNames)) { return null; } - List colNames = new ArrayList(); + List colNames = new ArrayList(cols.size()); for (FieldSchema fieldSchema : cols) { colNames.add(fieldSchema.getName()); } @@ -905,7 +890,7 @@ public static String getDDLFromFieldSchema(String structName, } ddl.append("}"); - LOG.debug("DDL: " + ddl); + LOG.trace("DDL: {}", ddl); return ddl.toString(); } @@ -1021,7 +1006,7 @@ public static Properties getPartSchemaFromTableSchema( (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 static Properties addCols(Properties schema, List cols) { } 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 static Properties getSchemaWithoutCols(org.apache.hadoop.hive.metastore.a } if (sd.getSerdeInfo() != null) { for (Map.Entry 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 static Properties getSchemaWithoutCols(org.apache.hadoop.hive.metastore.a 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 @@ private static String getAllThreadStacksAsString() { for (Map.Entry 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 static boolean isImmutableTable(Table table) { public static boolean isArchived( org.apache.hadoop.hive.metastore.api.Partition part) { Map 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 static boolean isView(Table table) { */ static List getMetaStoreListeners(Class clazz, HiveConf conf, String listenerImplList) throws MetaException { - List listeners = new ArrayList(); - listenerImplList = listenerImplList.trim(); - if (listenerImplList.equals("")) { + + if (StringUtils.isBlank(listenerImplList)) { return listeners; } @@ -1740,23 +1720,15 @@ public static boolean compareFieldColumns(List schema1, List its1 = schema1.iterator(); + Iterator 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 static int getArchivingLevel(Partition part) throws MetaException { 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 static int getArchivingLevel(Partition part) throws MetaException { if (names.length == 1) { return new String[] { defaultDbName, tableName}; } - return new String[] {names[0], names[1]}; + return names; } /** @@ -1817,11 +1789,7 @@ public static int getArchivingLevel(Partition part) throws MetaException { = new com.google.common.base.Function() { @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 static int getArchivingLevel(Partition part) throws MetaException { 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 @@ private static URL urlFromPathString(String onestr) { public static ClassLoader addToClassPath(ClassLoader cloader, String[] newPaths) throws Exception { URLClassLoader loader = (URLClassLoader) cloader; List curPath = Arrays.asList(loader.getURLs()); - ArrayList newPath = new ArrayList(); + ArrayList newPath = new ArrayList(curPath.size()); // get a list with the current classpath components for (URL onePath : curPath) { @@ -1902,15 +1870,15 @@ public static String encodeTableName(String name) { // 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 static void mergeColStats(ColumnStatistics csNew, ColumnStatistics csOld) // 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 map = new HashMap<>(); @@ -1970,7 +1938,7 @@ public static MetaException newMetaException(String errorMessage, Exception e) { } public static List getColumnNames(List schema) { - List cols = new ArrayList<>(); + List cols = new ArrayList<>(schema.size()); for (FieldSchema fs : schema) { cols.add(fs.getName()); }