2021-06-08 01:23:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Pagination
|
|
|
|
module Keyset
|
|
|
|
class Iterator
|
2021-11-18 22:05:49 +05:30
|
|
|
def initialize(scope:, cursor: {}, use_union_optimization: true, in_operator_optimization_options: nil)
|
2021-09-30 23:02:18 +05:30
|
|
|
@scope, success = Gitlab::Pagination::Keyset::SimpleOrderBuilder.build(scope)
|
2021-11-18 22:05:49 +05:30
|
|
|
raise(UnsupportedScopeOrder) unless success
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
@cursor = cursor
|
2022-01-26 12:08:38 +05:30
|
|
|
@order = Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(@scope)
|
2021-11-11 11:23:49 +05:30
|
|
|
@use_union_optimization = in_operator_optimization_options ? false : use_union_optimization
|
|
|
|
@in_operator_optimization_options = in_operator_optimization_options
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def each_batch(of: 1000)
|
|
|
|
loop do
|
2021-11-11 11:23:49 +05:30
|
|
|
current_scope = scope.dup
|
2021-11-18 22:05:49 +05:30
|
|
|
relation = order.apply_cursor_conditions(current_scope, cursor, keyset_options)
|
2021-11-11 11:23:49 +05:30
|
|
|
relation = relation.reorder(order) unless @in_operator_optimization_options
|
|
|
|
relation = relation.limit(of)
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
yield relation
|
|
|
|
|
|
|
|
last_record = relation.last
|
|
|
|
break unless last_record
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
@cursor = order.cursor_attributes_for_node(last_record)
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
attr_reader :scope, :cursor, :order
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
def keyset_options
|
|
|
|
{
|
|
|
|
use_union_optimization: @use_union_optimization,
|
|
|
|
in_operator_optimization_options: @in_operator_optimization_options
|
|
|
|
}
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|