summaryrefslogtreecommitdiff
path: root/app/models/repository.rb
diff options
context:
space:
mode:
authorAndrew Newdigate <andrew@gitlab.com>2017-10-04 13:55:21 +0100
committerAndrew Newdigate <andrew@gitlab.com>2017-10-04 13:55:21 +0100
commit437c8072e8a735cadad591d8593438e668b31fab (patch)
treeb56e66a936ed6fa3f91ea7c22fc28addc3f33ff5 /app/models/repository.rb
parentf0b4fe2fc2c1e89e67b236b640f8fe31aa37e73a (diff)
downloadgitlab-ce-437c8072e8a735cadad591d8593438e668b31fab.tar.gz
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r--app/models/repository.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb
index d47dc9a05cd..4d77b57b8b9 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -1,4 +1,5 @@
require 'securerandom'
+require 'batch-loader'
class Repository
REF_MERGE_REQUEST = 'merge-requests'.freeze
@@ -624,6 +625,46 @@ class Repository
end
end
+ # TODO: Ruby may already offer a function like this
+ # otherwise move it somewhere else, ie a collection
+ # utils class
+ def hash_by_kv(array, key, value)
+ array.inject(Hash.new) do |memo, item|
+ k = item[key]
+ v = item[value]
+
+ array = memo[k]
+ if array
+ array.push(v)
+ else
+ memo[k] = [v]
+ end
+
+ memo
+ end
+ end
+
+ def last_commit_for_path_lazy(sha, path)
+ BatchLoader.for({ sha: sha, path: path }).batch do |items, loader|
+ paths_by_sha = hash_by_kv(items, :sha, :path)
+ # Bulk fetch the paths for each SHA
+ last_commits_for_items = Hash.new
+ paths_by_sha.each do |for_sha, paths|
+ last_commits_for_sha = raw_repository.last_commit_for_paths(for_sha, paths)
+
+ last_commits_for_sha.each do |result_path, last_commit|
+ last_commits_for_items[{ sha: for_sha, path: result_path }] = last_commit
+ end
+ end
+
+ # Present the results to the loader
+ items.each do |item|
+ last_commit = last_commits_for_items[item]
+ loader.call(item, last_commit)
+ end
+ end
+ end
+
def last_commit_for_path(sha, path)
raw_repository.gitaly_migrate(:last_commit_for_path) do |is_enabled|
if is_enabled