mirror of
https://github.com/internetee/registry.git
synced 2025-05-16 17:37:17 +02:00
Story#119627029 - more efficient legal doc search by checksum
(cherry picked from commit 952d15d
)
This commit is contained in:
parent
9b6bd22be3
commit
71547d91a3
2 changed files with 6 additions and 6 deletions
|
@ -34,9 +34,9 @@ class LegalDocument < ActiveRecord::Base
|
||||||
def save_to_filesystem
|
def save_to_filesystem
|
||||||
digest = Digest::SHA1.new
|
digest = Digest::SHA1.new
|
||||||
binary = Base64.decode64(body)
|
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
|
loop do
|
||||||
rand = SecureRandom.random_number.to_s.last(4)
|
rand = SecureRandom.random_number.to_s.last(4)
|
||||||
next if rand.to_i == 0 || rand.length < 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?
|
File.open(path, 'wb') { |f| f.write(binary) } unless Rails.env.test?
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
else
|
else
|
||||||
|
self.path = ld.path
|
||||||
self.path = ld.first.path
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
class AddHashToLegalDoc < ActiveRecord::Migration
|
class AddHashToLegalDoc < ActiveRecord::Migration
|
||||||
def change
|
def change
|
||||||
add_column :legal_documents, :checksum, :text
|
add_column :legal_documents, :checksum, :string
|
||||||
|
add_index :legal_documents, :checksum
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue