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
c5fb4682
Commit
c5fb4682
authored
Oct 28, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Flexible approximate counts with fallback strategies.
parent
b6a530c9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
21 deletions
+32
-21
lib/gitlab/database/count.rb
lib/gitlab/database/count.rb
+18
-14
spec/lib/gitlab/database/count_spec.rb
spec/lib/gitlab/database/count_spec.rb
+14
-7
No files found.
lib/gitlab/database/count.rb
View file @
c5fb4682
...
...
@@ -25,21 +25,17 @@ module Gitlab
#
# @param [Array]
# @return [Hash] of Model -> count mapping
def
self
.
approximate_counts
(
models
)
counts_by_model
=
{}
def
self
.
approximate_counts
(
models
,
strategies
=
[
ReltuplesCountStrategy
,
ExactCountStrategy
])
strategies
.
each_with_object
({})
do
|
strategy
,
counts_by_model
|
if
strategy
.
enabled?
models_with_missing_counts
=
models
-
counts_by_model
.
keys
counts
=
strategy
.
new
(
models_with_missing_counts
).
count
if
Gitlab
::
Database
.
postgresql?
#counts_by_model = ReltuplesCountStrategy.new(models).count
counts_by_model
=
reltuples_from_recently_updated
(
models
)
end
missing_models
=
models
-
counts_by_model
.
keys
ExactCountStrategy
.
new
(
missing_models
).
count
.
each
do
|
model
,
count
|
counts
.
each
do
|
model
,
count
|
counts_by_model
[
model
]
=
count
end
counts_by_model
end
end
end
# Returns a hash of the table names that have recently updated tuples.
...
...
@@ -61,6 +57,10 @@ module Gitlab
data
[
model
]
=
model
.
count
end
end
def
self
.
enabled?
true
end
end
class
ReltuplesCountStrategy
...
...
@@ -92,6 +92,10 @@ module Gitlab
{}
end
def
self
.
enabled?
Gitlab
::
Database
.
postgresql?
end
private
def
table_names
...
...
spec/lib/gitlab/database/count_spec.rb
View file @
c5fb4682
...
...
@@ -23,10 +23,14 @@ describe Gitlab::Database::Count do
end
context
'with PostgreSQL'
,
:postgresql
do
let
(
:reltuples_strategy
)
{
double
(
'reltuples_strategy'
,
count:
{})
}
before
do
allow
(
Gitlab
::
Database
::
Count
::
ReltuplesCountStrategy
).
to
receive
(
:new
).
with
(
models
).
and_return
(
reltuples_strategy
)
end
describe
'when reltuples have not been updated'
do
it
'counts all models the normal way'
do
expect
(
described_class
).
to
receive
(
:reltuples_from_recently_updated
).
with
(
models
).
and_return
({})
expect
(
Project
).
to
receive
(
:count
).
and_call_original
expect
(
Identity
).
to
receive
(
:count
).
and_call_original
expect
(
described_class
.
approximate_counts
(
models
)).
to
eq
({
Project
=>
3
,
Identity
=>
1
})
...
...
@@ -45,7 +49,7 @@ describe Gitlab::Database::Count do
describe
'when some reltuples have been updated'
do
it
'counts projects in the fast way'
do
expect
(
described_class
).
to
receive
(
:reltuples_from_recently_updated
).
with
(
models
).
and_return
({
Project
=>
3
})
expect
(
reltuples_strategy
).
to
receive
(
:count
).
and_return
({
Project
=>
3
})
expect
(
Project
).
not_to
receive
(
:count
).
and_call_original
expect
(
Identity
).
to
receive
(
:count
).
and_call_original
...
...
@@ -53,13 +57,16 @@ describe Gitlab::Database::Count do
end
end
# TODO: This covers two parts: reltuple strategy itself and the fallback
# TODO: Add spec that covers strategy details for reltuple strategy
describe
'when all reltuples have been updated'
do
before
do
ActiveRecord
::
Base
.
connection
.
execute
(
'ANALYZE projects'
)
ActiveRecord
::
Base
.
connection
.
execute
(
'ANALYZE identities'
)
end
#
before do
#
ActiveRecord::Base.connection.execute('ANALYZE projects')
#
ActiveRecord::Base.connection.execute('ANALYZE identities')
#
end
it
'counts models with the standard way'
do
allow
(
reltuples_strategy
).
to
receive
(
:count
).
and_return
({
Project
=>
3
,
Identity
=>
1
})
expect
(
Project
).
not_to
receive
(
:count
)
expect
(
Identity
).
not_to
receive
(
:count
)
...
...
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