You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by we...@apache.org on 2019/11/20 22:40:38 UTC

[hadoop] branch trunk updated: HADOOP-15852. Refactor QuotaUsage. Contributed by David Mollitor.

This is an automated email from the ASF dual-hosted git repository.

weichiu pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


View the commit online:
https://github.com/apache/hadoop/commit/6f899e90300c478d2176a0b05b773f015c9ce7c7

The following commit(s) were added to refs/heads/trunk by this push:
     new 6f899e9  HADOOP-15852. Refactor QuotaUsage. Contributed by David Mollitor.
6f899e9 is described below

commit 6f899e90300c478d2176a0b05b773f015c9ce7c7
Author: David Mollitor <da...@cloudera.com>
AuthorDate: Wed Nov 20 14:39:30 2019 -0800

    HADOOP-15852. Refactor QuotaUsage. Contributed by David Mollitor.
    
    Signed-off-by: Wei-Chiu Chuang <we...@apache.org>
---
 .../main/java/org/apache/hadoop/fs/QuotaUsage.java | 126 ++++++++++-----------
 1 file changed, 62 insertions(+), 64 deletions(-)

diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/QuotaUsage.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/QuotaUsage.java
index 3472362..11cc934 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/QuotaUsage.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/QuotaUsage.java
@@ -40,14 +40,13 @@ public class QuotaUsage {
   /** Builder class for QuotaUsage. */
   public static class Builder {
     public Builder() {
-      this.quota = -1;
-      this.spaceQuota = -1;
+      this.quota = -1L;
+      this.spaceQuota = -1L;
 
       typeConsumed = new long[StorageType.values().length];
       typeQuota = new long[StorageType.values().length];
-      for (int i = 0; i < typeQuota.length; i++) {
-        typeQuota[i] = -1;
-      }
+
+      Arrays.fill(typeQuota, -1L);
     }
 
     public Builder fileAndDirectoryCount(long count) {
@@ -71,9 +70,8 @@ public class QuotaUsage {
     }
 
     public Builder typeConsumed(long[] typeConsumed) {
-      for (int i = 0; i < typeConsumed.length; i++) {
-        this.typeConsumed[i] = typeConsumed[i];
-      }
+      System.arraycopy(typeConsumed, 0, this.typeConsumed, 0,
+          typeConsumed.length);
       return this;
     }
 
@@ -88,9 +86,7 @@ public class QuotaUsage {
     }
 
     public Builder typeQuota(long[] typeQuota) {
-      for (int i = 0; i < typeQuota.length; i++) {
-        this.typeQuota[i] = typeQuota[i];
-      }
+      System.arraycopy(typeQuota, 0, this.typeQuota, 0, typeQuota.length);
       return this;
     }
 
@@ -153,32 +149,21 @@ public class QuotaUsage {
 
   /** Return storage type quota. */
   public long getTypeQuota(StorageType type) {
-    return (typeQuota != null) ? typeQuota[type.ordinal()] : -1;
+    return (typeQuota != null) ? typeQuota[type.ordinal()] : -1L;
   }
 
   /** Return storage type consumed. */
   public long getTypeConsumed(StorageType type) {
-    return (typeConsumed != null) ? typeConsumed[type.ordinal()] : 0;
-  }
-
-  /** Return storage type quota. */
-  private long[] getTypesQuota() {
-    return typeQuota;
-  }
-
-  /** Return storage type quota. */
-  private long[] getTypesConsumed() {
-    return typeConsumed;
+    return (typeConsumed != null) ? typeConsumed[type.ordinal()] : 0L;
   }
 
   /** Return true if any storage type quota has been set. */
   public boolean isTypeQuotaSet() {
-    if (typeQuota == null) {
-      return false;
-    }
-    for (StorageType t : StorageType.getTypesSupportingQuota()) {
-      if (typeQuota[t.ordinal()] > 0) {
-        return true;
+    if (typeQuota != null) {
+      for (StorageType t : StorageType.getTypesSupportingQuota()) {
+        if (typeQuota[t.ordinal()] > 0L) {
+          return true;
+        }
       }
     }
     return false;
@@ -186,45 +171,58 @@ public class QuotaUsage {
 
   /** Return true if any storage type consumption information is available. */
   public boolean isTypeConsumedAvailable() {
-    if (typeConsumed == null) {
-      return false;
-    }
-    for (StorageType t : StorageType.getTypesSupportingQuota()) {
-      if (typeConsumed[t.ordinal()] > 0) {
-        return true;
+    if (typeConsumed != null) {
+      for (StorageType t : StorageType.getTypesSupportingQuota()) {
+        if (typeConsumed[t.ordinal()] > 0L) {
+          return true;
+        }
       }
     }
     return false;
   }
 
   @Override
-  public boolean equals(Object to) {
-    return (this == to || (to instanceof QuotaUsage &&
-        getFileAndDirectoryCount() ==
-        ((QuotaUsage) to).getFileAndDirectoryCount() &&
-        getQuota() == ((QuotaUsage) to).getQuota() &&
-        getSpaceConsumed() == ((QuotaUsage) to).getSpaceConsumed() &&
-        getSpaceQuota() == ((QuotaUsage) to).getSpaceQuota() &&
-        Arrays.equals(getTypesQuota(), ((QuotaUsage) to).getTypesQuota()) &&
-        Arrays.equals(getTypesConsumed(),
-        ((QuotaUsage) to).getTypesConsumed())));
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result
+        + (int) (fileAndDirectoryCount ^ (fileAndDirectoryCount >>> 32));
+    result = prime * result + (int) (quota ^ (quota >>> 32));
+    result = prime * result + (int) (spaceConsumed ^ (spaceConsumed >>> 32));
+    result = prime * result + (int) (spaceQuota ^ (spaceQuota >>> 32));
+    result = prime * result + Arrays.hashCode(typeConsumed);
+    result = prime * result + Arrays.hashCode(typeQuota);
+    return result;
   }
 
   @Override
-  public int hashCode() {
-    long result = (getFileAndDirectoryCount() ^ getQuota() ^
-        getSpaceConsumed() ^ getSpaceQuota());
-    if (getTypesQuota() != null) {
-      for (long quota : getTypesQuota()) {
-        result ^= quota;
-      }
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
     }
-    if (getTypesConsumed() != null) {
-      for (long consumed : getTypesConsumed()) {
-        result ^= consumed;
-      }
+    if (!(obj instanceof QuotaUsage)) {
+      return false;
+    }
+    QuotaUsage other = (QuotaUsage) obj;
+    if (fileAndDirectoryCount != other.fileAndDirectoryCount) {
+      return false;
+    }
+    if (quota != other.quota) {
+      return false;
+    }
+    if (spaceConsumed != other.spaceConsumed) {
+      return false;
+    }
+    if (spaceQuota != other.spaceQuota) {
+      return false;
+    }
+    if (!Arrays.equals(typeConsumed, other.typeConsumed)) {
+      return false;
+    }
+    if (!Arrays.equals(typeQuota, other.typeQuota)) {
+      return false;
     }
-    return (int)result;
+    return true;
   }
 
   /**
@@ -292,11 +290,11 @@ public class QuotaUsage {
     String spaceQuotaStr = QUOTA_NONE;
     String spaceQuotaRem = QUOTA_INF;
 
-    if (quota > 0) {
+    if (quota > 0L) {
       quotaStr = formatSize(quota, hOption);
       quotaRem = formatSize(quota-fileAndDirectoryCount, hOption);
     }
-    if (spaceQuota >= 0) {
+    if (spaceQuota >= 0L) {
       spaceQuotaStr = formatSize(spaceQuota, hOption);
       spaceQuotaRem = formatSize(spaceQuota - spaceConsumed, hOption);
     }
@@ -307,20 +305,20 @@ public class QuotaUsage {
 
   protected String getTypesQuotaUsage(boolean hOption,
       List<StorageType> types) {
-    StringBuffer content = new StringBuffer();
+    StringBuilder content = new StringBuilder();
     for (StorageType st : types) {
       long typeQuota = getTypeQuota(st);
       long typeConsumed = getTypeConsumed(st);
       String quotaStr = QUOTA_NONE;
       String quotaRem = QUOTA_INF;
 
-      if (typeQuota >= 0) {
+      if (typeQuota >= 0L) {
         quotaStr = formatSize(typeQuota, hOption);
         quotaRem = formatSize(typeQuota - typeConsumed, hOption);
       }
 
-      content.append(String.format(STORAGE_TYPE_SUMMARY_FORMAT,
-          quotaStr, quotaRem));
+      content.append(
+          String.format(STORAGE_TYPE_SUMMARY_FORMAT, quotaStr, quotaRem));
     }
     return content.toString();
   }
@@ -332,7 +330,7 @@ public class QuotaUsage {
    * @return storage header string
    */
   public static String getStorageTypeHeader(List<StorageType> storageTypes) {
-    StringBuffer header = new StringBuffer();
+    StringBuilder header = new StringBuilder();
 
     for (StorageType st : storageTypes) {
       /* the field length is 13/17 for quota and remain quota


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org