Class: ActiveRecordCopy::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-copy/decoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Decoder

Returns a new instance of Decoder.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/activerecord-copy/decoder.rb', line 6

def initialize(options = {})
  @options = options
  @closed = false
  if options[:column_types].is_a?(Array)
    map = {}
    options[:column_types].each_with_index do |c, i|
      map[i] = c
    end
    options[:column_types] = map
  else
    options[:column_types] ||= {}
  end
  @io = nil
end

Instance Method Details

#eachObject



46
47
48
49
50
51
52
53
# File 'lib/activerecord-copy/decoder.rb', line 46

def each
  loop do
    result = read_line
    break unless result
    yield result
    break if @closed
  end
end

#read_lineObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/activerecord-copy/decoder.rb', line 21

def read_line
  return nil if @closed
  setup_io unless @io
  row = []
  bytes = @io.read(2)
  # p bytes
  column_count = bytes.unpack(PACKED_UINT_16).first
  if column_count == 65_535
    @closed = true
    return nil
  end
  # @io.write([row.size].pack(PACKED_UINT_32))
  0.upto(column_count - 1).each do |index|
    field = decode_field(@io)
    row[index] = if field.nil?
                   field
                 elsif @options[:column_types][index]
                   map_field(field, @options[:column_types][index])
                 else
                   field
                 end
  end
  row
end