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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
45af0d65
Commit
45af0d65
authored
Nov 04, 2020
by
Alex Kalderimis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Increase batchloader efficiency
parent
17bc419a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
13 deletions
+21
-13
lib/gitlab/graphql/loaders/batch_model_loader.rb
lib/gitlab/graphql/loaders/batch_model_loader.rb
+4
-7
spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb
spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb
+17
-6
No files found.
lib/gitlab/graphql/loaders/batch_model_loader.rb
View file @
45af0d65
...
@@ -12,14 +12,11 @@ module Gitlab
...
@@ -12,14 +12,11 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def
find
def
find
BatchLoader
::
GraphQL
.
for
({
model:
model_class
,
id:
model_id
.
to_i
}).
batch
do
|
loader_info
,
loader
|
BatchLoader
::
GraphQL
.
for
(
model_id
.
to_i
).
batch
(
key:
model_class
)
do
|
ids
,
loader
,
args
|
per_model
=
loader_info
.
group_by
{
|
info
|
info
[
:model
]
}
model
=
args
[
:key
]
per_model
.
each
do
|
model
,
info
|
results
=
model
.
where
(
id:
ids
)
ids
=
info
.
map
{
|
i
|
i
[
:id
]
}
results
=
model
.
where
(
id:
ids
)
results
.
each
{
|
record
|
loader
.
call
({
model:
model
,
id:
record
.
id
},
record
)
}
results
.
each
{
|
record
|
loader
.
call
(
record
.
id
,
record
)
}
end
end
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: enable CodeReuse/ActiveRecord
...
...
spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb
View file @
45af0d65
...
@@ -4,8 +4,9 @@ require 'spec_helper'
...
@@ -4,8 +4,9 @@ require 'spec_helper'
RSpec
.
describe
Gitlab
::
Graphql
::
Loaders
::
BatchModelLoader
do
RSpec
.
describe
Gitlab
::
Graphql
::
Loaders
::
BatchModelLoader
do
describe
'#find'
do
describe
'#find'
do
let
(
:issue
)
{
create
(
:issue
)
}
let_it_be
(
:issue
)
{
create
(
:issue
)
}
let
(
:user
)
{
create
(
:user
)
}
let_it_be
(
:other_user
)
{
create
(
:user
)
}
let_it_be
(
:user
)
{
create
(
:user
)
}
it
'finds a model by id'
do
it
'finds a model by id'
do
issue_result
=
described_class
.
new
(
Issue
,
issue
.
id
).
find
issue_result
=
described_class
.
new
(
Issue
,
issue
.
id
).
find
...
@@ -16,15 +17,25 @@ RSpec.describe Gitlab::Graphql::Loaders::BatchModelLoader do
...
@@ -16,15 +17,25 @@ RSpec.describe Gitlab::Graphql::Loaders::BatchModelLoader do
end
end
it
'only queries once per model'
do
it
'only queries once per model'
do
other_user
=
create
(
:user
)
user
issue
expect
do
expect
do
[
described_class
.
new
(
User
,
other_user
.
id
).
find
,
[
described_class
.
new
(
User
,
other_user
.
id
).
find
,
described_class
.
new
(
User
,
user
.
id
).
find
,
described_class
.
new
(
User
,
user
.
id
).
find
,
described_class
.
new
(
Issue
,
issue
.
id
).
find
].
map
(
&
:sync
)
described_class
.
new
(
Issue
,
issue
.
id
).
find
].
map
(
&
:sync
)
end
.
not_to
exceed_query_limit
(
2
)
end
.
not_to
exceed_query_limit
(
2
)
end
end
it
'does not force values unnecessarily'
do
expect
do
a
=
described_class
.
new
(
User
,
user
.
id
).
find
b
=
described_class
.
new
(
Issue
,
issue
.
id
).
find
b
.
sync
c
=
described_class
.
new
(
User
,
other_user
.
id
).
find
a
.
sync
c
.
sync
end
.
not_to
exceed_query_limit
(
2
)
end
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