json-bloomfilter 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,7 +35,7 @@ and the `json-bloomfilter.min.js` will be copied to your local directory. If you
35
35
  ### Ruby
36
36
 
37
37
  ```ruby
38
- require "json/bloomfilter"
38
+ require "json-bloomfilter"
39
39
 
40
40
  # use the factory to configure the filter
41
41
  filter = JsonBloomFilter.build 10000, 0.01 # number of expected items, desired error rate
@@ -129,7 +129,8 @@ Additionally you can pass along:
129
129
 
130
130
  ## Release notes
131
131
 
132
- * **0.1.4** Changed .build function to take a list of items
132
+ * **0.1.5** Changes namespacing
133
+ * **0.1.4** Changes .build function to take a list of items
133
134
  * **0.1.3** Adds a check for non positive capacity values on build
134
135
  * **0.1.2** Adds Zlib dependency
135
136
  * **0.1.1** Fixes a JS integer overflow issue and makes Ruby 1.8.7 compatible
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "json/bloomfilter/version"
3
+ require "json-bloomfilter/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "json-bloomfilter"
@@ -0,0 +1,68 @@
1
+ require "json-bloomfilter/bitarray"
2
+ require "json"
3
+ require "zlib"
4
+
5
+ class JsonBloomfilter
6
+ DEFAULTS = { "size" => 100, "hashes" => 4, "seed" => Time.new.to_i, "bits" => nil }
7
+
8
+ def self.build capacity_or_items, error_rate
9
+ capacity, items = capacity_or_items.is_a?(Array) ? [capacity_or_items.length, capacity_or_items] : [capacity_or_items, nil]
10
+ raise ArgumentError.new("Capacity needs to be a positive integer") if capacity <= 0
11
+ JsonBloomfilter.new :size => size_for(capacity, error_rate), :hashes => hashes_for(capacity, error_rate), :items => items
12
+ end
13
+
14
+ def self.size_for capacity, error_rate
15
+ (capacity * Math.log(error_rate) / Math.log(1.0 / 2**Math.log(2))).ceil
16
+ end
17
+
18
+ def self.hashes_for capacity, error_rate
19
+ (Math.log(2) * size_for(capacity, error_rate) / capacity).round
20
+ end
21
+
22
+ def initialize options = {}
23
+ items = options.delete("items")
24
+ @options = merge_defaults_with options
25
+ @bits = BitArray.new(@options["size"], @options["bits"])
26
+ add(items) if items
27
+ end
28
+
29
+ def add keys
30
+ [keys].flatten.each do |key|
31
+ indexes_for(key).each { |index| @bits.add(index) }
32
+ end
33
+ nil
34
+ end
35
+
36
+ def test keys
37
+ [keys].flatten.each do |key|
38
+ indexes_for(key).each do |index|
39
+ return false if @bits.get(index) == 0
40
+ end
41
+ end
42
+ true
43
+ end
44
+
45
+ def clear
46
+ @bits = BitArray.new(@options["size"])
47
+ end
48
+
49
+ def to_hash
50
+ @options.merge({ "bits" => @bits.field })
51
+ end
52
+
53
+ def to_json
54
+ JSON.generate to_hash
55
+ end
56
+
57
+ private
58
+
59
+ def indexes_for key
60
+ Array.new(@options["hashes"]).each_with_index.map do |item, index|
61
+ Zlib.crc32("#{key}:#{index+@options["seed"]}") % @options["size"]
62
+ end
63
+ end
64
+
65
+ def merge_defaults_with options
66
+ DEFAULTS.merge Hash[options.map {|k, v| ["#{k}", v] }]
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ class JsonBloomfilter
2
+ VERSION = "0.1.5" unless defined? JsonBloomfilter::VERSION
3
+ end
@@ -1,68 +1 @@
1
- require "json/bloomfilter/bitarray"
2
- require "json"
3
- require "zlib"
4
-
5
- class JsonBloomfilter
6
- DEFAULTS = { "size" => 100, "hashes" => 4, "seed" => Time.new.to_i, "bits" => nil }
7
-
8
- def self.build capacity_or_items, error_rate
9
- capacity, items = capacity_or_items.is_a?(Array) ? [capacity_or_items.length, capacity_or_items] : [capacity_or_items, nil]
10
- raise ArgumentError.new("Capacity needs to be a positive integer") if capacity <= 0
11
- JsonBloomfilter.new :size => size_for(capacity, error_rate), :hashes => hashes_for(capacity, error_rate), items: items
12
- end
13
-
14
- def self.size_for capacity, error_rate
15
- (capacity * Math.log(error_rate) / Math.log(1.0 / 2**Math.log(2))).ceil
16
- end
17
-
18
- def self.hashes_for capacity, error_rate
19
- (Math.log(2) * size_for(capacity, error_rate) / capacity).round
20
- end
21
-
22
- def initialize options = {}
23
- items = options.delete("items")
24
- @options = merge_defaults_with options
25
- @bits = BitArray.new(@options["size"], @options["bits"])
26
- add(items) if items
27
- end
28
-
29
- def add keys
30
- [keys].flatten.each do |key|
31
- indexes_for(key).each { |index| @bits.add(index) }
32
- end
33
- nil
34
- end
35
-
36
- def test keys
37
- [keys].flatten.each do |key|
38
- indexes_for(key).each do |index|
39
- return false if @bits.get(index) == 0
40
- end
41
- end
42
- true
43
- end
44
-
45
- def clear
46
- @bits = BitArray.new(@options["size"])
47
- end
48
-
49
- def to_hash
50
- @options.merge({ "bits" => @bits.field })
51
- end
52
-
53
- def to_json
54
- JSON.generate to_hash
55
- end
56
-
57
- private
58
-
59
- def indexes_for key
60
- Array.new(@options["hashes"]).each_with_index.map do |item, index|
61
- Zlib.crc32("#{key}:#{index+@options["seed"]}") % @options["size"]
62
- end
63
- end
64
-
65
- def merge_defaults_with options
66
- DEFAULTS.merge Hash[options.map {|k, v| ["#{k}", v] }]
67
- end
68
- end
1
+ require "json-bloomfilter"
@@ -1,2 +1,2 @@
1
- require "json/bloomfilter"
2
- require "json/bloomfilter/bitarray"
1
+ require "json-bloomfilter"
2
+ require "json-bloomfilter/bitarray"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-bloomfilter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -179,9 +179,10 @@ files:
179
179
  - coffee/zlib.coffee
180
180
  - js/json-bloomfilter.min.js
181
181
  - json-bloomfilter.gemspec
182
+ - lib/json-bloomfilter.rb
183
+ - lib/json-bloomfilter/bitarray.rb
184
+ - lib/json-bloomfilter/version.rb
182
185
  - lib/json/bloomfilter.rb
183
- - lib/json/bloomfilter/bitarray.rb
184
- - lib/json/bloomfilter/version.rb
185
186
  - spec/javascripts/bitarray_spec.js
186
187
  - spec/javascripts/bloomfilter_spec.js
187
188
  - spec/javascripts/support/jasmine.yml
@@ -210,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
211
  version: '0'
211
212
  segments:
212
213
  - 0
213
- hash: 762970087584583546
214
+ hash: -423997557885043161
214
215
  requirements: []
215
216
  rubyforge_project:
216
217
  rubygems_version: 1.8.24
@@ -1,3 +0,0 @@
1
- class JsonBloomfilter
2
- VERSION = "0.1.4" unless defined? JsonBloomfilter::VERSION
3
- end