This is an automated email from the ASF dual-hosted git repository. dahn pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/main by this push: new aad03530af9 Search for resource type efficiently (#6242) aad03530af9 is described below commit aad03530af930e033f33b5ba3ff07441adbaa863 Author: Rakesh <rakeshv....@gmail.com> AuthorDate: Thu Jan 12 13:29:38 2023 +0100 Search for resource type efficiently (#6242) Co-authored-by: Rakesh Venkatesh <rake...@apache.org> --- api/src/main/java/com/cloud/server/ResourceTag.java | 15 +++++++++++++++ .../main/java/com/cloud/tags/ResourceManagerUtilImpl.java | 10 +++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/cloud/server/ResourceTag.java b/api/src/main/java/com/cloud/server/ResourceTag.java index d502b6f9d81..6288446cbdd 100644 --- a/api/src/main/java/com/cloud/server/ResourceTag.java +++ b/api/src/main/java/com/cloud/server/ResourceTag.java @@ -20,6 +20,10 @@ import org.apache.cloudstack.acl.ControlledEntity; import org.apache.cloudstack.api.Identity; import org.apache.cloudstack.api.InternalIdentity; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + public interface ResourceTag extends ControlledEntity, Identity, InternalIdentity { // FIXME - extract enum to another interface as its used both by resourceTags and resourceMetaData code @@ -80,6 +84,7 @@ public interface ResourceTag extends ControlledEntity, Identity, InternalIdentit private final boolean resourceTagsSupport; private final boolean metadataSupport; private boolean resourceIconSupport; + private static final Map<String, ResourceObjectType> resourceObjectTypeMap = new HashMap<>(); public boolean resourceTagsSupport() { return resourceTagsSupport; @@ -92,6 +97,16 @@ public interface ResourceTag extends ControlledEntity, Identity, InternalIdentit public boolean resourceIconSupport() { return resourceIconSupport; } + + static { + for (var value : ResourceObjectType.values()) { + resourceObjectTypeMap.put(value.toString().toLowerCase(Locale.ROOT), value); + } + } + + public static ResourceObjectType getResourceObjectType(String type) { + return resourceObjectTypeMap.getOrDefault(type.toLowerCase(Locale.ROOT), null); + } } /** diff --git a/server/src/main/java/com/cloud/tags/ResourceManagerUtilImpl.java b/server/src/main/java/com/cloud/tags/ResourceManagerUtilImpl.java index 00d83aad659..928e54227a9 100644 --- a/server/src/main/java/com/cloud/tags/ResourceManagerUtilImpl.java +++ b/server/src/main/java/com/cloud/tags/ResourceManagerUtilImpl.java @@ -72,6 +72,7 @@ import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicVO; import com.cloud.vm.UserVmVO; import com.cloud.vm.snapshot.VMSnapshotVO; +import java.util.Optional; public class ResourceManagerUtilImpl implements ResourceManagerUtil { public static final Map<ResourceTag.ResourceObjectType, Class<?>> s_typeMap = new HashMap<>(); @@ -159,13 +160,8 @@ public class ResourceManagerUtilImpl implements ResourceManagerUtil { @Override public ResourceTag.ResourceObjectType getResourceType(String resourceTypeStr) { - - for (ResourceTag.ResourceObjectType type : ResourceTag.ResourceObjectType.values()) { - if (type.toString().equalsIgnoreCase(resourceTypeStr)) { - return type; - } - } - throw new InvalidParameterValueException("Invalid resource type: " + resourceTypeStr); + return Optional.ofNullable(ResourceTag.ResourceObjectType.getResourceObjectType(resourceTypeStr)) + .orElseThrow(() -> new InvalidParameterValueException("Invalid resource type " + resourceTypeStr)); } public void checkResourceAccessible(Long accountId, Long domainId, String exceptionMessage) {