debian-mirror-gitlab/lib/gitlab/pagination/gitaly_keyset_pager.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.2 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
module Gitlab
module Pagination
class GitalyKeysetPager
attr_reader :request_context, :project
2022-05-07 20:08:51 +05:30
2020-10-24 23:57:45 +05:30
delegate :params, to: :request_context
def initialize(request_context, project)
@request_context = request_context
@project = project
end
2022-08-27 11:52:29 +05:30
# It is expected that the given finder will respond to `execute` method with `gitaly_pagination:` option
2020-10-24 23:57:45 +05:30
# and supports pagination via gitaly.
def paginate(finder)
2022-08-27 11:52:29 +05:30
return finder.execute(gitaly_pagination: false) if no_pagination?
2021-11-11 11:23:49 +05:30
return paginate_via_gitaly(finder) if keyset_pagination_enabled?(finder)
return paginate_first_page_via_gitaly(finder) if paginate_first_page?(finder)
2020-10-24 23:57:45 +05:30
2021-11-11 11:23:49 +05:30
records = ::Kaminari.paginate_array(finder.execute)
2020-10-24 23:57:45 +05:30
Gitlab::Pagination::OffsetPagination
.new(request_context)
2021-11-11 11:23:49 +05:30
.paginate(records)
2020-10-24 23:57:45 +05:30
end
private
2022-08-27 11:52:29 +05:30
def no_pagination?
params[:pagination] == 'none'
end
2021-11-11 11:23:49 +05:30
def keyset_pagination_enabled?(finder)
return false unless params[:pagination] == "keyset"
if finder.is_a?(BranchesFinder)
2022-07-16 23:28:13 +05:30
Feature.enabled?(:branch_list_keyset_pagination, project)
2021-12-11 22:18:48 +05:30
elsif finder.is_a?(TagsFinder)
2022-10-11 01:57:18 +05:30
true
2021-11-11 11:23:49 +05:30
elsif finder.is_a?(::Repositories::TreeFinder)
2022-07-16 23:28:13 +05:30
Feature.enabled?(:repository_tree_gitaly_pagination, project)
2021-11-11 11:23:49 +05:30
else
false
end
2021-02-22 17:27:13 +05:30
end
2021-11-11 11:23:49 +05:30
def paginate_first_page?(finder)
return false unless params[:page].blank? || params[:page].to_i == 1
if finder.is_a?(BranchesFinder)
2022-07-16 23:28:13 +05:30
Feature.enabled?(:branch_list_keyset_pagination, project)
2021-12-11 22:18:48 +05:30
elsif finder.is_a?(TagsFinder)
2022-10-11 01:57:18 +05:30
true
2021-11-11 11:23:49 +05:30
elsif finder.is_a?(::Repositories::TreeFinder)
2022-07-16 23:28:13 +05:30
Feature.enabled?(:repository_tree_gitaly_pagination, project)
2021-11-11 11:23:49 +05:30
else
false
end
2020-10-24 23:57:45 +05:30
end
def paginate_via_gitaly(finder)
finder.execute(gitaly_pagination: true).tap do |records|
apply_headers(records)
end
end
2021-02-22 17:27:13 +05:30
# When first page is requested, we paginate the data via Gitaly
# Headers are added to immitate offset pagination, while it is the default option
def paginate_first_page_via_gitaly(finder)
finder.execute(gitaly_pagination: true).tap do |records|
2021-11-11 11:23:49 +05:30
total = finder.total
2021-02-22 17:27:13 +05:30
per_page = params[:per_page].presence || Kaminari.config.default_per_page
Gitlab::Pagination::OffsetHeaderBuilder.new(
request_context: request_context, per_page: per_page, page: 1, next_page: 2,
total: total, total_pages: total / per_page + 1
).execute
end
end
2020-10-24 23:57:45 +05:30
def apply_headers(records)
if records.count == params[:per_page]
Gitlab::Pagination::Keyset::HeaderBuilder
.new(request_context)
.add_next_page_header(
query_params_for(records.last)
)
end
end
def query_params_for(record)
# NOTE: page_token is name for now, but it could be dynamic if we have other gitaly finders
# that is based on something other than name
{ page_token: record.name }
end
end
end
end