Skip to content

Commit

Permalink
AVRO-1697. Ruby: Add support for the Snappy codec to the Ruby library…
Browse files Browse the repository at this point in the history
…. Contributed by Daniel Schierbeck.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1691330 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
tomwhite committed Jul 16, 2015
1 parent 946e966 commit bc657e4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -99,6 +99,9 @@ Trunk (not yet released)
AVRO-1692. Allow more than one logical type for a Java class. (blue via
tomwhite)

AVRO-1697. Ruby: Add support for the Snappy codec to the Ruby library.
(Daniel Schierbeck via tomwhite)

BUG FIXES

AVRO-1553. Java: MapReduce never uses MapOutputValueSchema (tomwhite)
Expand Down
1 change: 1 addition & 0 deletions lang/ruby/Gemfile
Expand Up @@ -17,3 +17,4 @@ source 'https://rubygems.org'
gem 'rake'
gem 'echoe'
gem 'multi_json'
gem 'snappy'
23 changes: 23 additions & 0 deletions lang/ruby/lib/avro/data_file.rb
Expand Up @@ -333,8 +333,31 @@ def compress(data)
end
end

class SnappyCodec
def codec_name; 'snappy'; end

def decompress(data)
load_snappy!
Snappy.inflate(data)
end

def compress(data)
load_snappy!
Snappy.deflate(data)
end

private

def load_snappy!
require 'snappy' unless defined?(Snappy)
rescue LoadError
raise LoadError, "Snappy compression is not available, please install the `snappy` gem."
end
end

DataFile.register_codec NullCodec
DataFile.register_codec DeflateCodec
DataFile.register_codec SnappyCodec

# TODO this constant won't be updated if you register another codec.
# Deprecated in favor of Avro::DataFile::codecs
Expand Down
13 changes: 13 additions & 0 deletions lang/ruby/test/test_datafile.rb
Expand Up @@ -167,6 +167,19 @@ def test_deflate
assert_equal records, ['a' * 10_000]
end

def test_snappy
Avro::DataFile.open('data.avr', 'w', '"string"', :snappy) do |writer|
writer << 'a' * 10_000
end
assert(File.size('data.avr') < 600)

records = []
Avro::DataFile.open('data.avr') do |reader|
reader.each {|record| records << record }
end
assert_equal records, ['a' * 10_000]
end

def test_append_to_deflated_file
schema = Avro::Schema.parse('"string"')
writer = Avro::IO::DatumWriter.new(schema)
Expand Down
5 changes: 3 additions & 2 deletions share/docker/Dockerfile
Expand Up @@ -31,7 +31,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
perl \
php5 phpunit php5-gmp \
python python-setuptools python3-setuptools \
ruby ruby-dev rake
ruby ruby-dev rake \
libsnappy1 libsnappy-dev

# Install Forrest
RUN mkdir -p /usr/local/apache-forrest
Expand All @@ -48,7 +49,7 @@ RUN cpanm install Module::Install Module::Install::ReadmeFromPod \
Test::Exception Test::Pod

# Install Ruby modules
RUN gem install echoe yajl-ruby multi_json
RUN gem install echoe yajl-ruby multi_json snappy

# Install global Node modules
RUN npm install -g grunt-cli

0 comments on commit bc657e4

Please sign in to comment.