class Avro::DataFile::SnappyCodec

Public Instance Methods

codec_name() click to toggle source
    # File lib/avro/data_file.rb
337 def codec_name; 'snappy'; end
compress(data) click to toggle source
    # File lib/avro/data_file.rb
359 def compress(data)
360   load_snappy!
361   crc32 = Zlib.crc32(data)
362   compressed = Snappy.deflate(data)
363   [compressed, crc32].pack('a*N')
364 end
decompress(data) click to toggle source
    # File lib/avro/data_file.rb
339 def decompress(data)
340   load_snappy!
341   crc32 = data.slice(-4..-1).unpack('N').first
342   uncompressed = Snappy.inflate(data.slice(0..-5))
343 
344   if crc32 == Zlib.crc32(uncompressed)
345     uncompressed
346   else
347     # older versions of avro-ruby didn't write the checksum, so if it
348     # doesn't match this must assume that it wasn't there and return
349     # the entire payload uncompressed.
350     Snappy.inflate(data)
351   end
352 rescue Snappy::Error
353   # older versions of avro-ruby didn't write the checksum, so removing
354   # the last 4 bytes may cause Snappy to fail. recover by assuming the
355   # payload is from an older file and uncompress the entire buffer.
356   Snappy.inflate(data)
357 end

Private Instance Methods

load_snappy!() click to toggle source
    # File lib/avro/data_file.rb
368 def load_snappy!
369   require 'snappy' unless defined?(Snappy)
370 rescue LoadError
371   raise LoadError, "Snappy compression is not available, please install the `snappy` gem."
372 end