Class: Flydata::Mysql::MysqlDumpParser

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/command/sync.rb

Defined Under Namespace

Modules: State Classes: InsertParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, option = {}) ⇒ MysqlDumpParser

Returns a new instance of MysqlDumpParser.



1013
1014
1015
1016
1017
1018
# File 'lib/flydata/command/sync.rb', line 1013

def initialize(file_path, option = {})
  @file_path = file_path
  raise "Dump file does not exist. file_path:#{file_path}" unless File.exist?(file_path)
  @binlog_pos = option[:binlog_pos]
  @option = option
end

Instance Attribute Details

#binlog_posObject

Returns the value of attribute binlog_pos.



1011
1012
1013
# File 'lib/flydata/command/sync.rb', line 1011

def binlog_pos
  @binlog_pos
end

Instance Method Details

#parse(create_table_block, insert_record_block, check_point_block) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/flydata/command/sync.rb', line 1020

def parse(create_table_block, insert_record_block, check_point_block)
  invalid_file = false
  current_state = State::START
  substate = nil

  state_start = Proc.new do |f|
    line = f.readline.strip
    # -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=120;
    m = /^\-\- CHANGE MASTER TO MASTER_LOG_FILE='(?<binfile>[^']+)', MASTER_LOG_POS=(?<pos>\d+)/.match(line)
    if m
      @binlog_pos = {binfile: m[:binfile], pos: m[:pos].to_i}
      current_state = State::CREATE_TABLE
      check_point_block.call(nil, f.pos, @binlog_pos, current_state)
    end
  end

  current_table = nil
  state_create_table = Proc.new do |f|
    line = f.readline.strip
    # CREATE TABLE `active_admin_comments` (
    m = /^CREATE TABLE `(?<table_name>[^`]+)`/.match(line)
    if m
      current_table = MysqlTable.new(m[:table_name])
      current_state = State::CREATE_TABLE_COLUMNS
    end
  end

  state_create_table_constraints = Proc.new do |f|
    line = f.readline.strip
    #  PRIMARY KEY (`id`),
    if line.start_with?(')')
      create_table_block.call(current_table)
      current_state = State::INSERT_RECORD
      check_point_block.call(current_table, f.pos, @binlog_pos, current_state)
    elsif m = /^PRIMARY KEY \((?<primary_keys>[^\)]+)\)/.match(line)
      current_table.primary_keys = m[:primary_keys].split(',').collect do |pk_str|
        pk_str[1..-2]
      end
    end
  end

  state_create_table_columns = Proc.new do |f|
    start_pos = f.pos
    line = f.readline.strip
    #  `author_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
    if line.start_with?("\`")
      column = {}

      # parse column line
      line = line[0..-2] if line.end_with?(',')
      items = line.split
      column[:column_name] = items.shift[1..-2]
      column[:format_type_str] = format_type_str = items.shift
      pos = format_type_str.index('(')
      if pos
        ft = column[:format_type] = format_type_str[0..pos-1]
        if ft == 'decimal'
          precision, scale = format_type_str[pos+1..-2].split(',').collect{|v| v.to_i}
          column[:decimal_precision] = precision
          column[:decimal_scale] = scale
        else
          column[:format_size] = format_type_str[pos+1..-2].to_i
        end
      else
        column[:format_type] = format_type_str
      end
      while (item = items.shift) do
        case item
        when 'DEFAULT'
          value = items.shift
          value = value.start_with?('\'') ? value[1..-2] : value
          value = nil if value == 'NULL'
          column[:default] = value
        when 'NOT'
          if items[1] == 'NULL'
            items.shift
            column[:not_null] = true
          end
        when 'unsigned'
          column[:unsigned] = true
        else
          #ignore other options
        end
      end

      current_table.add_column(column)
    else
      current_state = State::CREATE_TABLE_CONSTRAINTS
      f.pos = start_pos
      state_create_table_constraints.call(f)
    end
  end

  state_insert_record = Proc.new do |f|
    original_pos = f.pos
    command = f.read(6)
    if command == 'INSERT'
      current_state = State::PARSING_INSERT_RECORD
    else
      f.pos = original_pos
      f.readline
      if command == 'UNLOCK'
        current_state = State::CREATE_TABLE
        check_point_block.call(current_table, f.pos, @binlog_pos, current_state)
      end
    end
  end

  state_parsing_insert_record = Proc.new do |f|
    values_set = InsertParser.new(f).parse
    current_state = State::INSERT_RECORD
    if insert_record_block.call(current_table, values_set)
      check_point_block.call(current_table, f.pos, @binlog_pos, current_state)
    end
  end

  # Start reading file from top
  File.open(@file_path, 'r', encoding: "utf-8") do |f|
    last_saved_pos = 0

    # resume
    if @option[:last_pos]
      f.pos = @option[:last_pos].to_i
      current_state = @option[:state]
      substate = @option[:substate]
      current_table = @option[:mysql_table]
    end

    until f.eof? do
      case current_state
      when State::START
        state_start.call(f)
      when State::CREATE_TABLE
        state_create_table.call(f)
      when State::CREATE_TABLE_COLUMNS
        state_create_table_columns.call(f)
      when State::CREATE_TABLE_CONSTRAINTS
        state_create_table_constraints.call(f)
      when State::INSERT_RECORD
        state_insert_record.call(f)
      when State::PARSING_INSERT_RECORD
        state_parsing_insert_record.call(f)
      end
    end
  end
  @binlog_pos
end