jruby-bloomfilter 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/Gemfile.lock +25 -0
- data/bloomfilter.gemspec +24 -0
- data/lib/bloomfilter.rb +25 -30
- data/lib/bloomfilter/serialization/file.rb +29 -0
- data/lib/bloomfilter/serialization/s3.rb +31 -0
- data/lib/bloomfilter/serializer.rb +18 -0
- data/lib/ext/java-bloomfilter-0.9.3.jar +0 -0
- data/spec/bloomfilter_spec.rb +23 -21
- data/spec/serialization_spec.rb +54 -0
- metadata +10 -2
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/gaiottino/jets3t-rb.git
|
3
|
+
revision: f7c77fa1d393bb3ebaf42d4d871071d3e9652328
|
4
|
+
specs:
|
5
|
+
jets3t-rb (1.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
rspec (2.5.0)
|
12
|
+
rspec-core (~> 2.5.0)
|
13
|
+
rspec-expectations (~> 2.5.0)
|
14
|
+
rspec-mocks (~> 2.5.0)
|
15
|
+
rspec-core (2.5.1)
|
16
|
+
rspec-expectations (2.5.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.5.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
java
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
jets3t-rb!
|
25
|
+
rspec
|
data/bloomfilter.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$: << File.expand_path('../lib', __FILE__)
|
4
|
+
|
5
|
+
Dir['ext/*.jar'].each { |jar| require jar }
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'jruby-bloomfilter'
|
9
|
+
s.version = '1.0.2'
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ['Daniel Gaiottino']
|
12
|
+
s.email = ['daniel@burtcorp.com']
|
13
|
+
s.homepage = 'http://github.com/gaiottino/bloomfilter'
|
14
|
+
s.summary = %q{JRuby wrapper for java-bloomfilter}
|
15
|
+
s.description = %q{JRuby wrapper (+ some extra functionality) to http://code.google.com/p/java-bloomfilter}
|
16
|
+
|
17
|
+
s.rubyforge_project = 'jruby-bloomfilter'
|
18
|
+
s.add_dependency 'jets3t-rb', '~> 1.0.0'
|
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 = %w(lib)
|
24
|
+
end
|
data/lib/bloomfilter.rb
CHANGED
@@ -3,44 +3,39 @@
|
|
3
3
|
require 'java'
|
4
4
|
require 'ext/java-bloomfilter-0.9.3'
|
5
5
|
|
6
|
-
|
6
|
+
require_relative 'bloomfilter/serializer'
|
7
7
|
|
8
8
|
module Jar
|
9
9
|
import com.skjegstad.utils.BloomFilter
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
module Bloomfilter
|
13
|
+
class Bloomfilter
|
14
|
+
def initialize(options = {})
|
15
|
+
if options[:size] && options[:false_positive_percentage]
|
16
|
+
@filter = Jar::BloomFilter.new(options[:false_positive_percentage], options[:size])
|
17
|
+
elsif options[:filter]
|
18
|
+
@filter = options[:filter]
|
19
|
+
end
|
20
20
|
end
|
21
|
-
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def << (k)
|
23
|
+
@filter.add(k)
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_if_absent(k)
|
27
|
+
@filter.synchronized do
|
28
|
+
@filter.add(k) unless @filter.contains(k)
|
29
|
+
end
|
30
|
+
end
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
32
|
+
def include?(k)
|
33
|
+
@filter.contains(k)
|
34
|
+
end
|
30
35
|
|
31
|
-
|
32
|
-
|
33
|
-
|
36
|
+
def count
|
37
|
+
@filter.count
|
38
|
+
end
|
34
39
|
|
35
|
-
|
36
|
-
# raise 'Cannot call store without specifying :serializer in initlialize options' unless @serializer
|
37
|
-
# @serializer.store(path, @filter)
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# def self.load(type, path)
|
41
|
-
# serializer = Serialization.serializer(type)
|
42
|
-
# filter = serializer.load(path)
|
43
|
-
# Bloomfilter.new(:filter => filter)
|
44
|
-
# end
|
45
|
-
|
40
|
+
end
|
46
41
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Bloomfilter
|
2
|
+
module Serialization
|
3
|
+
class File
|
4
|
+
def store(path, filter)
|
5
|
+
dir = ::File.dirname(path)
|
6
|
+
unless ::File.directory?(dir)
|
7
|
+
%x(mkdir -p #{dir})
|
8
|
+
end
|
9
|
+
|
10
|
+
::File.open(path, 'w') do |f|
|
11
|
+
Marshal.dump(filter, f)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def load(path)
|
16
|
+
return nil unless ::File.exist?(path)
|
17
|
+
|
18
|
+
::File.open(path, 'r') do |f|
|
19
|
+
return Marshal.load(f)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def extract_folder_path
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'jets3t'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
module Bloomfilter
|
5
|
+
module Serialization
|
6
|
+
class S3
|
7
|
+
AWS_SECRET_PATH = '~/.awssecret'.freeze
|
8
|
+
TEMP_FILE_PREFIX = 'bloomfilter'.freeze
|
9
|
+
|
10
|
+
def initialize(s3_service, bucket_name)
|
11
|
+
@file_serializer = File.new
|
12
|
+
@bucket = s3_service.bucket(bucket_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def store(path, filter)
|
16
|
+
tmp = Tempfile.new(TEMP_FILE_PREFIX)
|
17
|
+
@file_serializer.store(tmp.path, filter)
|
18
|
+
@bucket.put(path, tmp)
|
19
|
+
end
|
20
|
+
|
21
|
+
def load(path)
|
22
|
+
s3_object = @bucket.get(path)
|
23
|
+
tmp = Tempfile.new(TEMP_FILE_PREFIX)
|
24
|
+
::File.open(tmp.path, 'w') do |f|
|
25
|
+
f << s3_object.data
|
26
|
+
end
|
27
|
+
@file_serializer.load(tmp.path)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'serialization/file'
|
2
|
+
require_relative 'serialization/s3'
|
3
|
+
|
4
|
+
module Bloomfilter
|
5
|
+
class Serializer
|
6
|
+
|
7
|
+
def self.s3(access_key, secret_key, bucket_name)
|
8
|
+
credentials = JetS3t::AWSCredentials.new(access_key_id, secret_access_key)
|
9
|
+
s3_service = JetS3t::RestS3Service.new(credentials)
|
10
|
+
return Serialization::S3.new(s3_service, bucket_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.file
|
14
|
+
return Serialization::File.new
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
Binary file
|
data/spec/bloomfilter_spec.rb
CHANGED
@@ -4,33 +4,35 @@ require_relative 'spec_helper'
|
|
4
4
|
|
5
5
|
require 'tempfile'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
it 'should not have any elements from start' do
|
14
|
-
@filter.count.should == 0
|
15
|
-
end
|
7
|
+
module Bloomfilter
|
8
|
+
describe Bloomfilter do
|
9
|
+
before :all do
|
10
|
+
@filter = Bloomfilter.new(:size => 1_000, :false_positive_percentage => 0.01)
|
11
|
+
@path = Tempfile.new('bloomfilter').path
|
12
|
+
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
@filter << 'hello'
|
20
|
-
@filter.count.should == 1
|
14
|
+
it 'should not have any elements from start' do
|
15
|
+
@filter.count.should == 0
|
21
16
|
end
|
22
|
-
end
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
describe '#<<' do
|
19
|
+
it 'should be possible to add elements which updates the count' do
|
20
|
+
@filter << 'hello'
|
21
|
+
@filter.count.should == 1
|
22
|
+
end
|
27
23
|
end
|
24
|
+
|
25
|
+
describe '#include?' do
|
26
|
+
it 'should return false if an element does not exist' do
|
27
|
+
@filter.include?('world').should be_false
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
it 'should return true if an element exists' do
|
31
|
+
@filter.include?('hello').should be_true
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
@filter << 'world'
|
34
|
+
@filter.include?('world').should be_true
|
35
|
+
end
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'spec_helper'
|
4
|
+
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
module Bloomfilter
|
8
|
+
describe 'Serialization' do
|
9
|
+
before :all do
|
10
|
+
@filter = Bloomfilter.new(:size => 1_000, :false_positive_percentage => 0.01)
|
11
|
+
@filter << 'hello'
|
12
|
+
@filter << 'world'
|
13
|
+
@path = Tempfile.new('bloomfilter').path
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'File' do
|
17
|
+
it 'should be possible to store a filter to a file' do
|
18
|
+
Serializer.file.store(@path, @filter)
|
19
|
+
File.new(@path).size.should == 1612
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should be possible to load a filter from a file' do
|
23
|
+
filter = Serializer.file.load(@path)
|
24
|
+
filter.count.should == 2
|
25
|
+
filter.include?('hello').should be_true
|
26
|
+
filter.include?('world').should be_true
|
27
|
+
filter.include?('bloomfilter').should be_false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'S3' do
|
32
|
+
before do
|
33
|
+
bucket_name = 'test-bucket'
|
34
|
+
s3_service = stub('s3_service')
|
35
|
+
@bucket = stub(bucket_name)
|
36
|
+
|
37
|
+
s3_service.stub(:bucket).with(bucket_name).and_return(@bucket)
|
38
|
+
@s3 = Serialization::S3.new(s3_service, bucket_name)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should be possible to store a filter to S3' do
|
42
|
+
@bucket.should_receive(:put).with('/some/path', anything)
|
43
|
+
@s3.store('/some/path', @filter)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should be possible to load a filter from S3' do
|
47
|
+
s3_object = stub('s3_object')
|
48
|
+
@bucket.should_receive(:get).with('/some/path').and_return(s3_object)
|
49
|
+
s3_object.should_receive(:data).and_return(Marshal.dump(@filter))
|
50
|
+
@s3.load('/some/path')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jruby-bloomfilter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Daniel Gaiottino
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-12 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: jets3t-rb
|
@@ -35,11 +35,18 @@ extra_rdoc_files: []
|
|
35
35
|
files:
|
36
36
|
- .gitignore
|
37
37
|
- .rvmrc
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
38
40
|
- README
|
39
41
|
- benchmark/memory.rb
|
42
|
+
- bloomfilter.gemspec
|
40
43
|
- lib/bloomfilter.rb
|
44
|
+
- lib/bloomfilter/serialization/file.rb
|
45
|
+
- lib/bloomfilter/serialization/s3.rb
|
46
|
+
- lib/bloomfilter/serializer.rb
|
41
47
|
- lib/ext/java-bloomfilter-0.9.3.jar
|
42
48
|
- spec/bloomfilter_spec.rb
|
49
|
+
- spec/serialization_spec.rb
|
43
50
|
- spec/spec_helper.rb
|
44
51
|
homepage: http://github.com/gaiottino/bloomfilter
|
45
52
|
licenses: []
|
@@ -70,4 +77,5 @@ specification_version: 3
|
|
70
77
|
summary: JRuby wrapper for java-bloomfilter
|
71
78
|
test_files:
|
72
79
|
- spec/bloomfilter_spec.rb
|
80
|
+
- spec/serialization_spec.rb
|
73
81
|
- spec/spec_helper.rb
|