Class: RedshiftConnector::Reader::RedshiftCSV::ScanBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_connector/reader/redshift_csv.rb

Constant Summary collapse

MAX_COLUMN_LENGTH =

1.2MB

(1.2 * (1024 ** 3)).to_i

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ ScanBuffer

Returns a new instance of ScanBuffer.



66
67
68
69
70
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 66

def initialize(f)
  @f = f
  @s = StringScanner.new("")
  @eof = false
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 72

def eof?
  @s.eos? && @eof
end

#fill_bufferObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 99

def fill_buffer
  line = @f.gets
  if line
    @s << line
    true
  else
    @eof = true
    false
  end
end

#linenoObject



76
77
78
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 76

def lineno
  @f.lineno
end

#next_rowObject



80
81
82
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 80

def next_row
  fill_buffer
end

#read_eolObject



114
115
116
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 114

def read_eol
  @s.skip(/[ \t\r]*(?:\n|\z)/)
end

#read_separatorObject



110
111
112
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 110

def read_separator
  @s.skip(/[ \t]*,/)
end

#scan_columnObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redshift_connector/reader/redshift_csv.rb', line 86

def scan_column
  s = @s
  s.skip(/[ \t]+/)
  until column = s.scan(/"(?:\\.|[^"\\])*"/m)
    fill_buffer or return nil
    return nil if s.eos?
    if s.rest_size > MAX_COLUMN_LENGTH
      raise Reader::MalformedCSVException, "CSV parse error: too long column at line #{@f.lineno}"
    end
  end
  column
end