From b67edaf3d4f3159e8450a9559966d9a5da667b92 Mon Sep 17 00:00:00 2001 From: Michael Dorf Date: Thu, 16 Jul 2026 14:49:52 -0700 Subject: [PATCH] Batch load SKOS root classes --- Gemfile.lock | 2 +- .../skos/skos_submission_roots.rb | 10 ++---- .../models/ontology_submission.rb | 19 +++++++--- test/models/test_skos_submission.rb | 35 ++++++++++++++++++- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8f0ebcbe..3e9a7246 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,7 +8,7 @@ GIT GIT remote: https://github.com/ncbo/goo.git - revision: cc806c845c79b707e7a10f44e4959caa15939b8b + revision: 752fb6b3fe2ec0ba9417397f3283baeac7a9abfc branch: development specs: goo (0.0.2) diff --git a/lib/ontologies_linked_data/concerns/ontology_submissions/skos/skos_submission_roots.rb b/lib/ontologies_linked_data/concerns/ontology_submissions/skos/skos_submission_roots.rb index 624c792d..f6b8437f 100644 --- a/lib/ontologies_linked_data/concerns/ontology_submissions/skos/skos_submission_roots.rb +++ b/lib/ontologies_linked_data/concerns/ontology_submissions/skos/skos_submission_roots.rb @@ -3,18 +3,12 @@ module Models module SKOS module RootsFetcher - def skos_roots(concept_schemes, page, paged, pagesize) - classes = [] + def skos_root_ids(concept_schemes, page, paged, pagesize) class_ids, count = roots_by_has_top_concept(concept_schemes, page, paged, pagesize) class_ids, count = roots_by_top_concept_of(concept_schemes, page, paged, pagesize) if class_ids.empty? - class_ids.each do |id| - classes << LinkedData::Models::Class.find(id).in(self).disable_rules.first - end - - classes = Goo::Base::Page.new(page, pagesize, count, classes) if paged - classes + [class_ids, count] end private diff --git a/lib/ontologies_linked_data/models/ontology_submission.rb b/lib/ontologies_linked_data/models/ontology_submission.rb index 8ce7e246..f43ba5a8 100644 --- a/lib/ontologies_linked_data/models/ontology_submission.rb +++ b/lib/ontologies_linked_data/models/ontology_submission.rb @@ -711,9 +711,11 @@ def roots(extra_include = [], page = nil, pagesize = nil, concept_schemes: [], c skos = self.skos? classes = [] + class_ids = [] + root_count = 0 if skos - classes = skos_roots(concept_schemes, page, paged, pagesize) + class_ids, root_count = skos_root_ids(concept_schemes, page, paged, pagesize) extra_include += LinkedData::Models::Class.concept_is_in_attributes else self.ontology.bring(:flat) @@ -744,7 +746,12 @@ def roots(extra_include = [], page = nil, pagesize = nil, concept_schemes: [], c end end - where = LinkedData::Models::Class.in(self).models(classes).include(:prefLabel, :definition, :synonym, :obsolete) + where = if skos + LinkedData::Models::Class.in(self).ids(class_ids) unless class_ids.empty? + else + LinkedData::Models::Class.in(self).models(classes) + end + where.include(:prefLabel, :definition, :synonym, :obsolete) if where if extra_include %i[prefLabel definition synonym obsolete childrenCount].each do |x| @@ -767,9 +774,13 @@ def roots(extra_include = [], page = nil, pagesize = nil, concept_schemes: [], c load_children = [:children] end - where.include(extra_include) if extra_include.length > 0 + where.include(extra_include) if where && extra_include.length > 0 end - where.all + if where + loaded_classes = where.all + classes = loaded_classes if skos + end + classes = Goo::Base::Page.new(page, pagesize, root_count, classes) if skos && paged LinkedData::Models::Class.partially_load_children(classes, 99, self) if load_children.length > 0 diff --git a/test/models/test_skos_submission.rb b/test/models/test_skos_submission.rb index 5ab2e16a..601c9565 100644 --- a/test/models/test_skos_submission.rb +++ b/test/models/test_skos_submission.rb @@ -104,6 +104,40 @@ def test_roots_of_multiple_scheme 'http://www.ebi.ac.uk/efo/EFO_0000324'].sort end + def test_roots_batch_loads_skos_classes + sub = before_suite + concept_schemes = ['http://www.ebi.ac.uk/efo/skos/EFO_GWAS_view_2', + 'http://www.ebi.ac.uk/efo/skos/EFO_GWAS_view'] + + roots = nil + queries = count_sparql_queries do + roots = sub.roots(concept_schemes: concept_schemes) + end + + assert_equal 6, roots.length + assert_operator queries, :<=, 3, + "loading #{roots.length} SKOS roots issued #{queries} SPARQL queries -- " \ + 'looks like per-root class hydration' + end + + def test_roots_batch_load_preserves_skos_pagination + sub = before_suite + concept_scheme = 'http://www.ebi.ac.uk/efo/skos/EFO_GWAS_view' + + roots = sub.roots([], 1, 2, concept_schemes: [concept_scheme]) + + assert_instance_of Goo::Base::Page, roots + assert_equal 2, roots.length + assert_equal 2, roots.total_pages + end + + def test_roots_with_no_skos_ids_returns_empty + sub = before_suite + roots = sub.roots(concept_schemes: ['http://example.org/missing-scheme']) + + assert_empty roots + end + def test_roots_of_scheme_collection sub = before_suite @@ -241,4 +275,3 @@ def test_concept_schemes_query_memoized_per_submission 'submission instance -- per-submission memoization (#302/#303) regressed' end end -