swift 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/API.rdoc +19 -12
- data/README.md +8 -5
- data/VERSION +1 -1
- data/lib/swift.rb +8 -12
- data/lib/swift/adapter.rb +3 -1
- data/lib/swift/eventmachine.rb +10 -1
- data/lib/swift/migrations.rb +3 -3
- data/lib/swift/synchrony.rb +0 -3
- data/swift.gemspec +2 -2
- metadata +2 -2
data/API.rdoc
CHANGED
@@ -13,18 +13,26 @@ Public API minus the optional stuff like Pool, IdentityMap, Migrations etc.
|
|
13
13
|
# Abstract.
|
14
14
|
Adapter
|
15
15
|
.new #=> Adapter
|
16
|
-
#begin #=> Adapter
|
17
|
-
#commit
|
18
16
|
#create #=> Record or Result
|
19
17
|
#delete #=> Result
|
20
18
|
#execute #=> Result
|
21
|
-
#async_execute #=> Result
|
22
19
|
#get #=> Record
|
23
20
|
#prepare #=> Statement
|
21
|
+
#update #=> Record or Result
|
22
|
+
|
23
|
+
# Adapter::Sql
|
24
|
+
#begin
|
25
|
+
#commit
|
24
26
|
#rollback
|
25
27
|
#transaction #=> Adapter
|
26
|
-
#
|
27
|
-
#
|
28
|
+
#ping #=> true or false
|
29
|
+
#close #=> true or false
|
30
|
+
#closed? #=> true or false
|
31
|
+
#escape #=> String
|
32
|
+
#query
|
33
|
+
#fileno #=> Integer
|
34
|
+
#result #=> Result
|
35
|
+
#write #=> Result
|
28
36
|
|
29
37
|
# Concrete.
|
30
38
|
DB
|
@@ -34,17 +42,16 @@ Public API minus the optional stuff like Pool, IdentityMap, Migrations etc.
|
|
34
42
|
|
35
43
|
# Enumerable collection of Record or Hash tuples.
|
36
44
|
Result
|
37
|
-
.new
|
38
|
-
#insert_id
|
39
|
-
#fields
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
45
|
+
.new #=> Result
|
46
|
+
#insert_id #=> Numeric
|
47
|
+
#fields #=> [Symbol, ...] # Field names identical to .first.keys if rows > 0
|
48
|
+
#types #=> [String, ...] # Type names: boolean, integer, float, numeric, timestamp, date, time, blob, text
|
49
|
+
#selected_rows #=> Fixnum
|
50
|
+
#affected_rows #=> Fixnum
|
43
51
|
|
44
52
|
Statement
|
45
53
|
.new #=> Statement
|
46
54
|
#execute #=> Result
|
47
|
-
#command #=> String # The SQL command executed or to be executed
|
48
55
|
|
49
56
|
Record
|
50
57
|
.attribute #=> Type
|
data/README.md
CHANGED
@@ -236,25 +236,26 @@ creating temporary files.
|
|
236
236
|
|
237
237
|
### Asynchronous API
|
238
238
|
|
239
|
-
`Swift::Adapter#
|
240
|
-
`Swift::Adapter#fileno` and then call `Swift::
|
239
|
+
`Swift::Adapter::Sql#query` runs a query asynchronously. You can either poll the corresponding
|
240
|
+
`Swift::Adapter::Sql#fileno` and then call `Swift::Adapter::Sql#result` when ready or use a block form like below
|
241
241
|
which implicitly uses `rb_thread_wait_fd`
|
242
242
|
|
243
243
|
```ruby
|
244
244
|
require 'swift'
|
245
|
+
require 'swift/adapter/postgres'
|
245
246
|
|
246
247
|
pool = 3.times.map.with_index {|n| Swift.setup n, Swift::Adapter::Postgres, db: 'swift' }
|
247
248
|
|
248
249
|
Thread.new do
|
249
|
-
pool[0].
|
250
|
+
pool[0].query('select pg_sleep(3), 1 as qid') {|row| p row}
|
250
251
|
end
|
251
252
|
|
252
253
|
Thread.new do
|
253
|
-
pool[1].
|
254
|
+
pool[1].query('select pg_sleep(2), 2 as qid') {|row| p row}
|
254
255
|
end
|
255
256
|
|
256
257
|
Thread.new do
|
257
|
-
pool[2].
|
258
|
+
pool[2].query('select pg_sleep(1), 3 as qid') {|row| p row}
|
258
259
|
end
|
259
260
|
|
260
261
|
Thread.list.reject {|thread| Thread.current == thread}.each(&:join)
|
@@ -264,6 +265,7 @@ or use the `swift/eventmachine` api.
|
|
264
265
|
|
265
266
|
```ruby
|
266
267
|
require 'swift/eventmachine'
|
268
|
+
require 'swift/adapter/postgres'
|
267
269
|
|
268
270
|
EM.run do
|
269
271
|
pool = 3.times.map { Swift.setup(:default, Swift::Adapter::Postgres, db: "swift") }
|
@@ -286,6 +288,7 @@ or use the `em-synchrony` api for `swift`
|
|
286
288
|
|
287
289
|
```ruby
|
288
290
|
require 'swift/synchrony'
|
291
|
+
require 'swift/adapter/postgres'
|
289
292
|
|
290
293
|
EM.run do
|
291
294
|
3.times.each do |n|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.3
|
data/lib/swift.rb
CHANGED
@@ -1,12 +1,3 @@
|
|
1
|
-
# try to require home_run in older rubies
|
2
|
-
unless %r{^1\.9\.[3-9]|^2\.}.match(RUBY_VERSION)
|
3
|
-
begin
|
4
|
-
require 'home_run'
|
5
|
-
rescue LoadError => e
|
6
|
-
warn "WARNING: DateTime parsing will be slow without home_run gem on Rubies older than 1.9.3"
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
1
|
# Extension.
|
11
2
|
require_relative 'swift/adapter'
|
12
3
|
require_relative 'swift/adapter/sql'
|
@@ -75,7 +66,6 @@ module Swift
|
|
75
66
|
# @option options [Integer] :port (DB default)
|
76
67
|
# @return [Swift::Adapter]
|
77
68
|
#
|
78
|
-
# @see Swift::DB
|
79
69
|
# @see Swift::Adapter
|
80
70
|
def setup name, type, options = {}
|
81
71
|
unless type.kind_of?(Class) && type < Swift::Adapter
|
@@ -103,9 +93,8 @@ module Swift
|
|
103
93
|
#--
|
104
94
|
# I pilfered the logic from DM but I don't really understand what is/isn't thread safe.
|
105
95
|
def db name = nil, &block
|
106
|
-
scopes = (Thread.current[:swift_db] ||= [])
|
107
96
|
repository = if name || scopes.empty?
|
108
|
-
@repositories[name || :default] or raise "Unknown db '#{name || :default}', did you forget to #setup?"
|
97
|
+
@repositories[name || :default] or raise "Unknown db '#{name || :default}', did you forget to #setup ?"
|
109
98
|
else
|
110
99
|
scopes.last
|
111
100
|
end
|
@@ -133,5 +122,12 @@ module Swift
|
|
133
122
|
def trace io = $stdout, &block
|
134
123
|
Swift.db.trace(io, &block)
|
135
124
|
end
|
125
|
+
|
126
|
+
def scopes
|
127
|
+
Thread.current[:swift_db] ||= []
|
128
|
+
end
|
136
129
|
end
|
130
|
+
|
131
|
+
class Error < StandardError; end
|
132
|
+
class RuntimeError < Error; end
|
137
133
|
end # Swift
|
data/lib/swift/adapter.rb
CHANGED
@@ -222,7 +222,9 @@ module Swift
|
|
222
222
|
private
|
223
223
|
|
224
224
|
def log_command start, command, bind
|
225
|
-
@trace.print Time.now.strftime('%F %T.%N'), ' - '
|
225
|
+
@trace.print Time.now.strftime('%F %T.%N'), ' - %.9f' % (Time.now - start).to_f, ' - ', command
|
226
|
+
@trace.print ' ', bind if bind && bind.size > 0
|
227
|
+
@trace.print $/
|
226
228
|
end
|
227
229
|
end # Adapter
|
228
230
|
end # Swift
|
data/lib/swift/eventmachine.rb
CHANGED
@@ -17,6 +17,7 @@ module Swift
|
|
17
17
|
|
18
18
|
def notify_readable
|
19
19
|
detach
|
20
|
+
@adapter.pending.shift
|
20
21
|
begin
|
21
22
|
@defer.succeed(@record ? Result.new(@record, @adapter.result) : @adapter.result)
|
22
23
|
rescue Exception => e
|
@@ -38,12 +39,20 @@ module Swift
|
|
38
39
|
#
|
39
40
|
# @see [Swift::Adapter]
|
40
41
|
def execute command, *bind
|
41
|
-
|
42
|
+
raise RuntimeError, 'Command already in progress' unless pending.empty?
|
43
|
+
|
44
|
+
pending << command
|
45
|
+
start = Time.now
|
42
46
|
record, command = command, bind.shift if command.kind_of?(Class) && command < Record
|
43
47
|
query(command, *bind)
|
48
|
+
|
44
49
|
EM::DefaultDeferrable.new.tap do |defer|
|
45
50
|
EM.watch(fileno, EMHandler, self, record, defer) {|c| c.notify_readable = true }
|
46
51
|
end
|
47
52
|
end
|
53
|
+
|
54
|
+
def pending
|
55
|
+
@pending ||= []
|
56
|
+
end
|
48
57
|
end
|
49
58
|
end
|
data/lib/swift/migrations.rb
CHANGED
@@ -34,8 +34,8 @@ module Swift
|
|
34
34
|
#
|
35
35
|
# @see Swift::Adapter::Sql
|
36
36
|
def migrate! record
|
37
|
-
keys
|
38
|
-
fields
|
37
|
+
keys = record.header.keys
|
38
|
+
fields = record.header.map{|p| field_definition(p)}.join(', ')
|
39
39
|
fields += ", primary key (#{keys.join(', ')})" unless keys.empty?
|
40
40
|
|
41
41
|
execute("drop table if exists #{record.store} cascade")
|
@@ -45,7 +45,7 @@ module Swift
|
|
45
45
|
end # Migrations
|
46
46
|
|
47
47
|
def self.migrate! name = nil
|
48
|
-
schema.each{|record| record.migrate!(db(name))
|
48
|
+
schema.each {|record| record.migrate!(db(name))}
|
49
49
|
end
|
50
50
|
|
51
51
|
class Record
|
data/lib/swift/synchrony.rb
CHANGED
data/swift.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "swift"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Shane Hanna", "Bharanee 'Barney' Rathna"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-09-21"
|
13
13
|
s.description = "A rational rudimentary database abstraction."
|
14
14
|
s.email = ["shane.hanna@gmail.com", "deepfryed@gmail.com"]
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swift
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-09-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: minitest
|