Class: Valkyrie::Persistence::CompositePersister

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

Overview

Wraps up multiple persisters under a common interface, to transparently persist to multiple places at once.

Examples:

persister = Valkyrie.config.metadata_adapter
index_persister = Valkyrie::MetadataAdapter.find(:index_solr)
Valkyrie::MetadataAdapter.register(
  Valkyrie::AdapterContainer.new(
    persister: Valkyrie::Persistence::CompositePersister.new(
      persister,
      index_persister
      )
    query_service: Valkyrie.config.metadata_adapter.query_service
  )
  :my_composite_persister
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*persisters) ⇒ CompositePersister

Returns a new instance of CompositePersister.



22
23
24
# File 'lib/valkyrie/persistence/composite_persister.rb', line 22

def initialize(*persisters)
  @persisters = persisters
end

Instance Attribute Details

#persistersObject (readonly)

Returns the value of attribute persisters.



21
22
23
# File 'lib/valkyrie/persistence/composite_persister.rb', line 21

def persisters
  @persisters
end

Instance Method Details

#delete(resource:) ⇒ Object

Delete a resource.

Parameters:



50
51
52
# File 'lib/valkyrie/persistence/composite_persister.rb', line 50

def delete(resource:)
  persisters.inject(resource) { |m, persister| persister.delete(resource: m) }
end

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

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.

Raises:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/valkyrie/persistence/composite_persister.rb', line 27

def save(resource:, external_resource: false)
  # Assume the first persister is the canonical data store; that's the optlock we want
  first, *rest = *persisters
  cached_resource = first.save(resource: resource, external_resource: external_resource)
  # Don't pass opt lock tokens to other persisters
  internal_resource = cached_resource.dup
  internal_resource.clear_optimistic_lock_token!
  rest.inject(internal_resource) { |m, persister| persister.save(resource: m, external_resource: true) }
  # return the one with the desired opt lock token
  cached_resource
end

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

Save a batch of resources.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

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

Raises:



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

def save_all(resources:)
  resources.map do |resource|
    save(resource: resource)
  end
rescue Valkyrie::Persistence::StaleObjectError
  # clear out any IDs returned to reduce potential confusion
  raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process."
end

#wipe!Object



54
55
56
# File 'lib/valkyrie/persistence/composite_persister.rb', line 54

def wipe!
  persisters.each_entry(&:wipe!)
end