Module: Gitlab::Database::Count

Defined in:
lib/gitlab/database/count.rb,
lib/gitlab/database/count/exact_count_strategy.rb,
lib/gitlab/database/count/reltuples_count_strategy.rb,
lib/gitlab/database/count/tablesample_count_strategy.rb

Defined Under Namespace

Classes: ExactCountStrategy, PgClass, ReltuplesCountStrategy, TablesampleCountStrategy

Constant Summary collapse

CONNECTION_ERRORS =
if defined?(PG)
  [
    ActionView::Template::Error,
    ActiveRecord::StatementInvalid,
    PG::Error
  ].freeze
else
  [
    ActionView::Template::Error,
    ActiveRecord::StatementInvalid
  ].freeze
end

Class Method Summary collapse

Class Method Details

.approximate_counts(models, strategies: [TablesampleCountStrategy, ReltuplesCountStrategy, ExactCountStrategy]) ⇒ Hash

Takes in an array of models and returns a Hash for the approximate counts for them.

Various count strategies can be specified that are executed in sequence until all tables have an approximate count attached or we run out of strategies.

Note that not all strategies are available on all supported RDBMS.

Parameters:

  • (Array)

Returns:

  • (Hash)

    of Model -> count mapping


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/database/count.rb', line 38

def self.approximate_counts(models, strategies: [TablesampleCountStrategy, ReltuplesCountStrategy, ExactCountStrategy])
  strategies.each_with_object({}) do |strategy, counts_by_model|
    models_with_missing_counts = models - counts_by_model.keys

    break counts_by_model if models_with_missing_counts.empty?

    counts = strategy.new(models_with_missing_counts).count

    counts.each do |model, count|
      counts_by_model[model] = count
    end
  end
end