diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-10-13 19:23:49 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-10-13 19:23:49 +0300 |
commit | 251e13666d04a1c8427401962e3e171e569d9088 (patch) | |
tree | d921f0594ab21a1bca23c7cee83e8336304d8242 /lib/gitlab/markdown | |
parent | 5dd77358f6293249494bf390140843f63dfd220a (diff) |
Efficiently load multiple references of one type.
Diffstat (limited to 'lib/gitlab/markdown')
-rw-r--r-- | lib/gitlab/markdown/issue_reference_filter.rb | 5 | ||||
-rw-r--r-- | lib/gitlab/markdown/label_reference_filter.rb | 5 | ||||
-rw-r--r-- | lib/gitlab/markdown/merge_request_reference_filter.rb | 5 | ||||
-rw-r--r-- | lib/gitlab/markdown/reference_filter.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/markdown/reference_gatherer_filter.rb | 29 | ||||
-rw-r--r-- | lib/gitlab/markdown/snippet_reference_filter.rb | 5 | ||||
-rw-r--r-- | lib/gitlab/markdown/user_reference_filter.rb | 5 |
7 files changed, 33 insertions, 23 deletions
diff --git a/lib/gitlab/markdown/issue_reference_filter.rb b/lib/gitlab/markdown/issue_reference_filter.rb index cd2a9b75680..481d282f7b1 100644 --- a/lib/gitlab/markdown/issue_reference_filter.rb +++ b/lib/gitlab/markdown/issue_reference_filter.rb @@ -28,10 +28,7 @@ module Gitlab end def self.referenced_by(node) - issue = Issue.find(node.attr("data-issue")) rescue nil - return unless issue - - { issue: issue } + { issue: LazyReference.new(Issue, node.attr("data-issue")) } end def call diff --git a/lib/gitlab/markdown/label_reference_filter.rb b/lib/gitlab/markdown/label_reference_filter.rb index 59568ab531f..618acb7a578 100644 --- a/lib/gitlab/markdown/label_reference_filter.rb +++ b/lib/gitlab/markdown/label_reference_filter.rb @@ -23,10 +23,7 @@ module Gitlab end def self.referenced_by(node) - label = Label.find(node.attr("data-label")) rescue nil - return unless label - - { label: label } + { label: LazyReference.new(Label, node.attr("data-label")) } end def call diff --git a/lib/gitlab/markdown/merge_request_reference_filter.rb b/lib/gitlab/markdown/merge_request_reference_filter.rb index 440574e574b..5bc63269808 100644 --- a/lib/gitlab/markdown/merge_request_reference_filter.rb +++ b/lib/gitlab/markdown/merge_request_reference_filter.rb @@ -28,10 +28,7 @@ module Gitlab end def self.referenced_by(node) - merge_request = MergeRequest.find(node.attr("data-merge-request")) rescue nil - return unless merge_request - - { merge_request: merge_request } + { merge_request: LazyReference.new(MergeRequest, node.attr("data-merge-request")) } end def call diff --git a/lib/gitlab/markdown/reference_filter.rb b/lib/gitlab/markdown/reference_filter.rb index 0ea966c744b..0ae0d93f70d 100644 --- a/lib/gitlab/markdown/reference_filter.rb +++ b/lib/gitlab/markdown/reference_filter.rb @@ -12,6 +12,8 @@ module Gitlab # :project (required) - Current project, ignored if reference is cross-project. # :only_path - Generate path-only links. class ReferenceFilter < HTML::Pipeline::Filter + LazyReference = Struct.new(:klass, :ids) + def self.user_can_reference?(user, node, context) if node.has_attribute?('data-project') project_id = node.attr('data-project').to_i diff --git a/lib/gitlab/markdown/reference_gatherer_filter.rb b/lib/gitlab/markdown/reference_gatherer_filter.rb index 89acb312cd5..cf9a2303db8 100644 --- a/lib/gitlab/markdown/reference_gatherer_filter.rb +++ b/lib/gitlab/markdown/reference_gatherer_filter.rb @@ -12,7 +12,8 @@ module Gitlab def initialize(*) super - result[:references] ||= Hash.new { |hash, type| hash[type] = [] } + result[:lazy_references] ||= Hash.new { |hash, type| hash[type] = [] } + result[:references] ||= Hash.new { |hash, type| hash[type] = [] } end def call @@ -20,6 +21,8 @@ module Gitlab gather_references(node) end + load_lazy_references + doc end @@ -35,9 +38,29 @@ module Gitlab references = reference_filter.referenced_by(node) return unless references - + references.each do |type, values| - result[:references][type].push(*values) + Array.wrap(values).each do |value| + refs = + if value.is_a?(ReferenceFilter::LazyReference) + result[:lazy_references] + else + result[:references] + end + + refs[type] << value + end + end + end + + # Will load all references of one type using one query. + def load_lazy_references + result[:lazy_references].each do |type, refs| + refs.group_by(&:klass).each do |klass, refs| + ids = refs.map(&:ids).flatten + values = klass.find(ids) + result[:references][type].push(*values) + end end end diff --git a/lib/gitlab/markdown/snippet_reference_filter.rb b/lib/gitlab/markdown/snippet_reference_filter.rb index a7396e96529..f783f951711 100644 --- a/lib/gitlab/markdown/snippet_reference_filter.rb +++ b/lib/gitlab/markdown/snippet_reference_filter.rb @@ -28,10 +28,7 @@ module Gitlab end def self.referenced_by(node) - snippet = Snippet.find(node.attr("data-snippet")) rescue nil - return unless snippet - - { snippet: snippet } + { snippet: LazyReference.new(Snippet, node.attr("data-snippet")) } end def call diff --git a/lib/gitlab/markdown/user_reference_filter.rb b/lib/gitlab/markdown/user_reference_filter.rb index 4567e983692..2a594e1662e 100644 --- a/lib/gitlab/markdown/user_reference_filter.rb +++ b/lib/gitlab/markdown/user_reference_filter.rb @@ -30,10 +30,7 @@ module Gitlab { user: group.users } elsif node.has_attribute?('data-user') - user = User.find(node.attr('data-user')) rescue nil - return unless user - - { user: user } + { user: LazyReference.new(User, node.attr('data-user')) } elsif node.has_attribute?('data-project') project = Project.find(node.attr('data-project')) rescue nil return unless project |