Class: Snappy::Hadoop::Writer

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

Constant Summary collapse

DEFAULT_BLOCK_SIZE =
256 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, block_size = DEFAULT_BLOCK_SIZE) ⇒ Writer

Returns a new instance of Writer.



12
13
14
15
16
17
18
19
20
# File 'lib/snappy/hadoop/writer.rb', line 12

def initialize(io, block_size = DEFAULT_BLOCK_SIZE)
  @block_size = block_size
  @buffer = String.new
  @io = Snappy.set_encoding io
  if block_given?
    yield self
    dump!
  end
end

Instance Attribute Details

#block_sizeObject (readonly)

Returns the value of attribute block_size.



10
11
12
# File 'lib/snappy/hadoop/writer.rb', line 10

def block_size
  @block_size
end

#ioObject (readonly)

Returns the value of attribute io.



10
11
12
# File 'lib/snappy/hadoop/writer.rb', line 10

def io
  @io
end

Instance Method Details

#<<(msg) ⇒ Object Also known as: write



22
23
24
25
# File 'lib/snappy/hadoop/writer.rb', line 22

def <<(msg)
  @buffer << msg.to_s
  dump! if @buffer.size >= @block_size
end

#closeObject



46
47
48
# File 'lib/snappy/hadoop/writer.rb', line 46

def close
  @io.close
end

#dump!Object Also known as: flush



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/snappy/hadoop/writer.rb', line 29

def dump!
  offset = 0
  while offset < @buffer.size
    uncompressed = @buffer[offset, @block_size]
    compressed = Snappy.deflate(uncompressed)

    # Uncompressed size (32 bit integer, BE), compressed size (32 bit integer, BE), data.
    @io << [uncompressed.size, compressed.size, compressed].pack("NNa#{compressed.size}")
    offset += uncompressed.size
  end

  @io.flush
  @buffer.clear
end