├── .gitignore ├── .rspec ├── Gemfile ├── README.md ├── Rakefile ├── ext ├── extconf.rb └── libsnappy.cc ├── libsnappy.gemspec └── spec └── snappy_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/igrigorik/libsnappy/ac407f0b723abf969076e2d3fbe05015f4c02bf8/.rspec -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gemspec -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snappy 2 | 3 | Ruby wrapper for Google's fast compressor/decompressor: http://code.google.com/p/snappy/ 4 | 5 | Snappy is a compression/decompression library. It does not aim for maximum compression, or compatibility with any other compression library; instead, it aims for very high speeds and reasonable compression. For instance, compared to the fastest mode of zlib, Snappy is an order of magnitude faster for most inputs, but the resulting compressed files are anywhere from 20% to 100% bigger. On a single core of a Core i7 processor in 64-bit mode, Snappy compresses at about 250 MB/sec or more and decompresses at about 500 MB/sec or more. 6 | 7 | Snappy is widely used inside Google, in everything from BigTable and MapReduce to our internal RPC systems. (Snappy has previously been referred to as “Zippy” in some presentations and the likes.) 8 | 9 | ## Installation 10 | 11 | - Grab the latest Snappy build and install it on your system: 12 | - [http://code.google.com/p/snappy/](http://code.google.com/p/snappy/) 13 | - You may need 'Google Test' and 'Google Flags' to build Snappy: 14 | - [http://code.google.com/p/googletest/](http://code.google.com/p/googletest/]) 15 | - [http://code.google.com/p/google-gflags/](http://code.google.com/p/google-gflags/) 16 | 17 | Once you have Snappy installed on your system, you can install the gem: 18 | 19 | gem install libsnappy 20 | 21 | ## Example 22 | 23 | compressed = Snappy.compress('string to compress') 24 | uncompressed = Snappy.uncompress(compressed) 25 | 26 | For benchmarks and motivation behind Snappy see: 27 | 28 | - [http://blog.sesse.net/blog/tech/2011-03-22-19-24_snappy](http://blog.sesse.net/blog/tech/2011-03-22-19-24_snappy) 29 | - [http://pastebin.com/SFaNzRuf](http://pastebin.com/SFaNzRuf) 30 | 31 | ### License 32 | 33 | (MIT License) - Copyright (c) 2011 Ilya Grigorik -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | 4 | require 'rspec/core/rake_task' 5 | require 'rake/extensiontask' 6 | 7 | Rake::ExtensionTask.new do |ext| 8 | ext.name = 'libsnappy' 9 | ext.ext_dir = 'ext' 10 | ext.lib_dir = 'lib' 11 | ext.config_script = 'extconf.rb' 12 | end 13 | 14 | desc "Run all RSpec tests" 15 | RSpec::Core::RakeTask.new(:spec) 16 | 17 | desc "Build libsnappy, then run tests." 18 | task :default => [:compile, :spec] -------------------------------------------------------------------------------- /ext/extconf.rb: -------------------------------------------------------------------------------- 1 | require 'mkmf' 2 | 3 | extension_name = 'libsnappy' 4 | $LIBS << " -lstdc++ -lsnappy" 5 | 6 | create_makefile(extension_name) -------------------------------------------------------------------------------- /ext/libsnappy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace snappy; 5 | 6 | typedef VALUE (ruby_method)(...); 7 | 8 | extern "C" VALUE compress(VALUE self, VALUE input) { 9 | string *out = new string(); 10 | 11 | size_t sz = snappy::Compress(RSTRING_PTR(input), RSTRING_LEN(input), out); 12 | VALUE ret = rb_str_new(out->c_str(), sz); 13 | delete out; 14 | 15 | return ret; 16 | } 17 | 18 | extern "C" VALUE uncompress(VALUE self, VALUE input) { 19 | VALUE ret; 20 | string *out = new string(); 21 | 22 | bool pass = snappy::Uncompress(RSTRING_PTR(input), RSTRING_LEN(input), out); 23 | 24 | if (pass) 25 | ret = rb_str_new(out->c_str(), out->length()); 26 | else 27 | ret = Qnil; 28 | 29 | delete out; 30 | return ret; 31 | } 32 | 33 | static VALUE RSnappy; 34 | 35 | extern "C" void Init_libsnappy() 36 | { 37 | RSnappy = rb_define_class("Snappy", rb_cObject); 38 | rb_define_singleton_method(RSnappy, "compress", (ruby_method*) &compress, 1); 39 | rb_define_singleton_method(RSnappy, "uncompress", (ruby_method*) &uncompress, 1); 40 | } -------------------------------------------------------------------------------- /libsnappy.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | 4 | Gem::Specification.new do |s| 5 | s.name = "libsnappy" 6 | s.version = "0.1.0" 7 | s.platform = Gem::Platform::RUBY 8 | s.authors = ["Ilya Grigorik", "Michael Bernstein"] 9 | s.email = ["ilya@igvita.com", "michael@spaceshipknows.com"] 10 | s.homepage = "http://code.google.com/p/snappy/" 11 | s.summary = "Snappy, a fast compressor/decompressor (courtesy of Google)" 12 | s.description = s.summary 13 | 14 | s.rubyforge_project = "libsnappy" 15 | s.extensions = ["ext/extconf.rb"] 16 | 17 | s.add_development_dependency "rake-compiler", "0.7.6" 18 | s.add_development_dependency "rspec" 19 | 20 | s.files = `git ls-files`.split("\n") 21 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 22 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 23 | s.require_paths = ["lib"] 24 | end 25 | -------------------------------------------------------------------------------- /spec/snappy_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require 'libsnappy' 3 | 4 | describe Snappy do 5 | it 'should compress data' do 6 | lambda do 7 | Snappy.compress('abcdefg') 8 | end.should_not raise_error 9 | end 10 | 11 | it 'should uncompress data' do 12 | lambda do 13 | Snappy.uncompress(Snappy.compress('abcdefg')) 14 | end.should_not raise_error 15 | end 16 | 17 | it 'should roundtrip the data' do 18 | original = 'abcdefg' * 100 19 | 20 | compressed = Snappy.compress(original) 21 | uncompressed = Snappy.uncompress(compressed) 22 | 23 | original.should == uncompressed 24 | compressed.size.should < original.size 25 | end 26 | end --------------------------------------------------------------------------------