diff options
author | Thong Kuah <tkuah@gitlab.com> | 2019-09-11 07:48:21 +0300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2019-09-11 07:48:21 +0300 |
commit | 28292d516abb33aeaf7e5bfaf94679d1317bd284 (patch) | |
tree | 0acb8ae2d6904b8d779230df7a170f2cd0c2b6bf /lib/gitlab | |
parent | bb7bbcf7b667197e71f696cc17d8c08677629e8d (diff) | |
parent | f1926b321deb8b922dead991fb4d8bea42699f9f (diff) |
Merge branch '65988-optimize-snippet-listings' into 'master'
Optimize queries for snippet listings
See merge request gitlab-org/gitlab-ce!32576
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/issuable_metadata.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/noteable_metadata.rb | 33 |
2 files changed, 35 insertions, 2 deletions
diff --git a/lib/gitlab/issuable_metadata.rb b/lib/gitlab/issuable_metadata.rb index be73bcd5506..6f760751b0f 100644 --- a/lib/gitlab/issuable_metadata.rb +++ b/lib/gitlab/issuable_metadata.rb @@ -19,7 +19,7 @@ module Gitlab return {} if issuable_ids.empty? - issuable_note_count = ::Note.count_for_collection(issuable_ids, collection_type) + issuable_notes_count = ::Note.count_for_collection(issuable_ids, collection_type) issuable_votes_count = ::AwardEmoji.votes_for_collection(issuable_ids, collection_type) issuable_merge_requests_count = if collection_type == 'Issue' @@ -31,7 +31,7 @@ module Gitlab issuable_ids.each_with_object({}) do |id, issuable_meta| downvotes = issuable_votes_count.find { |votes| votes.awardable_id == id && votes.downvote? } upvotes = issuable_votes_count.find { |votes| votes.awardable_id == id && votes.upvote? } - notes = issuable_note_count.find { |notes| notes.noteable_id == id } + notes = issuable_notes_count.find { |notes| notes.noteable_id == id } merge_requests = issuable_merge_requests_count.find { |mr| mr.first == id } issuable_meta[id] = ::Issuable::IssuableMeta.new( diff --git a/lib/gitlab/noteable_metadata.rb b/lib/gitlab/noteable_metadata.rb new file mode 100644 index 00000000000..f3f8933b81f --- /dev/null +++ b/lib/gitlab/noteable_metadata.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module NoteableMetadata + def noteable_meta_data(noteable_collection, collection_type) + # ActiveRecord uses Object#extend for null relations. + if !(noteable_collection.singleton_class < ActiveRecord::NullRelation) && + noteable_collection.respond_to?(:limit_value) && + noteable_collection.limit_value.nil? + + raise 'Collection must have a limit applied for preloading meta-data' + end + + # map has to be used here since using pluck or select will + # throw an error when ordering noteables which inserts + # a new order into the collection. + # We cannot use reorder to not mess up the paginated collection. + noteable_ids = noteable_collection.map(&:id) + + return {} if noteable_ids.empty? + + noteable_notes_count = ::Note.count_for_collection(noteable_ids, collection_type) + + noteable_ids.each_with_object({}) do |id, noteable_meta| + notes = noteable_notes_count.find { |notes| notes.noteable_id == id } + + noteable_meta[id] = ::Noteable::NoteableMeta.new( + notes.try(:count).to_i + ) + end + end + end +end |