snappy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,8 +6,8 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "rspec", "~> 2.3.0"
10
- gem "bundler", "~> 1.0.0"
11
- gem "jeweler", "~> 1.5.2"
9
+ gem "rspec", "~> 2.11.0"
10
+ gem "bundler", "~> 1.1.0"
11
+ gem "jeweler", "~> 1.8.4"
12
12
  gem "rcov", ">= 0"
13
13
  end
@@ -1,28 +1,32 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- diff-lcs (1.1.2)
4
+ diff-lcs (1.1.3)
5
5
  git (1.2.5)
6
- jeweler (1.5.2)
7
- bundler (~> 1.0.0)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
8
  git (>= 1.2.5)
9
9
  rake
10
- rake (0.8.7)
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (0.9.2.2)
11
13
  rcov (0.9.9)
12
- rspec (2.3.0)
13
- rspec-core (~> 2.3.0)
14
- rspec-expectations (~> 2.3.0)
15
- rspec-mocks (~> 2.3.0)
16
- rspec-core (2.3.1)
17
- rspec-expectations (2.3.0)
18
- diff-lcs (~> 1.1.2)
19
- rspec-mocks (2.3.0)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.11.0)
17
+ rspec-core (~> 2.11.0)
18
+ rspec-expectations (~> 2.11.0)
19
+ rspec-mocks (~> 2.11.0)
20
+ rspec-core (2.11.1)
21
+ rspec-expectations (2.11.3)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.11.2)
20
24
 
21
25
  PLATFORMS
22
26
  ruby
23
27
 
24
28
  DEPENDENCIES
25
- bundler (~> 1.0.0)
26
- jeweler (~> 1.5.2)
29
+ bundler (~> 1.1.0)
30
+ jeweler (~> 1.8.4)
27
31
  rcov
28
- rspec (~> 2.3.0)
32
+ rspec (~> 2.11.0)
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
+ require 'rbconfig'
3
4
  begin
4
5
  Bundler.setup(:default, :development)
5
6
  rescue Bundler::BundlerError => e
@@ -9,6 +10,8 @@ rescue Bundler::BundlerError => e
9
10
  end
10
11
  require 'rake'
11
12
 
13
+ DLEXT = RbConfig::CONFIG['DLEXT']
14
+
12
15
  require 'jeweler'
13
16
  Jeweler::Tasks.new do |gem|
14
17
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
@@ -48,3 +51,17 @@ Rake::RDocTask.new do |rdoc|
48
51
  rdoc.rdoc_files.include('README*')
49
52
  rdoc.rdoc_files.include('ext/**/*.cc')
50
53
  end
54
+
55
+ task :spec => "ext/snappy.#{DLEXT}"
56
+
57
+ file "ext/snappy.#{DLEXT}" => Dir.glob("ext/*{.rb,.c}") do
58
+ Dir.chdir("ext") do
59
+ ruby "extconf.rb"
60
+ sh "make"
61
+ end
62
+ cp "ext/snappy.#{DLEXT}", "lib"
63
+ end
64
+
65
+ task :clean do
66
+ rm_rf(["ext/snappy.#{DLEXT}", "lib/snappy.#{DLEXT}", "ext/mkmf.log", "ext/config.h", "ext/api.o", "ext/Makefile", "ext/snappy.cc", "ext/snappy.h", "ext/snappy.o"] + Dir["ext/snappy-*"])
67
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/ext/api.cc CHANGED
@@ -65,5 +65,8 @@ void Init_snappy()
65
65
 
66
66
  rb_define_singleton_method(rb_mSnappy, "compress", (VALUE (*)(...))snappy_deflate, -1);
67
67
  rb_define_singleton_method(rb_mSnappy, "uncompress", (VALUE (*)(...))snappy_inflate, -1);
68
+
69
+ rb_require("snappy/writer");
70
+ rb_require("snappy/reader");
68
71
  }
69
72
  }
@@ -0,0 +1,54 @@
1
+ module Snappy
2
+ class Reader
3
+ attr_reader :io, :magic, :default_version, :minimum_compatible_version
4
+
5
+ def initialize io
6
+ @io = io
7
+ @io.set_encoding Encoding::ASCII_8BIT unless RUBY_VERSION =~ /^1\.8/
8
+ read_header!
9
+ yield self if block_given?
10
+ end
11
+
12
+ def each
13
+ until @io.eof?
14
+ size = @chunked ? @io.read(4).unpack("N").first : @io.length
15
+ yield Snappy.inflate(@io.read(size)) if block_given?
16
+ end
17
+ end
18
+
19
+ def read
20
+ @buff = StringIO.new
21
+ each do |chunk|
22
+ @buff << chunk
23
+ end
24
+ @buff.string
25
+ end
26
+
27
+ def each_line(sep_string=$/)
28
+ last = ""
29
+ each do |chunk|
30
+ chunk = last + chunk
31
+ lines = chunk.split(sep_string)
32
+ last = lines.pop
33
+ lines.each do |line|
34
+ yield line if block_given?
35
+ end
36
+ end
37
+ yield last
38
+ end
39
+
40
+ private
41
+
42
+ def read_header!
43
+ magic_byte = @io.readchar()
44
+ @io.ungetc(magic_byte)
45
+
46
+ if @io.length >= 16 && magic_byte == Snappy::Writer::MAGIC[0]
47
+ @magic, @default_version, @minimum_compatible_version = @io.read(16).unpack("a8NN")
48
+ @chunked = true
49
+ else
50
+ @chunked = false
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,42 @@
1
+ module Snappy
2
+ class Writer
3
+ MAGIC = "\202SNAPPY\000"
4
+ DEFAULT_VERSION = 1
5
+ MINIMUM_COMPATIBLE_VERSION = 1
6
+ DEFAULT_BLOCK_SIZE = 32 * 1024
7
+
8
+ attr_reader :io, :block_size
9
+
10
+ def initialize io, block_size = DEFAULT_BLOCK_SIZE
11
+ @block_size = block_size
12
+ @buffer = ""
13
+ @io = io
14
+ @io.set_encoding Encoding::ASCII_8BIT unless RUBY_VERSION =~ /^1\.8/
15
+ write_header!
16
+ if block_given?
17
+ yield self
18
+ dump!
19
+ end
20
+ end
21
+
22
+ def << msg
23
+ @buffer << msg.to_s
24
+ dump! if @buffer.size > @block_size
25
+ end
26
+
27
+ def dump!
28
+ compressed = Snappy.deflate(@buffer)
29
+ @io << [compressed.size, compressed].pack("Na#{compressed.size}")
30
+ @io.flush
31
+ @buffer = ""
32
+ end
33
+
34
+ alias_method :flush, :dump!
35
+
36
+ private
37
+
38
+ def write_header!
39
+ @io << [MAGIC, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION].pack("a8NN")
40
+ end
41
+ end
42
+ end
@@ -4,15 +4,15 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{snappy}
8
- s.version = "0.0.3"
7
+ s.name = "snappy"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{miyucy}]
12
- s.date = %q{2011-10-13}
13
- s.description = %q{libsnappy binding for Ruby}
14
- s.email = %q{miyucy@gmail.com}
15
- s.extensions = [%q{ext/extconf.rb}]
11
+ s.authors = ["miyucy"]
12
+ s.date = "2012-12-17"
13
+ s.description = "libsnappy binding for Ruby"
14
+ s.email = "miyucy@gmail.com"
15
+ s.extensions = ["ext/extconf.rb"]
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE.txt",
18
18
  "README.rdoc"
@@ -28,38 +28,37 @@ Gem::Specification.new do |s|
28
28
  "VERSION",
29
29
  "ext/api.cc",
30
30
  "ext/extconf.rb",
31
+ "lib/snappy/reader.rb",
32
+ "lib/snappy/writer.rb",
31
33
  "snappy.gemspec",
32
- "spec/snappy_spec.rb",
33
- "spec/spec_helper.rb"
34
- ]
35
- s.homepage = %q{http://github.com/miyucy/snappy}
36
- s.licenses = [%q{MIT}]
37
- s.require_paths = [%q{lib}]
38
- s.rubygems_version = %q{1.8.9}
39
- s.summary = %q{libsnappy binding for Ruby}
40
- s.test_files = [
41
- "spec/snappy_spec.rb",
34
+ "spec/snappy/reader_spec.rb",
35
+ "spec/snappy/writer_spec.rb",
42
36
  "spec/spec_helper.rb"
43
37
  ]
38
+ s.homepage = "http://github.com/miyucy/snappy"
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = "1.8.24"
42
+ s.summary = "libsnappy binding for Ruby"
44
43
 
45
44
  if s.respond_to? :specification_version then
46
45
  s.specification_version = 3
47
46
 
48
47
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
- s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
50
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
48
+ s.add_development_dependency(%q<rspec>, ["~> 2.11.0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
52
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
53
52
  else
54
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
55
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.11.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
57
56
  s.add_dependency(%q<rcov>, [">= 0"])
58
57
  end
59
58
  else
60
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
61
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.11.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
63
62
  s.add_dependency(%q<rcov>, [">= 0"])
64
63
  end
65
64
  end
@@ -0,0 +1,112 @@
1
+ require "spec_helper"
2
+
3
+ module Snappy
4
+ describe Reader do
5
+ before do
6
+ @buffer = StringIO.new
7
+ Writer.new @buffer do |w|
8
+ w << "foo"
9
+ w << "bar"
10
+ w << "baz"
11
+ w << "quux"
12
+ end
13
+ @buffer.rewind
14
+ end
15
+
16
+ subject do
17
+ Reader.new @buffer
18
+ end
19
+
20
+ describe :initialize do
21
+ it "should yield itself to the block" do
22
+ yielded = nil
23
+ returned = Reader.new @buffer do |r|
24
+ yielded = r
25
+ end
26
+ returned.should == yielded
27
+ end
28
+
29
+ it "should read the header" do
30
+ subject.magic.should == Writer::MAGIC
31
+ subject.default_version.should == Writer::DEFAULT_VERSION
32
+ subject.minimum_compatible_version == Writer::MINIMUM_COMPATIBLE_VERSION
33
+ end
34
+ end
35
+
36
+ describe :initialize_without_headers do
37
+ before do
38
+ @buffer = StringIO.new
39
+ @buffer << Snappy.deflate("HelloWorld" * 10)
40
+ @buffer.rewind
41
+ end
42
+
43
+ it "should inflate with a magic header" do
44
+ subject.read.should == ("HelloWorld" * 10)
45
+ end
46
+ end
47
+
48
+ describe :io do
49
+ it "should be a constructor argument" do
50
+ subject.io.should == @buffer
51
+ end
52
+ end
53
+
54
+ describe :each do
55
+ before do
56
+ Writer.new @buffer do |w|
57
+ w << "foo"
58
+ w << "bar"
59
+ w.dump!
60
+ w << "baz"
61
+ w << "quux"
62
+ end
63
+ @buffer.rewind
64
+ end
65
+
66
+ it "should yield each chunk" do
67
+ chunks = []
68
+ Reader.new(@buffer).each do |chunk|
69
+ chunks << chunk
70
+ end
71
+ chunks.should == ["foobar", "bazquux"]
72
+ end
73
+ end
74
+
75
+ describe :read do
76
+ before do
77
+ Writer.new @buffer do |w|
78
+ w << "foo"
79
+ w << "bar"
80
+ w << "baz"
81
+ w << "quux"
82
+ end
83
+ @buffer.rewind
84
+ end
85
+
86
+ it "should return the bytes" do
87
+ Reader.new(@buffer).read.should == "foobarbazquux"
88
+ end
89
+ end
90
+
91
+ describe :each_line do
92
+ before do
93
+ Writer.new @buffer do |w|
94
+ w << "foo\n"
95
+ w << "bar"
96
+ w.dump!
97
+ w << "baz\n"
98
+ w << "quux\n"
99
+ end
100
+ @buffer.rewind
101
+ end
102
+
103
+ it "should yield each line" do
104
+ lines = []
105
+ Reader.new(@buffer).each_line do |line|
106
+ lines << line
107
+ end
108
+ lines.should == ["foo", "barbaz", "quux"]
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+
3
+ module Snappy
4
+ describe Writer do
5
+ before do
6
+ @buffer = StringIO.new
7
+ end
8
+
9
+ subject do
10
+ Writer.new @buffer
11
+ end
12
+
13
+ describe :initialize do
14
+ it "should yield itself to the block" do
15
+ yielded = nil
16
+ returned = Writer.new @buffer do |w|
17
+ yielded = w
18
+ end
19
+ returned.should == yielded
20
+ end
21
+
22
+ it "should write the header" do
23
+ subject.io.string.should == [Writer::MAGIC, Writer::DEFAULT_VERSION, Writer::MINIMUM_COMPATIBLE_VERSION].pack("a8NN")
24
+ end
25
+
26
+ it "should dump on the end of yield" do
27
+ Writer.new @buffer do |w|
28
+ w << "foo"
29
+ end
30
+ foo = Snappy.deflate "foo"
31
+ @buffer.string[16, @buffer.size - 16].should == [foo.size, foo].pack("Na#{foo.size}")
32
+ end
33
+ end
34
+
35
+ describe :io do
36
+ it "should be a constructor argument" do
37
+ io = StringIO.new
38
+ Writer.new(io).io.should == io
39
+ end
40
+ end
41
+
42
+ describe :block_size do
43
+ it "should default to DEFAULT_BLOCK_SIZE" do
44
+ Writer.new(StringIO.new).block_size.should == Writer::DEFAULT_BLOCK_SIZE
45
+ end
46
+
47
+ it "should be settable via the constructor" do
48
+ Writer.new(StringIO.new, 42).block_size.should == 42
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,7 +1,8 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rbconfig'
3
4
  require 'rspec'
4
- require 'snappy'
5
+ require "snappy.#{RbConfig::CONFIG['DLEXT']}"
5
6
 
6
7
  # Requires supporting files with custom matchers and macros, etc,
7
8
  # in ./support/ and its subdirectories.
metadata CHANGED
@@ -1,94 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: snappy
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - miyucy
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-10-13 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
22
17
  none: false
23
- requirements:
18
+ requirements:
24
19
  - - ~>
25
- - !ruby/object:Gem::Version
26
- hash: 3
27
- segments:
28
- - 2
29
- - 3
30
- - 0
31
- version: 2.3.0
32
- requirement: *id001
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
33
22
  type: :development
34
23
  prerelease: false
35
- name: rspec
36
- - !ruby/object:Gem::Dependency
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
38
25
  none: false
39
- requirements:
26
+ requirements:
40
27
  - - ~>
41
- - !ruby/object:Gem::Version
42
- hash: 23
43
- segments:
44
- - 1
45
- - 0
46
- - 0
47
- version: 1.0.0
48
- requirement: *id002
49
- type: :development
50
- prerelease: false
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.0
30
+ - !ruby/object:Gem::Dependency
51
31
  name: bundler
52
- - !ruby/object:Gem::Dependency
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
54
33
  none: false
55
- requirements:
34
+ requirements:
56
35
  - - ~>
57
- - !ruby/object:Gem::Version
58
- hash: 7
59
- segments:
60
- - 1
61
- - 5
62
- - 2
63
- version: 1.5.2
64
- requirement: *id003
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.0
65
38
  type: :development
66
39
  prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.0
46
+ - !ruby/object:Gem::Dependency
67
47
  name: jeweler
68
- - !ruby/object:Gem::Dependency
69
- version_requirements: &id004 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
70
49
  none: false
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- hash: 3
75
- segments:
76
- - 0
77
- version: "0"
78
- requirement: *id004
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
79
54
  type: :development
80
55
  prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ - !ruby/object:Gem::Dependency
81
63
  name: rcov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
82
78
  description: libsnappy binding for Ruby
83
79
  email: miyucy@gmail.com
84
80
  executables: []
85
-
86
- extensions:
81
+ extensions:
87
82
  - ext/extconf.rb
88
- extra_rdoc_files:
83
+ extra_rdoc_files:
89
84
  - LICENSE.txt
90
85
  - README.rdoc
91
- files:
86
+ files:
92
87
  - .document
93
88
  - .rspec
94
89
  - Gemfile
@@ -99,42 +94,38 @@ files:
99
94
  - VERSION
100
95
  - ext/api.cc
101
96
  - ext/extconf.rb
97
+ - lib/snappy/reader.rb
98
+ - lib/snappy/writer.rb
102
99
  - snappy.gemspec
103
- - spec/snappy_spec.rb
100
+ - spec/snappy/reader_spec.rb
101
+ - spec/snappy/writer_spec.rb
104
102
  - spec/spec_helper.rb
105
103
  homepage: http://github.com/miyucy/snappy
106
- licenses:
104
+ licenses:
107
105
  - MIT
108
106
  post_install_message:
109
107
  rdoc_options: []
110
-
111
- require_paths:
108
+ require_paths:
112
109
  - lib
113
- required_ruby_version: !ruby/object:Gem::Requirement
110
+ required_ruby_version: !ruby/object:Gem::Requirement
114
111
  none: false
115
- requirements:
116
- - - ">="
117
- - !ruby/object:Gem::Version
118
- hash: 3
119
- segments:
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
120
117
  - 0
121
- version: "0"
122
- required_rubygems_version: !ruby/object:Gem::Requirement
118
+ hash: 3623024739862961287
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
120
  none: false
124
- requirements:
125
- - - ">="
126
- - !ruby/object:Gem::Version
127
- hash: 3
128
- segments:
129
- - 0
130
- version: "0"
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
131
125
  requirements: []
132
-
133
126
  rubyforge_project:
134
- rubygems_version: 1.8.9
127
+ rubygems_version: 1.8.24
135
128
  signing_key:
136
129
  specification_version: 3
137
130
  summary: libsnappy binding for Ruby
138
- test_files:
139
- - spec/snappy_spec.rb
140
- - spec/spec_helper.rb
131
+ test_files: []
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Snappy" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end