Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only join required tables #2528

Merged
Merged
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
69 changes: 59 additions & 10 deletions app/fetchers/base_service_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ class << self
private

def select_readable(dataset, message, omniscient: false, readable_space_guids: [], readable_org_guids: [])
dataset = join_tables(dataset, message, omniscient)

if readable_org_guids.any?
dataset = join_service_plans(dataset)
dataset = join_plan_orgs(dataset)
dataset = join_broker_spaces(dataset)

dataset = dataset.where do
(Sequel[:service_plans][:public] =~ true) |
(Sequel[:plan_orgs][:guid] =~ readable_org_guids) |
(Sequel[:broker_spaces][:guid] =~ readable_space_guids)
end
elsif !omniscient
dataset = join_service_plans(dataset)
dataset = dataset.where { Sequel[:service_plans][:public] =~ true }
end

Expand All @@ -36,17 +39,23 @@ def select_readable(dataset, message, omniscient: false, readable_space_guids: [

def filter(message, dataset, klass)
if message.requested?(:service_broker_guids)
dataset = join_service_brokers(dataset)
dataset = dataset.where { Sequel[:service_brokers][:guid] =~ message.service_broker_guids }
end

if message.requested?(:service_broker_names)
dataset = join_service_brokers(dataset)
dataset = dataset.where { Sequel[:service_brokers][:name] =~ message.service_broker_names }
end

super(message, dataset, klass)
end

def filter_orgs(dataset, organization_guids)
dataset = join_service_plans(dataset)
dataset = join_plan_orgs(dataset)
dataset = join_broker_orgs(dataset)

dataset.where do
(Sequel[:service_plans][:public] =~ true) |
(Sequel[:plan_orgs][:guid] =~ organization_guids) |
Expand All @@ -61,6 +70,10 @@ def filter_spaces(dataset, filtered_space_guids:, readable_space_guids:, omnisci
omniscient: omniscient,
)

dataset = join_service_plans(dataset)
dataset = join_plan_spaces(dataset)
dataset = join_broker_spaces(dataset)

dataset.where do
(Sequel[:service_plans][:public] =~ true) |
(Sequel[:plan_spaces][:guid] =~ space_guids) |
Expand All @@ -78,14 +91,50 @@ def visibility_filter?(message)
[:space_guids, :organization_guids].any? { |filter| message.requested?(filter) }
end

def join_all_parent_tables(dataset)
dataset.
join(:service_brokers, id: Sequel[:services][:service_broker_id]).
left_join(Sequel[:spaces].as(:broker_spaces), id: Sequel[:service_brokers][:space_id]).
left_join(Sequel[:organizations].as(:broker_orgs), id: Sequel[:broker_spaces][:organization_id]).
left_join(:service_plan_visibilities, service_plan_id: Sequel[:service_plans][:id]).
left_join(Sequel[:organizations].as(:plan_orgs), id: Sequel[:service_plan_visibilities][:organization_id]).
left_join(Sequel[:spaces].as(:plan_spaces), organization_id: Sequel[:plan_orgs][:id])
def join_service_plans(dataset)
dataset
end

def join_services(dataset)
dataset
end

def join_service_instances(dataset)
dataset = join_service_plans(dataset)
join(dataset, :inner, :service_instances, service_plan_id: Sequel[:service_plans][:id])
end

def join_service_brokers(dataset)
dataset = join_services(dataset)
join(dataset, :inner, :service_brokers, id: Sequel[:services][:service_broker_id])
end

def join_broker_spaces(dataset)
dataset = join_service_brokers(dataset)
join(dataset, :left, Sequel[:spaces].as(:broker_spaces), id: Sequel[:service_brokers][:space_id])
end

def join_broker_orgs(dataset)
dataset = join_broker_spaces(dataset)
join(dataset, :left, Sequel[:organizations].as(:broker_orgs), id: Sequel[:broker_spaces][:organization_id])
end

def join_plan_orgs(dataset)
dataset = join_service_plans(dataset)
dataset = join(dataset, :left, :service_plan_visibilities, service_plan_id: Sequel[:service_plans][:id])
join(dataset, :left, Sequel[:organizations].as(:plan_orgs), id: Sequel[:service_plan_visibilities][:organization_id])
end

def join_plan_spaces(dataset)
dataset = join_plan_orgs(dataset)
join(dataset, :left, Sequel[:spaces].as(:plan_spaces), organization_id: Sequel[:plan_orgs][:id])
end

def join(dataset, type, table, on)
dataset.opts[:join]&.each do |j|
return dataset if j.table_expr == table
end
dataset.join_table(type, table, on)
end
end
end
Expand Down
19 changes: 3 additions & 16 deletions app/fetchers/service_offering_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,10 @@ def fetch(message, omniscient: false, readable_space_guids: [], readable_org_gui
filter(message, dataset).select_all(:services).distinct
end

def join_tables(dataset, message, omniscient)
need_all_parent_tables = !omniscient || visibility_filter?(message)
private

filter_properties = [
:service_broker_guids,
:service_broker_names,
]

need_broker_tables = filter_properties.any? { |filter| message.requested?(filter) }

if need_all_parent_tables
dataset = join_all_parent_tables(dataset.left_join(:service_plans, service_id: Sequel[:services][:id]))
elsif need_broker_tables
dataset = dataset.join(:service_brokers, id: Sequel[:services][:service_broker_id])
end

dataset
def join_service_plans(dataset)
join(dataset, :left, :service_plans, service_id: Sequel[:services][:id])
end

def filter(message, dataset)
Expand Down
30 changes: 5 additions & 25 deletions app/fetchers/service_plan_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,8 @@ def fetch(message, omniscient: false, readable_space_guids: [], readable_org_gui

private

def join_tables(dataset, message, omniscient)
need_all_parent_tables = !omniscient || visibility_filter?(message)
filter_properties = [
:service_broker_guids,
:service_broker_names,
:service_offering_guids,
:service_offering_names,
:broker_catalog_ids
]

need_broker_and_offering_tables = filter_properties.any? { |filter| message.requested?(filter) }

if need_all_parent_tables
dataset = join_all_parent_tables(dataset.join(:services, id: Sequel[:service_plans][:service_id]))
elsif need_broker_and_offering_tables
dataset = dataset.
join(:services, id: Sequel[:service_plans][:service_id]).
join(:service_brokers, id: Sequel[:services][:service_broker_id])
end

if message.requested?(:service_instance_guids)
dataset = dataset.join(Sequel[:service_instances], service_plan_id: Sequel[:service_plans][:id])
end

dataset
def join_services(dataset)
join(dataset, :inner, :services, id: Sequel[:service_plans][:service_id])
end

def filter(message, dataset)
Expand All @@ -54,14 +31,17 @@ def filter(message, dataset)
end

if message.requested?(:service_offering_guids)
dataset = join_services(dataset)
dataset = dataset.where { Sequel[:services][:guid] =~ message.service_offering_guids }
end

if message.requested?(:service_offering_names)
dataset = join_services(dataset)
dataset = dataset.where { Sequel[:services][:label] =~ message.service_offering_names }
end

if message.requested?(:service_instance_guids)
dataset = join_service_instances(dataset)
dataset = dataset.where { Sequel[:service_instances][:guid] =~ message.service_instance_guids }
end

Expand Down