summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Drozdov <idrozdov@gitlab.com>2019-06-17 13:00:30 +0300
committerIgor Drozdov <idrozdov@gitlab.com>2019-06-17 13:10:05 +0300
commit1c1954e70e91092f5bbd4042dd5e1793cfab22ff (patch)
treea01d4fa72f3916c1835de05f3069a5b4258cd510
parent814e1af72e654aa5f8ea9e200c26ce8ea99954df (diff)
downloadgitlab-ce-id-graphql-tree-commit.tar.gz
Remove N+1 from LastCommitResolverid-graphql-tree-commit
-rw-r--r--app/graphql/resolvers/last_commit_resolver.rb16
-rw-r--r--app/graphql/types/tree/tree_type.rb4
-rw-r--r--app/models/tree.rb3
-rw-r--r--app/models/tree_entry.rb23
-rw-r--r--lib/gitlab/graphql/representation/tree_entry.rb11
5 files changed, 23 insertions, 34 deletions
diff --git a/app/graphql/resolvers/last_commit_resolver.rb b/app/graphql/resolvers/last_commit_resolver.rb
index 34f7c71fa08..2245568777f 100644
--- a/app/graphql/resolvers/last_commit_resolver.rb
+++ b/app/graphql/resolvers/last_commit_resolver.rb
@@ -3,8 +3,20 @@
module Resolvers
class LastCommitResolver < BaseResolver
def resolve
- Gitlab::GitalyClient.allow_n_plus_1_calls do
- Gitlab::Git::Commit.last_for_path(object.repository, object.commit_id, object.path)
+ batched_commit
+ end
+
+ private
+
+ def batched_commit
+ BatchLoader.for(object.path).batch(key: object.tree) do |paths, loader, args|
+ tree = args[:key]
+ commits = tree.repository.list_last_commits_for_tree(tree.sha, tree.path, limit: 2 ** 31 - 1)
+
+ paths.each do |path|
+ commit = commits[path]
+ loader.call(path, commit) if commit
+ end
end
end
end
diff --git a/app/graphql/types/tree/tree_type.rb b/app/graphql/types/tree/tree_type.rb
index 1ee93ed9542..2d485148f5e 100644
--- a/app/graphql/types/tree/tree_type.rb
+++ b/app/graphql/types/tree/tree_type.rb
@@ -5,13 +5,13 @@ module Types
graphql_name 'Tree'
field :trees, Types::Tree::TreeEntryType.connection_type, null: false, resolve: -> (obj, args, ctx) do
- Gitlab::Graphql::Representation::TreeEntry.decorate(obj.trees, obj.repository)
+ Gitlab::Graphql::Representation::TreeEntry.decorate(obj.trees, obj)
end
field :submodules, Types::Tree::SubmoduleType.connection_type, null: false
field :blobs, Types::Tree::BlobType.connection_type, null: false, resolve: -> (obj, args, ctx) do
- Gitlab::Graphql::Representation::TreeEntry.decorate(obj.blobs, obj.repository)
+ Gitlab::Graphql::Representation::TreeEntry.decorate(obj.blobs, obj)
end
end
end
diff --git a/app/models/tree.rb b/app/models/tree.rb
index e30c2c8742c..cd385872171 100644
--- a/app/models/tree.rb
+++ b/app/models/tree.rb
@@ -14,8 +14,7 @@ class Tree
@path = path
git_repo = @repository.raw_repository
- raw_entries = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive)
- @entries = TreeEntry.decorate(raw_entries, repository)
+ @entries = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive)
end
def readme_path
diff --git a/app/models/tree_entry.rb b/app/models/tree_entry.rb
deleted file mode 100644
index 5dc55cfb33b..00000000000
--- a/app/models/tree_entry.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-# frozen_string_literal: true
-
-class TreeEntry < SimpleDelegator
- class << self
- def decorate(entries, repository)
- entries.map do |entry|
- if entry.is_a?(TreeEntry)
- entry
- else
- self.new(entry, repository)
- end
- end
- end
- end
-
- attr_accessor :repository, :raw_entry
-
- def initialize(raw_entry, repository)
- @repository = repository
-
- super(raw_entry)
- end
-end
diff --git a/lib/gitlab/graphql/representation/tree_entry.rb b/lib/gitlab/graphql/representation/tree_entry.rb
index 7ea83db5876..4467c6634ea 100644
--- a/lib/gitlab/graphql/representation/tree_entry.rb
+++ b/lib/gitlab/graphql/representation/tree_entry.rb
@@ -5,23 +5,24 @@ module Gitlab
module Representation
class TreeEntry < SimpleDelegator
class << self
- def decorate(entries, repository)
+ def decorate(entries, tree)
return if entries.nil?
entries.map do |entry|
if entry.is_a?(TreeEntry)
entry
else
- self.new(entry, repository)
+ self.new(entry, tree)
end
end
end
end
- attr_accessor :repository
+ attr_accessor :tree
+ delegate :repository, to: :tree
- def initialize(raw_entry, repository)
- @repository = repository
+ def initialize(raw_entry, tree)
+ @tree = tree
super(raw_entry)
end