From ef73d6e16f0088e5abdfb217b166acbe21cb95d1 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 24 Apr 2018 16:17:48 -0500 Subject: [PATCH] Use nested hashes instead of string keys This avoids needing to generate a new string value for each key on setting of the value and retrieving, and the extra hashes are not a huge consumer of space. Of note, the keys are now not strings at all. Top level will be the class names, and the child keys will be just the raw IDs. The @ems_xml_nodes value is not used anywhere besides the lookup that was changed in this commit, so the change of this structure should not have an adverse affect... hopefully... --- app/models/miq_request_workflow.rb | 11 ++++++++--- spec/models/miq_request_workflow_spec.rb | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/models/miq_request_workflow.rb b/app/models/miq_request_workflow.rb index 881b90542a0..e496c51b8de 100644 --- a/app/models/miq_request_workflow.rb +++ b/app/models/miq_request_workflow.rb @@ -842,8 +842,12 @@ def find_classes_under_ci(item, klass) def load_ems_node(item, log_header) @ems_xml_nodes ||= {} - klass_name = item.kind_of?(MiqHashStruct) ? item.evm_object_class : item.class.base_class.name - node = @ems_xml_nodes["#{klass_name}_#{item.id}"] + klass_name = if item.kind_of?(MiqHashStruct) + Object.const_get(item.evm_object_class) + else + item.class.base_class + end + node = @ems_xml_nodes[klass_name][item.id] $log.error("#{log_header} Resource <#{klass_name}_#{item.id} - #{item.name}> not found in cached resource tree.") if node.nil? node end @@ -957,7 +961,8 @@ def ems_metadata_tree_add_hosts_under_clusters!(result) def convert_to_xml(xml, result) result.each do |obj, children| - @ems_xml_nodes["#{obj.class.base_class}_#{obj.id}"] = node = xml.add_element(obj.class.base_class.name, :object => ci_to_hash_struct(obj)) + @ems_xml_nodes[obj.class.base_class] ||= {} + @ems_xml_nodes[obj.class.base_class][obj.id] = node = xml.add_element(obj.class.base_class.name, :object => ci_to_hash_struct(obj)) convert_to_xml(node, children) end end diff --git a/spec/models/miq_request_workflow_spec.rb b/spec/models/miq_request_workflow_spec.rb index caf924dedc2..ae3c49c461f 100644 --- a/spec/models/miq_request_workflow_spec.rb +++ b/spec/models/miq_request_workflow_spec.rb @@ -465,7 +465,7 @@ ems_folder.ext_management_system = ems attrs = ems_folder.attributes.merge(:object => ems_folder) xml_hash = XmlHash::Element.new('EmsFolder', attrs) - hash = {"ResourcePool_#{resource_pool.id}" => xml_hash} + hash = { ResourcePool => { resource_pool.id => xml_hash } } workflow.instance_variable_set("@ems_xml_nodes", hash) end @@ -486,7 +486,7 @@ ems_folder.ext_management_system = ems attrs = resource_pool.attributes.merge(:object => resource_pool, :ems => ems) xml_hash = XmlHash::Element.new('ResourcePool', attrs) - hash = {"EmsFolder_#{ems_folder.id}" => xml_hash} + hash = { EmsFolder => { ems_folder.id => xml_hash } } workflow.instance_variable_set("@ems_xml_nodes", hash) end @@ -506,7 +506,7 @@ datacenter.ext_management_system = ems attrs = datacenter.attributes.merge(:object => datacenter, :ems => ems) xml_hash = XmlHash::Element.new('EmsFolder', attrs) - hash = {"EmsFolder_#{datacenter.id}" => xml_hash} + hash = { EmsFolder => { datacenter.id => xml_hash } } workflow.instance_variable_set("@ems_xml_nodes", hash) end