Class: Snappy::Hadoop::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/snappy/hadoop/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) {|_self| ... } ⇒ Reader

Returns a new instance of Reader.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
# File 'lib/snappy/hadoop/reader.rb', line 11

def initialize(io)
  @io = Snappy.set_encoding io
  yield self if block_given?
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



9
10
11
# File 'lib/snappy/hadoop/reader.rb', line 9

def io
  @io
end

Instance Method Details

#eachObject



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

def each
  return to_enum unless block_given?

  until @io.eof?
    # Uncompressed size (32 bit integer, BE).
    uncompressed_size = @io.read(4).unpack('N').first

    uncompressed_block_io = StringIO.new
    while uncompressed_block_io.size < uncompressed_size
      # Compressed subblock size (32 bit integer, BE).
      compressed_size = @io.read(4).unpack('N').first
      uncompressed_block_io << Snappy.inflate(@io.read(compressed_size))
    end

    if uncompressed_block_io.size > uncompressed_size
      raise RuntimeError("Invalid data: expected #{uncompressed_size} bytes, got #{Uncompressed.size}")
    end

    yield uncompressed_block_io.string
  end
end

#each_line(sep_string = $/) {|last| ... } ⇒ Object

Yields:

  • (last)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/snappy/hadoop/reader.rb', line 46

def each_line(sep_string = $/)
  return to_enum(:each_line, sep_string) unless block_given?

  last = ""
  each do |chunk|
    chunk = last + chunk
    lines = chunk.split(sep_string)
    last = lines.pop
    lines.each do |line|
      yield line
    end
  end
  yield last
end

#readObject



38
39
40
41
42
43
44
# File 'lib/snappy/hadoop/reader.rb', line 38

def read
  buff = StringIO.new
  each do |chunk|
    buff << chunk
  end
  buff.string
end