Class: DbBlaster::Chunker

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_chunkObject (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_chunkObject (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_sizeObject (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

#recordsObject (readonly)

Returns the value of attribute records.



9
10
11
# File 'lib/db_blaster/chunker.rb', line 9

def records
  @records
end

#source_tableObject (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_recordsObject



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

#chunkObject



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_bytesObject



36
37
38
# File 'lib/db_blaster/chunker.rb', line 36

def max_bytes
  @max_bytes ||= 1000 * max_kilobytes
end

#max_kilobytesObject



40
41
42
43
# File 'lib/db_blaster/chunker.rb', line 40

def max_kilobytes
  DbBlaster.configuration.max_message_size_in_kilobytes ||
    DbBlaster.configuration.class::DEFAULT_MAX_MESSAGE_SIZE_IN_KILOBYTES
end

#yield_if_records_acceptable?Boolean

Returns:

  • (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