class EM::Voldemort::Compressor

Compression/decompression codec for keys and values in a store

Attributes

options[R]
type[R]

Public Class Methods

new(xml) click to toggle source
# File lib/em-voldemort/compressor.rb, line 7
def initialize(xml)
  @type = xml && xml.xpath('type').text
  @options = xml && xml.xpath('options').text
  if type != nil && type != 'gzip'
    raise "Unsupported compression codec: #{type}"
  end
end

Public Instance Methods

decode(data) click to toggle source
# File lib/em-voldemort/compressor.rb, line 30
def decode(data)
  case type
  when nil
    data
  when 'gzip'
    Zlib::GzipReader.new(StringIO.new(data)).read.force_encoding(Encoding::BINARY)
  end
end
encode(data) click to toggle source
# File lib/em-voldemort/compressor.rb, line 15
def encode(data)
  case type
  when nil
    data
  when 'gzip'
    buffer = StringIO.new
    buffer.set_encoding(Encoding::BINARY)
    gz = Zlib::GzipWriter.new(buffer)
    gz.write(data)
    gz.close
    buffer.rewind
    buffer.string
  end
end