Permalink
Browse files

em-synchrony'fy swift

  • Loading branch information...
1 parent 518c8de commit f55e60fe753c637f07bb6540760285c733d1ff6d @deepfryed deepfryed committed Apr 17, 2012
Showing with 79 additions and 23 deletions.
  1. +34 −22 README.md
  2. +1 −1 ext/extconf.rb
  3. +33 −0 lib/swift/eventmachine.rb
  4. +11 −0 lib/swift/synchrony.rb
View
@@ -11,7 +11,7 @@ A rational rudimentary object relational mapper.
## Dependencies
* ruby >= 1.9.1
-* [dbic++](http://github.com/deepfryed/dbicpp) >= 0.6.0
+* [dbic++](http://github.com/deepfryed/dbicpp) >= 0.6.1
* mysql >= 5.0.17, postgresql >= 8.4 or sqlite3 >= 3.7
## Features
@@ -245,31 +245,43 @@ which implicitly uses `rb_thread_wait_fd`
Thread.list.reject {|thread| Thread.current == thread}.each(&:join)
```
+or use the `swift/eventmachine` api.
+
```ruby
- require 'swift'
- require 'eventmachine'
-
- pool = 3.times.map.with_index {|n| Swift.setup n, Swift::DB::Postgres, db: 'swift'}
-
- module Handler
- attr_reader :result
-
- def initialize result
- @result = result
- end
-
- def notify_readable
- result.retrieve
- result.each {|row| p row }
- unbind
+ require 'swift/eventmachine'
+
+ EM.run do
+ pool = 3.times.map { Swift.setup(:default, Swift::DB::Postgres, db: "swift") }
+
+ 3.times.each do |n|
+ defer = pool[n].execute("select pg_sleep(3 - #{n}), #{n + 1} as qid")
+
+ defer.callback do |res|
+ p res.first
+ end
+
+ defer.errback do |e|
+ p 'error', e
+ end
end
end
-
+```
+
+or use the `em-synchrony` api for `swift`
+
+```ruby
+ require 'swift/synchrony'
+
EM.run do
- EM.watch(pool[0].fileno, Handler, pool[0].async_execute('select pg_sleep(3), 1 as qid')){|c| c.notify_readable = true}
- EM.watch(pool[1].fileno, Handler, pool[1].async_execute('select pg_sleep(2), 2 as qid')){|c| c.notify_readable = true}
- EM.watch(pool[2].fileno, Handler, pool[2].async_execute('select pg_sleep(1), 3 as qid')){|c| c.notify_readable = true}
- EM.add_timer(4) { EM.stop }
+ 3.times.each do |n|
+ EM.synchrony do
+ db = Swift.setup(:default, Swift::DB::Postgres, db: "swift")
+ result = db.execute("select pg_sleep(3 - #{n}), #{n + 1} as qid")
+
+ p result.first
+ EM.stop if n == 0
+ end
+ end
end
```
View
@@ -57,5 +57,5 @@ def assert_dbicpp_version ver
exit 1 unless library_installed? 'uuid', apt_install_hint('uuid-dev')
exit 1 unless library_installed? 'dbic++', apt_install_hint('dbic++-dev')
-assert_dbicpp_version '0.6.0'
+assert_dbicpp_version '0.6.1'
create_makefile 'swift'
View
@@ -0,0 +1,33 @@
+require 'eventmachine'
+require 'swift'
+
+module Swift
+ class Adapter
+ alias :blocking_execute :execute
+
+ class EMHandler < EM::Connection
+ attr_reader :result, :defer
+ def initialize result, defer
+ @result = result
+ @defer = defer
+ end
+
+ def notify_readable
+ detach
+ begin
+ result.retrieve
+ rescue Exception => e
+ defer.fail(e)
+ else
+ defer.succeed(result)
+ end
+ end
+ end
+
+ def execute *args
+ EM::DefaultDeferrable.new.tap do |defer|
+ EM.watch(fileno, EMHandler, async_execute(*args), defer) {|c| c.notify_readable = true }
+ end
+ end
+ end
+end
View
@@ -0,0 +1,11 @@
+require 'em-synchrony'
+require 'swift/eventmachine'
+
+module Swift
+ class Adapter
+ alias :aexecute :execute
+ def execute *args
+ EM::Synchrony.sync aexecute(*args)
+ end
+ end
+end

0 comments on commit f55e60f

Please sign in to comment.