Skip to content
Snippets Groups Projects
Commit 44eba709 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

speedup dspace:import_items

parent 0691e281
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ class LearningObject < ActiveRecord::Base
has_many :collection_items, as: :collectionable
has_many :collections, through: :collection_items
has_many :complaints
has_many :attachments, class_name: 'LearningObject::Attachment', dependent: :destroy
has_many :attachments, class_name: 'LearningObject::Attachment', dependent: :destroy, autosave: true # autosave to allow import
belongs_to :publisher, polymorphic: true
belongs_to :language
......
......@@ -5,14 +5,24 @@ class Dspace::LearningObjectImporter
end
def import
items = []
@items.each do |item|
next if item_exists? item
learning_object = LearningObjectBuilder.build_from_dspace item
create_attachments learning_object, item.bit_streams if learning_object.save
learning_object = LearningObjectBuilder.build_from_dspace(item)
next if learning_object.blank?
create_attachments(learning_object, item.bit_streams)
items << learning_object
yield(learning_object) if block_given?
end
LearningObject.import items, recursive: true
end
private
......@@ -20,7 +30,7 @@ class Dspace::LearningObjectImporter
def create_attachments(learning_object, bitstreams)
bitstreams = JSON.parse bitstreams if bitstreams.class == String
bitstreams.each do |bitstream|
learning_object.attachments.create ::Dspace::AttachmentMapper.call(bitstream)
learning_object.attachments.new ::Dspace::AttachmentMapper.call(bitstream)
end
end
......
......@@ -11,7 +11,7 @@ namespace :dspace do
logger.level = Log::DatabaseLogger::CREATE
# Quantity of items fetched on each iteration
limit = 1000
limit = 500
# Start point from where items will be fetched
offset = 0
......@@ -48,7 +48,7 @@ namespace :dspace do
# Initializes new LearningObjects
# and saves them on LearningObjects's repository
importer = Dspace::LearningObjectImporter.new(dspace_items)
importer.import { |learning_object| logger.create "Imported: #{learning_object.id_dspace} -> #{learning_object.id}" }
importer.import
end
end
logger.close
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment