Class: Valkyrie::Persistence::Memory::Persister

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

Overview

Note:

Documentation for persisters in general is maintained here.

Persister for the memory metadata adapter.

Direct Known Subclasses

DeleteTrackingBuffer::Persister

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ Persister

Note:

Many persister methods are part of Valkyrie’s public API, but instantiation itself is not

Returns a new instance of Persister.

Parameters:



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

def initialize(adapter)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/valkyrie/persistence/memory/persister.rb', line 7

def adapter
  @adapter
end

Instance Method Details

#delete(resource:) ⇒ Object

Delete a resource.

Parameters:



70
71
72
# File 'lib/valkyrie/persistence/memory/persister.rb', line 70

def delete(resource:)
  cache.delete(resource.id)
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:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/valkyrie/persistence/memory/persister.rb', line 28

def save(resource:, external_resource: false)
  raise Valkyrie::Persistence::StaleObjectError, "The object #{resource.id} has been updated by another process." unless valid_lock?(resource)
  raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless external_resource || valid_for_save?(resource)

  # duplicate the resource so we are not creating side effects on the caller's resource
  internal_resource = resource.dup

  internal_resource = generate_id(internal_resource) if internal_resource.id.blank?
  internal_resource.created_at ||= Time.current
  internal_resource.updated_at = Time.current
  internal_resource.new_record = false
  generate_lock_token(internal_resource)
  normalize_dates!(internal_resource)
  cache[internal_resource.id] = internal_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:



58
59
60
61
62
63
64
65
# File 'lib/valkyrie/persistence/memory/persister.rb', line 58

def save_all(resources:)
  resources.map do |resource|
    save(resource: resource)
  end
rescue Valkyrie::Persistence::StaleObjectError
  # Re-raising with no error message to prevent confusion
  raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process."
end

#valid_for_save?(resource) ⇒ Boolean

return true if resource is persisted and found or not persisted

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/valkyrie/persistence/memory/persister.rb', line 48

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.



75
76
77
# File 'lib/valkyrie/persistence/memory/persister.rb', line 75

def wipe!
  cache.clear
end