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.



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

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.



3
4
5
# File 'lib/bloomfilter/native.rb', line 3

def bf
  @bf
end

Class Method Details

.load(filename) ⇒ Object



54
55
56
# File 'lib/bloomfilter/native.rb', line 54

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

Instance Method Details

#bitmapObject



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

def bitmap
  @bf.bitmap
end

#clearObject



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

def clear; @bf.clear; end

#delete(key) ⇒ Object



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

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

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/bloomfilter/native.rb', line 28

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

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



23
24
25
# File 'lib/bloomfilter/native.rb', line 23

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

#marshal_dumpObject



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

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

#marshal_load(ary) ⇒ Object



43
44
45
46
47
48
# File 'lib/bloomfilter/native.rb', line 43

def marshal_load(ary)
  opts, bitmap = *ary

  @bf = Native.new(opts)
  @bf.bf.load(bitmap) if !bitmap.nil?
end

#merge!(o) ⇒ Object



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

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

#save(filename) ⇒ Object



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

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

#sizeObject



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

def size; @bf.num_set; end