Class: GroongaDelta::MySQLSource

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga-delta/mysql-source.rb

Defined Under Namespace

Classes: CurrentStatus

Instance Method Summary collapse

Constructor Details

#initialize(config, status, writer) ⇒ MySQLSource

Returns a new instance of MySQLSource.



25
26
27
28
29
30
31
32
33
# File 'lib/groonga-delta/mysql-source.rb', line 25

def initialize(config, status, writer)
  @logger = config.logger
  @config = config.mysql
  @binlog_dir = @config.binlog_dir
  @mapping = config.mapping
  @status = status.mysql
  @writer = writer
  @tables = {}
end

Instance Method Details

#importObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/groonga-delta/mysql-source.rb', line 35

def import
  current_status = read_current_status
  is_mysql_56_or_later = mysql(@config.select_user,
                               @config.select_password) do |select_client|
    mysql_version(select_client) >= Gem::Version.new("5.6")
  end
  mysql(@config.replication_slave_user,
        @config.replication_slave_password) do |client|
    if is_mysql_56_or_later
      replication_client = Mysql2Replication::Client.new(client)
    else
      replication_client = Mysql2Replication::Client.new(client,
                                                         checksum: "NONE")
    end
    current_file = current_status.last_table_map_file
    replication_client.file_name = current_file
    current_event_position = current_status.last_table_map_position
    if current_event_position
      replication_client.start_position = current_event_position
    end
    replication_client.open do
      replication_client.each do |event|
        begin
          @logger.debug do
            event.inspect
          end
          case event
          when Mysql2Replication::RotateEvent
            current_file = event.file_name
            current_status.last_file = current_file
            current_status.last_position = event.position
          when Mysql2Replication::TableMapEvent
            current_status.last_table_map_file = current_file
            current_status.last_table_map_position = current_event_position
          when Mysql2Replication::RowsEvent
            next if current_file != current_status.last_file
            next if current_event_position < current_status.last_position
            import_rows_event(event, current_status)
          end
        ensure
          # next_position may be 0. For example,
          # Mysql2Replication::FormatDescriptionEvent#next_position
          # returns 0.
          next_position = event.next_position
          current_event_position = next_position unless next_position.zero?
        end
      end
    end
  end
end