Skip to content

Commit

Permalink
Merge pull request #856 from dchandekstark/redundant-queries
Browse files Browse the repository at this point in the history
Memoizes resource in Solr FindByIdQuery to eliminate redundant request.
  • Loading branch information
tpendragon authored May 6, 2021
2 parents 8e8112f + c2e6519 commit 73c043d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def alternate_identifier
# @note the field used here is alternate_ids_ssim and the value is prefixed by "id-"
# @return [Hash]
def resource
connection.get("select", params: { q: "alternate_ids_ssim:\"id-#{alternate_identifier}\"", fl: "*", rows: 1 })["response"]["docs"].first
@resource ||= connection.get("select", params: { q: "alternate_ids_ssim:\"id-#{alternate_identifier}\"", fl: "*", rows: 1 })["response"]["docs"].first
end
end
end
2 changes: 1 addition & 1 deletion lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def id
# Query Solr for for the first document with the ID in a field
# @return [Hash]
def resource
connection.get("select", params: { q: "id:\"#{id}\"", fl: "*", rows: 1 })["response"]["docs"].first
@resource ||= connection.get("select", params: { q: "id:\"#{id}\"", fl: "*", rows: 1 })["response"]["docs"].first
end
end
end
41 changes: 40 additions & 1 deletion spec/valkyrie/persistence/solr/query_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CustomLockingResource < Valkyrie::Resource
end
end

describe "find_members" do
describe "#find_members" do
before do
class CustomResource < Valkyrie::Resource
attribute :member_ids, Valkyrie::Types::Array
Expand All @@ -57,4 +57,43 @@ class CustomResource < Valkyrie::Resource
expect(members.map(&:id).to_a).to eq [child2.id, child1.id]
end
end

describe "#find_by" do
before do
class CustomResource < Valkyrie::Resource; end
end

after do
Object.send(:remove_const, :CustomResource)
end

let!(:resource) { adapter.persister.save(resource: CustomResource.new) }

it "makes one Solr request" do
allow(adapter.query_service.connection).to receive(:get).and_call_original
adapter.query_service.find_by(id: resource.id)
expect(adapter.query_service.connection).to have_received(:get).once
end
end

describe "#find_by_alternate_identifier" do
before do
class CustomResource < Valkyrie::Resource
attribute :alternate_ids, Valkyrie::Types::Set.of(Valkyrie::Types::ID)
end
end

after do
Object.send(:remove_const, :CustomResource)
end

let(:alt_id) { Valkyrie::ID.new('p9s0xfj') }
let!(:resource) { adapter.persister.save(resource: CustomResource.new(alternate_ids: [alt_id])) }

it "makes one Solr request" do
allow(adapter.query_service.connection).to receive(:get).and_call_original
adapter.query_service.find_by_alternate_identifier(alternate_identifier: resource.alternate_ids.first)
expect(adapter.query_service.connection).to have_received(:get).once
end
end
end

0 comments on commit 73c043d

Please sign in to comment.