Class: Lhm::Chunker
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from SqlHelper
#annotation, #idx_name, #idx_spec, #sql, #table?, #update, #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
@stride = options[:stride] || 40_000
@throttle = options[:throttle] || 100
@start = options[:start] || select_start
@limit = options[:limit] || select_limit
end
|
Instance Attribute Details
#connection ⇒ Object
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
49
|
# File 'lib/lhm/chunker.rb', line 44
def copy(lowest, highest)
"insert ignore into `#{ destination_name }` (#{ columns }) " +
"select #{ columns_with_joins } from `#{ origin_name }` " +
"#{ joins } " +
"where #{origin_name}.`id` between #{ lowest } and #{ highest }"
end
|
#join_fields ⇒ Object
59
60
61
|
# File 'lib/lhm/chunker.rb', line 59
def join_fields
@migration.insert_joins.map {|j| "`#{ j[:destination_field] }`"}
end
|
#join_fields_typed ⇒ Object
55
56
57
|
# File 'lib/lhm/chunker.rb', line 55
def join_fields_typed
@migration.insert_joins.map {|j| "#{j[:table]}.`#{j[:origin_field]}`"}
end
|
#joins ⇒ Object
51
52
53
|
# File 'lib/lhm/chunker.rb', line 51
def joins
@migration.insert_joins.map{|j| "join #{ j[:table] } on #{ j[:statement] }"}.join(" ")
end
|
#select_limit ⇒ Object
68
69
70
71
|
# File 'lib/lhm/chunker.rb', line 68
def select_limit
limit = connection.select_value("select max(id) from #{ origin_name }")
limit ? limit.to_i : nil
end
|
#select_start ⇒ Object
63
64
65
66
|
# File 'lib/lhm/chunker.rb', line 63
def select_start
start = connection.select_value("select min(id) from #{ origin_name }")
start ? start.to_i : nil
end
|
#throttle_seconds ⇒ Object
73
74
75
|
# File 'lib/lhm/chunker.rb', line 73
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_size ⇒ Object
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
|