40 lines
1.1 KiB
Ruby
40 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Gitlab
|
|
module Pagination
|
|
module Keyset
|
|
class Iterator
|
|
def initialize(scope:, use_union_optimization: false)
|
|
@scope = scope
|
|
@order = Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(scope)
|
|
@use_union_optimization = use_union_optimization
|
|
end
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
def each_batch(of: 1000)
|
|
cursor_attributes = {}
|
|
|
|
loop do
|
|
current_scope = scope.dup.limit(of)
|
|
relation = order
|
|
.apply_cursor_conditions(current_scope, cursor_attributes, { use_union_optimization: @use_union_optimization })
|
|
.reorder(order)
|
|
.limit(of)
|
|
|
|
yield relation
|
|
|
|
last_record = relation.last
|
|
break unless last_record
|
|
|
|
cursor_attributes = order.cursor_attributes_for_node(last_record)
|
|
end
|
|
end
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
private
|
|
|
|
attr_reader :scope, :order
|
|
end
|
|
end
|
|
end
|
|
end
|