Class: Valkyrie::Persistence::Solr::Persister

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

Overview

Persister for Solr MetadataAdapter.

Most methods are delegated to Repository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ Persister

Returns a new instance of Persister.

Parameters:



13
14
15
# File 'lib/valkyrie/persistence/solr/persister.rb', line 13

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

Instance Method Details

#delete(resource:) ⇒ Object

Delete a resource.

Parameters:



40
41
42
# File 'lib/valkyrie/persistence/solr/persister.rb', line 40

def delete(resource:)
  repository([resource]).delete.first
end

#repository(resources) ⇒ Valkyrie::Persistence::Solr::Repository

Constructs a Solr::Repository object for a set of Valkyrie Resources

Parameters:

Returns:



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

def repository(resources)
  Valkyrie::Persistence::Solr::Repository.new(resources: resources, persister: self)
end

#save(resource:, external_resource: false) ⇒ Valkyrie::Resource, Boolean

Save a single resource.

Parameters:

  • resource (Valkyrie::Resource)

    The resource to save.

  • external_resource (Boolean) (defaults to: false)

    Whether the resource to be saved comes from a different metadata store. Allows a resource to be saved even if it’s not already in the store. For example, if you’re indexing a resource into Solr - it’s saved in your primary metadata store, but not in Solr, so it’s okay if it doesn’t exist in Solr but is marked as persisted.

Returns:

  • (Valkyrie::Resource)

    The resource with an ‘#id` value generated by the persistence backend.

  • (Boolean)

    If write_only, whether saving succeeded.

Raises:



19
20
21
22
23
24
25
26
# File 'lib/valkyrie/persistence/solr/persister.rb', line 19

def save(resource:, external_resource: false)
  if write_only?
    repository([resource]).persist
  else
    raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless external_resource || valid_for_save?(resource)
    repository([resource]).persist.first
  end
end

#save_all(resources:) ⇒ Array<Valkyrie::Resource>, Boolean

Save a batch of resources.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    List of resources with an ‘#id` value generated by the persistence backend.

  • (Boolean)

    If write_only, whether saving succeeded.

Raises:



35
36
37
# File 'lib/valkyrie/persistence/solr/persister.rb', line 35

def save_all(resources:)
  repository(resources).persist
end

#valid_for_save?(resource) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/valkyrie/persistence/solr/persister.rb', line 28

def valid_for_save?(resource)
  return true unless resource.persisted? # a new resource
  query_service.find_by(id: resource.id).present? # a persisted resource must be found
end

#wipe!Object

Removes all data from the persistence backend.



45
46
47
48
# File 'lib/valkyrie/persistence/solr/persister.rb', line 45

def wipe!
  connection.delete_by_query("*:*")
  connection.commit
end