Class: Swift::Pool

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

Defined Under Namespace

Modules: Handler

Instance Method Summary collapse

Constructor Details

#initialize(size, options) ⇒ Pool

Returns a new instance of Pool.



27
28
29
30
31
32
33
34
35
# File 'lib/swift/pool.rb', line 27

def initialize size, options
  @pool         = Swift::DB::Pool.new size, options

  # TODO move driver specific options to extension.
  @writable     = options[:driver] == 'db2'

  @pending      = {}
  @queue        = []
end

Instance Method Details

#attach(c) ⇒ Object



37
38
39
# File 'lib/swift/pool.rb', line 37

def attach c
  @pending[c] = true
end

#attached?(fd) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/swift/pool.rb', line 49

def attached? fd
  @pending.keys.select{|c| c.socket == fd}.length > 0
end

#detach(c) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/swift/pool.rb', line 41

def detach c
  @pending.delete(c)
  unless @queue.empty?
    sql, bind, callback = @queue.shift
    execute(sql, *bind, &callback)
  end
end

#execute(sql, *bind, &callback) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/swift/pool.rb', line 53

def execute sql, *bind, &callback
  request = @pool.execute sql, *bind, &callback
  # TODO EM throws exceptions in C++ land which are not trapped in the extension.
  #      This is somehow causing everything to unravel and result in a segfault which
  #      I cannot track down. I'll buy a beer for someone who can get this fixed :)
  #      Oh, here it throws an exception if we try to attach same fd twice.
  if request && !attached?(request.socket)
    EM.watch(request.socket, Handler, request, self) do |c|
      attach c
      c.notify_writable = @writable
      c.notify_readable = true
    end
  else
    @queue << [ sql, bind, callback ]
  end
end

#run(&block) ⇒ Object



70
71
72
# File 'lib/swift/pool.rb', line 70

def run &block
  EM.run{ yield self }
end