Class: MedPipe::BatchReader
- Inherits:
-
Object
- Object
- MedPipe::BatchReader
- Defined in:
- lib/med_pipe/batch_reader.rb
Overview
大量データを分割取得するためのクラス in_batches では scope が全クエリに含まれるが、本クラスではidの取得でのみ scope を使用する
Instance Method Summary collapse
-
#around_load(&block) ⇒ Object
EXAMPLE: MedPipe::BatchReader.new(User) .around_load { |&block| ApplicationRecord.connected_to(role: :reading, &block) }.
- #each {|pluck結果を1件ずつ渡す| ... } ⇒ Object
-
#initialize(model_class, scope: nil, pluck_columns: [:id], batch_size: 1_000, max_id_load_size: 100_000) ⇒ BatchReader
constructor
A new instance of BatchReader.
Constructor Details
#initialize(model_class, scope: nil, pluck_columns: [:id], batch_size: 1_000, max_id_load_size: 100_000) ⇒ BatchReader
Returns a new instance of BatchReader.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/med_pipe/batch_reader.rb', line 6 def initialize(model_class, scope: nil, pluck_columns: [:id], batch_size: 1_000, max_id_load_size: 100_000) @model_class = model_class @scope = scope || model_class.all @pluck_columns = pluck_columns @batch_size = batch_size @max_id_load_size = max_id_load_size @around_load_callback = nil validate_parameters end |
Instance Method Details
#around_load(&block) ⇒ Object
EXAMPLE:
MedPipe::BatchReader.new(User)
.around_load { |&block| ApplicationRecord.connected_to(role: :reading, &block) }
20 21 22 23 |
# File 'lib/med_pipe/batch_reader.rb', line 20 def around_load(&block) @around_load_callback = block self end |
#each {|pluck結果を1件ずつ渡す| ... } ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/med_pipe/batch_reader.rb', line 26 def each(&block) return enum_for(:each) unless block each_ids = MedPipe::BatchIdFetcher.new(@scope, batch_size: @batch_size, max_load_size: @max_id_load_size).each loop do records = @around_load_callback&.call { batch_load(each_ids) } || batch_load(each_ids) records.each(&block) rescue StopIteration break end end |