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



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

def codec_name; 'snappy'; end

#compress(data) ⇒ Object



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

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

#decompress(data) ⇒ Object



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

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