Skip to content

Commit

Permalink
Always standardize query results.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpendragon committed Jan 4, 2019
1 parent 0881fd3 commit 6e8a3e9
Show file tree
Hide file tree
Showing 16 changed files with 13 additions and 86 deletions.
8 changes: 1 addition & 7 deletions lib/valkyrie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ def logger=(logger)
@logger = logger
end

def warn_about_standard_queries!
warn "[DEPRECATION] Please enable query normalization to avoid inconsistent results between different adapters by adding `standardize_query_results: true` to your environment block" \
" in config\/valkyrie.yml. This will be the behavior in Valkyrie 2.0."
end

class Config < OpenStruct
def initialize(hsh = {})
super(defaults.merge(hsh))
Expand All @@ -90,10 +85,9 @@ def storage_adapter

def defaults
{
standardize_query_result: false
}
end
end

module_function :config, :logger, :logger=, :config_root_path, :environment, :warn_about_standard_queries!, :config_file, :config_hash
module_function :config, :logger, :logger=, :config_root_path, :environment, :config_file, :config_hash
end
5 changes: 0 additions & 5 deletions lib/valkyrie/persistence/fedora/metadata_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,5 @@ def pair_path(id)
def connection_prefix
"#{connection.http.url_prefix}/#{base_path}"
end

def standardize_query_result?
Valkyrie.warn_about_standard_queries! if Valkyrie.config.standardize_query_result != true
Valkyrie.config.standardize_query_result == true
end
end
end
8 changes: 4 additions & 4 deletions lib/valkyrie/persistence/fedora/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def find_by_alternate_identifier(alternate_identifier:)

# (see Valkyrie::Persistence::Memory::QueryService#find_many_by_ids)
def find_many_by_ids(ids:)
ids = ids.uniq if adapter.standardize_query_result?
ids = ids.uniq
ids.map do |id|
begin
find_by(id: id)
Expand All @@ -43,7 +43,7 @@ def find_many_by_ids(ids:)
def find_parents(resource:)
content = content_with_inbound(id: resource.id)
parent_ids = content.graph.query([nil, RDF::Vocab::ORE.proxyFor, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) }
parent_ids.uniq! if adapter.standardize_query_result?
parent_ids.uniq!
parent_ids.lazy.map do |id|
find_by(id: id)
end
Expand Down Expand Up @@ -131,14 +131,14 @@ def find_inverse_references_by_unordered(resource:, property:)
content = content_with_inbound(id: resource.id)
property_uri = adapter.schema.predicate_for(property: property, resource: nil)
ids = content.graph.query([nil, property_uri, adapter.id_to_uri(resource.id)]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) }
ids.uniq! if adapter.standardize_query_result?
ids.uniq!
ids.lazy.map { |id| find_by(id: id) }
end

def find_inverse_references_by_ordered(resource:, property:)
content = content_with_inbound(id: resource.id)
ids = content.graph.query([nil, ::RDF::Vocab::ORE.proxyFor, adapter.id_to_uri(resource.id)]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) }
ids.uniq! if adapter.standardize_query_result?
ids.uniq!
ids.lazy.map { |id| find_by(id: id) }.select { |o| o[property].include?(resource.id) }
end

Expand Down
5 changes: 0 additions & 5 deletions lib/valkyrie/persistence/memory/metadata_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,5 @@ def cache
def id
@id ||= Valkyrie::ID.new(Digest::MD5.hexdigest(self.class.to_s))
end

def standardize_query_result?
Valkyrie.warn_about_standard_queries! if Valkyrie.config.standardize_query_result != true
Valkyrie.config.standardize_query_result == true
end
end
end
4 changes: 2 additions & 2 deletions lib/valkyrie/persistence/memory/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_by_alternate_identifier(alternate_identifier:)
# @raise [ArgumentError] Raised when any ID is not a String or a Valkyrie::ID
# @return [Array<Valkyrie::Resource>] All requested objects that were found
def find_many_by_ids(ids:)
ids = ids.uniq if adapter.standardize_query_result?
ids = ids.uniq
ids.map do |id|
begin
find_by(id: id)
Expand Down Expand Up @@ -97,7 +97,7 @@ def find_references_by(resource:, property:)
nil
end
end.reject(&:nil?)
refs.uniq! if adapter.standardize_query_result? && !ordered_property?(resource: resource, property: property)
refs.uniq! unless ordered_property?(resource: resource, property: property)
refs
end

Expand Down
5 changes: 0 additions & 5 deletions lib/valkyrie/persistence/postgres/metadata_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,5 @@ def id
Valkyrie::ID.new(Digest::MD5.hexdigest(to_hash))
end
end

def standardize_query_result?
Valkyrie.warn_about_standard_queries! if Valkyrie.config.standardize_query_result != true
Valkyrie.config.standardize_query_result == true
end
end
end
2 changes: 1 addition & 1 deletion lib/valkyrie/persistence/postgres/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def find_inverse_references_query
# @return [String]
def find_references_query
<<-SQL
SELECT #{adapter.standardize_query_result? ? 'DISTINCT' : ''} member.* FROM orm_resources a,
SELECT DISTINCT member.* FROM orm_resources a,
jsonb_array_elements(a.metadata->?) AS b(member)
JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
SQL
Expand Down
5 changes: 0 additions & 5 deletions lib/valkyrie/persistence/solr/metadata_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ def resource_factory
Valkyrie::Persistence::Solr::ResourceFactory.new(resource_indexer: resource_indexer, adapter: self)
end

def standardize_query_result?
Valkyrie.warn_about_standard_queries! if Valkyrie.config.standardize_query_result != true
Valkyrie.config.standardize_query_result == true
end

# Class modeling the indexer for cases where indexing is *not* performed
class NullIndexer
# @note this is a no-op
Expand Down
15 changes: 4 additions & 11 deletions lib/valkyrie/persistence/solr/queries/find_members_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ module Valkyrie::Persistence::Solr::Queries
# Responsible for returning all members of a given resource as
# {Valkyrie::Resource}s
class FindMembersQuery
attr_reader :resource, :connection, :resource_factory, :model, :standardize_query_result
attr_reader :resource, :connection, :resource_factory, :model

# @param [Valkyrie::Resource] resource
# @param [RSolr::Client] connection
# @param [ResourceFactory] resource_factory
# @param [Class] model
def initialize(resource:, connection:, resource_factory:, model:, standardize_query_result:)
def initialize(resource:, connection:, resource_factory:, model:)
@resource = resource
@connection = connection
@resource_factory = resource_factory
@model = model
@standardize_query_result = standardize_query_result
end

# Iterate over each Solr Document and convert each Document into a Valkyrie Resource
Expand All @@ -29,14 +28,8 @@ def run
# @yield [Valkyrie::Resource]
def each
return [] unless resource.id.present?
if standardize_query_result
member_ids.map { |id| unordered_members.find { |member| member.id == id } }.reject(&:nil?).each do |member|
yield member
end
else
unordered_members.sort_by { |x| member_ids.index(x.id) }.each do |member|
yield member
end
member_ids.map { |id| unordered_members.find { |member| member.id == id } }.reject(&:nil?).each do |member|
yield member
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/valkyrie/persistence/solr/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def find_members(resource:, model: nil)
resource: resource,
model: model,
connection: connection,
resource_factory: resource_factory,
standardize_query_result: adapter.standardize_query_result?
resource_factory: resource_factory
).run
end

Expand Down
2 changes: 0 additions & 2 deletions lib/valkyrie/specs/shared_specs/queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class SecondResource < Valkyrie::Resource
let(:persister) { adapter.persister }
subject { adapter.query_service }

before { allow(Valkyrie.config).to receive(:standardize_query_result).and_return(true) }

it { is_expected.to respond_to(:find_all).with(0).arguments }
it { is_expected.to respond_to(:find_all_of_model).with_keywords(:model) }
it { is_expected.to respond_to(:find_by).with_keywords(:id) }
Expand Down
9 changes: 0 additions & 9 deletions spec/valkyrie/persistence/fedora/metadata_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@
expect(adapter.id.to_s).to eq expected
end
end

# rubocop:disable Metrics/LineLength
describe "#standardize_query_result?" do
it "throws a deprecation warning when it's set to false" do
allow(Valkyrie.config).to receive(:standardize_query_result).and_return(false)
expect { adapter.standardize_query_result? }.to output(/Please enable query normalization to avoid inconsistent results between different adapters by adding `standardize_query_results: true` to your environment block in config\/valkyrie.yml. This will be the behavior in Valkyrie 2.0./).to_stderr
end
end
# rubocop:enable Metrics/LineLength
end
end
end
9 changes: 0 additions & 9 deletions spec/valkyrie/persistence/memory/metadata_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,4 @@
expect(adapter.id.to_s).to eq expected
end
end

# rubocop:disable Metrics/LineLength
describe "#standardize_query_result?" do
it "throws a deprecation warning when it's set to false" do
allow(Valkyrie.config).to receive(:standardize_query_result).and_return(false)
expect { adapter.standardize_query_result? }.to output(/Please enable query normalization to avoid inconsistent results between different adapters by adding `standardize_query_results: true` to your environment block in config\/valkyrie.yml. This will be the behavior in Valkyrie 2.0./).to_stderr
end
end
# rubocop:enable Metrics/LineLength
end
9 changes: 0 additions & 9 deletions spec/valkyrie/persistence/postgres/metadata_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,4 @@
expect(adapter.id.to_s).to eq expected
end
end

# rubocop:disable Metrics/LineLength
describe "#standardize_query_result?" do
it "throws a deprecation warning when it's set to false" do
allow(Valkyrie.config).to receive(:standardize_query_result).and_return(false)
expect { adapter.standardize_query_result? }.to output(/Please enable query normalization to avoid inconsistent results between different adapters by adding `standardize_query_results: true` to your environment block in config\/valkyrie.yml. This will be the behavior in Valkyrie 2.0./).to_stderr
end
end
# rubocop:enable Metrics/LineLength
end
9 changes: 0 additions & 9 deletions spec/valkyrie/persistence/solr/metadata_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,4 @@
expect(adapter.id.to_s).to eq expected
end
end

# rubocop:disable Metrics/LineLength
describe "#standardize_query_result?" do
it "throws a deprecation warning when it's set to false" do
allow(Valkyrie.config).to receive(:standardize_query_result).and_return(false)
expect { adapter.standardize_query_result? }.to output(/Please enable query normalization to avoid inconsistent results between different adapters by adding `standardize_query_results: true` to your environment block in config\/valkyrie.yml. This will be the behavior in Valkyrie 2.0./).to_stderr
end
end
# rubocop:enable Metrics/LineLength
end
1 change: 0 additions & 1 deletion spec/valkyrie/persistence/solr/query_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CustomLockingResource < Valkyrie::Resource
class CustomResource < Valkyrie::Resource
attribute :member_ids, Valkyrie::Types::Array
end
allow(Valkyrie.config).to receive(:standardize_query_result).and_return(false)
end

after do
Expand Down

0 comments on commit 6e8a3e9

Please sign in to comment.