Skip to content

Commit 643c6a3

Browse files
committed
[E] Adjust text section node link view
Join against text_section_id to take advantage of optimized index for building the associations.
1 parent cb16b53 commit 643c6a3

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

api/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ gem "citeproc-ruby", "~> 1.1.0"
2727
gem "classy_enum", "~> 4.0.0"
2828
gem "cleanroom"
2929
gem "closure_tree", "~> 7.0"
30+
# Until Rails 7.1
31+
gem "composite_primary_keys", "~> 14.0.10"
3032
gem "crass", "~> 1.0.5"
3133
gem "csl-styles", "~> 1.0"
3234
gem "cssbeautify"

api/Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ GEM
169169
activerecord (>= 4.2.10)
170170
with_advisory_lock (>= 4.0.0)
171171
coderay (1.1.3)
172+
composite_primary_keys (14.0.10)
173+
activerecord (~> 7.0.2)
172174
concurrent-ruby (1.2.2)
173175
connection_pool (2.4.1)
174176
content_disposition (1.0.0)
@@ -880,6 +882,7 @@ DEPENDENCIES
880882
classy_enum (~> 4.0.0)
881883
cleanroom
882884
closure_tree (~> 7.0)
885+
composite_primary_keys (~> 14.0.10)
883886
crass (~> 1.0.5)
884887
csl-styles (~> 1.0)
885888
css_parser (~> 1.0)

api/app/models/text_section_node.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ class TextSectionNode < ApplicationRecord
4141

4242
scope :sans_search_indexed, -> { where(search_indexed: false) }
4343

44-
has_many_readonly :text_section_node_links, -> { in_order }, inverse_of: :parent, foreign_key: :parent_id
45-
has_many_readonly :ancestor_links, -> { in_reverse_order }, class_name: "TextSectionNodeLink", inverse_of: :child, foreign_key: :child_id
46-
47-
has_one_readonly :text_section_node_derivation, inverse_of: :text_section_node
44+
has_many_readonly :text_section_node_links, -> { in_order }, inverse_of: :parent, primary_key: %i[text_section_id id], foreign_key: %i[text_section_id parent_id]
45+
has_many_readonly :ancestor_links, -> { in_reverse_order }, class_name: "TextSectionNodeLink", inverse_of: :child, primary_key: %i[text_section_id id], foreign_key: %i[text_section_id child_id]
4846

4947
has_many :parents, -> { terminal }, through: :ancestor_links, source: :parent
5048
has_many :children, through: :text_section_node_links, source: :child
@@ -58,8 +56,6 @@ class TextSectionNode < ApplicationRecord
5856
},
5957
}
6058

61-
delegate :to_apply, to: :text_section_node_contained_content, allow_nil: true, prefix: :contained_content
62-
6359
def node
6460
node_extra.with_indifferent_access.merge(
6561
slice(:node_type, :tag, :node_uuid, :text_digest, :content).compact

api/app/models/text_section_node_link.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
class TextSectionNodeLink < ApplicationRecord
55
include View
66

7-
belongs_to_readonly :parent, class_name: "TextSectionNode", inverse_of: :text_section_node_links
8-
belongs_to_readonly :child, class_name: "TextSectionNode", inverse_of: :ancestor_links
7+
belongs_to_readonly :parent, class_name: "TextSectionNode", inverse_of: :text_section_node_links, primary_key: %i[text_section_id id], foreign_key: %i[text_section_id parent_id]
8+
belongs_to_readonly :child, class_name: "TextSectionNode", inverse_of: :ancestor_links, primary_key: %i[text_section_id id], foreign_key: %i[text_section_id child_id]
99

1010
scope :in_order, -> { order(child_depth: :asc, child_node_index: :asc) }
1111
scope :in_reverse_order, -> { order(parent_depth: :desc, parent_node_index: :desc) }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class UpdateTextSectionNodeLinksToVersion2 < ActiveRecord::Migration[7.0]
2+
def change
3+
4+
update_view :text_section_node_links, version: 2, revert_to_version: 1
5+
end
6+
end

api/db/structure.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,14 +2977,15 @@ CREATE VIEW public.text_section_aggregations AS
29772977
--
29782978

29792979
CREATE VIEW public.text_section_node_links AS
2980-
SELECT parent.id AS parent_id,
2980+
SELECT parent.text_section_id,
2981+
parent.id AS parent_id,
29812982
child.id AS child_id,
29822983
parent.depth AS parent_depth,
29832984
parent.node_index AS parent_node_index,
29842985
child.depth AS child_depth,
29852986
child.node_index AS child_node_index
29862987
FROM (public.text_section_nodes parent
2987-
JOIN public.text_section_nodes child ON (((parent.node_path OPERATOR(public.@>) child.node_path) AND (child.depth > parent.depth))));
2988+
JOIN public.text_section_nodes child ON (((parent.text_section_id = child.text_section_id) AND (parent.node_path OPERATOR(public.@>) child.node_path) AND (child.depth > parent.depth))));
29882989

29892990

29902991
--
@@ -7840,6 +7841,7 @@ INSERT INTO "schema_migrations" (version) VALUES
78407841
('20251103175506'),
78417842
('20251103175949'),
78427843
('20251103180007'),
7843-
('20251105165521');
7844+
('20251105165521'),
7845+
('20251121202033');
78447846

78457847

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SELECT
2+
parent.text_section_id,
3+
parent.id AS parent_id,
4+
child.id AS child_id,
5+
parent.depth AS parent_depth,
6+
parent.node_index AS parent_node_index,
7+
child.depth AS child_depth,
8+
child.node_index AS child_node_index
9+
FROM text_section_nodes parent
10+
INNER JOIN text_section_nodes child ON parent.text_section_id = child.text_section_id AND parent.node_path @> child.node_path AND child.depth > parent.depth

0 commit comments

Comments
 (0)