Flexible approximate counts with fallback strategies.
This commit is contained in:
parent
b6a530c9b1
commit
c5fb468255
2 changed files with 31 additions and 20 deletions
|
@ -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)
|
||||
counts.each do |model, count|
|
||||
counts_by_model[model] = count
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
missing_models = models - counts_by_model.keys
|
||||
|
||||
ExactCountStrategy.new(missing_models).count.each do |model, count|
|
||||
counts_by_model[model] = count
|
||||
end
|
||||
|
||||
counts_by_model
|
||||
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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue