valkyrie 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +0 -24
  3. data/Appraisals +0 -4
  4. data/CHANGELOG.md +65 -0
  5. data/README.md +2 -2
  6. data/db/schema.rb +0 -40
  7. data/gemfiles/activerecord_5_2.gemfile +2 -0
  8. data/gemfiles/activerecord_6_0.gemfile +2 -0
  9. data/lib/valkyrie.rb +20 -2
  10. data/lib/valkyrie/id.rb +16 -1
  11. data/lib/valkyrie/logging.rb +72 -0
  12. data/lib/valkyrie/persistence/composite_persister.rb +1 -1
  13. data/lib/valkyrie/persistence/fedora.rb +2 -0
  14. data/lib/valkyrie/persistence/fedora/list_node.rb +3 -6
  15. data/lib/valkyrie/persistence/fedora/metadata_adapter.rb +2 -2
  16. data/lib/valkyrie/persistence/fedora/permissive_schema.rb +2 -2
  17. data/lib/valkyrie/persistence/fedora/persister.rb +2 -1
  18. data/lib/valkyrie/persistence/fedora/query_service.rb +20 -17
  19. data/lib/valkyrie/persistence/memory/query_service.rb +32 -10
  20. data/lib/valkyrie/persistence/postgres/query_service.rb +66 -13
  21. data/lib/valkyrie/persistence/solr/persister.rb +4 -17
  22. data/lib/valkyrie/persistence/solr/queries/find_all_query.rb +6 -0
  23. data/lib/valkyrie/persistence/solr/query_service.rb +33 -44
  24. data/lib/valkyrie/persistence/solr/repository.rb +2 -1
  25. data/lib/valkyrie/rdf_patches.rb +2 -2
  26. data/lib/valkyrie/resource.rb +10 -2
  27. data/lib/valkyrie/specs/shared_specs/persister.rb +3 -3
  28. data/lib/valkyrie/specs/shared_specs/queries.rb +112 -9
  29. data/lib/valkyrie/storage/fedora.rb +1 -1
  30. data/lib/valkyrie/storage_adapter.rb +5 -2
  31. data/lib/valkyrie/types.rb +2 -0
  32. data/lib/valkyrie/version.rb +1 -1
  33. data/solr/config/solrconfig.xml +0 -10
  34. data/valkyrie.gemspec +1 -0
  35. metadata +17 -4
  36. data/db/seeds.rb +0 -8
  37. data/gemfiles/activerecord_5_1.gemfile +0 -10
@@ -6,8 +6,8 @@ module Valkyrie::Persistence::Fedora
6
6
  #
7
7
  # @example Passing in a mapping
8
8
  # schema = Valkyrie::Persistence::Fedora::PermissiveSchema.new(member_ids:
9
- # RDF::URI("http://mypredicates.com/member_ids"))
10
- # schema.predicate_for(resource: Resource.new, property: :member_ids) # => RDF::URI<"http://mypredicates.com/member_ids">
9
+ # RDF::URI("http://example.com/member_ids"))
10
+ # schema.predicate_for(resource: Resource.new, property: :member_ids) # => RDF::URI<"http://example.com/member_ids">
11
11
  # schema.predicate_for(resource: Resource.new, property: :unknown) # => RDF::URI<"http://example.com/predicate/unknown">
12
12
  class PermissiveSchema
13
13
  URI_PREFIX = 'http://example.com/predicate/'
@@ -64,11 +64,12 @@ module Valkyrie::Persistence::Fedora
64
64
  # (see Valkyrie::Persistence::Memory::Persister#wipe!)
65
65
  # Deletes Fedora repository resource *and* the tombstone resources which remain
66
66
  # @see https://wiki.duraspace.org/display/FEDORA4x/RESTful+HTTP+API#RESTfulHTTPAPI-RedDELETEDeletearesource
67
+ # @see Valkyrie::Logging for details concerning log suppression.
67
68
  def wipe!
68
69
  connection.delete(base_path)
69
70
  connection.delete("#{base_path}/fcr:tombstone")
70
71
  rescue => error
71
- Valkyrie.logger.debug("Failed to wipe Fedora for some reason: #{error}") unless error.is_a?(::Ldp::NotFound)
72
+ Valkyrie.logger.debug("Failed to wipe Fedora for some reason: #{error}", logging_context: "Valkyrie::Persistence::Fedora::Persister#wipe") unless error.is_a?(::Ldp::NotFound)
72
73
  end
73
74
 
74
75
  # Creates the root LDP Container for the connection with Fedora
@@ -86,11 +86,18 @@ module Valkyrie::Persistence::Fedora
86
86
  end
87
87
  end
88
88
 
89
+ # (see Valkyrie::Persistence::Memory::QueryService#count_all_of_model)
90
+ def count_all_of_model(model:)
91
+ find_all_of_model(model: model).count
92
+ end
93
+
89
94
  # (see Valkyrie::Persistence::Memory::QueryService#find_references_by)
90
- def find_references_by(resource:, property:)
91
- (resource[property] || []).select { |x| x.is_a?(Valkyrie::ID) }.lazy.map do |id|
95
+ def find_references_by(resource:, property:, model: nil)
96
+ objects = (resource[property] || []).select { |x| x.is_a?(Valkyrie::ID) }.lazy.map do |id|
92
97
  find_by(id: id)
93
98
  end
99
+ return objects unless model
100
+ objects.select { |obj| obj.is_a?(model) }
94
101
  end
95
102
 
96
103
  # Retrieves the RDF graph for the LDP container for a resource
@@ -111,15 +118,16 @@ module Valkyrie::Persistence::Fedora
111
118
  # Find all resources referencing a given resource (e. g. parents)
112
119
  # *This is done by iterating through the ID of each resource referencing the resource in the query, and requesting each resource over the HTTP*
113
120
  # *Also, an initial request is made to find the URIs of the resources referencing the resource in the query*
114
- def find_inverse_references_by(resource: nil, id: nil, property:)
121
+ def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
115
122
  raise ArgumentError, "Provide resource or id" unless resource || id
116
123
  ensure_persisted(resource) if resource
117
124
  resource ||= find_by(id: id)
118
- if ordered_property?(resource: resource, property: property)
119
- find_inverse_references_by_ordered(resource: resource, property: property)
120
- else
121
- find_inverse_references_by_unordered(resource: resource, property: property)
122
- end
125
+ ids = find_inverse_reference_ids_by_unordered(resource: resource, property: property).uniq
126
+ objects_from_unordered = ids.lazy.map { |ref_id| find_by(id: ref_id) }
127
+ objects_from_ordered = find_inverse_references_by_ordered(resource: resource, property: property, ignore_ids: ids)
128
+ objects = [objects_from_unordered, objects_from_ordered].lazy.flat_map(&:lazy)
129
+ return objects unless model
130
+ objects.select { |obj| obj.is_a?(model) }
123
131
  end
124
132
 
125
133
  # (see Valkyrie::Persistence::Memory::QueryService#custom_queries)
@@ -129,18 +137,17 @@ module Valkyrie::Persistence::Fedora
129
137
 
130
138
  private
131
139
 
132
- def find_inverse_references_by_unordered(resource:, property:)
140
+ def find_inverse_reference_ids_by_unordered(resource:, property:)
133
141
  content = content_with_inbound(id: resource.id)
134
142
  property_uri = adapter.schema.predicate_for(property: property, resource: nil)
135
- 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) }
136
- ids.uniq!
137
- ids.lazy.map { |id| find_by(id: id) }
143
+ 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) }
138
144
  end
139
145
 
140
- def find_inverse_references_by_ordered(resource:, property:)
146
+ def find_inverse_references_by_ordered(resource:, property:, ignore_ids: [])
141
147
  content = content_with_inbound(id: resource.id)
142
148
  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) }
143
149
  ids.uniq!
150
+ ids.delete_if { |id| ignore_ids.include? id }
144
151
  ids.lazy.map { |id| find_by(id: id) }.select { |o| o[property].include?(resource.id) }
145
152
  end
146
153
 
@@ -169,9 +176,5 @@ module Valkyrie::Persistence::Fedora
169
176
  def ensure_persisted(resource)
170
177
  raise ArgumentError, 'resource is not saved' unless resource.persisted?
171
178
  end
172
-
173
- def ordered_property?(resource:, property:)
174
- resource.ordered_attribute?(property)
175
- end
176
179
  end
177
180
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
  module Valkyrie::Persistence::Memory
3
+ # Query Service for the memory metadata adapter.
4
+ # @see Valkyrie::Persistence::Memory
5
+ # @note Documentation for Query Services in general is maintained here.
3
6
  class QueryService
4
- # Query Service for the memory metadata adapter.
5
- # @see Valkyrie::Persistence::Memory
6
- # @note Documentation for Query Services in general is maintained here.
7
7
  attr_reader :adapter, :query_handlers
8
8
  delegate :cache, to: :adapter
9
9
 
@@ -27,7 +27,9 @@ module Valkyrie::Persistence::Memory
27
27
  cache[id] || raise(::Valkyrie::Persistence::ObjectNotFoundError)
28
28
  end
29
29
 
30
- # Get a single resource by `alternate_identifier`.
30
+ # Get a single resource by `alternate_identifier`. Alternate identifiers are identifiers (like NOIDs,
31
+ # DOIs, ARKs, etc.) that are not the system-generated ID, but might be used to identify a resource in an
32
+ # application (e.g., to make shorter URLs).
31
33
  # @param alternate_identifier [Valkyrie::ID] The alternate identifier to query for.
32
34
  # @raise [Valkyrie::Persistence::ObjectNotFoundError] Raised when the alternate identifier
33
35
  # isn't in the persistence backend.
@@ -73,9 +75,17 @@ module Valkyrie::Persistence::Memory
73
75
  end
74
76
  end
75
77
 
78
+ # Count all objects of a given model.
79
+ # @param model [Class] Class to query for.
80
+ # @return integer. Count objects in the persistence backend
81
+ # with the given class.
82
+ def count_all_of_model(model:)
83
+ cache.values.select { |obj| obj.is_a?(model) }.count
84
+ end
85
+
76
86
  # Get all members of a given resource.
77
87
  # @param resource [Valkyrie::Resource] Model whose members are being searched for.
78
- # @param model [Class] Class to query for. (optional)
88
+ # @param model [Class] Filter results to include only instances of this model. (optional)
79
89
  # @return [Array<Valkyrie::Resource>] child objects of type `model` referenced by
80
90
  # `resource`'s `member_ids` method. Returned in order.
81
91
  def find_members(resource:, model: nil)
@@ -90,9 +100,10 @@ module Valkyrie::Persistence::Memory
90
100
  # @param resource [Valkyrie::Resource] Model whose property is being searched.
91
101
  # @param property [Symbol] Property which, on the `resource`, contains {Valkyrie::ID}s which are
92
102
  # to be de-referenced.
103
+ # @param model [Class] Filter results to include only instances of this model. (optional)
93
104
  # @return [Array<Valkyrie::Resource>] All objects which are referenced by the
94
105
  # `property` property on `resource`. Not necessarily in order.
95
- def find_references_by(resource:, property:)
106
+ def find_references_by(resource:, property:, model: nil)
96
107
  refs = Array.wrap(resource[property]).map do |id|
97
108
  begin
98
109
  find_by(id: id)
@@ -101,25 +112,31 @@ module Valkyrie::Persistence::Memory
101
112
  end
102
113
  end.reject(&:nil?)
103
114
  refs.uniq! unless ordered_property?(resource: resource, property: property)
104
- refs
115
+ return refs unless model
116
+ refs.select { |obj| obj.is_a?(model) }
105
117
  end
106
118
 
107
119
  # Get all resources which link to a resource with a given property.
108
120
  # @param resource [Valkyrie::Resource] The resource which is being referenced by
109
- # other resources.
121
+ # other resources. Requires either resource or id parameter to be specified.
122
+ # @param id [Valkyrie::ID] ID of the resource which is being reference by other
123
+ # resources. Requires either resource or id parameter to be specified.
110
124
  # @param property [Symbol] The property which, on other resources, is
111
125
  # referencing the given `resource`
126
+ # @param model [Class] Filter results to include only instances of this model. (optional)
112
127
  # @raise [ArgumentError] Raised when the ID is not in the persistence backend.
113
128
  # @return [Array<Valkyrie::Resource>] All resources in the persistence backend
114
129
  # which have the ID of the given `resource` in their `property` property. Not
115
130
  # in order.
116
- def find_inverse_references_by(resource: nil, id: nil, property:)
131
+ def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
117
132
  raise ArgumentError, "Provide resource or id" unless resource || id
118
133
  ensure_persisted(resource) if resource
119
134
  id ||= resource.id
120
- find_all.select do |obj|
135
+ result = find_all.select do |obj|
121
136
  Array.wrap(obj[property]).include?(id)
122
137
  end
138
+ return result unless model
139
+ result.select { |obj| obj.is_a?(model) }
123
140
  end
124
141
 
125
142
  # Find all parents of a given resource.
@@ -148,10 +165,15 @@ module Valkyrie::Persistence::Memory
148
165
  resource.member_ids || []
149
166
  end
150
167
 
168
+ # Determine whether or not a value is a Valkyrie ID
169
+ # @param [Object] id
170
+ # @return [Boolean]
151
171
  def validate_id(id)
152
172
  raise ArgumentError, 'id must be a Valkyrie::ID' unless id.is_a? Valkyrie::ID
153
173
  end
154
174
 
175
+ # Ensure that a given Valkyrie Resource has been persisted
176
+ # @param [Valkyrie::Resource] resource
155
177
  def ensure_persisted(resource)
156
178
  raise ArgumentError, 'resource is not saved' unless resource.persisted?
157
179
  end
@@ -35,6 +35,13 @@ module Valkyrie::Persistence::Postgres
35
35
  end
36
36
  end
37
37
 
38
+ # Count all records for a specific resource type
39
+ # @param [Class] model
40
+ # @return integer
41
+ def count_all_of_model(model:)
42
+ orm_class.where(internal_resource: model.to_s).count
43
+ end
44
+
38
45
  # Find a record using a Valkyrie ID, and map it to a Valkyrie Resource
39
46
  # @param [Valkyrie::ID, String] id
40
47
  # @return [Valkyrie::Resource]
@@ -94,30 +101,28 @@ module Valkyrie::Persistence::Postgres
94
101
  find_inverse_references_by(resource: resource, property: :member_ids)
95
102
  end
96
103
 
97
- # Find all resources related to a given Valkyrie Resource by a property
98
- # @param [Valkyrie::Resource] resource
99
- # @param [String] property
100
- # @return [Array<Valkyrie::Resource>]
101
- def find_references_by(resource:, property:)
104
+ # (see Valkyrie::Persistence::Memory::QueryService#find_references_by)
105
+ def find_references_by(resource:, property:, model: nil)
102
106
  return [] if resource.id.blank? || resource[property].blank?
103
107
  # only return ordered if needed to avoid performance penalties
104
108
  if ordered_property?(resource: resource, property: property)
105
- run_query(find_ordered_references_query, property, resource.id.to_s)
109
+ find_ordered_references_by(resource: resource, property: property, model: model)
106
110
  else
107
- run_query(find_references_query, property, resource.id.to_s)
111
+ find_unordered_references_by(resource: resource, property: property, model: model)
108
112
  end
109
113
  end
110
114
 
111
- # Find all resources referencing a given Valkyrie Resource by a property
112
- # @param [Valkyrie::Resource] resource
113
- # @param [String] property
114
- # @return [Array<Valkyrie::Resource>]
115
- def find_inverse_references_by(resource: nil, id: nil, property:)
115
+ # (see Valkyrie::Persistence::Memory::QueryService#find_inverse_references_by)
116
+ def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
116
117
  raise ArgumentError, "Provide resource or id" unless resource || id
117
118
  ensure_persisted(resource) if resource
118
119
  id ||= resource.id
119
120
  internal_array = "{\"#{property}\": [{\"id\": \"#{id}\"}]}"
120
- run_query(find_inverse_references_query, internal_array)
121
+ if model
122
+ run_query(find_inverse_references_with_type_query, internal_array, model)
123
+ else
124
+ run_query(find_inverse_references_query, internal_array)
125
+ end
121
126
  end
122
127
 
123
128
  # Execute a query in SQL for resource records and map them to Valkyrie
@@ -180,6 +185,21 @@ module Valkyrie::Persistence::Postgres
180
185
  SQL
181
186
  end
182
187
 
188
+ # Generate the SQL query for retrieving member resources in PostgreSQL using a
189
+ # JSON object literal (e. g. { "alternate_ids": [{"id": "d6e88f80-41b3-4dbf-a2a0-cd79e20f6d10"}] }).
190
+ # and resource type as arguments
191
+ # @see https://guides.rubyonrails.org/active_record_querying.html#array-conditions
192
+ # This uses JSON functions in order to retrieve JSON property values
193
+ # @see https://www.postgresql.org/docs/current/static/functions-json.html
194
+ # @return [String]
195
+ def find_inverse_references_with_type_query
196
+ <<-SQL
197
+ SELECT * FROM orm_resources WHERE
198
+ metadata @> ?
199
+ AND internal_resource = ?
200
+ SQL
201
+ end
202
+
183
203
  # Generate the SQL query for retrieving member resources in PostgreSQL using a
184
204
  # JSON object literal and resource ID as arguments.
185
205
  # @see https://guides.rubyonrails.org/active_record_querying.html#array-conditions
@@ -197,6 +217,14 @@ module Valkyrie::Persistence::Postgres
197
217
  SQL
198
218
  end
199
219
 
220
+ def find_references_with_type_query
221
+ <<-SQL
222
+ SELECT DISTINCT member.* FROM orm_resources a,
223
+ jsonb_array_elements(a.metadata->?) AS b(member)
224
+ JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ? AND member.internal_resource = ?
225
+ SQL
226
+ end
227
+
200
228
  def find_ordered_references_query
201
229
  <<-SQL
202
230
  SELECT member.* FROM orm_resources a,
@@ -206,6 +234,15 @@ module Valkyrie::Persistence::Postgres
206
234
  SQL
207
235
  end
208
236
 
237
+ def find_ordered_references_with_type_query
238
+ <<-SQL
239
+ SELECT member.* FROM orm_resources a,
240
+ jsonb_array_elements(a.metadata->?) WITH ORDINALITY AS b(member, member_pos)
241
+ JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ? AND member.internal_resource = ?
242
+ ORDER BY b.member_pos
243
+ SQL
244
+ end
245
+
209
246
  # Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service
210
247
  # @return [Valkyrie::Persistence::CustomQueryContainer]
211
248
  def custom_queries
@@ -214,6 +251,22 @@ module Valkyrie::Persistence::Postgres
214
251
 
215
252
  private
216
253
 
254
+ def find_ordered_references_by(resource:, property:, model: nil)
255
+ if model
256
+ run_query(find_ordered_references_with_type_query, property, resource.id.to_s, model)
257
+ else
258
+ run_query(find_ordered_references_query, property, resource.id.to_s)
259
+ end
260
+ end
261
+
262
+ def find_unordered_references_by(resource:, property:, model: nil)
263
+ if model
264
+ run_query(find_references_with_type_query, property, resource.id.to_s, model)
265
+ else
266
+ run_query(find_references_query, property, resource.id.to_s)
267
+ end
268
+ end
269
+
217
270
  # Determines whether or not an Object is a Valkyrie ID
218
271
  # @param [Object] id
219
272
  # @raise [ArgumentError]
@@ -14,35 +14,22 @@ module Valkyrie::Persistence::Solr
14
14
  @adapter = adapter
15
15
  end
16
16
 
17
- # Persists a Valkyrie Resource into a Solr index
18
- # @note Fields are saved using Solr's dynamic fields functionality.
19
- # If the text has length > 1000, it is stored as *_tsim
20
- # otherwise it's stored as *_tsim, *_ssim, and *_tesim
21
- # e.g., a field called 'title' would be stored as 3 solr fields:
22
- # 'title_tsim'
23
- # 'title_ssim'
24
- # 'title_tesim'
25
- # @param [Valkyrie::Resource] resource
26
- # @return [Valkyrie::Resource] the persisted resource
17
+ # (see Valkyrie::Persistence::Memory::Persister#save)
27
18
  def save(resource:)
28
19
  repository([resource]).persist.first
29
20
  end
30
21
 
31
- # Persists a set of Valkyrie Resources into a Solr index
32
- # @param [Array<Valkyrie::Resource>] resources
33
- # @return [Valkyrie::Resource] the set of persisted resources
22
+ # (see Valkyrie::Persistence::Memory::Persister#save_all)
34
23
  def save_all(resources:)
35
24
  repository(resources).persist
36
25
  end
37
26
 
38
- # Deletes a Valkyrie Resource persisted into a Solr index
39
- # @param [Valkyrie::Resource] resource
40
- # @return [Valkyrie::Resource] the deleted resource
27
+ # (see Valkyrie::Persistence::Memory::Persister#delete)
41
28
  def delete(resource:)
42
29
  repository([resource]).delete.first
43
30
  end
44
31
 
45
- # Delete the Solr index of all Documents
32
+ # (see Valkyrie::Persistence::Memory::Persister#wipe!)
46
33
  def wipe!
47
34
  connection.delete_by_query("*:*")
48
35
  connection.commit
@@ -33,6 +33,12 @@ module Valkyrie::Persistence::Solr::Queries
33
33
  end
34
34
  end
35
35
 
36
+ # Queries without making Resrouces and returns the RSolr page_total value
37
+ # @return [Integer]
38
+ def count
39
+ connection.get("select", params: { q: query })["response"]["numFound"].to_s.to_i
40
+ end
41
+
36
42
  # Generates the Solr query for retrieving all Documents in the index
37
43
  # If a model is specified for the query, it is scoped to that Valkyrie resource type
38
44
  # @return [String]
@@ -12,27 +12,21 @@ module Valkyrie::Persistence::Solr
12
12
  @adapter = adapter
13
13
  end
14
14
 
15
- # Find resources by Valkyrie ID
16
- # @param [Valkyrie::ID] id
17
- # @return [Valkyrie::Resource]
15
+ # (see Valkyrie::Persistence::Memory::QueryService#find_by)
18
16
  def find_by(id:)
19
17
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
20
18
  validate_id(id)
21
19
  Valkyrie::Persistence::Solr::Queries::FindByIdQuery.new(id, connection: connection, resource_factory: resource_factory).run
22
20
  end
23
21
 
24
- # Find resources by a Valkyrie alternate identifier
25
- # @param [Valkyrie::ID] alternate_identifier
26
- # @return [Valkyrie::Resource]
22
+ # (see Valkyrie::Persistence::Memory::QueryService#find_by_alternate_identifier)
27
23
  def find_by_alternate_identifier(alternate_identifier:)
28
24
  alternate_identifier = Valkyrie::ID.new(alternate_identifier.to_s) if alternate_identifier.is_a?(String)
29
25
  validate_id(alternate_identifier)
30
26
  Valkyrie::Persistence::Solr::Queries::FindByAlternateIdentifierQuery.new(alternate_identifier, connection: connection, resource_factory: resource_factory).run
31
27
  end
32
28
 
33
- # Find resources using a set of Valkyrie IDs
34
- # @param [Array<Valkyrie::ID>] ids
35
- # @return [Array<Valkyrie::Resource>]
29
+ # (see Valkyrie::Persistence::Memory::QueryService#find_many_by_ids)
36
30
  def find_many_by_ids(ids:)
37
31
  ids.map! do |id|
38
32
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
@@ -42,29 +36,29 @@ module Valkyrie::Persistence::Solr
42
36
  Valkyrie::Persistence::Solr::Queries::FindManyByIdsQuery.new(ids, connection: connection, resource_factory: resource_factory).run
43
37
  end
44
38
 
45
- # Find all of the Valkyrie Resources persisted in the Solr index
46
- # @return [Array<Valkyrie::Resource>]
39
+ # (see Valkyrie::Persistence::Memory::QueryService#find_all)
47
40
  def find_all
48
41
  Valkyrie::Persistence::Solr::Queries::FindAllQuery.new(connection: connection, resource_factory: resource_factory).run
49
42
  end
50
43
 
51
- # Find all of the Valkyrie Resources of a model persisted in the Solr index
52
- # @param [Class, String] model the Valkyrie::Resource Class
53
- # @return [Array<Valkyrie::Resource>]
44
+ # (see Valkyrie::Persistence::Memory::QueryService#find_all_of_model)
54
45
  def find_all_of_model(model:)
55
46
  Valkyrie::Persistence::Solr::Queries::FindAllQuery.new(connection: connection, resource_factory: resource_factory, model: model).run
56
47
  end
57
48
 
58
- # Find all of the parent resources for a given Valkyrie Resource
59
- # @param [Valkyrie::Resource] member resource
60
- # @return [Array<Valkyrie::Resource>] parent resources
49
+ # Count all of the Valkyrie Resources of a model persisted in the Solr index
50
+ # @param [Class, String] model the Valkyrie::Resource Class
51
+ # @return integer
52
+ def count_all_of_model(model:)
53
+ Valkyrie::Persistence::Solr::Queries::FindAllQuery.new(connection: connection, resource_factory: resource_factory, model: model).count
54
+ end
55
+
56
+ # (see Valkyrie::Persistence::Memory::QueryService#find_parents)
61
57
  def find_parents(resource:)
62
58
  find_inverse_references_by(resource: resource, property: :member_ids)
63
59
  end
64
60
 
65
- # Find all of the member resources for a given Valkyrie Resource
66
- # @param [Valkyrie::Resource] parent resource
67
- # @return [Array<Valkyrie::Resource>] member resources
61
+ # (see Valkyrie::Persistence::Memory::QueryService#find_members)
68
62
  def find_members(resource:, model: nil)
69
63
  Valkyrie::Persistence::Solr::Queries::FindMembersQuery.new(
70
64
  resource: resource,
@@ -74,51 +68,46 @@ module Valkyrie::Persistence::Solr
74
68
  ).run
75
69
  end
76
70
 
77
- # Find all of the resources referenced by a given Valkyrie Resource using a specific property
78
- # @param [Valkyrie::Resource] resource
79
- # @param [Symbol, String] property
80
- # @return [Array<Valkyrie::Resource>] referenced resources
81
- def find_references_by(resource:, property:)
82
- if ordered_property?(resource: resource, property: property)
83
- Valkyrie::Persistence::Solr::Queries::FindOrderedReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run
84
- else
85
- Valkyrie::Persistence::Solr::Queries::FindReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run
86
- end
71
+ # (see Valkyrie::Persistence::Memory::QueryService#find_references_by)
72
+ def find_references_by(resource:, property:, model: nil)
73
+ result =
74
+ if ordered_property?(resource: resource, property: property)
75
+ Valkyrie::Persistence::Solr::Queries::FindOrderedReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run
76
+ else
77
+ Valkyrie::Persistence::Solr::Queries::FindReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run
78
+ end
79
+ return result unless model
80
+ result.select { |obj| obj.is_a?(model) }
87
81
  end
88
82
 
89
- # Find all of the resources referencing a given Valkyrie Resource using a specific property
90
- # (e. g. find all resources referencing a parent resource as a collection using the property "member_of_collections")
91
- # @param [Valkyrie::Resource] referenced resource
92
- # @param [Symbol, String] property
93
- # @return [Array<Valkyrie::Resource>] related resources
94
- def find_inverse_references_by(resource: nil, id: nil, property:)
83
+ # (see Valkyrie::Persistence::Memory::QueryService#find_inverse_references_by)
84
+ def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
95
85
  raise ArgumentError, "Provide resource or id" unless resource || id
96
86
  ensure_persisted(resource) if resource
97
87
  id ||= resource.id
98
- Valkyrie::Persistence::Solr::Queries::FindInverseReferencesQuery.new(id: id, property: property, connection: connection, resource_factory: resource_factory).run
88
+ result = Valkyrie::Persistence::Solr::Queries::FindInverseReferencesQuery.new(id: id, property: property, connection: connection, resource_factory: resource_factory).run
89
+ return result unless model
90
+ result.select { |obj| obj.is_a?(model) }
99
91
  end
100
92
 
101
- # Construct the Valkyrie::Persistence::CustomQueryContainer object using this query service
102
- # @return [Valkyrie::Persistence::CustomQueryContainer]
93
+ # (see Valkyrie::Persistence::Memory::QueryService#custom_queries)
103
94
  def custom_queries
104
95
  @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self)
105
96
  end
106
97
 
107
98
  private
108
99
 
109
- # Determine whether or not a value is a Valkyrie ID
110
- # @param [Object] id
111
- # @return [Boolean]
100
+ # (see Valkyrie::Persistence::Memory::QueryService#validate_id)
112
101
  def validate_id(id)
113
102
  raise ArgumentError, 'id must be a Valkyrie::ID' unless id.is_a? Valkyrie::ID
114
103
  end
115
104
 
116
- # Ensure that a given Valkyrie Resource has been persisted
117
- # @param [Valkyrie::Resource] resource
105
+ # (see Valkyrie::Persistence::Memory::QueryService#ensure_persisted)
118
106
  def ensure_persisted(resource)
119
107
  raise ArgumentError, 'resource is not saved' unless resource.persisted?
120
108
  end
121
109
 
110
+ # (see Valkyrie::Persistence::Memory::QueryService#ordered_property?)
122
111
  def ordered_property?(resource:, property:)
123
112
  resource.ordered_attribute?(property)
124
113
  end