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, #sql, #table?, #update

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
  @stride = options[:stride] || 40_000
  @throttle = options[:throttle] || 100
  @start = options[:start] || select_start
  @limit = options[:limit] || select_limit
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

#bottom(chunk) ⇒ Object



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

def bottom(chunk)
  (chunk - 1) * @stride + @start
end

#copy(lowest, highest) ⇒ Object



44
45
46
47
48
# File 'lib/lhm/chunker.rb', line 44

def copy(lowest, highest)
  "insert ignore into `#{ destination_name }` (#{ columns }) " +
  "select #{ columns } from `#{ origin_name }` " +
  "where `id` between #{ lowest } and #{ highest }"
end

#select_limitObject



55
56
57
58
# File 'lib/lhm/chunker.rb', line 55

def select_limit
  limit = connection.select_value("select max(id) from #{ origin_name }")
  limit ? limit.to_i : nil
end

#select_startObject



50
51
52
53
# File 'lib/lhm/chunker.rb', line 50

def select_start
  start = connection.select_value("select min(id) from #{ origin_name }")
  start ? start.to_i : nil
end

#throttle_secondsObject



60
61
62
# File 'lib/lhm/chunker.rb', line 60

def throttle_seconds
  @throttle / 1000.0
end

#top(chunk) ⇒ Object



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

def top(chunk)
  [chunk * @stride + @start - 1, @limit].min
end

#traversable_chunks_sizeObject



32
33
34
# File 'lib/lhm/chunker.rb', line 32

def traversable_chunks_size
  @limit && @start ? ((@limit - @start + 1) / @stride.to_f).ceil : 0
end

#up_to(&block) ⇒ Object

Copies chunks of size ‘stride`, starting from `start` up to id `limit`.



26
27
28
29
30
# File 'lib/lhm/chunker.rb', line 26

def up_to(&block)
  1.upto(traversable_chunks_size) do |n|
    yield(bottom(n), top(n))
  end
end