em-pg-sequel 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem "sequel"
5
+ gem "minitest"
6
+ gem "minitest-reporters"
7
+ gem "debugger"
8
+ end
9
+
10
+ # Specify your gem's dependencies in ruby-em-pg-client-sequel.gemspec
11
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Petr Yanovich
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ em-pg-sequel
2
+ ===========
3
+
4
+ [Sequel](http://sequel.rubyforge.org/) adapter for [ruby-em-pg-client](https://github.com/royaltm/ruby-em-pg-client).
5
+
6
+ Usage
7
+ -----
8
+
9
+ ```ruby
10
+ require "em-pg-sequel"
11
+ EM.synchrony do
12
+ url = "postgres://postgres:postgres@localhost:5432/test"
13
+ db = Sequel.connect(url, pool_class: EM::PG::Sequel::ConnectionPool)
14
+
15
+ puts db[:test].all.inspect
16
+
17
+ EM.stop
18
+ end
19
+ ```
20
+
21
+ Right now em-pg-client doesn't support this adapter, you should use [my patched fork](https://github.com/fl00r/ruby-em-pg-client).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:spec) do |test|
7
+ test.libs << 'lib' << 'spec'
8
+ test.pattern = 'spec/**/*_spec.rb'
9
+ test.verbose = true
10
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "em-pg-sequel"
7
+ gem.version = "0.0.1"
8
+ gem.authors = ["Petr Yanovich"]
9
+ gem.email = ["fl00r@yandex.ru"]
10
+ gem.description = %q{Sequel adapter for ruby-em-pg-client}
11
+ gem.summary = %q{Sequel adapter for ruby-em-pg-client}
12
+ gem.homepage = ""
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency "pg"
20
+ gem.add_dependency "em-pg-client"
21
+
22
+ gem.add_development_dependency "em-synchrony"
23
+ end
@@ -0,0 +1,36 @@
1
+ require 'em-synchrony/pg'
2
+ require 'sequel'
3
+ require 'em-pg-sequel/connection_pool'
4
+
5
+ module EM::PG
6
+ class ConnectionPool < ::Sequel::ConnectionPool
7
+ DEFAULT_SIZE = 4
8
+ attr_accessor :pool
9
+ def initialize(db, opts = {})
10
+ super
11
+ size = opts[:max_connections] || DEFAULT_SIZE
12
+ @pool = ::EM::PG::Sequel::ConnectionPool.new(size: size, disconnect_class: ::Sequel::DatabaseConnectionError) do
13
+ make_new(DEFAULT_SERVER)
14
+ end
15
+ end
16
+
17
+ def size
18
+ @pool.available.size
19
+ end
20
+
21
+ def hold(server = nil, &blk)
22
+ @pool.execute(&blk)
23
+ end
24
+
25
+ def disconnect(server = nil)
26
+ @pool.available.each{ |conn| db.disconnect_connection(conn) }
27
+ @pool.available.clear
28
+ end
29
+ end
30
+ end
31
+
32
+ PGconn = PG::EM::Client
33
+
34
+ require 'sequel/adapters/postgres'
35
+
36
+ # Sequel::Postgres::CONVERTED_EXCEPTIONS << ::EM::PG::Error
@@ -0,0 +1,48 @@
1
+ module EM::PG
2
+ module Sequel
3
+ class ConnectionPool
4
+ attr_reader :available
5
+
6
+ def initialize(opts, &blk)
7
+ @available = []
8
+ @pending = []
9
+ @acquire_blk = blk
10
+
11
+ @disconnected_class = opts[:disconnect_class]
12
+
13
+ opts[:size].times do
14
+ @available.push @acquire_blk.call
15
+ end
16
+ end
17
+
18
+ def execute
19
+ conn = acquire
20
+ yield conn
21
+ rescue => e
22
+ puts e.inspect
23
+ conn = @acquire_blk.call if @disconnected_class && @disconnected_class === e
24
+ raise
25
+ ensure
26
+ release(conn)
27
+ end
28
+
29
+ def acquire
30
+ f = Fiber.current
31
+ if conn = @available.pop
32
+ conn
33
+ else
34
+ @pending << f
35
+ Fiber.yield
36
+ end
37
+ end
38
+
39
+ def release(conn)
40
+ if job = @pending.shift
41
+ EM.next_tick{ job.resume conn }
42
+ else
43
+ @available << conn
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+ require 'em-synchrony'
3
+ require 'em-synchrony/fiber_iterator'
4
+
5
+ describe EM::PG::Sequel do
6
+ DELAY = 1
7
+ QUERY = "select pg_sleep(#{DELAY})"
8
+
9
+ let(:url) { DB_URL }
10
+ let(:size) { 1 }
11
+ let(:db) { Sequel.connect(url, max_connection: size, pool_class: EM::PG::ConnectionPool, db_logger: Logger.new(nil)) }
12
+ let(:test) { db[:test] }
13
+
14
+ describe "unexist table" do
15
+ it "should raise exception" do
16
+ EM.synchrony do
17
+ proc { test.all }.must_raise Sequel::DatabaseError
18
+
19
+ EM.stop
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "exist table" do
25
+
26
+ before do
27
+ EM.synchrony do
28
+ db.create_table!(:test) do
29
+ text :name
30
+ integer :value, index: true
31
+ end
32
+
33
+ EM.stop
34
+ end
35
+ end
36
+
37
+ after do
38
+ EM.synchrony do
39
+ db.drop_table?(:test)
40
+
41
+ EM.stop
42
+ end
43
+ end
44
+
45
+ it "should connect and execute query" do
46
+ EM.synchrony do
47
+ puts Sequel::DatabaseDisconnectError
48
+ test.insert name: "andrew", value: 42
49
+ test.where(name: "andrew").first[:value].must_equal 42
50
+
51
+ EM.stop
52
+ end
53
+ end
54
+
55
+
56
+ describe "pool size is exceeded" do
57
+ let(:size) { 1 }
58
+ it "should queue requests" do
59
+ EM.synchrony do
60
+ start = Time.now.to_f
61
+
62
+ res = []
63
+ EM::Synchrony::FiberIterator.new([1,2], 1).each do |t|
64
+ res << db[QUERY].all
65
+ end
66
+ (Time.now.to_f - start.to_f).must_be_within_delta DELAY * 2, DELAY * 2 * 0.15
67
+ res.size.must_equal 2
68
+
69
+ EM.stop
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "pool size is enough" do
75
+ let(:size) { 2 }
76
+ it "should parallel requests" do
77
+ EM.synchrony do
78
+ start = Time.now.to_f
79
+
80
+ res = []
81
+ EM::Synchrony::FiberIterator.new([1,2], 2).each do |t|
82
+ res << db[QUERY].all
83
+ end
84
+
85
+ (Time.now.to_f - start.to_f).must_be_within_delta DELAY, DELAY * 0.30
86
+ res.size.must_equal 2
87
+
88
+ EM.stop
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,21 @@
1
+ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
2
+
3
+ require 'bundler/setup'
4
+ require 'em-pg-sequel'
5
+
6
+ require 'logger'
7
+ require 'minitest/spec'
8
+ require 'minitest/autorun'
9
+ require 'minitest/reporters'
10
+
11
+ DB_CONFIG = {
12
+ host: "localhost",
13
+ port: 5432,
14
+ dbname: "postgres",
15
+ user: "postgres",
16
+ password: "postgres",
17
+ }
18
+
19
+ DB_URL = "postgres://%s:%s@%s:%d/%s" % [DB_CONFIG[:user], DB_CONFIG[:password], DB_CONFIG[:host], DB_CONFIG[:port], DB_CONFIG[:dbname]]
20
+
21
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-pg-sequel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Petr Yanovich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pg
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: em-pg-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: em-synchrony
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Sequel adapter for ruby-em-pg-client
63
+ email:
64
+ - fl00r@yandex.ru
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - em-pg-sequel.gemspec
75
+ - lib/em-pg-sequel.rb
76
+ - lib/em-pg-sequel/connection_pool.rb
77
+ - spec/em-pg-sequel/sequel_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: ''
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Sequel adapter for ruby-em-pg-client
103
+ test_files:
104
+ - spec/em-pg-sequel/sequel_spec.rb
105
+ - spec/spec_helper.rb