Story#119627029 - more efficient legal doc search by checksum

(cherry picked from commit 952d15d)
This commit is contained in:
Vladimir Krylov 2016-06-30 11:33:21 +03:00
parent 9b6bd22be3
commit 71547d91a3
2 changed files with 6 additions and 6 deletions

View file

@ -34,9 +34,9 @@ class LegalDocument < ActiveRecord::Base
def save_to_filesystem
digest = Digest::SHA1.new
binary = Base64.decode64(body)
ld = LegalDocument.where(checksum: digest.update(binary))
ld = LegalDocument.find_by(checksum: digest.update(binary))
if !ld
if ld.nil?
loop do
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4
@ -49,10 +49,9 @@ class LegalDocument < ActiveRecord::Base
File.open(path, 'wb') { |f| f.write(binary) } unless Rails.env.test?
self.path = path
else
self.path = ld.first.path
self.path = ld.path
end
end

View file

@ -1,5 +1,6 @@
class AddHashToLegalDoc < ActiveRecord::Migration
def change
add_column :legal_documents, :checksum, :text
add_column :legal_documents, :checksum, :string
add_index :legal_documents, :checksum
end
end