summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-10-13 22:53:05 +0200
committerDouwe Maan <douwe@gitlab.com>2015-10-13 22:53:05 +0200
commit93fcddd7a7ce4ed259794a4511ae04035ae33be2 (patch)
treee866959ff93d0e473e993b4bd727f92aad0fac7c /lib
parent251e13666d04a1c8427401962e3e171e569d9088 (diff)
downloadgitlab-ce-93fcddd7a7ce4ed259794a4511ae04035ae33be2.tar.gz
Allow ReferenceExtractor to efficiently load references from multiple texts at once
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/markdown/reference_filter.rb9
-rw-r--r--lib/gitlab/markdown/reference_gatherer_filter.rb9
-rw-r--r--lib/gitlab/reference_extractor.rb23
3 files changed, 29 insertions, 12 deletions
diff --git a/lib/gitlab/markdown/reference_filter.rb b/lib/gitlab/markdown/reference_filter.rb
index 0ae0d93f70d..ede9f8865ff 100644
--- a/lib/gitlab/markdown/reference_filter.rb
+++ b/lib/gitlab/markdown/reference_filter.rb
@@ -12,7 +12,14 @@ 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)
+ LazyReference = Struct.new(:klass, :ids) do
+ def self.load(refs)
+ refs.group_by(&:klass).flat_map do |klass, refs|
+ ids = refs.flat_map(&:ids)
+ klass.where(id: ids)
+ end
+ end
+ end
def self.user_can_reference?(user, node, context)
if node.has_attribute?('data-project')
diff --git a/lib/gitlab/markdown/reference_gatherer_filter.rb b/lib/gitlab/markdown/reference_gatherer_filter.rb
index cf9a2303db8..18df5db94d6 100644
--- a/lib/gitlab/markdown/reference_gatherer_filter.rb
+++ b/lib/gitlab/markdown/reference_gatherer_filter.rb
@@ -21,7 +21,7 @@ module Gitlab
gather_references(node)
end
- load_lazy_references
+ load_lazy_references unless context[:load_lazy_references] == false
doc
end
@@ -56,11 +56,8 @@ module Gitlab
# 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
+ values = ReferenceFilter::LazyReference.load(refs)
+ result[:references][type].concat(values)
end
end
diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb
index 2e546ef0d54..8100f2675a7 100644
--- a/lib/gitlab/reference_extractor.rb
+++ b/lib/gitlab/reference_extractor.rb
@@ -10,9 +10,10 @@ module Gitlab
@current_user = current_user
end
- def analyze(text)
+ def analyze(texts)
references.clear
- @text = Gitlab::Markdown.render_without_gfm(text)
+ texts = Array(texts)
+ @texts = texts.map { |text| Gitlab::Markdown.render_without_gfm(text) }
end
%i(user label issue merge_request snippet commit commit_range).each do |type|
@@ -47,13 +48,25 @@ module Gitlab
current_user: current_user,
# We don't actually care about the links generated
only_path: true,
- ignore_blockquotes: true
+ ignore_blockquotes: true,
+ load_lazy_references: false
}
pipeline = HTML::Pipeline.new([filter, Gitlab::Markdown::ReferenceGathererFilter], context)
- result = pipeline.call(@text)
- result[:references][filter_type]
+ values = []
+ lazy_references = []
+
+ @texts.each do |text|
+ result = pipeline.call(text)
+
+ values.concat(result[:references][filter_type])
+ lazy_references.concat(result[:lazy_references][filter_type])
+ end
+
+ lazy_values = Gitlab::Markdown::ReferenceFilter::LazyReference.load(lazy_references)
+ values.concat(lazy_values)
+ values
end
end
end