Class: Lhm::Chunker

Inherits:
Object
  • Object
show all
Includes:
Command, SqlHelper
Defined in:
lib/lhm/chunker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #tagged, #version_string

Methods included from Command

#run

Constructor Details

#initialize(migration, connection = nil, options = {}) ⇒ Chunker

Copy from origin to destination in chunks of size `stride`. Use the `throttler` class to sleep between each stride.



16
17
18
19
20
21
22
23
24
25
# File 'lib/lhm/chunker.rb', line 16

def initialize(migration, connection = nil, options = {})
  @migration = migration
  @connection = connection
  if @throttler = options[:throttler]
    @throttler.connection = @connection if @throttler.respond_to?(:connection=)
  end
  @start = options[:start] || select_start
  @limit = options[:limit] || select_limit
  @printer = options[:printer] || Printer::Percentage.new
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection



12
13
14
# File 'lib/lhm/chunker.rb', line 12

def connection
  @connection
end

Instance Method Details

#executeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lhm/chunker.rb', line 27

def execute
  return unless @start && @limit
  @next_to_insert = @start
  while @next_to_insert < @limit || (@start == @limit)
    stride = @throttler.stride
    affected_rows = @connection.update(copy(bottom, top(stride)))

    if @throttler && affected_rows > 0
      @throttler.run
    end

    @printer.notify(bottom, @limit)
    @next_to_insert = top(stride) + 1
    break if @start == @limit
  end
  @printer.end
end