From c5fb4682556d955ecbcbd589ffb18a95c7fbe0a1 Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Sun, 28 Oct 2018 17:07:05 +0100 Subject: Flexible approximate counts with fallback strategies. --- lib/gitlab/database/count.rb | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/database/count.rb b/lib/gitlab/database/count.rb index 55f0cbd18be..beb5a8d3556 100644 --- a/lib/gitlab/database/count.rb +++ b/lib/gitlab/database/count.rb @@ -25,21 +25,17 @@ module Gitlab # # @param [Array] # @return [Hash] of Model -> count mapping - def self.approximate_counts(models) - counts_by_model = {} - - 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_by_model[model] = count + 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 + + counts.each do |model, count| + counts_by_model[model] = count + end + end 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 -- cgit v1.2.1