Skip to content

Commit 4398d2c

Browse files
authored
[ruby|rack-sequel] Batch update for Postgres (#9130)
Also remove Rack::Chunked middleware as it is no longer supported.
1 parent 57fa858 commit 4398d2c

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

frameworks/Ruby/rack-sequel/boot.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def connect(dbtype)
3333
Bundler.require(dbtype) # Load database-specific modules
3434

3535
adapters = {
36-
:mysql=>{ :jruby=>'jdbc:mysql', :mri=>'mysql2' },
37-
:postgresql=>{ :jruby=>'jdbc:postgresql', :mri=>'postgres' }
36+
mysql: { jruby: 'jdbc:mysql', mri: 'mysql2' },
37+
postgresql: { jruby: 'jdbc:postgresql', mri: 'postgres' }
3838
}
3939

4040
opts = {}
@@ -52,19 +52,39 @@ def connect(dbtype)
5252

5353
Sequel.connect \
5454
'%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
55-
:adapter=>adapters.fetch(dbtype).fetch(defined?(JRUBY_VERSION) ? :jruby : :mri),
56-
:host=>'tfb-database',
57-
:database=>'hello_world',
58-
:user=>'benchmarkdbuser',
59-
:password=>'benchmarkdbpass'
55+
adapter: adapters.fetch(dbtype).fetch(defined?(JRUBY_VERSION) ? :jruby : :mri),
56+
host: 'tfb-database',
57+
database: 'hello_world',
58+
user: 'benchmarkdbuser',
59+
password: 'benchmarkdbpass'
6060
}, opts
6161
end
6262

6363
DB = connect ENV.fetch('DBTYPE').to_sym
6464

6565
# Define ORM models
6666
class World < Sequel::Model(:World)
67+
BY_ID = naked.where(id: :$id).prepare(:first, :world_by_id)
68+
UPDATE = where(id: :$id).prepare(:update, :world_update, randomnumber: :$randomnumber)
69+
6770
def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
71+
72+
def self.batch_update(worlds)
73+
if DB.database_type == :mysql
74+
worlds.each do |world|
75+
UPDATE.(id: world[:id], randomnumber: world[:randomnumber])
76+
end
77+
else
78+
ids = []
79+
sql = String.new("UPDATE world SET randomnumber = CASE id ")
80+
worlds.each do |world|
81+
sql << "when #{world[:id]} then #{world[:randomnumber]} "
82+
ids << world[:id]
83+
end
84+
sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
85+
DB.run(sql)
86+
end
87+
end
6888
end
6989

7090
class Fortune < Sequel::Model(:Fortune)

frameworks/Ruby/rack-sequel/config.ru

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require_relative 'boot'
22
require_relative 'hello_world'
33
use Rack::ContentLength
4-
use Rack::Chunked
54
run HelloWorld.new

frameworks/Ruby/rack-sequel/hello_world.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,23 @@ def rand1
2020
rand(MAX_PK).succ
2121
end
2222

23-
WORLD_BY_ID = World.naked.where(:id=>:$id).prepare(:first, :world_by_id)
24-
WORLD_UPDATE = World.where(:id=>:$id).prepare(:update, :world_update, :randomnumber=>:$randomnumber)
25-
2623
def db
27-
WORLD_BY_ID.(:id=>rand1)
24+
World::BY_ID.(id: rand1)
2825
end
2926

3027
def queries(env)
3128
DB.synchronize do
3229
ALL_IDS.sample(bounded_queries(env)).map do |id|
33-
WORLD_BY_ID.(id: id)
30+
World::BY_ID.(id: id)
3431
end
3532
end
3633
end
3734

3835
def fortunes
3936
fortunes = Fortune.all
4037
fortunes << Fortune.new(
41-
:id=>0,
42-
:message=>'Additional fortune added at request time.'
38+
id: 0,
39+
message: 'Additional fortune added at request time.'
4340
)
4441
fortunes.sort_by!(&:message)
4542

@@ -78,11 +75,14 @@ def fortunes
7875

7976
def updates(env)
8077
DB.synchronize do
81-
ALL_IDS.sample(bounded_queries(env)).map do |id|
82-
world = WORLD_BY_ID.(id: id)
83-
WORLD_UPDATE.(id: world[:id], randomnumber: (world[:randomnumber] = rand1))
84-
world
85-
end
78+
worlds =
79+
ALL_IDS.sample(bounded_queries(env)).map do |id|
80+
world = World::BY_ID.(id: id)
81+
world[:randomnumber] = rand1
82+
world
83+
end
84+
World.batch_update(worlds)
85+
worlds
8686
end
8787
end
8888

@@ -91,7 +91,7 @@ def call(env)
9191
case env['PATH_INFO']
9292
when '/json'
9393
# Test type 1: JSON serialization
94-
[JSON_TYPE, JSON.fast_generate(:message=>'Hello, World!')]
94+
[JSON_TYPE, JSON.fast_generate(message: 'Hello, World!')]
9595
when '/db'
9696
# Test type 2: Single database query
9797
[JSON_TYPE, JSON.fast_generate(db)]

0 commit comments

Comments
 (0)