Created
February 21, 2018 10:31
-
-
Save kanevk/86a6977c83f0c32162f78a55fec2a691 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS non_duplicate_cat_typles') | |
ActiveRecord::Base.connection.execute(<<-SQL) | |
CREATE TABLE non_duplicate_cat_typles ( | |
id serial primary key, | |
main_id integer NOT NULL, | |
non_duplicate_id integer NOT NULL | |
); | |
SQL | |
class NonDuplicateTyples < ActiveRecord::Base | |
end | |
class NonDuplicates | |
# include SqlImplementation | |
# include RubyImplementation | |
def non_duplicates | |
raise 'Implement me!' | |
end | |
def include?(*typle) | |
raise 'Implement me!' | |
end | |
end | |
not_duplicates = NonDuplicates.new | |
cat_duplicates = [] | |
# We have a place where | |
1.upto(100_000).each do |remote_cat_id| | |
1.upto(100_000).each do |cat_id| | |
next if not_duplicates.include?(cat_id, remote_cat_id) | |
# we need to check if the | |
cat_duplicates << [remote_cat_id, cat_id] | |
end | |
end | |
# We were wondering about two implementations of NonDuplicates and we call them with | |
# naive names SqlImplementation and RubyImplementation | |
module SqlImplementation | |
def not_duplicates | |
return @not_duplicates if @not_duplicates | |
result = | |
connection.execute <<-SQL | |
WITH all_non_duplicates AS ( | |
SELECT main_id AS main, non_duplicate_id AS non_duplicate | |
FROM non_duplicate_cat_typles | |
UNION | |
SELECT non_duplicate_id AS main, main_id AS non_duplicate | |
FROM non_duplicate_cat_typles | |
) | |
SELECT main, array_agg(non_duplicate) FROM all_non_duplicates GROUP BY main; | |
SQL | |
@not_duplicates = | |
result. | |
values. | |
to_h. | |
transform_values { |v| PG::TextDecoder::Array.new.decode(v).map(&:to_i) } | |
end | |
def include?(first_id, second_id) | |
@not_duplicates[first_id]&.include?(second_id) | |
end | |
end | |
module RubyImplementation | |
def not_duplicates | |
@not_duplicates = | |
NonDuplicateTyples. | |
pluck(:main_id, :non_duplicate_id). | |
map(&:to_set). | |
to_set | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment