Class: Valkyrie::Persistence::Solr::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/solr/repository.rb

Overview

Responsible for handling the logic for persisting or deleting multiple objects into or out of solr.

Constant Summary collapse

COMMIT_PARAMS =
{ softCommit: true, versions: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources:, connection:, resource_factory:) ⇒ Repository

Returns a new instance of Repository.

Parameters:


13
14
15
16
17
# File 'lib/valkyrie/persistence/solr/repository.rb', line 13

def initialize(resources:, connection:, resource_factory:)
  @resources = resources
  @connection = connection
  @resource_factory = resource_factory
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


8
9
10
# File 'lib/valkyrie/persistence/solr/repository.rb', line 8

def connection
  @connection
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory


8
9
10
# File 'lib/valkyrie/persistence/solr/repository.rb', line 8

def resource_factory
  @resource_factory
end

#resourcesObject (readonly)

Returns the value of attribute resources


8
9
10
# File 'lib/valkyrie/persistence/solr/repository.rb', line 8

def resources
  @resources
end

Instance Method Details

#add_documents(documents) ⇒ RSolr::HashWithResponse

rubocop:disable Style/IfUnlessModifier

Parameters:

  • array (Array<Hash>)

    of Solr documents

Returns:

  • (RSolr::HashWithResponse)

37
38
39
40
41
42
43
44
45
# File 'lib/valkyrie/persistence/solr/repository.rb', line 37

def add_documents(documents)
  connection.add documents, params: COMMIT_PARAMS
rescue RSolr::Error::Http => exception
  # Error 409 conflict is returned when versions do not match
  if exception.response&.fetch(:status) == 409
    handle_conflict
  end
  raise exception
end

#deleteArray<Valkyrie::Resource>

Deletes a Solr Document using the ID

Returns:


50
51
52
53
# File 'lib/valkyrie/persistence/solr/repository.rb', line 50

def delete
  connection.delete_by_id resources.map { |resource| resource.id.to_s }, params: COMMIT_PARAMS
  resources
end

#generate_id(resource) ⇒ Object

Given a new Valkyrie Resource, generate a random UUID and assign it to the Resource

Parameters:

See Also:


66
67
68
69
# File 'lib/valkyrie/persistence/solr/repository.rb', line 66

def generate_id(resource)
  Valkyrie.logger.warn("The Solr adapter is not meant to persist new resources, but is now generating an ID.", logging_context: "Valkyrie::Persistence::Solr::Repository#generate_id")
  resource.id = SecureRandom.uuid
end

#handle_conflictObject

If a 409 conflict response is encountered when attempting to commit updates to Solr, raise a StaleObjectError


74
75
76
77
# File 'lib/valkyrie/persistence/solr/repository.rb', line 74

def handle_conflict
  raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process." if resources.count > 1
  raise Valkyrie::Persistence::StaleObjectError, "The object #{resources.first.id} has been updated by another process."
end

#persistArray<Valkyrie::Resource>

Persist the resources into Solr

Returns:


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/valkyrie/persistence/solr/repository.rb', line 21

def persist
  documents = resources.map do |resource|
    generate_id(resource) if resource.id.blank?
    solr_document(resource)
  end
  results = add_documents(documents)
  versions = results["adds"]&.each_slice(2)&.to_h
  documents.map do |document|
    document["_version_"] = versions.fetch(document[:id])
    resource_factory.to_resource(object: document.stringify_keys)
  end
end

#solr_document(resource) ⇒ Hash

Given a Valkyrie Resource, generate the Hash for the Solr Document

Parameters:

Returns:

  • (Hash)

58
59
60
# File 'lib/valkyrie/persistence/solr/repository.rb', line 58

def solr_document(resource)
  resource_factory.from_resource(resource: resource).to_h
end