HDFS-12143. Improve performance of getting and removing inode features. Contributed by Daryn Sharp.

This commit is contained in:
Kihwal Lee 2017-07-25 10:28:57 -05:00
parent cca51e916b
commit 1a79dcfc45
1 changed files with 17 additions and 7 deletions

View File

@ -283,12 +283,14 @@ public abstract class INodeWithAdditionalFields extends INode
protected void removeFeature(Feature f) { protected void removeFeature(Feature f) {
int size = features.length; int size = features.length;
Preconditions.checkState(size > 0, "Feature " if (size == 0) {
+ f.getClass().getSimpleName() + " not found."); throwFeatureNotFoundException(f);
}
if (size == 1) { if (size == 1) {
Preconditions.checkState(features[0] == f, "Feature " if (features[0] != f) {
+ f.getClass().getSimpleName() + " not found."); throwFeatureNotFoundException(f);
}
features = EMPTY_FEATURE; features = EMPTY_FEATURE;
return; return;
} }
@ -307,14 +309,22 @@ public abstract class INodeWithAdditionalFields extends INode
} }
} }
Preconditions.checkState(!overflow && j == size - 1, "Feature " if (overflow || j != size - 1) {
+ f.getClass().getSimpleName() + " not found."); throwFeatureNotFoundException(f);
}
features = arr; features = arr;
} }
private void throwFeatureNotFoundException(Feature f) {
throw new IllegalStateException(
"Feature " + f.getClass().getSimpleName() + " not found.");
}
protected <T extends Feature> T getFeature(Class<? extends Feature> clazz) { protected <T extends Feature> T getFeature(Class<? extends Feature> clazz) {
Preconditions.checkArgument(clazz != null); Preconditions.checkArgument(clazz != null);
for (Feature f : features) { final int size = features.length;
for (int i=0; i < size; i++) {
Feature f = features[i];
if (clazz.isAssignableFrom(f.getClass())) { if (clazz.isAssignableFrom(f.getClass())) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T ret = (T) f; T ret = (T) f;