HADOOP-15852. Refactor QuotaUsage. Contributed by David Mollitor.

Signed-off-by: Wei-Chiu Chuang <weichiu@apache.org>
This commit is contained in:
David Mollitor 2019-11-20 14:39:30 -08:00 committed by Wei-Chiu Chuang
parent 9664b9c7a6
commit 6f899e9030

View File

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