Class: Avro::DataFile::SnappyCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/avro/data_file.rb

Instance Method Summary collapse

Instance Method Details

#codec_nameObject



337
# File 'lib/avro/data_file.rb', line 337

def codec_name; 'snappy'; end

#compress(data) ⇒ Object



359
360
361
362
363
364
# File 'lib/avro/data_file.rb', line 359

def compress(data)
  load_snappy!
  crc32 = Zlib.crc32(data)
  compressed = Snappy.deflate(data)
  [compressed, crc32].pack('a*N')
end

#decompress(data) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/avro/data_file.rb', line 339

def decompress(data)
  load_snappy!
  crc32 = data.slice(-4..-1).unpack('N').first
  uncompressed = Snappy.inflate(data.slice(0..-5))

  if crc32 == Zlib.crc32(uncompressed)
    uncompressed
  else
    # older versions of avro-ruby didn't write the checksum, so if it
    # doesn't match this must assume that it wasn't there and return
    # the entire payload uncompressed.
    Snappy.inflate(data)
  end
rescue Snappy::Error
  # older versions of avro-ruby didn't write the checksum, so removing
  # the last 4 bytes may cause Snappy to fail. recover by assuming the
  # payload is from an older file and uncompress the entire buffer.
  Snappy.inflate(data)
end