Class: Cql::Compression::Lz4Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/cql/compression/lz4_compressor.rb

Overview

Note:

This compressor requires the lz4-ruby gem (v0.3.2 or later required).

A compressor that uses the LZ4 compression library.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_size = 64) ⇒ Lz4Compressor

Returns a new instance of Lz4Compressor.

Parameters:

  • min_size (Integer) (defaults to: 64)

    (64) Don't compress frames smaller than this size (see #compress?).


21
22
23
24
# File 'lib/cql/compression/lz4_compressor.rb', line 21

def initialize(min_size=64)
  @algorithm = 'lz4'.freeze
  @min_size = min_size
end

Instance Attribute Details

#algorithmString (readonly)

Returns:

  • (String)

17
18
19
# File 'lib/cql/compression/lz4_compressor.rb', line 17

def algorithm
  @algorithm
end

Instance Method Details

#compress(str) ⇒ Object


32
33
34
# File 'lib/cql/compression/lz4_compressor.rb', line 32

def compress(str)
  [str.bytesize, LZ4::Raw.compress(str.to_s).first].pack(BUFFER_FORMAT)
end

#compress?(str) ⇒ true, false

Returns will return false for frames smaller than the min_size given to the constructor.

Returns:

  • (true, false)

    will return false for frames smaller than the min_size given to the constructor.


28
29
30
# File 'lib/cql/compression/lz4_compressor.rb', line 28

def compress?(str)
  str.bytesize > @min_size
end

#decompress(str) ⇒ Object


36
37
38
39
# File 'lib/cql/compression/lz4_compressor.rb', line 36

def decompress(str)
  decompressed_size, compressed_data = str.to_s.unpack(BUFFER_FORMAT)
  LZ4::Raw.decompress(compressed_data, decompressed_size).first
end