From d06ca9631644b67ecc11336ed421ee4730e69288 Mon Sep 17 00:00:00 2001 From: Anna Headley Date: Thu, 22 Oct 2020 13:12:23 -0400 Subject: [PATCH 01/15] Add a shared persister spec for saving a deleted resource refs #837 Co-authored-by: Ayse Durmaz --- lib/valkyrie/specs/shared_specs/persister.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/valkyrie/specs/shared_specs/persister.rb b/lib/valkyrie/specs/shared_specs/persister.rb index f82af60d7..be4cd62b1 100644 --- a/lib/valkyrie/specs/shared_specs/persister.rb +++ b/lib/valkyrie/specs/shared_specs/persister.rb @@ -50,6 +50,18 @@ class CustomResource < Valkyrie::Resource expect(reloaded.nested_resource.first.title).to eq ["Nested"] end + context "when a persisted resource is not in the database" do + it "throws an ObjectNotFoundError" do + expect(resource).not_to be_persisted + saved = persister.save(resource: resource) + + expect(saved).to be_persisted + persister.delete(resource: saved) + + expect { persister.save(resource: saved) }.to raise_error(Valkyrie::Persistence::ObjectNotFoundError) + end + end + it "can persist single values" do resource.single_value = "A single value" From e85a12a02a588e571d84b69227af043b8f40a7e4 Mon Sep 17 00:00:00 2001 From: Anna Headley Date: Thu, 22 Oct 2020 13:15:06 -0400 Subject: [PATCH 02/15] Raise ObjectNotFound on save for memory adapter refs #837 Co-authored-by: Ayse Durmaz --- lib/valkyrie/persistence/memory/persister.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/valkyrie/persistence/memory/persister.rb b/lib/valkyrie/persistence/memory/persister.rb index 594e28a9f..d8088acec 100644 --- a/lib/valkyrie/persistence/memory/persister.rb +++ b/lib/valkyrie/persistence/memory/persister.rb @@ -5,7 +5,7 @@ module Valkyrie::Persistence::Memory # @note Documentation for persisters in general is maintained here. class Persister attr_reader :adapter - delegate :cache, to: :adapter + delegate :cache, :query_service, to: :adapter # @param adapter [Valkyrie::Persistence::Memory::MetadataAdapter] The memory adapter which # holds the cache for this persister. @@ -21,6 +21,7 @@ def initialize(adapter) # @raise [Valkyrie::Persistence::StaleObjectError] def save(resource:) 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 valid_for_save?(resource) # duplicate the resource so we are not creating side effects on the caller's resource internal_resource = resource.dup @@ -34,6 +35,15 @@ def save(resource:) cache[internal_resource.id] = internal_resource end + # return true if resource is + # persisted and found + # or + # not persisted + 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 + # Save a batch of resources. # @param resources [Array] List of resources to save. # @return [Array] List of resources with an `#id` value From a40b215f9078e39a695db04750e2b96bdc48a3b5 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Thu, 29 Oct 2020 14:28:03 -0700 Subject: [PATCH 03/15] Fix Fedora persister. --- lib/valkyrie/persistence/fedora/persister.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/valkyrie/persistence/fedora/persister.rb b/lib/valkyrie/persistence/fedora/persister.rb index 026f59729..9aa887544 100644 --- a/lib/valkyrie/persistence/fedora/persister.rb +++ b/lib/valkyrie/persistence/fedora/persister.rb @@ -35,6 +35,8 @@ def save(resource:) alternate_resources ? save_reference_to_resource(persisted_resource, alternate_resources) : persisted_resource rescue Ldp::PreconditionFailed raise Valkyrie::Persistence::StaleObjectError, "The object #{internal_resource.id} has been updated by another process." + rescue Ldp::Gone + raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." end # (see Valkyrie::Persistence::Memory::Persister#save_all) From 122e8e3287750d82116f95c65101c5d9667bbcf6 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Thu, 29 Oct 2020 14:28:10 -0700 Subject: [PATCH 04/15] Fix Postgres Persister --- lib/valkyrie/persistence/postgres/persister.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/valkyrie/persistence/postgres/persister.rb b/lib/valkyrie/persistence/postgres/persister.rb index eb218794c..2ef59b45a 100644 --- a/lib/valkyrie/persistence/postgres/persister.rb +++ b/lib/valkyrie/persistence/postgres/persister.rb @@ -20,6 +20,7 @@ def initialize(adapter:) def save(resource:) orm_object = resource_factory.from_resource(resource: resource) orm_object.transaction do + raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." if resource.persisted? && !orm_object.persisted? orm_object.save! if resource.id && resource.id.to_s != orm_object.id raise Valkyrie::Persistence::UnsupportedDatatype, From 05800c3bc91428b3b22aa3a3472797750cc2f96f Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Thu, 29 Oct 2020 14:28:18 -0700 Subject: [PATCH 05/15] Fix Composite Persister. --- lib/valkyrie/persistence/composite_persister.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/valkyrie/persistence/composite_persister.rb b/lib/valkyrie/persistence/composite_persister.rb index 8b5b71f67..8d50b0e03 100644 --- a/lib/valkyrie/persistence/composite_persister.rb +++ b/lib/valkyrie/persistence/composite_persister.rb @@ -31,6 +31,8 @@ def save(resource:) # Don't pass opt lock tokens to other persisters internal_resource = cached_resource.dup internal_resource.clear_optimistic_lock_token! + # Reset new_record to avoid not-saved checks. + internal_resource.new_record = resource.new_record rest.inject(internal_resource) { |m, persister| persister.save(resource: m) } # return the one with the desired opt lock token cached_resource From fd7e0d0f8fa24638d0b0d577da7fabcb872f85e1 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 10:34:13 -0700 Subject: [PATCH 06/15] Fix Solr. --- lib/valkyrie/persistence/solr/persister.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/valkyrie/persistence/solr/persister.rb b/lib/valkyrie/persistence/solr/persister.rb index b76e2edb9..674f006e3 100644 --- a/lib/valkyrie/persistence/solr/persister.rb +++ b/lib/valkyrie/persistence/solr/persister.rb @@ -6,7 +6,7 @@ module Valkyrie::Persistence::Solr # Most methods are delegated to {Valkyrie::Persistence::Solr::Repository} class Persister attr_reader :adapter - delegate :connection, :resource_factory, :write_only?, to: :adapter + delegate :connection, :query_service, :resource_factory, :write_only?, to: :adapter # @param adapter [Valkyrie::Persistence::Solr::MetadataAdapter] The adapter with the # configured solr connection. @@ -17,6 +17,7 @@ def initialize(adapter:) # (see Valkyrie::Persistence::Memory::Persister#save) # @return [Boolean] If write_only, whether saving succeeded. def save(resource:) + raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless write_only? || valid_for_save?(resource) if write_only? repository([resource]).persist else @@ -24,6 +25,11 @@ def save(resource:) end end + 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 + # (see Valkyrie::Persistence::Memory::Persister#save_all) # @return [Boolean] If write_only, whether saving succeeded. def save_all(resources:) From f5331bfe0de0a7aa8f9bfe629c0424b137208998 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 12:30:15 -0700 Subject: [PATCH 07/15] Fix BufferedPersister. --- lib/valkyrie/persistence/delete_tracking_buffer.rb | 6 ++++++ spec/valkyrie/persistence/buffered_persister_spec.rb | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/valkyrie/persistence/delete_tracking_buffer.rb b/lib/valkyrie/persistence/delete_tracking_buffer.rb index 67322aabf..131dab2f2 100644 --- a/lib/valkyrie/persistence/delete_tracking_buffer.rb +++ b/lib/valkyrie/persistence/delete_tracking_buffer.rb @@ -24,6 +24,12 @@ def delete(resource:) @deletes << resource super end + + # A buffered memory persister can save anything, it doesn't have to hold + # a consistent internal state. + def valid_for_save?(_) + true + end end end end diff --git a/spec/valkyrie/persistence/buffered_persister_spec.rb b/spec/valkyrie/persistence/buffered_persister_spec.rb index fd4de90ac..46dde9d17 100644 --- a/spec/valkyrie/persistence/buffered_persister_spec.rb +++ b/spec/valkyrie/persistence/buffered_persister_spec.rb @@ -30,5 +30,15 @@ class Resource < Valkyrie::Resource end expect(buffer.query_service.find_all.length).to eq 1 end + it "can handle saving an object which was previously saved" do + buffer = nil + resource = Resource.new + output = adapter.persister.save(resource: resource) + persister.with_buffer do |inner_persister, memory_buffer| + inner_persister.save(resource: output) + buffer = memory_buffer + end + expect(buffer.query_service.find_all.length).to eq 1 + end end end From fc16567d13f05048e910857fbeeebcf1a7af5d0a Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 12:42:35 -0700 Subject: [PATCH 08/15] Add failing test for CompositePersister. --- spec/valkyrie/persistence/composite_persister_spec.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/valkyrie/persistence/composite_persister_spec.rb b/spec/valkyrie/persistence/composite_persister_spec.rb index aa1a948b1..d26514721 100644 --- a/spec/valkyrie/persistence/composite_persister_spec.rb +++ b/spec/valkyrie/persistence/composite_persister_spec.rb @@ -17,10 +17,13 @@ let(:client) { RSolr.connect(url: SOLR_TEST_URL) } let(:persister) do described_class.new( - Valkyrie::Persistence::Postgres::MetadataAdapter.new.persister, + postgres_adapter.persister, adapter.persister ) end + let(:postgres_adapter) do + Valkyrie::Persistence::Postgres::MetadataAdapter.new + end let(:adapter) { Valkyrie::Persistence::Solr::MetadataAdapter.new(connection: client) } before do @@ -35,5 +38,9 @@ class CustomResource < Valkyrie::Resource book = persister.save(resource: CustomResource.new) expect { query_service.find_by(id: book.id) }.not_to raise_error(Valkyrie::Persistence::ObjectNotFoundError) end + it "can save in postgres and then index freshly into solr" do + book = postgres_adapter.persister.save(resource: CustomResource.new) + expect { adapter.persister.save(resource: book) }.not_to raise_error(Valkyrie::Persistence::ObjectNotFoundError) + end end end From 8c9e3bad6f29cc2c3463be4767d2c37d5e4b9e45 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 13:46:44 -0700 Subject: [PATCH 09/15] Add a parameter to save. --- lib/valkyrie/persistence/fedora/persister.rb | 2 +- lib/valkyrie/persistence/memory/persister.rb | 7 +++++-- lib/valkyrie/persistence/postgres/persister.rb | 4 ++-- lib/valkyrie/persistence/solr/persister.rb | 4 ++-- lib/valkyrie/specs/shared_specs/persister.rb | 6 ++++++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/valkyrie/persistence/fedora/persister.rb b/lib/valkyrie/persistence/fedora/persister.rb index 9aa887544..fed97d134 100644 --- a/lib/valkyrie/persistence/fedora/persister.rb +++ b/lib/valkyrie/persistence/fedora/persister.rb @@ -13,7 +13,7 @@ def initialize(adapter:) end # (see Valkyrie::Persistence::Memory::Persister#save) - def save(resource:) + def save(resource:, external_resource: false) initialize_repository internal_resource = resource.dup internal_resource.created_at ||= Time.current diff --git a/lib/valkyrie/persistence/memory/persister.rb b/lib/valkyrie/persistence/memory/persister.rb index d8088acec..0270ac286 100644 --- a/lib/valkyrie/persistence/memory/persister.rb +++ b/lib/valkyrie/persistence/memory/persister.rb @@ -16,12 +16,15 @@ def initialize(adapter) # Save a single resource. # @param resource [Valkyrie::Resource] The resource to save. + # @param external_resource [Boolean] 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. # @return [Valkyrie::Resource] The resource with an `#id` value generated by the # persistence backend. # @raise [Valkyrie::Persistence::StaleObjectError] - def save(resource:) + 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 valid_for_save?(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 diff --git a/lib/valkyrie/persistence/postgres/persister.rb b/lib/valkyrie/persistence/postgres/persister.rb index 2ef59b45a..d105b2e49 100644 --- a/lib/valkyrie/persistence/postgres/persister.rb +++ b/lib/valkyrie/persistence/postgres/persister.rb @@ -17,10 +17,10 @@ def initialize(adapter:) # @return [Valkyrie::Resource] the persisted/updated resource # @raise [Valkyrie::Persistence::StaleObjectError] raised if the resource # was modified in the database between been read into memory and persisted - def save(resource:) + def save(resource:, external_resource: false) orm_object = resource_factory.from_resource(resource: resource) orm_object.transaction do - raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." if resource.persisted? && !orm_object.persisted? + raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." if !external_resource && resource.persisted? && !orm_object.persisted? orm_object.save! if resource.id && resource.id.to_s != orm_object.id raise Valkyrie::Persistence::UnsupportedDatatype, diff --git a/lib/valkyrie/persistence/solr/persister.rb b/lib/valkyrie/persistence/solr/persister.rb index 674f006e3..fbdc638e1 100644 --- a/lib/valkyrie/persistence/solr/persister.rb +++ b/lib/valkyrie/persistence/solr/persister.rb @@ -16,11 +16,11 @@ def initialize(adapter:) # (see Valkyrie::Persistence::Memory::Persister#save) # @return [Boolean] If write_only, whether saving succeeded. - def save(resource:) - raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless write_only? || valid_for_save?(resource) + 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 diff --git a/lib/valkyrie/specs/shared_specs/persister.rb b/lib/valkyrie/specs/shared_specs/persister.rb index be4cd62b1..fbb2dbb8f 100644 --- a/lib/valkyrie/specs/shared_specs/persister.rb +++ b/lib/valkyrie/specs/shared_specs/persister.rb @@ -60,6 +60,12 @@ class CustomResource < Valkyrie::Resource expect { persister.save(resource: saved) }.to raise_error(Valkyrie::Persistence::ObjectNotFoundError) end + it "is okay if it's from another persister" do + memory_adapter = Valkyrie::Persistence::Memory::MetadataAdapter.new + saved = memory_adapter.persister.save(resource: resource) + + expect { persister.save(resource: saved, external_resource: true) }.not_to raise_error + end end it "can persist single values" do From 0afbbca203c7025d17b2a0a763d7e6fbdff7ef56 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 13:49:07 -0700 Subject: [PATCH 10/15] CompositePersister uses new #external_resource --- lib/valkyrie/persistence/composite_persister.rb | 2 +- lib/valkyrie/persistence/delete_tracking_buffer.rb | 6 ------ spec/valkyrie/persistence/composite_persister_spec.rb | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/valkyrie/persistence/composite_persister.rb b/lib/valkyrie/persistence/composite_persister.rb index 8d50b0e03..1338b00ba 100644 --- a/lib/valkyrie/persistence/composite_persister.rb +++ b/lib/valkyrie/persistence/composite_persister.rb @@ -33,7 +33,7 @@ def save(resource:) internal_resource.clear_optimistic_lock_token! # Reset new_record to avoid not-saved checks. internal_resource.new_record = resource.new_record - rest.inject(internal_resource) { |m, persister| persister.save(resource: m) } + 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 diff --git a/lib/valkyrie/persistence/delete_tracking_buffer.rb b/lib/valkyrie/persistence/delete_tracking_buffer.rb index 131dab2f2..67322aabf 100644 --- a/lib/valkyrie/persistence/delete_tracking_buffer.rb +++ b/lib/valkyrie/persistence/delete_tracking_buffer.rb @@ -24,12 +24,6 @@ def delete(resource:) @deletes << resource super end - - # A buffered memory persister can save anything, it doesn't have to hold - # a consistent internal state. - def valid_for_save?(_) - true - end end end end diff --git a/spec/valkyrie/persistence/composite_persister_spec.rb b/spec/valkyrie/persistence/composite_persister_spec.rb index d26514721..fba6c973e 100644 --- a/spec/valkyrie/persistence/composite_persister_spec.rb +++ b/spec/valkyrie/persistence/composite_persister_spec.rb @@ -40,7 +40,7 @@ class CustomResource < Valkyrie::Resource end it "can save in postgres and then index freshly into solr" do book = postgres_adapter.persister.save(resource: CustomResource.new) - expect { adapter.persister.save(resource: book) }.not_to raise_error(Valkyrie::Persistence::ObjectNotFoundError) + expect { persister.save(resource: book) }.not_to raise_error(Valkyrie::Persistence::ObjectNotFoundError) end end end From 928f42343ef6ecbb653571906a413ff349a32a76 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 13:58:23 -0700 Subject: [PATCH 11/15] Rubocop. --- .rubocop.yml | 7 +++++++ lib/valkyrie/persistence/postgres/persister.rb | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 961ef651a..73ec09652 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -39,3 +39,10 @@ Naming/FileName: Exclude: - 'Appraisals' - 'Gemfile' +Metrics/MethodLength: + Exclude: + - 'lib/valkyrie/persistence/postgres/persister.rb' + - 'lib/valkyrie/persistence/fedora/persister.rb' +Metrics/CyclomaticComplexity: + Exclude: + - 'lib/valkyrie/persistence/postgres/persister.rb' diff --git a/lib/valkyrie/persistence/postgres/persister.rb b/lib/valkyrie/persistence/postgres/persister.rb index d105b2e49..f64f2e51a 100644 --- a/lib/valkyrie/persistence/postgres/persister.rb +++ b/lib/valkyrie/persistence/postgres/persister.rb @@ -20,7 +20,9 @@ def initialize(adapter:) def save(resource:, external_resource: false) orm_object = resource_factory.from_resource(resource: resource) orm_object.transaction do - raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." if !external_resource && resource.persisted? && !orm_object.persisted? + if !external_resource && resource.persisted? && !orm_object.persisted? + raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." + end orm_object.save! if resource.id && resource.id.to_s != orm_object.id raise Valkyrie::Persistence::UnsupportedDatatype, From e5021522ea59cbc24dae59a90830f7327517cb33 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 14:04:41 -0700 Subject: [PATCH 12/15] Fix. --- lib/valkyrie/persistence/buffered_persister.rb | 4 ++-- lib/valkyrie/persistence/composite_persister.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/valkyrie/persistence/buffered_persister.rb b/lib/valkyrie/persistence/buffered_persister.rb index 5541d6e72..deae6e2cb 100644 --- a/lib/valkyrie/persistence/buffered_persister.rb +++ b/lib/valkyrie/persistence/buffered_persister.rb @@ -24,8 +24,8 @@ def initialize(persister, buffer_class: Valkyrie::Persistence::DeleteTrackingBuf @buffer_class = buffer_class end - def save(resource:) - persister.save(resource: resource) + def save(resource:, external_resource: false) + persister.save(resource: resource, external_resource: external_resource) end def save_all(resources:) diff --git a/lib/valkyrie/persistence/composite_persister.rb b/lib/valkyrie/persistence/composite_persister.rb index 1338b00ba..cb6ca8363 100644 --- a/lib/valkyrie/persistence/composite_persister.rb +++ b/lib/valkyrie/persistence/composite_persister.rb @@ -24,10 +24,10 @@ def initialize(*persisters) end # (see Valkyrie::Persistence::Memory::Persister#save) - def save(resource:) + 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) + 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! From d81d2ae2562a86a5767ed945ecfa59ffe3ebe918 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Wed, 28 Jul 2021 14:14:30 -0700 Subject: [PATCH 13/15] Don't reset new record state. --- lib/valkyrie/persistence/composite_persister.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/valkyrie/persistence/composite_persister.rb b/lib/valkyrie/persistence/composite_persister.rb index cb6ca8363..e137765bc 100644 --- a/lib/valkyrie/persistence/composite_persister.rb +++ b/lib/valkyrie/persistence/composite_persister.rb @@ -31,8 +31,6 @@ def save(resource:, external_resource: false) # Don't pass opt lock tokens to other persisters internal_resource = cached_resource.dup internal_resource.clear_optimistic_lock_token! - # Reset new_record to avoid not-saved checks. - internal_resource.new_record = resource.new_record rest.inject(internal_resource) { |m, persister| persister.save(resource: m, external_resource: true) } # return the one with the desired opt lock token cached_resource From 43042a0256248185e9c4db88238686760ad1b358 Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Tue, 14 Sep 2021 14:54:40 -0700 Subject: [PATCH 14/15] Add memo-ization of persister. --- lib/valkyrie/persistence/solr/metadata_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valkyrie/persistence/solr/metadata_adapter.rb b/lib/valkyrie/persistence/solr/metadata_adapter.rb index c17631b38..5c44f783d 100644 --- a/lib/valkyrie/persistence/solr/metadata_adapter.rb +++ b/lib/valkyrie/persistence/solr/metadata_adapter.rb @@ -39,7 +39,7 @@ def initialize(connection:, resource_indexer: NullIndexer, write_only: false) # @return [Valkyrie::Persistence::Solr::Persister] The solr persister. def persister - Valkyrie::Persistence::Solr::Persister.new(adapter: self) + @persister ||= Valkyrie::Persistence::Solr::Persister.new(adapter: self) end def write_only? From 265ccd5ce163a9205d66f5527534a402151873ec Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Fri, 15 Oct 2021 09:17:25 -0700 Subject: [PATCH 15/15] Add more to the external_resource comment. --- lib/valkyrie/persistence/memory/persister.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/valkyrie/persistence/memory/persister.rb b/lib/valkyrie/persistence/memory/persister.rb index 0270ac286..f8754176b 100644 --- a/lib/valkyrie/persistence/memory/persister.rb +++ b/lib/valkyrie/persistence/memory/persister.rb @@ -18,7 +18,10 @@ def initialize(adapter) # @param resource [Valkyrie::Resource] The resource to save. # @param external_resource [Boolean] 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. + # 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. # @return [Valkyrie::Resource] The resource with an `#id` value generated by the # persistence backend. # @raise [Valkyrie::Persistence::StaleObjectError]