Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
167fd713
Commit
167fd713
authored
Oct 02, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Always preload all elements in a hierarchy to avoid extra queries.
parent
ef043063
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
5 deletions
+31
-5
app/models/concerns/group_descendant.rb
app/models/concerns/group_descendant.rb
+12
-3
spec/models/concerns/group_descendant_spec.rb
spec/models/concerns/group_descendant_spec.rb
+19
-2
No files found.
app/models/concerns/group_descendant.rb
View file @
167fd713
module
GroupDescendant
module
GroupDescendant
def
hierarchy
(
hierarchy_top
=
nil
,
preloaded
=
[])
def
hierarchy
(
hierarchy_top
=
nil
,
preloaded
=
nil
)
preloaded
||=
ancestors_upto
(
hierarchy_top
)
expand_hierarchy_for_child
(
self
,
self
,
hierarchy_top
,
preloaded
)
expand_hierarchy_for_child
(
self
,
self
,
hierarchy_top
,
preloaded
)
end
end
...
@@ -24,12 +25,20 @@ module GroupDescendant
...
@@ -24,12 +25,20 @@ module GroupDescendant
private
private
def
expand_hierarchy_for_child
(
child
,
hierarchy
,
hierarchy_top
,
preloaded
=
[])
def
ancestors_upto
(
hierarchy_top
=
nil
)
if
hierarchy_top
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
hierarchy_top
)).
base_and_descendants
else
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
self
)).
all_groups
end
end
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
=
preloaded
.
detect
{
|
possible_parent
|
possible_parent
.
is_a?
(
Group
)
&&
possible_parent
.
id
==
child
.
parent_id
}
parent
||=
child
.
parent
parent
||=
child
.
parent
if
parent
.
nil?
&&
hierarchy_top
.
present?
if
parent
.
nil?
&&
hierarchy_top
.
present?
raise
ArgumentError
.
new
(
'specified
base
is not part of the tree'
)
raise
ArgumentError
.
new
(
'specified
top
is not part of the tree'
)
end
end
if
parent
&&
parent
!=
hierarchy_top
if
parent
&&
parent
!=
hierarchy_top
...
...
spec/models/concerns/group_descendant_spec.rb
View file @
167fd713
...
@@ -7,6 +7,23 @@ describe GroupDescendant, :nested_groups do
...
@@ -7,6 +7,23 @@ describe GroupDescendant, :nested_groups do
context
'for a group'
do
context
'for a group'
do
describe
'#hierarchy'
do
describe
'#hierarchy'
do
it
'only queries once for the ancestors'
do
# make sure the subsub_group does not have anything cached
test_group
=
create
(
:group
,
parent:
subsub_group
).
reload
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
test_group
.
hierarchy
}.
count
expect
(
query_count
).
to
eq
(
1
)
end
it
'only queries once for the ancestors when a top is given'
do
test_group
=
create
(
:group
,
parent:
subsub_group
).
reload
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
test_group
.
hierarchy
(
subgroup
)
}.
count
expect
(
query_count
).
to
eq
(
1
)
end
it
'builds a hierarchy for a group'
do
it
'builds a hierarchy for a group'
do
expected_hierarchy
=
{
parent
=>
{
subgroup
=>
subsub_group
}
}
expected_hierarchy
=
{
parent
=>
{
subgroup
=>
subsub_group
}
}
...
@@ -20,7 +37,7 @@ describe GroupDescendant, :nested_groups do
...
@@ -20,7 +37,7 @@ describe GroupDescendant, :nested_groups do
end
end
it
'raises an error if specifying a base that is not part of the tree'
do
it
'raises an error if specifying a base that is not part of the tree'
do
expect
{
subsub_group
.
hierarchy
(
double
)
}.
to
raise_error
(
'specified base
is not part of the tree'
)
expect
{
subsub_group
.
hierarchy
(
build_stubbed
(
:group
))
}.
to
raise_error
(
'specified top
is not part of the tree'
)
end
end
end
end
...
@@ -77,7 +94,7 @@ describe GroupDescendant, :nested_groups do
...
@@ -77,7 +94,7 @@ describe GroupDescendant, :nested_groups do
end
end
it
'raises an error if specifying a base that is not part of the tree'
do
it
'raises an error if specifying a base that is not part of the tree'
do
expect
{
project
.
hierarchy
(
double
)
}.
to
raise_error
(
'specified base
is not part of the tree'
)
expect
{
project
.
hierarchy
(
build_stubbed
(
:group
))
}.
to
raise_error
(
'specified top
is not part of the tree'
)
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment