app/models/spotlight/resources/dor_resource.rb in spotlight-dor-resources-0.0.6 vs app/models/spotlight/resources/dor_resource.rb in spotlight-dor-resources-0.1.0

- old
+ new

@@ -1,21 +1,38 @@ module Spotlight::Resources # Base Resource indexer for objects in DOR class DorResource < Spotlight::Resource + include ActiveSupport::Benchmarkable + ## - # Generate solr documents for the DOR resources identified by this object + # Generate solr documents for the DOR resources identified by this object. # # @return [Enumerator] an enumerator of solr document hashes for indexing - def to_solr - return to_enum :to_solr unless block_given? + # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + def to_solr(&block) + return to_enum(:to_solr) { indexable_resources.size } unless block_given? - base_doc = super + # We use the Parallel gem to support parallel processing of the collection, + # but need to jump through some hoops to make it yield an enumerable in the end. + # + # Here, we create a hook that simply yields the result to the enumerable. We configure + # this as a 'finish' hook, which Parallel will run on the main process. + yield_to_enum = ->(_item, _i, result) { block.call(result) } - indexable_resources.each do |res| - yield base_doc.merge(to_solr_document(res)) + size = indexable_resources.size + + benchmark "Indexing resource #{inspect} (est. #{size} items)" do + base_doc = super + + Parallel.each_with_index(indexable_resources, parallel_options.merge(finish: yield_to_enum)) do |res, idx| + benchmark "Indexing item #{res.druid} in resource #{id} (#{idx} / #{size})" do + base_doc.merge(to_solr_document(res)) + end + end end end + # rubocop:enable Metrics/AbcSize, Metrics/MethodLength def resource @resource ||= Spotlight::Dor::Resources.indexer.resource doc_id end @@ -25,11 +42,11 @@ # Enumerate the resource, and any collection members, that should be indexed # into this exhibit # # @return [Enumerator] an enumerator of resources to index def indexable_resources - return to_enum(:indexable_resources) unless block_given? + return to_enum(:indexable_resources) { 1 + resource.items.size } unless block_given? yield resource resource.items.each do |r| yield r @@ -41,8 +58,18 @@ # indexer steps. # # @return [Hash] def to_solr_document(resource) Spotlight::Dor::Resources.indexer.solr_document(resource) + end + + ## + # Write any logs (or benchmarking information) from this class to the gdor logs + def logger + Spotlight::Dor::Resources.indexer.logger + end + + def parallel_options + Spotlight::Dor::Resources::Engine.config.parallel_options end end end