forked from igrigorik/bloomfilter-rb
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getbit/setbit (non-counting) redis filter backend
- Loading branch information
Showing
5 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module BloomFilter | ||
class Redis < Filter | ||
|
||
def initialize(opts = {}) | ||
@opts = { | ||
:size => 100, | ||
:hashes => 4, | ||
:seed => Time.now.to_i, | ||
:bucket => 3, | ||
:ttl => false, | ||
:server => {} | ||
}.merge opts | ||
@db = ::Redis.new(@opts[:server]) | ||
end | ||
|
||
def insert(key, ttl=nil) | ||
indexes_for(key) { |idx| @db.setbit 'redis', idx, 1 } | ||
end | ||
alias :[]= :insert | ||
|
||
def include?(*keys) | ||
keys.each do |key| | ||
indexes_for(key) do |idx| | ||
return false if @db.getbit('redis', idx).zero? | ||
end | ||
end | ||
|
||
true | ||
end | ||
alias :key? :include? | ||
|
||
def delete(key) | ||
indexes_for(key) do |idx| | ||
@db.setbit 'redis', idx, 0 | ||
end | ||
end | ||
|
||
def clear | ||
@db.set 'redis', 0 | ||
end | ||
|
||
def num_set | ||
@db.strlen 'redis' | ||
end | ||
alias :size :num_set | ||
|
||
def stats | ||
printf "Number of filter buckets (m): %d\n" % @opts[:size] | ||
printf "Number of filter hashes (k) : %d\n" % @opts[:hashes] | ||
end | ||
|
||
private | ||
|
||
# compute index offsets for provided key | ||
def indexes_for(key) | ||
indexes = [] | ||
@opts[:hashes].times do |i| | ||
yield Zlib.crc32("#{key}:#{i+@opts[:seed]}") % @opts[:size] | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'helper' | ||
|
||
describe BloomFilter::Redis do | ||
include BloomFilter | ||
|
||
context "use Redis bitstring for storage" do | ||
let(:bf) { Redis.new } | ||
|
||
it "should store data in Redis" do | ||
bf.insert(:abcd) | ||
bf.insert('test') | ||
bf.include?('test').should be_true | ||
bf.key?('test').should be_true | ||
|
||
bf.include?('test', 'test2').should be_false | ||
bf.include?('test', 'abcd').should be_true | ||
end | ||
|
||
it "should delete keys from Redis" do | ||
bf.insert('test') | ||
bf.include?('test').should be_true | ||
|
||
bf.delete('test') | ||
bf.include?('test').should be_false | ||
end | ||
|
||
it "should clear Redis filter" do | ||
bf.insert('test') | ||
bf.include?('test').should be_true | ||
|
||
bf.clear | ||
bf.include?('test').should be_false | ||
end | ||
|
||
it "should output current stats" do | ||
bf.clear | ||
bf.insert('test') | ||
lambda { bf.stats }.should_not raise_error | ||
end | ||
|
||
it "should connect to remote redis server" do | ||
lambda { Redis.new }.should_not raise_error | ||
end | ||
end | ||
end |