Class: Cassandra::Compression::Compressors::Lz4

Inherits:
Cassandra::Compression::Compressor show all
Defined in:
lib/cassandra/compression/compressors/lz4.rb

Overview

Note:

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

Note:

No need to instantiate this class manually, use compression: :lz4 option when calling Cassandra.cluster and one will be created automatically for you.

A compressor that uses the LZ4 compression library.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_size = 64) ⇒ Lz4

Returns a new instance of Lz4.

Parameters:

  • min_size (Integer) (defaults to: 64)

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



42
43
44
45
# File 'lib/cassandra/compression/compressors/lz4.rb', line 42

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

Instance Attribute Details

#algorithmString (readonly)

Returns 'lz4'.

Returns:

  • (String)

    'lz4'



38
39
40
# File 'lib/cassandra/compression/compressors/lz4.rb', line 38

def algorithm
  @algorithm
end

Instance Method Details

#compress(str) ⇒ Object



55
56
57
# File 'lib/cassandra/compression/compressors/lz4.rb', line 55

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.

See Also:



50
51
52
# File 'lib/cassandra/compression/compressors/lz4.rb', line 50

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

#decompress(str) ⇒ Object



60
61
62
63
# File 'lib/cassandra/compression/compressors/lz4.rb', line 60

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