blob: bcd17fd0d34a0c0a397be433e83a8c377db8d7c4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# frozen_string_literal: true
module Gitlab
module Pagination
module Keyset
class Iterator
def initialize(scope:, cursor: {}, use_union_optimization: true, in_operator_optimization_options: nil)
@scope, success = Gitlab::Pagination::Keyset::SimpleOrderBuilder.build(scope)
raise(UnsupportedScopeOrder) unless success
@cursor = cursor
@order = Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(scope)
@use_union_optimization = in_operator_optimization_options ? false : use_union_optimization
@in_operator_optimization_options = in_operator_optimization_options
end
# rubocop: disable CodeReuse/ActiveRecord
def each_batch(of: 1000)
loop do
current_scope = scope.dup
relation = order.apply_cursor_conditions(current_scope, cursor, keyset_options)
relation = relation.reorder(order) unless @in_operator_optimization_options
relation = relation.limit(of)
yield relation
last_record = relation.last
break unless last_record
@cursor = order.cursor_attributes_for_node(last_record)
end
end
# rubocop: enable CodeReuse/ActiveRecord
private
attr_reader :scope, :cursor, :order
def keyset_options
{
use_union_optimization: @use_union_optimization,
in_operator_optimization_options: @in_operator_optimization_options
}
end
end
end
end
end
|