class Avro::DataFile::SnappyCodec

Public Instance Methods

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

Private Instance Methods

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