From ef51574e64e3872612c813c8211a42c066f9d89e Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 4 Aug 2021 17:55:53 -0700 Subject: [PATCH] Improve ModelConverter performance. Reducing Array.wrap and not processing Nil values speeds up the model converter significantly. --- .../persistence/solr/model_converter.rb | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/lib/valkyrie/persistence/solr/model_converter.rb b/lib/valkyrie/persistence/solr/model_converter.rb index 5639bce8..fd149cb1 100644 --- a/lib/valkyrie/persistence/solr/model_converter.rb +++ b/lib/valkyrie/persistence/solr/model_converter.rb @@ -114,6 +114,7 @@ def attribute_hash properties.each_with_object({}) do |property, hsh| next if property == Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK attr = resource_attributes[property] + next if attr.nil? mapper_val = SolrMapperValue.for(Property.new(property, attr)).result unless mapper_val.respond_to?(:apply_to) raise "Unable to cast #{resource_attributes[:internal_resource]}#" \ @@ -162,8 +163,8 @@ class SolrRow # @param values [Array] Values to index into the given fields. def initialize(key:, fields:, values:) @key = key - @fields = Array.wrap(fields) - @values = Array.wrap(values) + @fields = fields + @values = values end # @param hsh [Hash] The solr hash to apply to. @@ -241,7 +242,7 @@ def self.handles?(value) # @see https://github.com/samvera-labs/valkyrie/blob/main/solr/config/schema.xml # @return [SolrRow] def result - SolrRow.new(key: value.key, fields: ["tsim"], values: "serialized-#{value.value.to_json}") + SolrRow.new(key: value.key, fields: ["tsim"], values: ["serialized-#{value.value.to_json}"]) end end @@ -267,24 +268,6 @@ def result end end - # Skips nil values. - class NilPropertyValue < ::Valkyrie::ValueMapper - SolrMapperValue.register(self) - - # Determines whether or not a Property value responds as nil - # @param [Object] value - # @return [Boolean] - def self.handles?(value) - value.is_a?(Property) && value.value.nil? - end - - # Constructs a SolrRow object for a Property with a nil value - # @return [SolrRow] - def result - SolrRow.new(key: value.key, fields: [], values: nil) - end - end - # Casts {Valkyrie::ID} values into a recognizable string in solr. class IDPropertyValue < ::Valkyrie::ValueMapper SolrMapperValue.register(self) @@ -423,7 +406,7 @@ def self.handles?(value) # Constructs a SolrRow object with the String values and Solr field settings # @return [SolrRow] def result - SolrRow.new(key: value.key, fields: fields, values: value.value) + SolrRow.new(key: value.key, fields: fields, values: [value.value]) end # Generates the Solr fields used during the indexing