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

SOFT_COMMIT_PARAMS =
{ softCommit: true, versions: true }.freeze
NO_COMMIT_PARAMS =
{ versions: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources:, persister:) ⇒ Repository

Returns a new instance of Repository.

Parameters:



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

def initialize(resources:, persister:)
  @resources = resources
  @persister = persister
end

Instance Attribute Details

#persisterObject (readonly)

Returns the value of attribute persister.



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

def persister
  @persister
end

#resourcesObject (readonly)

Returns the value of attribute resources.



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

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)


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

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

#commit_paramsObject



81
82
83
84
85
86
87
# File 'lib/valkyrie/persistence/solr/repository.rb', line 81

def commit_params
  if persister.soft_commit?
    SOFT_COMMIT_PARAMS
  else
    NO_COMMIT_PARAMS
  end
end

#deleteArray<Valkyrie::Resource>

Deletes a Solr Document using the ID

Returns:



52
53
54
55
# File 'lib/valkyrie/persistence/solr/repository.rb', line 52

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:



68
69
70
71
# File 'lib/valkyrie/persistence/solr/repository.rb', line 68

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



76
77
78
79
# File 'lib/valkyrie/persistence/solr/repository.rb', line 76

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:



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

def persist
  documents = resources.map do |resource|
    generate_id(resource) if resource.id.blank?
    solr_document(resource)
  end
  results = add_documents(documents)
  return true if write_only?
  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)


60
61
62
# File 'lib/valkyrie/persistence/solr/repository.rb', line 60

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