Class: EachBatch::PluckedBatchEnumerator
- Inherits:
-
Object
- Object
- EachBatch::PluckedBatchEnumerator
- Includes:
- Enumerable
- Defined in:
- lib/each_batch/plucked_batch_enumerator.rb
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #batch_size ⇒ Object
- #each ⇒ Object
- #each_row(&block) ⇒ Object
-
#initialize(relation, of:, order:, keys:, pluck_keys:) ⇒ PluckedBatchEnumerator
constructor
A new instance of PluckedBatchEnumerator.
Constructor Details
#initialize(relation, of:, order:, keys:, pluck_keys:) ⇒ PluckedBatchEnumerator
Returns a new instance of PluckedBatchEnumerator.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 10 def initialize(relation, of:, order:, keys:, pluck_keys:) if pluck_keys.present? && (pluck_keys & keys).to_set != keys.to_set raise ArgumentError, 'Not all keys are included in the custom select clause for pluck' end @relation = relation @of = of @order = order @keys = keys @pluck_keys = pluck_keys @key_indices = keys.map { |key| pluck_keys.index(key) } end |
Instance Attribute Details
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
8 9 10 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 8 def keys @keys end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
8 9 10 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 8 def order @order end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
8 9 10 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 8 def relation @relation end |
Instance Method Details
#batch_size ⇒ Object
23 24 25 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 23 def batch_size @of end |
#each ⇒ Object
27 28 29 30 31 32 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 60 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 27 def each return self unless block_given? batch_relation = relation.reorder(keys.product([order]).to_h).limit(batch_size) batch_relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching yielded_relation = batch_relation op = order.to_s.casecmp('desc').zero? ? :lt : :gt last_idx = batch_size - 1 pk = relation.primary_key.to_sym single_pluck_key = @pluck_keys.length == 1 loop do results = yielded_relation.pluck(*@pluck_keys) break if results.empty? yield results # grab the offsets from the plucked results offsets = if single_pluck_key results[last_idx] else results[last_idx]&.values_at(*@key_indices) end break if offsets.nil? yielded_relation = batch_relation.where_row(*keys).public_send(op, *offsets) end end |
#each_row(&block) ⇒ Object
62 63 64 65 66 |
# File 'lib/each_batch/plucked_batch_enumerator.rb', line 62 def each_row(&block) return to_enum(:each_row) unless block_given? each { |row_batch| row_batch.each(&block) } end |