Class: BloomFilter::Client
- Inherits:
-
Object
- Object
- BloomFilter::Client
- Defined in:
- lib/bloom_filter/client.rb
Constant Summary collapse
- PACK_N =
"N"
Instance Method Summary collapse
- #&(els) ⇒ Object
- #add(el) ⇒ Object (also: #<<)
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #dump(path, timeout = nil) ⇒ Object
- #include?(el) ⇒ Boolean
-
#initialize(host, port, options = {}) ⇒ Client
constructor
A new instance of Client.
- #load(path, timeout = nil) ⇒ Object
- #reconnect ⇒ Object
Constructor Details
#initialize(host, port, options = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 |
# File 'lib/bloom_filter/client.rb', line 11 def initialize(host, port, = {}) @host, @port = host, port @timeout = [:timeout] @raise_on_timeout = [:raise_on_timeout] || false self.connect end |
Instance Method Details
#&(els) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bloom_filter/client.rb', line 37 def &(els) self.connect if els.size == 1 el = els.first self.include?(el) ? [el] : [] else elements = els.collect { |el| el.to_s }.join(Protocol::DEFAULT_SEPARATOR) @socket.write("#{[elements.size + 1].pack(PACK_N)}#{Protocol::INCLUDE_MANY}#{elements}") timeout_or_default(els) do response = BitVector.new(els.size, @socket.read(@socket.read(4).unpack(PACK_N).first)) result = [] els.size.times do |i| result << els[i] if response[i] == 1 end result end end end |
#add(el) ⇒ Object Also known as: <<
18 19 20 21 22 23 24 25 |
# File 'lib/bloom_filter/client.rb', line 18 def add(el) self.connect el = el.to_s @socket.write("#{[el.size + 1].pack(PACK_N)}#{Protocol::ADD}#{el}") timeout_or_default(false) do @socket.read(@socket.read(4).unpack(PACK_N).first) == Protocol::TRUE end end |
#connect ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/bloom_filter/client.rb', line 73 def connect unless self.connected? @socket = begin TCPSocket.new(@host, @port) rescue Exception => e nil end end end |
#connected? ⇒ Boolean
92 93 94 |
# File 'lib/bloom_filter/client.rb', line 92 def connected? !!(@socket && !@socket.closed?) end |
#disconnect ⇒ Object
88 89 90 |
# File 'lib/bloom_filter/client.rb', line 88 def disconnect @socket.close if self.connected? end |
#dump(path, timeout = nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/bloom_filter/client.rb', line 57 def dump(path, timeout = nil) self.connect @socket.write("#{[path.size + 1].pack(PACK_N)}#{Protocol::DUMP}#{path}") timeout_or_default(false, timeout) do @socket.read(@socket.read(4).unpack(PACK_N).first) == Protocol::TRUE end end |
#include?(el) ⇒ Boolean
28 29 30 31 32 33 34 35 |
# File 'lib/bloom_filter/client.rb', line 28 def include?(el) self.connect el = el.to_s @socket.write("#{[el.size + 1].pack(PACK_N)}#{Protocol::INCLUDE}#{el}") timeout_or_default(true) do @socket.read(@socket.read(4).unpack(PACK_N).first) == Protocol::TRUE end end |
#load(path, timeout = nil) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/bloom_filter/client.rb', line 65 def load(path, timeout = nil) self.connect @socket.write("#{[path.size + 1].pack(PACK_N)}#{Protocol::LOAD}#{path}") timeout_or_default(false, timeout) do @socket.read(@socket.read(4).unpack(PACK_N).first) == Protocol::TRUE end end |
#reconnect ⇒ Object
83 84 85 86 |
# File 'lib/bloom_filter/client.rb', line 83 def reconnect self.disconnect self.connect end |