uri_service 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/uri_service/client.rb +104 -102
- data/lib/uri_service/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a23d6006da06917691b8db5b5a1a7c77aa7cc50d
|
4
|
+
data.tar.gz: f89dc8f0cabf1ccaccde2e6bb5d595b9a9e4a280
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90e9d786e5bb9277c9118aed2fc38fb840f1d5a6d35d9f04a0a78b7bc04bed1182dc0312397d93f3dd37793d015faddab473ce883c17b1c0e85c1c054b6d747b
|
7
|
+
data.tar.gz: fd86538bd5259d7300b26061030ad26f36c75aee391bd51456330996ee87dab85ba82fbd25fb91df8c5543af4f08149434d35cecdb13fb43fdacd19e663bcf30
|
data/lib/uri_service/client.rb
CHANGED
@@ -1,31 +1,33 @@
|
|
1
1
|
class UriService::Client
|
2
|
-
|
2
|
+
|
3
3
|
attr_reader :db, :rsolr_pool, :local_uri_base, :temporary_uri_base
|
4
|
-
|
4
|
+
|
5
5
|
ALPHANUMERIC_UNDERSCORE_KEY_REGEX = /\A[a-z]+[a-z0-9_]*\z/
|
6
6
|
CORE_FIELD_NAMES = ['uri', 'vocabulary_string_key', 'value', 'authority', 'type', 'internal_id']
|
7
7
|
VALID_TYPES = [UriService::TermType::EXTERNAL, UriService::TermType::LOCAL, UriService::TermType::TEMPORARY]
|
8
|
-
|
8
|
+
|
9
9
|
def initialize(opts)
|
10
10
|
raise UriService::InvalidOptsError, "Must supply opts['local_uri_base'] to initialize method." if opts['local_uri_base'].nil?
|
11
11
|
raise UriService::InvalidOptsError, "Must supply opts['temporary_uri_base'] to initialize method." if opts['temporary_uri_base'].nil?
|
12
12
|
raise UriService::InvalidOptsError, "Must supply opts['database'] to initialize method." if opts['database'].nil?
|
13
13
|
raise UriService::InvalidOptsError, "Must supply opts['solr'] to initialize method." if opts['solr'].nil?
|
14
|
-
|
14
|
+
|
15
15
|
# Set local_uri_base and temporary_uri_base
|
16
16
|
@local_uri_base = opts['local_uri_base']
|
17
17
|
@temporary_uri_base = opts['temporary_uri_base']
|
18
|
-
|
18
|
+
|
19
19
|
# Create DB connection pool
|
20
20
|
@db = Sequel.connect(opts['database'])
|
21
|
-
|
21
|
+
|
22
22
|
# Create Solr connection pool
|
23
23
|
@rsolr_pool = ConnectionPool.new( size: opts['solr']['pool_size'], timeout: (opts['solr']['pool_timeout'].to_f/1000.to_f) ) { RSolr.connect(:url => opts['solr']['url']) }
|
24
|
+
|
25
|
+
@auto_commit_after_term_creation = opts['solr'].fetch('auto_commit_after_term_creation', true).to_s == 'true'
|
24
26
|
end
|
25
|
-
|
27
|
+
|
26
28
|
def reindex_all_terms(clear=false, print_progress_to_console=false)
|
27
29
|
self.handle_database_disconnect do
|
28
|
-
|
30
|
+
|
29
31
|
if print_progress_to_console
|
30
32
|
puts "Getting database term count..."
|
31
33
|
total = @db[UriService::TERMS].count
|
@@ -33,13 +35,13 @@ class UriService::Client
|
|
33
35
|
puts "Number of terms to index: #{total.to_s}"
|
34
36
|
puts ""
|
35
37
|
end
|
36
|
-
|
38
|
+
|
37
39
|
if clear
|
38
40
|
@rsolr_pool.with do |rsolr|
|
39
41
|
rsolr.delete_by_query('*:*');
|
40
42
|
end
|
41
43
|
end
|
42
|
-
|
44
|
+
|
43
45
|
# Need to use unambiguous order when using paged_each, so we choose to order by DB :id
|
44
46
|
@db[UriService::TERMS].order(:id).paged_each(:rows_per_fetch=>100) do |term_db_row|
|
45
47
|
self.send_term_to_solr(
|
@@ -51,26 +53,26 @@ class UriService::Client
|
|
51
53
|
term_db_row[:type],
|
52
54
|
term_db_row[:id],
|
53
55
|
false)
|
54
|
-
|
56
|
+
|
55
57
|
if print_progress_to_console
|
56
58
|
reindex_counter += 1
|
57
59
|
print "\rIndexed #{reindex_counter.to_s} of #{total.to_s}"
|
58
60
|
end
|
59
61
|
end
|
60
|
-
|
62
|
+
|
61
63
|
puts "\n" + "Committing solr updates..." if print_progress_to_console
|
62
64
|
self.do_solr_commit
|
63
65
|
puts "Done." if print_progress_to_console
|
64
66
|
end
|
65
67
|
end
|
66
|
-
|
68
|
+
|
67
69
|
def disconnect!
|
68
70
|
unless @db.nil?
|
69
71
|
db_reference = @db
|
70
72
|
@db = nil
|
71
73
|
db_reference.disconnect
|
72
74
|
end
|
73
|
-
|
75
|
+
|
74
76
|
unless @rsolr_pool.nil?
|
75
77
|
rsolr_pool_reference = @rsolr_pool
|
76
78
|
@rsolr_pool = nil
|
@@ -79,10 +81,10 @@ class UriService::Client
|
|
79
81
|
# but this doesn't hurt.
|
80
82
|
end
|
81
83
|
end
|
82
|
-
|
84
|
+
|
83
85
|
def connected?
|
84
86
|
return false if @db.nil? || @rsolr_pool.nil?
|
85
|
-
|
87
|
+
|
86
88
|
begin
|
87
89
|
self.test_connection
|
88
90
|
return true
|
@@ -90,23 +92,23 @@ class UriService::Client
|
|
90
92
|
return false
|
91
93
|
end
|
92
94
|
end
|
93
|
-
|
95
|
+
|
94
96
|
def test_connection
|
95
97
|
@db.test_connection # Raises Sequel::DatabaseConnectionError if connection didn't work
|
96
98
|
@rsolr_pool.with do |rsolr|
|
97
99
|
rsolr.get('admin/ping') # Raises Errno::ECONNREFUSED if connection didn't work
|
98
100
|
end
|
99
101
|
end
|
100
|
-
|
102
|
+
|
101
103
|
def required_tables_exist?
|
102
104
|
return (UriService.required_tables - @db.tables).length == 0
|
103
105
|
end
|
104
|
-
|
106
|
+
|
105
107
|
def create_required_tables
|
106
108
|
self.handle_database_disconnect do
|
107
|
-
|
109
|
+
|
108
110
|
current_tables = @db.tables
|
109
|
-
|
111
|
+
|
110
112
|
unless current_tables.include?(UriService::VOCABULARIES)
|
111
113
|
@db.create_table UriService::VOCABULARIES do |t|
|
112
114
|
primary_key :id
|
@@ -117,7 +119,7 @@ class UriService::Client
|
|
117
119
|
else
|
118
120
|
puts 'Skipped creation of table ' + UriService::VOCABULARIES.to_s + ' because it already exists.'
|
119
121
|
end
|
120
|
-
|
122
|
+
|
121
123
|
unless current_tables.include?(UriService::TERMS)
|
122
124
|
@db.create_table UriService::TERMS do |t|
|
123
125
|
primary_key :id
|
@@ -134,14 +136,14 @@ class UriService::Client
|
|
134
136
|
else
|
135
137
|
puts 'Skipped creation of table ' + UriService::TERMS.to_s + ' because it already exists.'
|
136
138
|
end
|
137
|
-
|
139
|
+
|
138
140
|
end
|
139
141
|
end
|
140
|
-
|
142
|
+
|
141
143
|
##################
|
142
144
|
# Create methods #
|
143
145
|
##################
|
144
|
-
|
146
|
+
|
145
147
|
def create_vocabulary(string_key, display_label)
|
146
148
|
self.handle_database_disconnect do
|
147
149
|
if string_key.to_s == 'all'
|
@@ -151,7 +153,7 @@ class UriService::Client
|
|
151
153
|
unless string_key =~ ALPHANUMERIC_UNDERSCORE_KEY_REGEX
|
152
154
|
raise UriService::InvalidVocabularyStringKeyError, "Invalid key (can only include lower case letters, numbers or underscores, but cannot start with an underscore): " + string_key
|
153
155
|
end
|
154
|
-
|
156
|
+
|
155
157
|
@db.transaction do
|
156
158
|
begin
|
157
159
|
@db[UriService::VOCABULARIES].insert(string_key: string_key, display_label: display_label)
|
@@ -161,27 +163,27 @@ class UriService::Client
|
|
161
163
|
end
|
162
164
|
end
|
163
165
|
end
|
164
|
-
|
166
|
+
|
165
167
|
# Creates a new term
|
166
168
|
def create_term(type, opts)
|
167
169
|
raise UriService::InvalidTermTypeError, 'Invalid type: ' + type unless VALID_TYPES.include?(type)
|
168
|
-
|
170
|
+
|
169
171
|
vocabulary_string_key = opts.delete(:vocabulary_string_key)
|
170
172
|
value = opts.delete(:value)
|
171
173
|
uri = opts.delete(:uri)
|
172
174
|
authority = opts.has_key?(:authority) ? opts.delete(:authority) : ''
|
173
175
|
authority = '' if authority.nil?
|
174
176
|
additional_fields = opts.delete(:additional_fields) || {}
|
175
|
-
|
177
|
+
|
176
178
|
if type == UriService::TermType::EXTERNAL
|
177
179
|
# URI is required
|
178
180
|
raise UriService::InvalidOptsError, "A uri must be supplied for terms of type #{type}." if uri.nil?
|
179
|
-
|
181
|
+
|
180
182
|
return create_term_impl(type, vocabulary_string_key, value, uri, authority, additional_fields)
|
181
183
|
else
|
182
184
|
# URI should not be present
|
183
185
|
raise UriService::InvalidOptsError, "A uri cannot supplied for term type: #{type}" unless uri.nil?
|
184
|
-
|
186
|
+
|
185
187
|
if type == UriService::TermType::TEMPORARY
|
186
188
|
# No two TEMPORARY terms within the same vocabulary can have the same value, so we generate a unique URI from a hash of the (vocabulary_string_key + value) to ensure uniqueness.
|
187
189
|
uri = self.generate_uri_for_temporary_term(vocabulary_string_key, value)
|
@@ -204,15 +206,15 @@ class UriService::Client
|
|
204
206
|
# Probabilistically, the error below should never be raised.
|
205
207
|
raise UriService::CouldNotGenerateUriError, "UriService generated a duplicate random UUID (via SecureRandom.uuid) too many times in a row. Probabilistically, this should never happen."
|
206
208
|
end
|
207
|
-
|
209
|
+
|
208
210
|
end
|
209
211
|
end
|
210
|
-
|
212
|
+
|
211
213
|
def generate_uri_for_temporary_term(vocabulary_string_key, term_value)
|
212
214
|
uri = URI(@temporary_uri_base + Digest::SHA256.hexdigest(vocabulary_string_key + term_value))
|
213
215
|
return uri.to_s
|
214
216
|
end
|
215
|
-
|
217
|
+
|
216
218
|
def generate_frozen_term_hash(vocabulary_string_key, value, uri, authority, additional_fields, type, internal_id)
|
217
219
|
hash_to_return = {}
|
218
220
|
hash_to_return['uri'] = uri
|
@@ -221,20 +223,20 @@ class UriService::Client
|
|
221
223
|
hash_to_return['authority'] = authority unless authority == ''
|
222
224
|
hash_to_return['vocabulary_string_key'] = vocabulary_string_key
|
223
225
|
hash_to_return['internal_id'] = internal_id
|
224
|
-
|
226
|
+
|
225
227
|
additional_fields.each do |key, val|
|
226
228
|
hash_to_return[key] = val
|
227
229
|
end
|
228
|
-
|
230
|
+
|
229
231
|
# Delete nil values
|
230
232
|
hash_to_return.delete_if { |k, v| v.nil? }
|
231
|
-
|
233
|
+
|
232
234
|
# Freeze hash
|
233
235
|
hash_to_return.freeze # To make this a read-only hash
|
234
|
-
|
236
|
+
|
235
237
|
return hash_to_return
|
236
238
|
end
|
237
|
-
|
239
|
+
|
238
240
|
def create_term_solr_doc(vocabulary_string_key, value, uri, authority, additional_fields, type, internal_id)
|
239
241
|
doc = {}
|
240
242
|
doc['uri'] = uri
|
@@ -243,21 +245,21 @@ class UriService::Client
|
|
243
245
|
doc['vocabulary_string_key'] = vocabulary_string_key
|
244
246
|
doc['authority'] = authority
|
245
247
|
doc['internal_id'] = internal_id
|
246
|
-
|
248
|
+
|
247
249
|
doc['additional_fields'] = JSON.generate(additional_fields)
|
248
|
-
|
250
|
+
|
249
251
|
return doc
|
250
252
|
end
|
251
|
-
|
253
|
+
|
252
254
|
# Index the DB row term data into solr
|
253
|
-
def send_term_to_solr(vocabulary_string_key, value, uri, authority, additional_fields, type, internal_id, commit=
|
255
|
+
def send_term_to_solr(vocabulary_string_key, value, uri, authority, additional_fields, type, internal_id, commit = @auto_commit_after_term_creation)
|
254
256
|
doc = create_term_solr_doc(vocabulary_string_key, value, uri, authority, additional_fields, type, internal_id)
|
255
257
|
@rsolr_pool.with do |rsolr|
|
256
258
|
rsolr.add(doc)
|
257
259
|
rsolr.commit if commit
|
258
260
|
end
|
259
261
|
end
|
260
|
-
|
262
|
+
|
261
263
|
# Validates additional_fields and verifies that no reserved words are supplied
|
262
264
|
def validate_additional_field_keys(additional_fields)
|
263
265
|
additional_fields.each do |key, value|
|
@@ -269,41 +271,41 @@ class UriService::Client
|
|
269
271
|
end
|
270
272
|
end
|
271
273
|
end
|
272
|
-
|
274
|
+
|
273
275
|
################
|
274
276
|
# Find methods #
|
275
277
|
################
|
276
|
-
|
278
|
+
|
277
279
|
def find_vocabulary(vocabulary_string_key)
|
278
280
|
self.handle_database_disconnect do
|
279
281
|
@db[UriService::VOCABULARIES].where(string_key: vocabulary_string_key).first
|
280
282
|
end
|
281
283
|
end
|
282
|
-
|
284
|
+
|
283
285
|
# Finds the term with the given uri
|
284
286
|
def find_term_by_uri(uri)
|
285
287
|
results = self.find_terms_where({uri: uri}, 1)
|
286
288
|
return results.length == 1 ? results.first : nil
|
287
289
|
end
|
288
|
-
|
290
|
+
|
289
291
|
# Finds the term with the given uri
|
290
292
|
def find_term_by_internal_id(id)
|
291
293
|
results = self.find_terms_where({internal_id: id}, 1)
|
292
294
|
return results.length == 1 ? results.first : nil
|
293
295
|
end
|
294
|
-
|
296
|
+
|
295
297
|
# Finds terms that match the specified conditions
|
296
298
|
def find_terms_where(opts, limit=10)
|
297
299
|
fqs = []
|
298
|
-
|
300
|
+
|
299
301
|
# Only search on allowed fields
|
300
302
|
unsupported_search_fields = opts.map{|key, val| key.to_s} - CORE_FIELD_NAMES
|
301
303
|
raise UriService::UnsupportedSearchFieldError, "Unsupported search fields: #{unsupported_search_fields.join(', ')}" if unsupported_search_fields.present?
|
302
|
-
|
304
|
+
|
303
305
|
opts.each do |field_name, val|
|
304
306
|
fqs << (field_name.to_s + ':"' + UriService.solr_escape(val.to_s) + '"')
|
305
307
|
end
|
306
|
-
|
308
|
+
|
307
309
|
@rsolr_pool.with do |rsolr|
|
308
310
|
response = rsolr.get('select', params: {
|
309
311
|
:q => '*:*',
|
@@ -323,9 +325,9 @@ class UriService::Client
|
|
323
325
|
end
|
324
326
|
end
|
325
327
|
end
|
326
|
-
|
328
|
+
|
327
329
|
def term_solr_doc_to_frozen_term_hash(term_solr_doc)
|
328
|
-
|
330
|
+
|
329
331
|
uri = term_solr_doc.delete('uri')
|
330
332
|
vocabulary_string_key = term_solr_doc.delete('vocabulary_string_key')
|
331
333
|
value = term_solr_doc.delete('value')
|
@@ -333,19 +335,19 @@ class UriService::Client
|
|
333
335
|
type = term_solr_doc.delete('type')
|
334
336
|
additional_fields = JSON.parse(term_solr_doc.delete('additional_fields'))
|
335
337
|
internal_id = term_solr_doc.delete('internal_id')
|
336
|
-
|
338
|
+
|
337
339
|
return generate_frozen_term_hash(vocabulary_string_key, value, uri, authority, additional_fields, type, internal_id)
|
338
340
|
end
|
339
|
-
|
341
|
+
|
340
342
|
def find_terms_by_query(vocabulary_string_key, value_query, limit=10, start=0)
|
341
|
-
|
343
|
+
|
342
344
|
if value_query.blank?
|
343
345
|
return self.list_terms(vocabulary_string_key, limit, start)
|
344
346
|
end
|
345
|
-
|
347
|
+
|
346
348
|
terms_to_return = []
|
347
349
|
@rsolr_pool.with do |rsolr|
|
348
|
-
|
350
|
+
|
349
351
|
solr_params = {
|
350
352
|
:q => UriService.solr_escape(value_query),
|
351
353
|
:fq => 'vocabulary_string_key:' + UriService.solr_escape(vocabulary_string_key),
|
@@ -353,15 +355,15 @@ class UriService::Client
|
|
353
355
|
:start => start,
|
354
356
|
:sort => 'score desc, value_ssort asc, uri asc' # For consistent sorting
|
355
357
|
}
|
356
|
-
|
358
|
+
|
357
359
|
if value_query.length < 3
|
358
360
|
# For efficiency, we only do whole term matches for queries < 3 characters
|
359
361
|
solr_params[:qf] = 'value_suggest'
|
360
362
|
solr_params[:pf] = 'value_suggest'
|
361
363
|
end
|
362
|
-
|
364
|
+
|
363
365
|
response = rsolr.get('suggest', params: solr_params)
|
364
|
-
|
366
|
+
|
365
367
|
if response['response']['numFound'] > 0
|
366
368
|
response['response']['docs'].each do |doc|
|
367
369
|
terms_to_return << term_solr_doc_to_frozen_term_hash(doc)
|
@@ -370,11 +372,11 @@ class UriService::Client
|
|
370
372
|
end
|
371
373
|
return terms_to_return
|
372
374
|
end
|
373
|
-
|
375
|
+
|
374
376
|
################
|
375
377
|
# List methods #
|
376
378
|
################
|
377
|
-
|
379
|
+
|
378
380
|
# Lists vocabularies alphabetically (by string key) and supports paging through results.
|
379
381
|
def list_vocabularies(limit=10, start=0)
|
380
382
|
self.handle_database_disconnect do
|
@@ -382,13 +384,13 @@ class UriService::Client
|
|
382
384
|
return db_rows.map{|row| row.except(:id).stringify_keys!}
|
383
385
|
end
|
384
386
|
end
|
385
|
-
|
387
|
+
|
386
388
|
# Lists terms alphabetically and supports paging through results.
|
387
389
|
# Useful for browsing through a term list without a query.
|
388
390
|
def list_terms(vocabulary_string_key, limit=10, start=0)
|
389
391
|
terms_to_return = []
|
390
392
|
@rsolr_pool.with do |rsolr|
|
391
|
-
|
393
|
+
|
392
394
|
solr_params = {
|
393
395
|
:fq => 'vocabulary_string_key:' + UriService.solr_escape(vocabulary_string_key),
|
394
396
|
:q => '*:*',
|
@@ -396,7 +398,7 @@ class UriService::Client
|
|
396
398
|
:start => start,
|
397
399
|
:sort => 'value_ssort asc, uri asc' # Include 'uri asc' as part of sort to ensure consistent sorting
|
398
400
|
}
|
399
|
-
|
401
|
+
|
400
402
|
response = rsolr.get('select', params: solr_params)
|
401
403
|
if response['response']['numFound'] > 0
|
402
404
|
response['response']['docs'].each do |doc|
|
@@ -406,17 +408,17 @@ class UriService::Client
|
|
406
408
|
end
|
407
409
|
return terms_to_return
|
408
410
|
end
|
409
|
-
|
411
|
+
|
410
412
|
##################
|
411
413
|
# Delete methods #
|
412
414
|
##################
|
413
|
-
|
415
|
+
|
414
416
|
def delete_vocabulary(vocabulary_string_key)
|
415
417
|
self.handle_database_disconnect do
|
416
418
|
@db[UriService::VOCABULARIES].where(string_key: vocabulary_string_key).delete
|
417
419
|
end
|
418
420
|
end
|
419
|
-
|
421
|
+
|
420
422
|
def delete_term(uri, commit=true)
|
421
423
|
self.handle_database_disconnect do
|
422
424
|
@db.transaction do
|
@@ -428,37 +430,37 @@ class UriService::Client
|
|
428
430
|
end
|
429
431
|
end
|
430
432
|
end
|
431
|
-
|
433
|
+
|
432
434
|
##################
|
433
435
|
# Update methods #
|
434
436
|
##################
|
435
|
-
|
437
|
+
|
436
438
|
def update_vocabulary(string_key, new_display_label)
|
437
439
|
self.handle_database_disconnect do
|
438
440
|
dataset = @db[UriService::VOCABULARIES].where(string_key: string_key)
|
439
441
|
raise UriService::NonExistentVocabularyError, "No vocabulary found with string_key: " + string_key if dataset.count == 0
|
440
|
-
|
442
|
+
|
441
443
|
@db.transaction do
|
442
444
|
dataset.update(display_label: new_display_label)
|
443
445
|
end
|
444
446
|
end
|
445
447
|
end
|
446
|
-
|
448
|
+
|
447
449
|
# opts format: {:value => 'new value', :authority => 'newauthority', :additional_fields => {'key' => 'value'}}
|
448
450
|
def update_term(uri, opts, merge_additional_fields=true)
|
449
451
|
self.handle_database_disconnect do
|
450
452
|
term_db_row = @db[UriService::TERMS].first(uri: uri)
|
451
453
|
raise UriService::NonExistentUriError, "No term found with uri: " + uri if term_db_row.nil?
|
452
|
-
|
454
|
+
|
453
455
|
new_value = opts[:value] || term_db_row[:value]
|
454
456
|
new_authority = opts[:authority] || term_db_row[:authority]
|
455
457
|
new_additional_fields = term_db_row[:additional_fields].nil? ? {} : JSON.parse(term_db_row[:additional_fields])
|
456
|
-
|
458
|
+
|
457
459
|
if term_db_row[:type] == UriService::TermType::TEMPORARY && new_value != term_db_row[:value]
|
458
460
|
# TEMPORARY terms cannot have their values changed, but it is possible to update other fields
|
459
461
|
raise UriService::CannotChangeTemporaryTermValue, "The value of a temporary term cannot be changed. Delete unusued temporary terms or create a new one with a different value."
|
460
462
|
end
|
461
|
-
|
463
|
+
|
462
464
|
unless opts[:additional_fields].nil?
|
463
465
|
if merge_additional_fields
|
464
466
|
new_additional_fields.merge!(opts[:additional_fields])
|
@@ -468,16 +470,16 @@ class UriService::Client
|
|
468
470
|
end
|
469
471
|
end
|
470
472
|
validate_additional_field_keys(new_additional_fields)
|
471
|
-
|
473
|
+
|
472
474
|
@db.transaction do
|
473
475
|
@db[UriService::TERMS].where(uri: uri).update(value: new_value, value_hash: Digest::SHA256.hexdigest(new_value), authority: new_authority, additional_fields: JSON.generate(new_additional_fields))
|
474
476
|
self.send_term_to_solr(term_db_row[:vocabulary_string_key], new_value, uri, new_authority, new_additional_fields, term_db_row[:type], term_db_row[:id])
|
475
477
|
end
|
476
|
-
|
478
|
+
|
477
479
|
return generate_frozen_term_hash(term_db_row[:vocabulary_string_key], new_value, uri, new_authority, new_additional_fields, term_db_row[:type], term_db_row[:id])
|
478
480
|
end
|
479
481
|
end
|
480
|
-
|
482
|
+
|
481
483
|
def handle_database_disconnect
|
482
484
|
tries ||= 3
|
483
485
|
begin
|
@@ -487,26 +489,26 @@ class UriService::Client
|
|
487
489
|
retry unless tries == 0
|
488
490
|
end
|
489
491
|
end
|
490
|
-
|
492
|
+
|
491
493
|
def do_solr_commit
|
492
494
|
@rsolr_pool.with do |rsolr|
|
493
495
|
rsolr.commit
|
494
496
|
end
|
495
497
|
end
|
496
|
-
|
498
|
+
|
497
499
|
def clear_solr_index
|
498
500
|
@rsolr_pool.with do |rsolr|
|
499
501
|
rsolr.delete_by_query('*:*');
|
500
502
|
rsolr.commit
|
501
503
|
end
|
502
504
|
end
|
503
|
-
|
505
|
+
|
504
506
|
#########################
|
505
507
|
# BEGIN PRIVATE METHODS #
|
506
508
|
#########################
|
507
|
-
|
509
|
+
|
508
510
|
private
|
509
|
-
|
511
|
+
|
510
512
|
# Backing implementation for actual term creation in db/solr.
|
511
513
|
# - Performs some data validations.
|
512
514
|
# - Ensures uniqueness of URIs in database.
|
@@ -514,11 +516,11 @@ class UriService::Client
|
|
514
516
|
# create a new TEMPORARY term with an existing value/vocabulary combo,
|
515
517
|
# also adding non-existent supplied additional_fields to the existing temporary term.
|
516
518
|
def create_term_impl(type, vocabulary_string_key, value, uri, authority, additional_fields)
|
517
|
-
|
519
|
+
|
518
520
|
raise UriService::InvalidTermTypeError, 'Invalid type: ' + type unless VALID_TYPES.include?(type)
|
519
|
-
|
521
|
+
|
520
522
|
self.handle_database_disconnect do
|
521
|
-
|
523
|
+
|
522
524
|
if type == UriService::TermType::TEMPORARY
|
523
525
|
# If this is a TEMPORARY term, we need to ensure that the temporary
|
524
526
|
# passed in URI is a hash of the vocabulary + value, just in case this
|
@@ -529,20 +531,20 @@ class UriService::Client
|
|
529
531
|
raise UriService::InvalidTemporaryTermUriError, "The supplied URI was not derived from the supplied (vocabulary_string_key+value) pair."
|
530
532
|
end
|
531
533
|
end
|
532
|
-
|
534
|
+
|
533
535
|
unless uri =~ UriService::VALID_URI_REGEX
|
534
536
|
raise UriService::InvalidUriError, "Invalid URI supplied during term creation: #{uri}"
|
535
537
|
end
|
536
|
-
|
538
|
+
|
537
539
|
#Ensure that vocabulary with vocabulary_string_key exists
|
538
540
|
if self.find_vocabulary(vocabulary_string_key).nil?
|
539
541
|
raise UriService::NonExistentVocabularyError, "There is no vocabulary with string key: " + vocabulary_string_key.to_s
|
540
542
|
end
|
541
|
-
|
543
|
+
|
542
544
|
# Stringify and validate keys for additional_fields
|
543
545
|
additional_fields.stringify_keys!
|
544
546
|
validate_additional_field_keys(additional_fields) # This method call raises an error if an invalid additional_field key is supplied
|
545
|
-
|
547
|
+
|
546
548
|
@db.transaction do
|
547
549
|
value_hash = Digest::SHA256.hexdigest(value)
|
548
550
|
|
@@ -559,41 +561,41 @@ class UriService::Client
|
|
559
561
|
)
|
560
562
|
send_term_to_solr(vocabulary_string_key, value, uri, authority, additional_fields, type, db_id)
|
561
563
|
rescue Sequel::UniqueConstraintViolation
|
562
|
-
|
564
|
+
|
563
565
|
# If the user is trying to create a new TEMPORARY term and we ran into a Sequel::UniqueConstraintViolation,
|
564
566
|
# that means that the term already exists. We will return that existing term, but also update the term with
|
565
567
|
# any non-existent additional_fields supplied by the user and during this create operation, and a supplied
|
566
568
|
# authority if the term did not already have an authority.
|
567
569
|
if type == UriService::TermType::TEMPORARY
|
568
570
|
temporary_term = self.find_term_by_uri(uri)
|
569
|
-
|
571
|
+
|
570
572
|
opts = {}
|
571
573
|
non_existent_additional_fields = additional_fields.keys - temporary_term.keys
|
572
|
-
|
574
|
+
|
573
575
|
if non_existent_additional_fields.length > 0
|
574
576
|
additional_fields_to_merge_in = additional_fields.select{|k, v| non_existent_additional_fields.include?(k)}
|
575
577
|
opts[:additional_fields] = additional_fields_to_merge_in
|
576
578
|
end
|
577
|
-
|
579
|
+
|
578
580
|
if temporary_term['authority'].nil? && authority.length > 0 && authority != temporary_term['authority']
|
579
581
|
opts[:authority] = authority
|
580
582
|
end
|
581
|
-
|
583
|
+
|
582
584
|
if opts.size > 0
|
583
585
|
temporary_term = UriService.client.update_term(temporary_term['uri'], opts, true)
|
584
586
|
end
|
585
|
-
|
587
|
+
|
586
588
|
return temporary_term
|
587
589
|
end
|
588
|
-
|
590
|
+
|
589
591
|
raise UriService::ExistingUriError, "A term already exists with uri: " + uri + " (conflict found via uri_hash check)"
|
590
592
|
end
|
591
|
-
|
593
|
+
|
592
594
|
return generate_frozen_term_hash(vocabulary_string_key, value, uri, authority, additional_fields, type, db_id)
|
593
595
|
end
|
594
596
|
end
|
595
597
|
end
|
596
|
-
|
598
|
+
|
597
599
|
end
|
598
600
|
|
599
601
|
class UriService::Error < StandardError;end
|
@@ -610,4 +612,4 @@ class UriService::ExistingVocabularyStringKeyError < UriService::Error;end
|
|
610
612
|
class UriService::NonExistentUriError < UriService::Error;end
|
611
613
|
class UriService::NonExistentVocabularyError < UriService::Error;end
|
612
614
|
class UriService::UnsupportedObjectTypeError < UriService::Error;end
|
613
|
-
class UriService::UnsupportedSearchFieldError < UriService::Error;end
|
615
|
+
class UriService::UnsupportedSearchFieldError < UriService::Error;end
|
data/lib/uri_service/version.rb
CHANGED