Class: DbBlaster::Chunker
- Inherits:
-
Object
- Object
- DbBlaster::Chunker
- Defined in:
- lib/db_blaster/chunker.rb
Overview
Chunk the records into sizes < Configuration.max_message_size_in_kilobytes yielding the chunks inline to the provided block If the records’ size is already less than Configuration.max_message_size_in_kilobytes, all the records are yielded to the provided block
Instance Attribute Summary collapse
-
#block_on_chunk ⇒ Object
readonly
Returns the value of attribute block_on_chunk.
-
#current_chunk ⇒ Object
readonly
Returns the value of attribute current_chunk.
-
#current_chunk_size ⇒ Object
readonly
Returns the value of attribute current_chunk_size.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#source_table ⇒ Object
readonly
Returns the value of attribute source_table.
Class Method Summary collapse
Instance Method Summary collapse
- #breakup_records ⇒ Object
- #chunk ⇒ Object
-
#initialize(source_table, records, &block_on_chunk) ⇒ Chunker
constructor
A new instance of Chunker.
- #max_bytes ⇒ Object
- #max_kilobytes ⇒ Object
- #yield_if_records_acceptable? ⇒ Boolean
Constructor Details
#initialize(source_table, records, &block_on_chunk) ⇒ Chunker
Returns a new instance of Chunker.
11 12 13 14 15 16 17 |
# File 'lib/db_blaster/chunker.rb', line 11 def initialize(source_table, records, &block_on_chunk) @source_table = source_table @records = records @block_on_chunk = block_on_chunk @current_chunk_size = 0 @current_chunk = [] end |
Instance Attribute Details
#block_on_chunk ⇒ Object (readonly)
Returns the value of attribute block_on_chunk.
9 10 11 |
# File 'lib/db_blaster/chunker.rb', line 9 def block_on_chunk @block_on_chunk end |
#current_chunk ⇒ Object (readonly)
Returns the value of attribute current_chunk.
9 10 11 |
# File 'lib/db_blaster/chunker.rb', line 9 def current_chunk @current_chunk end |
#current_chunk_size ⇒ Object (readonly)
Returns the value of attribute current_chunk_size.
9 10 11 |
# File 'lib/db_blaster/chunker.rb', line 9 def current_chunk_size @current_chunk_size end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
9 10 11 |
# File 'lib/db_blaster/chunker.rb', line 9 def records @records end |
#source_table ⇒ Object (readonly)
Returns the value of attribute source_table.
9 10 11 |
# File 'lib/db_blaster/chunker.rb', line 9 def source_table @source_table end |
Class Method Details
.chunk(source_table, records, &block) ⇒ Object
19 20 21 |
# File 'lib/db_blaster/chunker.rb', line 19 def self.chunk(source_table, records, &block) new(source_table, records, &block).chunk end |
Instance Method Details
#breakup_records ⇒ Object
45 46 47 48 |
# File 'lib/db_blaster/chunker.rb', line 45 def breakup_records records.each(&method(:process_record)) block_on_chunk.call(current_chunk) if current_chunk.length.positive? end |
#chunk ⇒ Object
23 24 25 26 27 |
# File 'lib/db_blaster/chunker.rb', line 23 def chunk return if yield_if_records_acceptable? breakup_records end |
#max_bytes ⇒ Object
36 37 38 |
# File 'lib/db_blaster/chunker.rb', line 36 def max_bytes @max_bytes ||= 1000 * max_kilobytes end |
#max_kilobytes ⇒ Object
40 41 42 43 |
# File 'lib/db_blaster/chunker.rb', line 40 def max_kilobytes DbBlaster.configuration. || DbBlaster.configuration.class::DEFAULT_MAX_MESSAGE_SIZE_IN_KILOBYTES end |
#yield_if_records_acceptable? ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/db_blaster/chunker.rb', line 29 def yield_if_records_acceptable? return if records.to_json.size >= max_bytes block_on_chunk.call(records) true end |