solis 0.127.0 → 0.129.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7cf89eceec897d7b5fe15ef1669b031480a7aab2850bd5638cfac379e4798ae
4
- data.tar.gz: 56a9ce9652925e1be2904dc150bba6d31f3f2ca13870641c883fb0dde65b341c
3
+ metadata.gz: c4f9f15ce1664f194768963f29ad7d3edc8e394a4970e443ab27b7dde1a61e35
4
+ data.tar.gz: d2591a1d9481ce5e7b1577fedd22ae8a93ee096acdc46838fdb07770967e2c6d
5
5
  SHA512:
6
- metadata.gz: 499ff31724baaa76c0ae960916d5b01c0f25f535e07e89fd0cba72cbc042fe271898b5c422842201463daef4903d1207bf4fdcbd248e3bbff346153f4dba1765
7
- data.tar.gz: 264f64d77cf8b15d708da87cbf0602a6f8be5282c04a365f52966e865895032e9c08b4952e99abbcb6e5f4aa80fcbd20887e3e4d9c961398e350a4cbaff17b0b
6
+ metadata.gz: ef99b8aab43b5c0a534afcf6caef943567664d596ad878c2e08a14465402955d3c88930f80c7294acaa5626d784cef4ab62f42dfbbc6b1c19a55103f47aed80a
7
+ data.tar.gz: 0d21f625fc63af46c3817ba70f3bb334487ac99b0951cdfe40f549938e41096748a2d70a1a78e034e11b0d7c7bf78604bcfd829ceadf19f2447609da59368d20
data/lib/solis/model.rb CHANGED
@@ -156,7 +156,8 @@ values ?s {<#{self.graph_id}>}
156
156
 
157
157
  def save(validate_dependencies = true, top_level = true)
158
158
  raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil?
159
- sparql = SPARQL::Client.new(self.class.sparql_endpoint)
159
+ pool = Solis::Store::Sparql::Client.pool_for(self.class.sparql_endpoint)
160
+ sparql = pool.checkout
160
161
 
161
162
  before_create_proc&.call(self)
162
163
 
@@ -225,6 +226,8 @@ values ?s {<#{self.graph_id}>}
225
226
  rescue StandardError => e
226
227
  Solis::LOGGER.error e.message
227
228
  raise e
229
+ ensure
230
+ pool.checkin if pool && sparql
228
231
  end
229
232
 
230
233
  # Update an entity.
@@ -249,7 +252,8 @@ values ?s {<#{self.graph_id}>}
249
252
  raise "id is mandatory when updating" unless attributes.keys.include?('id')
250
253
 
251
254
  id = attributes.delete('id')
252
- sparql = sparql_client || SPARQL::Client.new(self.class.sparql_endpoint)
255
+ pool = Solis::Store::Sparql::Client.pool_for(self.class.sparql_endpoint) if sparql_client.nil?
256
+ sparql = sparql_client || pool.checkout
253
257
 
254
258
  # prefetched_original is used only when it is a complete entity; an id-only stub
255
259
  # cannot seed updated_klass (omitted mandatory attributes would be lost).
@@ -419,6 +423,9 @@ values ?s {<#{self.graph_id}>}
419
423
  sparql.insert_data(original_graph, graph: original_graph.name) if defined?(original_graph) && original_graph && defined?(sparql) && sparql
420
424
 
421
425
  raise e
426
+ ensure
427
+ # Only return a client this call borrowed itself; a passed-in sparql_client belongs to the caller.
428
+ pool.checkin if pool && sparql
422
429
  end
423
430
 
424
431
  def graph_id
@@ -443,7 +450,8 @@ values ?s {<#{self.graph_id}>}
443
450
  raise "I need a SPARQL endpoint" if sparql_endpoint.nil?
444
451
  return [] if entities.empty?
445
452
 
446
- sparql = SPARQL::Client.new(sparql_endpoint)
453
+ pool = Solis::Store::Sparql::Client.pool_for(sparql_endpoint)
454
+ sparql = pool.checkout
447
455
 
448
456
  # Batch check existence of all entities
449
457
  existing_ids = batch_exists?(sparql, entities)
@@ -487,6 +495,8 @@ values ?s {<#{self.graph_id}>}
487
495
  end
488
496
 
489
497
  entities
498
+ ensure
499
+ pool.checkin if pool && sparql
490
500
  end
491
501
 
492
502
  # Check existence of multiple entities in a single SPARQL query
@@ -590,7 +600,10 @@ values ?s {<#{self.graph_id}>}
590
600
  description: attribute_metadata[:comment]&.value
591
601
  }
592
602
  attribute_data[:order] = attribute_metadata[:order]&.value.to_i if attribute_metadata.key?(:order) && !attribute_metadata[:order].nil?
593
- attribute_data[:group] = attribute_metadata[:group]&.value.gsub(graph_name,'').gsub(/Group$/,'') if attribute_metadata.key?(:group) && !attribute_metadata[:group].nil?
603
+ if attribute_metadata.key?(:group) && !attribute_metadata[:group].nil?
604
+ attribute_data[:group] = { name: attribute_metadata[:group][:name],
605
+ label: attribute_metadata[:group][:label] || {} }
606
+ end
594
607
  attribute_data[:attributes] = cm[:attributes] if cm && cm[:attributes]
595
608
 
596
609
  m[:attributes] << attribute_data
@@ -56,6 +56,10 @@ module Solis
56
56
  end
57
57
 
58
58
  #SPARQL::Client.new(@sparql_endpoint, graph: @model.class.graph_name, read_timeout: 120)
59
+ # Querying the live endpoint goes through the shared pool; an in-memory
60
+ # repository (constructed or cached result) needs no HTTP connection.
61
+ return @sparql_client unless sparql_repository.is_a?(RDF::Queryable)
62
+
59
63
  SPARQL::Client.new(sparql_repository, read_timeout: 120)
60
64
  end
61
65
 
@@ -45,7 +45,8 @@ module Solis
45
45
 
46
46
  if sheets.key?('_CATEGORIES')
47
47
  categories = sheets['_CATEGORIES']
48
- raise "_CATEGORIES tab must have ['name'] as a header at row 1" unless (%w[name] - categories.header).length == 0
48
+ has_name = categories.header.include?('name') || categories.header.any? { |h| language_column('name').match?(h.to_s) }
49
+ raise "_CATEGORIES tab must have ['name'] or ['name_XX', ...] as a header at row 1" unless has_name
49
50
  end
50
51
 
51
52
  sheets.each do |sheet_name, sheet|
@@ -141,17 +142,20 @@ module Solis
141
142
  raise Solis::Error::GeneralError, e.message
142
143
  end
143
144
 
144
- # Matches the 'Label_XX' columns (header is downcased by ::Sheet#header) where
145
- # XX is a language tag, ex. 'Label_NL', 'Label_EN', 'Label_nl-BE'
146
- LANGUAGE_LABEL_COLUMN = /\Alabel_(?<language>[a-z]{2,3}([-_][a-z0-9]{2,8})*)\z/.freeze
145
+ # Matches the '<prefix>_XX' columns (header is downcased by ::Sheet#header) where
146
+ # XX is a language tag, ex. 'Label_NL', 'Label_EN', 'Label_nl-BE', 'Name_nl'
147
+ def language_column(prefix)
148
+ /\A#{Regexp.escape(prefix.to_s.downcase)}_(?<language>[a-z]{2,3}([-_][a-z0-9]{2,8})*)\z/
149
+ end
147
150
 
148
- # Collects all 'Label_XX' columns of a row into { nl: 'Publiceren', en: 'Publish' }
149
- def parse_labels(row)
151
+ # Collects all 'Label_XX' (or '<prefix>_XX') columns of a row into { nl: 'Publiceren', en: 'Publish' }
152
+ def parse_labels(row, prefix = 'label')
150
153
  labels = {}
151
154
  return labels unless row.respond_to?(:each_pair)
152
155
 
156
+ column_pattern = language_column(prefix)
153
157
  row.each_pair do |column, value|
154
- match = LANGUAGE_LABEL_COLUMN.match(column.to_s)
158
+ match = column_pattern.match(column.to_s)
155
159
  next if match.nil?
156
160
  next if value.nil? || value.to_s.strip.empty?
157
161
 
@@ -283,7 +287,7 @@ hide empty members
283
287
  "\n#{indent}#{rdfs_prefix}:label #{literals.join(', ')} ;"
284
288
  end
285
289
 
286
- def build_shacl(datas)
290
+ def build_shacl(datas, categories = [])
287
291
  shacl_prefix = datas.first[:ontologies][:all].select { |_, v| v[:uri] =~ /shacl/ }.keys.first
288
292
  shacl_prefix = 'sh' if shacl_prefix.nil?
289
293
 
@@ -385,10 +389,17 @@ hide empty members
385
389
  out += ".\n"
386
390
 
387
391
  groups.each do |group, category|
392
+ # use the per language names from _CATEGORIES when the category is referenced by any of them
393
+ category_labels = categories.find { |c| c[:name].eql?(category) || c[:labels].value?(category) }&.dig(:labels)
394
+ group_label = if category_labels.nil? || category_labels.empty?
395
+ %(#{rdfs_prefix}:label "#{category}")
396
+ else
397
+ build_labels(category_labels, rdfs_prefix, '').strip.chomp(' ;')
398
+ end
388
399
  out += %(
389
400
  #{graph_prefix}:#{group}
390
401
  a #{shacl_prefix}:PropertyGroup ;
391
- #{rdfs_prefix}:label "#{category}" .
402
+ #{group_label} .
392
403
 
393
404
  )
394
405
  end
@@ -526,7 +537,7 @@ hide empty members
526
537
  # categories are uninflected, singular and plural are the same word.
527
538
  # an entity by the same name keeps its own plural.
528
539
  # categories.each do |category|
529
- # name = category.to_s.strip
540
+ # name = category[:name].to_s.strip
530
541
  # next if name.empty?
531
542
  #
532
543
  # inflections[name.to_sym] = name unless inflections.key?(name.to_sym) || inclections.value?(name.to_s)
@@ -780,7 +791,19 @@ hide empty members
780
791
  options[:metadata] = metadata
781
792
 
782
793
  # like _REFERENCES, _CATEGORIES only lives in the main sheet
783
- categories = sheet_data.is_a?(Hash) && sheet_data.key?('_CATEGORIES') ? sheet_data['_CATEGORIES'].map { |c| c['name'] } : []
794
+ # a category can have a name per language ('Name_nl', 'Name_en', ...) next to, or instead of, 'Name'.
795
+ # entity sheets refer to a category by 'Name' or, when missing, by the first 'Name_XX' column.
796
+ categories = []
797
+ if sheet_data.is_a?(Hash) && sheet_data.key?('_CATEGORIES')
798
+ categories = sheet_data['_CATEGORIES'].map do |c|
799
+ labels = parse_labels(c, 'name')
800
+ name = c['name'].to_s.strip
801
+ name = labels.values.first.to_s if name.empty?
802
+ next if name.empty?
803
+
804
+ { name: name, labels: labels }
805
+ end.compact
806
+ end
784
807
  Solis::LOGGER.info("Found #{categories.length} categories") unless categories.empty?
785
808
 
786
809
  #TODO: cleanup
@@ -820,7 +843,7 @@ hide empty members
820
843
  end
821
844
 
822
845
  Solis::LOGGER.info('Generating SHACL')
823
- shacl = build_shacl(datas)
846
+ shacl = build_shacl(datas, categories)
824
847
  Solis::LOGGER.info('Generating PLANTUML')
825
848
  plantuml = build_plantuml(datas)
826
849
  Solis::LOGGER.info('Generating SCHEMA')
data/lib/solis/shape.rb CHANGED
@@ -156,12 +156,25 @@ module Solis
156
156
  next if attribute.nil?
157
157
 
158
158
  attribute[:label] = labels_of(graph, property.object)
159
+ attribute[:group] = group_of(graph, attribute[:group]) unless attribute[:group].nil?
159
160
  end
160
161
  end
161
162
 
162
163
  shapes
163
164
  end
164
165
 
166
+ # Turns a sh:PropertyGroup URI into
167
+ # { uri: RDF::URI, name: 'OpleidingEnLoopbaan', label: { nl: 'opleiding_en_loopbaan', en: 'education_and_career' } }
168
+ def group_of(graph, group)
169
+ return group if group.is_a?(Hash)
170
+
171
+ {
172
+ uri: group,
173
+ name: group.to_s.split(%r{[/#]}).last.to_s.gsub(/Group$/, ''),
174
+ label: labels_of(graph, group)
175
+ }
176
+ end
177
+
165
178
  def name_of(graph, subject)
166
179
  graph.query([subject, RDF::Vocab::SHACL.name, nil]).first&.object&.value
167
180
  end
@@ -6,39 +6,119 @@ require_relative 'client/query'
6
6
  module Solis
7
7
  module Store
8
8
  module Sparql
9
+ # Thin wrapper around a process-wide pool of SPARQL::Client instances.
10
+ #
11
+ # Every SPARQL::Client over HTTP owns a Net::HTTP::Persistent keep-alive
12
+ # connection that is only closed by #close or by the garbage collector.
13
+ # Creating clients per request therefore leaks idle connections to the triple
14
+ # store (Virtuoso: "number of licensed connections exceeded"). All instances
15
+ # of this class share one bounded pool per endpoint/graph/read_timeout, so a
16
+ # process never holds more than `pool_size` connections per pool.
17
+ #
18
+ # Pool size: Solis::Options[:sparql_pool_size], else ENV['SOLIS_SPARQL_POOL_SIZE'], else 5.
19
+ # Keep it at or slightly above the number of request threads (e.g. Puma max threads).
20
+ # connection_pool >= 2.5 rebuilds pools in forked children (Puma workers).
9
21
  class Client
10
- def initialize(endpoint, options = {})
11
- @endpoint = endpoint
12
- @graph_name = options[:graph_name] || ''
13
- @read_timeout = options[:read_timeout] || 120
14
- @logger = options[:logger] || Solis::LOGGER
22
+ DEFAULT_POOL_SIZE = 5
23
+ DEFAULT_POOL_TIMEOUT = 160
24
+ DEFAULT_READ_TIMEOUT = 120
25
+ UP_CHECK_INTERVAL = 30
15
26
 
16
- @pool = ConnectionPool.new(size:5, timeout: 160) do
17
- Solis::LOGGER.level = Logger::DEBUG if ConfigFile[:debug]
27
+ @pools = {}
28
+ @up_checks = {}
29
+ @mutex = Mutex.new
18
30
 
19
- if @graph_name
20
- client = SPARQL::Client.new(@endpoint, graph: @graph_name, read_timeout: @read_timeout, logger: @logger)
21
- else
22
- client = SPARQL::Client.new(@endpoint, read_timeout: @read_timeout, logger: @logger)
31
+ class << self
32
+ # Shared pool for an endpoint. graph_name nil means: no graph option on the client.
33
+ def pool_for(endpoint, graph_name: nil, read_timeout: DEFAULT_READ_TIMEOUT, logger: nil)
34
+ key = pool_key(endpoint, graph_name, read_timeout)
35
+ @mutex.synchronize do
36
+ @pools[key] ||= ConnectionPool.new(size: pool_size, timeout: DEFAULT_POOL_TIMEOUT) do
37
+ Solis::LOGGER.level = Logger::DEBUG if ConfigFile[:debug]
38
+ options = { read_timeout: read_timeout, logger: logger || Solis::LOGGER }
39
+ options[:graph] = graph_name unless graph_name.nil?
40
+ SPARQL::Client.new(endpoint, **options)
23
41
  end
42
+ end
43
+ end
44
+
45
+ # Borrow a raw SPARQL::Client from the shared pool. Re-entrant per thread.
46
+ def with(endpoint, graph_name: nil, read_timeout: DEFAULT_READ_TIMEOUT, &block)
47
+ pool_for(endpoint, graph_name: graph_name, read_timeout: read_timeout).with(&block)
48
+ end
49
+
50
+ # Close every pooled connection (e.g. on shutdown or in tests).
51
+ def shutdown_all
52
+ pools = @mutex.synchronize do
53
+ current = @pools.values
54
+ @pools = {}
55
+ @up_checks = {}
56
+ current
57
+ end
58
+ pools.each { |pool| pool.shutdown { |client| client.close rescue nil } }
59
+ end
60
+
61
+ def pool_count
62
+ @mutex.synchronize { @pools.size }
63
+ end
64
+
65
+ def pool_size
66
+ configured = (Solis::Options.instance.get[:sparql_pool_size] rescue nil) || ENV['SOLIS_SPARQL_POOL_SIZE']
67
+ size = configured.to_i
68
+ size > 0 ? size : DEFAULT_POOL_SIZE
69
+ end
70
+
71
+ def cached_up(key)
72
+ @mutex.synchronize do
73
+ check = @up_checks[key]
74
+ check && (monotonic_now - check[:at]) < UP_CHECK_INTERVAL ? check : nil
75
+ end
76
+ end
77
+
78
+ def store_up(key, result)
79
+ @mutex.synchronize { @up_checks[key] = { at: monotonic_now, result: result } }
80
+ end
24
81
 
25
- client
82
+ def clear_up(key)
83
+ @mutex.synchronize { @up_checks.delete(key) }
26
84
  end
85
+
86
+ def pool_key(endpoint, graph_name, read_timeout)
87
+ [endpoint.to_s, graph_name.nil? ? nil : graph_name.to_s, read_timeout.to_i]
88
+ end
89
+
90
+ def monotonic_now
91
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
92
+ end
93
+ end
94
+
95
+ def initialize(endpoint, options = {})
96
+ @endpoint = endpoint
97
+ @graph_name = options[:graph_name] || ''
98
+ @read_timeout = options[:read_timeout] || DEFAULT_READ_TIMEOUT
99
+ @logger = options[:logger] || Solis::LOGGER
100
+ @key = self.class.pool_key(@endpoint, @graph_name, @read_timeout)
101
+ @pool = self.class.pool_for(@endpoint, graph_name: @graph_name, read_timeout: @read_timeout, logger: @logger)
102
+ end
103
+
104
+ def with(&block)
105
+ @pool.with(&block)
27
106
  end
28
107
 
108
+ # The result is cached per pool for UP_CHECK_INTERVAL seconds across all instances,
109
+ # so a request no longer pays an extra ASK round trip.
29
110
  def up?
30
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
- if @up_checked_at && (now - @up_checked_at) < 30
32
- return @up_result
33
- end
34
- @up_result = nil
111
+ cached = self.class.cached_up(@key)
112
+ return cached[:result] if cached
113
+
114
+ result = nil
35
115
  @pool.with do |c|
36
- @up_result = c.query("ASK WHERE { ?s ?p ?o }")
116
+ result = c.query("ASK WHERE { ?s ?p ?o }")
37
117
  end
38
- @up_checked_at = now
39
- @up_result
118
+ self.class.store_up(@key, result)
119
+ result
40
120
  rescue HTTP::Error => e
41
- @up_checked_at = nil
121
+ self.class.clear_up(@key)
42
122
  return false
43
123
  end
44
124
 
@@ -53,4 +133,4 @@ module Solis
53
133
  end
54
134
  end
55
135
  end
56
- end
136
+ end
data/lib/solis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Solis
2
- VERSION = "0.127.0"
2
+ VERSION = "0.129.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.127.0
4
+ version: 0.129.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Celik