Class: MysqlDumpSlow::SlowLog

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_dump_slow/slow_log.rb

Instance Method Summary collapse

Constructor Details

#initialize(logs) ⇒ SlowLog

Returns a new instance of SlowLog.



3
4
5
# File 'lib/mysql_dump_slow/slow_log.rb', line 3

def initialize(logs)
  @logs = logs
end

Instance Method Details

#find_each(options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mysql_dump_slow/slow_log.rb', line 7

def find_each(options={})
  return @logs.each{ |log| yield log } unless active_record_relation?

  find_in_batches(options) do |records|
    records.each { |record| yield record }
  end
end

#find_in_batches(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mysql_dump_slow/slow_log.rb', line 15

def find_in_batches(options={})
  return @logs.each{ |log| yield log } unless active_record_relation?

  batch_order = options[:batch_order] || :start_time
  batch_size  = options[:batch_size]  || 1000

  relation = @logs
  relation = relation.reorder(batch_order).limit(batch_size)
  records  = relation.to_a

  while records.any?
    records_size = records.size
    primary_key_offset = records.last.start_time

    yield records

    break if records_size < batch_size

    records = relation.where("#{batch_order} > ?", primary_key_offset).to_a
  end
end