debian-mirror-gitlab/lib/gitlab/counters/legacy_counter.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module Gitlab
module Counters
# This class is a wrapper over ActiveRecord counter
# for attributes that have not adopted Redis-backed BufferedCounter.
class LegacyCounter
def initialize(counter_record, attribute)
@counter_record = counter_record
@attribute = attribute
@current_value = counter_record.method(attribute).call
end
2023-03-17 16:20:25 +05:30
def increment(increment)
updated = update_counter_record_attribute(increment.amount)
2023-03-04 22:38:38 +05:30
if updated == 1
counter_record.execute_after_commit_callbacks
2023-03-17 16:20:25 +05:30
@current_value += increment.amount
2023-03-04 22:38:38 +05:30
end
@current_value
end
2023-03-17 16:20:25 +05:30
def bulk_increment(increments)
total_increment = increments.sum(&:amount)
updated = update_counter_record_attribute(total_increment)
if updated == 1
counter_record.execute_after_commit_callbacks
@current_value += total_increment
end
@current_value
2023-03-04 22:38:38 +05:30
end
private
2023-03-17 16:20:25 +05:30
def update_counter_record_attribute(amount)
counter_record.class.update_counters(counter_record.id, { attribute => amount })
end
2023-03-04 22:38:38 +05:30
attr_reader :counter_record, :attribute
end
end
end