Class: Gitlab::Diff::FileCollection::MergeRequestDiffBatch

Inherits:
MergeRequestDiffBase show all
Defined in:
lib/gitlab/diff/file_collection/merge_request_diff_batch.rb

Overview

Builds a paginated diff file collection and collects pagination metadata.

It doesn't handle caching yet as we're not prepared to write/read separate file keys (gitlab.com/gitlab-org/gitlab/issues/30550).

Constant Summary collapse

DEFAULT_BATCH_PAGE =
1
DEFAULT_BATCH_SIZE =
20

Instance Attribute Summary collapse

Attributes inherited from Base

#diff_options, #diff_refs, #diffable, #fallback_diff_refs, #project

Instance Method Summary collapse

Methods inherited from MergeRequestDiffBase

#clear_cache, #diff_files, #real_size, #write_cache

Methods included from Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods inherited from Base

#clear_cache, default_options, #diff_file_paths, #diff_file_with_new_path, #diff_file_with_old_path, #diff_files, #raw_diff_files, #unfold_diff_files, #write_cache

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(merge_request_diff, batch_page, batch_size, diff_options:) ⇒ MergeRequestDiffBatch

Returns a new instance of MergeRequestDiffBatch.


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/diff/file_collection/merge_request_diff_batch.rb', line 18

def initialize(merge_request_diff, batch_page, batch_size, diff_options:)
  super(merge_request_diff, diff_options: diff_options)

  batch_page ||= DEFAULT_BATCH_PAGE
  batch_size ||= DEFAULT_BATCH_SIZE

  @paginated_collection = relation.page(batch_page).per(batch_size)
  @pagination_data = {
    current_page: @paginated_collection.current_page,
    next_page: @paginated_collection.next_page,
    total_pages: @paginated_collection.total_pages
  }
end

Instance Attribute Details

#pagination_dataObject (readonly)

Returns the value of attribute pagination_data


16
17
18
# File 'lib/gitlab/diff/file_collection/merge_request_diff_batch.rb', line 16

def pagination_data
  @pagination_data
end

Instance Method Details

#diffsObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gitlab/diff/file_collection/merge_request_diff_batch.rb', line 33

def diffs
  strong_memoize(:diffs) do
    @merge_request_diff.opening_external_diff do
      # Avoiding any extra queries.
      collection = @paginated_collection.to_a

      # The offset collection and calculation is required so that we
      # know how much has been loaded in previous batches, collapsing
      # the current paginated set accordingly (collection limit calculation).
      # See: https://docs.gitlab.com/ee/development/diffs.html#diff-collection-limits
      #
      offset_index = collection.first&.index
      options = diff_options.dup

      collection =
        if offset_index && offset_index > 0
          offset_collection = relation.limit(offset_index) # rubocop:disable CodeReuse/ActiveRecord
          options[:offset_index] = offset_index
          offset_collection + collection
        else
          collection
        end

      Gitlab::Git::DiffCollection.new(collection.map(&:to_hash), options)
    end
  end
end