summaryrefslogtreecommitdiff
path: root/app/models/concerns/group_descendant.rb
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-09-26 11:22:52 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:49:42 +0200
commitac0b104ae4968adaed7b94db76c0ac86badb6d6b (patch)
treebe0737e1d9971911bedc48bb05d1fe84e46ac93a /app/models/concerns/group_descendant.rb
parentcd85c22faa7092edabf252fa157125ea571ed054 (diff)
downloadgitlab-ce-ac0b104ae4968adaed7b94db76c0ac86badb6d6b.tar.gz
Minimize the number of queries by preloading counts and ancestors
By preloading the count of members, projects and subgroups of a group, we don't need to query them later. We also preload the entire hierarchy for a search result and include the counts so we don't need to query for them again
Diffstat (limited to 'app/models/concerns/group_descendant.rb')
-rw-r--r--app/models/concerns/group_descendant.rb21
1 files changed, 12 insertions, 9 deletions
diff --git a/app/models/concerns/group_descendant.rb b/app/models/concerns/group_descendant.rb
index 528fcaa1917..f850f71d661 100644
--- a/app/models/concerns/group_descendant.rb
+++ b/app/models/concerns/group_descendant.rb
@@ -1,16 +1,19 @@
module GroupDescendant
- def hierarchy(hierarchy_top = nil)
- expand_hierarchy_for_child(self, self, hierarchy_top)
+ def hierarchy(hierarchy_top = nil, preloaded = [])
+ expand_hierarchy_for_child(self, self, hierarchy_top, preloaded)
end
- def expand_hierarchy_for_child(child, hierarchy, hierarchy_top)
- if child.parent.nil? && hierarchy_top.present?
+ def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded = [])
+ parent = preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id }
+ parent ||= child.parent
+
+ if parent.nil? && hierarchy_top.present?
raise ArgumentError.new('specified base is not part of the tree')
end
- if child.parent && child.parent != hierarchy_top
- expand_hierarchy_for_child(child.parent,
- { child.parent => hierarchy },
+ if parent && parent != hierarchy_top
+ expand_hierarchy_for_child(parent,
+ { parent => hierarchy },
hierarchy_top)
else
hierarchy
@@ -30,10 +33,10 @@ module GroupDescendant
end
first_descendant, *other_descendants = descendants
- merged = first_descendant.hierarchy(hierarchy_top)
+ merged = first_descendant.hierarchy(hierarchy_top, descendants)
other_descendants.each do |descendant|
- next_descendant = descendant.hierarchy(hierarchy_top)
+ next_descendant = descendant.hierarchy(hierarchy_top, descendants)
merged = merge_hash_tree(merged, next_descendant)
end