Commit 8c0161d0 authored by Alex Kalderimis's avatar Alex Kalderimis

Better memoization and resetting

Also includes more namespacing for better readibility and determinism.

The presence of a diff_refs method on Version allows us to have better
code in Design that does not violate the laws of Demeter so much.
parent b02e0d5d
...@@ -108,7 +108,7 @@ module DesignManagement ...@@ -108,7 +108,7 @@ module DesignManagement
def diff_refs def diff_refs
strong_memoize(:diff_refs) do strong_memoize(:diff_refs) do
head_version.presence && repository.commit(head_version.sha).diff_refs head_version.try(:diff_refs)
end end
end end
...@@ -158,7 +158,7 @@ module DesignManagement ...@@ -158,7 +158,7 @@ module DesignManagement
def user_notes_count_service def user_notes_count_service
strong_memoize(:user_notes_count_service) do strong_memoize(:user_notes_count_service) do
DesignManagement::DesignUserNotesCountService.new(self) # rubocop: disable CodeReuse/ServiceClass ::DesignManagement::DesignUserNotesCountService.new(self) # rubocop: disable CodeReuse/ServiceClass
end end
end end
end end
......
...@@ -4,6 +4,7 @@ module DesignManagement ...@@ -4,6 +4,7 @@ module DesignManagement
class Version < ApplicationRecord class Version < ApplicationRecord
include Importable include Importable
include ShaAttribute include ShaAttribute
include Gitlab::Utils::StrongMemoize
NotSameIssue = Class.new(StandardError) NotSameIssue = Class.new(StandardError)
...@@ -50,7 +51,7 @@ module DesignManagement ...@@ -50,7 +51,7 @@ module DesignManagement
delegate :project, to: :issue delegate :project, to: :issue
scope :for_designs, -> (designs) do scope :for_designs, -> (designs) do
where(id: Action.where(design_id: designs).select(:version_id)).distinct where(id: ::DesignManagement::Action.where(design_id: designs).select(:version_id)).distinct
end end
scope :earlier_or_equal_to, -> (version) { where('id <= ?', version) } scope :earlier_or_equal_to, -> (version) { where('id <= ?', version) }
scope :ordered, -> { order(id: :desc) } scope :ordered, -> { order(id: :desc) }
...@@ -81,7 +82,7 @@ module DesignManagement ...@@ -81,7 +82,7 @@ module DesignManagement
rows = design_actions.map { |action| action.row_attrs(version) } rows = design_actions.map { |action| action.row_attrs(version) }
Gitlab::Database.bulk_insert(Action.table_name, rows) Gitlab::Database.bulk_insert(::DesignManagement::Action.table_name, rows)
version.designs.reset version.designs.reset
version.validate! version.validate!
design_actions.each(&:performed) design_actions.each(&:performed)
...@@ -103,6 +104,15 @@ module DesignManagement ...@@ -103,6 +104,15 @@ module DesignManagement
super || (commit_author if persisted?) super || (commit_author if persisted?)
end end
def diff_refs
strong_memoize(:diff_refs) { commit.try(:diff_refs) }
end
def reset
%i[diff_refs commit].each { |k| clear_memoization(k) }
super
end
private private
def commit_author def commit_author
...@@ -110,7 +120,7 @@ module DesignManagement ...@@ -110,7 +120,7 @@ module DesignManagement
end end
def commit def commit
@commit ||= issue.project.design_repository.commit(sha) strong_memoize(:commit) { issue.project.design_repository.commit(sha) }
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment