Module: SidekiqIteration::Enumerators
- Included in:
- Iteration
- Defined in:
- lib/sidekiq_iteration/enumerators.rb
Instance Method Summary collapse
-
#active_record_batches_enumerator(scope, cursor:, **options) ⇒ Object
Builds Enumerator from Active Record Relation and enumerates on batches of records.
-
#active_record_records_enumerator(scope, cursor:, **options) ⇒ Object
Builds Enumerator from Active Record Relation.
-
#active_record_relations_enumerator(scope, cursor:, **options) ⇒ Object
Builds Enumerator from Active Record Relation and enumerates on batches, yielding Active Record Relations.
-
#array_enumerator(array, cursor:) ⇒ Object
Builds Enumerator object from a given array, using
cursor
as an offset. -
#csv_batches_enumerator(csv, cursor:, **options) ⇒ Object
Builds Enumerator from a CSV file and enumerates on batches of records.
-
#csv_enumerator(csv, cursor:) ⇒ Object
Builds Enumerator from a CSV file.
-
#nested_enumerator(enums, cursor:) ⇒ Object
Builds Enumerator for nested iteration.
Instance Method Details
#active_record_batches_enumerator(scope, cursor:, **options) ⇒ Object
Builds Enumerator from Active Record Relation and enumerates on batches of records. Each Enumerator tick moves the cursor batch_size
rows forward.
91 92 93 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 91 def active_record_batches_enumerator(scope, cursor:, **) ActiveRecordEnumerator.new(scope, cursor: cursor, **).batches end |
#active_record_records_enumerator(scope, cursor:, **options) ⇒ Object
Builds Enumerator from Active Record Relation. Each Enumerator tick moves the cursor one row forward.
columns:
argument is used to build the actual query for iteration. columns
: defaults to primary key:
1) SELECT * FROM users ORDER BY id LIMIT 100
When iteration is resumed, cursor:
and columns:
values will be used to continue from the point where iteration stopped:
2) SELECT * FROM users WHERE id > $CURSOR ORDER BY id LIMIT 100
columns:
can also take more than one column. In that case, cursor
will contain serialized values of all columns at the point where iteration stopped.
Consider this example with columns: [:created_at, :id]. Here’s the query will use on the first iteration:
1) SELECT * FROM "products" ORDER BY created_at, id LIMIT 100
And the query on the next iteration:
2) SELECT * FROM "products"
WHERE (created_at > '$LAST_CREATED_AT_CURSOR'
OR (created_at = '$LAST_CREATED_AT_CURSOR' AND (id > '$LAST_ID_CURSOR')))
ORDER BY created_at, id LIMIT 100
As a result of this query pattern, if the values in these columns change for the records in scope during iteration, they may be skipped or yielded multiple times depending on the nature of the update and the cursor’s value. If the value gets updated to a greater value than the cursor’s value, it will get yielded again. Similarly, if the value gets updated to a lesser value than the cursor’s value, it will get skipped.
69 70 71 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 69 def active_record_records_enumerator(scope, cursor:, **) ActiveRecordEnumerator.new(scope, cursor: cursor, **).records end |
#active_record_relations_enumerator(scope, cursor:, **options) ⇒ Object
Builds Enumerator from Active Record Relation and enumerates on batches, yielding Active Record Relations.
112 113 114 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 112 def active_record_relations_enumerator(scope, cursor:, **) ActiveRecordEnumerator.new(scope, cursor: cursor, **).relations end |
#array_enumerator(array, cursor:) ⇒ Object
Builds Enumerator object from a given array, using cursor
as an offset.
17 18 19 20 21 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 17 def array_enumerator(array, cursor:) raise ArgumentError, "array must be an Array" unless array.is_a?(Array) array.each_with_index.drop(cursor || 0).to_enum { array.size } end |
#csv_batches_enumerator(csv, cursor:, **options) ⇒ Object
Builds Enumerator from a CSV file and enumerates on batches of records.
151 152 153 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 151 def csv_batches_enumerator(csv, cursor:, **) CsvEnumerator.new(csv).batches(cursor: cursor, **) end |
#csv_enumerator(csv, cursor:) ⇒ Object
Builds Enumerator from a CSV file.
131 132 133 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 131 def csv_enumerator(csv, cursor:) CsvEnumerator.new(csv).rows(cursor: cursor) end |
#nested_enumerator(enums, cursor:) ⇒ Object
Builds Enumerator for nested iteration.
178 179 180 |
# File 'lib/sidekiq_iteration/enumerators.rb', line 178 def nested_enumerator(enums, cursor:) NestedEnumerator.new(enums, cursor: cursor).each end |