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, #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`. Sleeps for `throttle` milliseconds between each stride.



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

def initialize(migration, connection = nil, options = {})
  @migration = migration
  @connection = connection
  @throttler = options[:throttler]
  @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



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

def execute
  return unless @start && @limit
  @next_to_insert = @start
  while @next_to_insert < @limit || (@next_to_insert == 1 && @start == 1)
    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
  end
  @printer.end
end