Class: Bloomrb

Inherits:
Object
  • Object
show all
Defined in:
lib/bloomrb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 8673, retries = 5) ⇒ Bloomrb

Returns a new instance of Bloomrb.


6
7
8
9
10
# File 'lib/bloomrb.rb', line 6

def initialize(host = 'localhost', port = 8673, retries = 5)
  self.host    = host
  self.port    = port
  self.retries = retries
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.


4
5
6
# File 'lib/bloomrb.rb', line 4

def host
  @host
end

#portObject

Returns the value of attribute port.


4
5
6
# File 'lib/bloomrb.rb', line 4

def port
  @port
end

#retriesObject

Returns the value of attribute retries.


4
5
6
# File 'lib/bloomrb.rb', line 4

def retries
  @retries
end

Instance Method Details

#all?(filter, keys) ⇒ Boolean

Returns:

  • (Boolean)

68
69
70
71
72
# File 'lib/bloomrb.rb', line 68

def all?(filter, keys)
  return true if keys.empty?

  !!(execute('m', filter, *keys) !~ /No/)
end

#any?(filter, keys) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
65
66
# File 'lib/bloomrb.rb', line 62

def any?(filter, keys)
  return false if keys.empty?

  !!(execute('m', filter, *keys) =~ /Yes/)
end

#bulk(filter, keys) ⇒ Object


78
79
80
81
82
# File 'lib/bloomrb.rb', line 78

def bulk(filter, keys)
  return "" if keys.empty?

  execute('b', filter, *keys)
end

#check(filter, key) ⇒ Object


52
53
54
# File 'lib/bloomrb.rb', line 52

def check(filter, key)
  execute('c', filter, key) == 'Yes'
end

#clear(filter) ⇒ Object


48
49
50
# File 'lib/bloomrb.rb', line 48

def clear(filter)
  execute('clear', filter) == 'Done'
end

#close(filter) ⇒ Object


44
45
46
# File 'lib/bloomrb.rb', line 44

def close(filter)
  execute('close', filter) == 'Done'
end

#create(filter, opts = {}) ⇒ Object


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

def create(filter, opts = {})
  execute('create', filter, opts) == 'Done'
end

#disconnectObject


16
17
18
19
# File 'lib/bloomrb.rb', line 16

def disconnect
  @socket.close if @socket
  @socket = nil
end

#drop(filter) ⇒ Object


40
41
42
# File 'lib/bloomrb.rb', line 40

def drop(filter)
  execute('drop', filter) == 'Done'
end

#filter(name) ⇒ Object


21
22
23
# File 'lib/bloomrb.rb', line 21

def filter(name)
  BloomFilter.new(name, self)
end

#flush(filter = nil) ⇒ Object


88
89
90
# File 'lib/bloomrb.rb', line 88

def flush(filter = nil)
  execute('flush', filter) == 'Done'
end

#info(filter) ⇒ Object


84
85
86
# File 'lib/bloomrb.rb', line 84

def info(filter)
  Hash[execute('info', filter).map{|s| s.split(' ')}]
end

#listObject


29
30
31
32
33
34
35
36
37
38
# File 'lib/bloomrb.rb', line 29

def list
  execute('list').map do |line|
    name, probability, size, capacity, items = line.split(' ')
    {:name => name,
     :probability => probability.to_f,
     :size => size.to_i,
     :capacity => capacity.to_i,
     :items => items.to_i}
  end
end

#multi(filter, keys) ⇒ Object


56
57
58
59
60
# File 'lib/bloomrb.rb', line 56

def multi(filter, keys)
  return Hash.new if keys.empty?

  Hash[keys.zip(execute('m', filter, *keys).split(' ').map{|r| r == 'Yes'})]
end

#set(filter, key) ⇒ Object


74
75
76
# File 'lib/bloomrb.rb', line 74

def set(filter, key)
  execute('s', filter, key) == 'Yes'
end

#socketObject


12
13
14
# File 'lib/bloomrb.rb', line 12

def socket
  @socket ||= TCPSocket.new(host, port)
end