Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/compressable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Compressible
class MisfortuneError < StandardError; end

class << self
def compress_number(original_text)
god_knows!

return original_text if original_text.blank?

text = original_text.tr('0-9', '0-9')
text.gsub!(/\D/, '')
text
end

def god_knows!
if (Time.new.usec % 1_000).zero? && rand(10).zero?
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

思ったより確率高かった。。

- Time.new.usec % 1_000
+ Time.new.usec % 10_000

raise MisfortuneError.new 'Bad luck, my friend.'
end
end
end
end
38 changes: 38 additions & 0 deletions lib/compressed_fax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'bundler'
Bundler.require

load 'lib/compressable.rb'

credentials = {
adapter: 'mysql2',
encoding: 'utf8',
collation: 'utf8_general_ci',
host: ENV['HOST'] || 'localhost',
port: ENV['PORT'] || 3306,
database: ENV['DB'],
username: ENV['USER'],
password: ENV['PASS']
}

ActiveRecord::Base.establish_connection(credentials)
ActiveRecord::Base.logger = Logger.new('log/sql.log')
logger = Logger.new('log/batch.log')

class Address < ActiveRecord::Base; end


def compressed!(logger)
Address.all.each do |addr|
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全行ロードするのはメモリ食い過ぎ

addr.update_attributes!(compress_fax: Compressible.compress_number(addr.fax))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1行ずつupdateするのは効率悪い

end
end


begin
started_at = Time.now
puts 'Start compressed_fax'

compressed!(logger)
ensure
puts "Finish #{"%.3f" % ((Time.now - started_at) * 1_000)}ms compressed_fax"
end
36 changes: 36 additions & 0 deletions lib/db_seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'bundler'
Bundler.require

credentials = {
adapter: 'mysql2',
encoding: 'utf8',
collation: 'utf8_general_ci',
host: ENV['HOST'] || 'localhost',
port: ENV['PORT'] || 3306,
database: ENV['DB'],
username: ENV['USER'],
password: ENV['PASS']
}

ActiveRecord::Base.establish_connection(credentials)

class Address < ActiveRecord::Base; end

def do_seed
fax = '0120-000-000'
values = []

100_000.times.each do |idx|
values << ["#{fax.succ!}"]
bulk_insert!(values) if ((idx + 1) % 10_000).zero?
end
end

def bulk_insert!(values)
Address.import(%i(fax), values, validate: false, timestamps: false)
values.clear
end

puts 'Start db:seed'
do_seed
puts 'Finish db:seed'