Class: EM::Voldemort::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/em-voldemort/compressor.rb

Overview

Compression/decompression codec for keys and values in a store

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Compressor

Returns a new instance of Compressor.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/em-voldemort/compressor.rb', line 5

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/em-voldemort/compressor.rb', line 5

def type
  @type
end

Instance Method Details

#decode(data) ⇒ Object



30
31
32
33
34
35
36
37
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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