Class: BloomFilter::Native

Inherits:
Filter
  • Object
show all
Defined in:
lib/bloomfilter/native.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Filter

#stats

Constructor Details

#initialize(opts = {}) ⇒ Native

Returns a new instance of Native.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bloomfilter/native.rb', line 7

def initialize(opts = {})
  @opts = {
    :size    => 100,
    :hashes  => 4,
    :seed    => Time.now.to_i,
    :bucket  => 3,
    :raise   => false
  }.merge(opts)

  # arg 1: m => size : number of buckets in a bloom filter
  # arg 2: k => hashes : number of hash functions
  # arg 3: s => seed : seed of hash functions
  # arg 4: b => bucket : number of bits in a bloom filter bucket
  # arg 5: r => raise : raise on bucket overflow?

  @bf = CBloomFilter.new(@opts[:size], @opts[:hashes], @opts[:seed], @opts[:bucket], @opts[:raise])
end

Instance Attribute Details

#bfObject (readonly)

Returns the value of attribute bf.



5
6
7
# File 'lib/bloomfilter/native.rb', line 5

def bf
  @bf
end

Class Method Details

.load(filename) ⇒ Object



81
82
83
# File 'lib/bloomfilter/native.rb', line 81

def self.load(filename)
  Marshal.load(File.open(filename, 'r'))
end

Instance Method Details

#&(o) ⇒ Object

Computes the intersection of two Bloom filters. It assumes that both filters have the same size - if this is not true BloomFilter::ConfigurationMismatch is raised.



49
50
51
52
53
54
# File 'lib/bloomfilter/native.rb', line 49

def &(o)
  raise BloomFilter::ConfigurationMismatch.new unless same_parameters?(o)
  result = self.class.new
  result.instance_variable_set(:@bf,@bf.&(o.bf))
  result
end

#bitmapObject



66
67
68
# File 'lib/bloomfilter/native.rb', line 66

def bitmap
  @bf.bitmap
end

#clearObject



37
# File 'lib/bloomfilter/native.rb', line 37

def clear; @bf.clear; end

#delete(key) ⇒ Object



36
# File 'lib/bloomfilter/native.rb', line 36

def delete(key); @bf.delete(key); end

#include?(*keys) ⇒ Boolean Also known as: key?, []

Returns:

  • (Boolean)


30
31
32
# File 'lib/bloomfilter/native.rb', line 30

def include?(*keys)
  @bf.include?(*keys)
end

#insert(key) ⇒ Object Also known as: []=



25
26
27
# File 'lib/bloomfilter/native.rb', line 25

def insert(key)
  @bf.insert(key)
end

#marshal_dumpObject



77
78
79
# File 'lib/bloomfilter/native.rb', line 77

def marshal_dump
  [@opts, @bf.bitmap]
end

#marshal_load(ary) ⇒ Object



70
71
72
73
74
75
# File 'lib/bloomfilter/native.rb', line 70

def marshal_load(ary)
  opts, bitmap = *ary

  initialize(opts)
  @bf.load(bitmap) if !bitmap.nil?
end

#merge!(o) ⇒ Object



39
# File 'lib/bloomfilter/native.rb', line 39

def merge!(o); @bf.merge!(o.bf); end

#save(filename) ⇒ Object



85
86
87
88
89
# File 'lib/bloomfilter/native.rb', line 85

def save(filename)
  File.open(filename, 'w') do |f|
    f << Marshal.dump(self)
  end
end

#set_bitsObject

Returns the number of bits that are set to 1 in the filter.



42
43
44
# File 'lib/bloomfilter/native.rb', line 42

def set_bits
  @bf.set_bits
end

#sizeObject



38
# File 'lib/bloomfilter/native.rb', line 38

def size; @bf.set_bits; end

#|(o) ⇒ Object

Computes the union of two Bloom filters. It assumes that both filters have the same size - if this is not true BloomFilter::ConfigurationMismatch is raised.



59
60
61
62
63
64
# File 'lib/bloomfilter/native.rb', line 59

def |(o)
  raise BloomFilter::ConfigurationMismatch.new unless same_parameters?(o)
  result = self.class.new
  result.instance_variable_set(:@bf,@bf.|(o.bf))
  result
end