Skip to content

Commit 2a885c1

Browse files
committed
Replace .include? / .map with more efficient SQL statements
1 parent 3f610e8 commit 2a885c1

File tree

9 files changed

+31
-26
lines changed

9 files changed

+31
-26
lines changed

app/access/space_quota_definition_access.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def delete?(space_quota_definition, params=nil)
7070
def read?(space_quota_definition, *_)
7171
context.admin_override || (
7272
!context.user.nil? && (
73-
(context.user.managed_organizations.include? space_quota_definition.organization) ||
73+
context.user.managed_organizations_dataset.where(id: space_quota_definition.organization_id).any? ||
7474
!(context.user.managed_spaces & space_quota_definition.spaces).empty? ||
7575
!(context.user.audited_spaces & space_quota_definition.spaces).empty? ||
7676
!(context.user.spaces & space_quota_definition.spaces).empty?

app/actions/service_instance_share.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ def validate_plan_visibility!(service_instance, space)
5454
end
5555

5656
def validate_name_uniqueness!(service_instance, space)
57-
if space.service_instances.map(&:name).include?(service_instance.name)
57+
if space.service_instances_dataset.where(name: service_instance.name).any?
5858
error_msg = "A service instance called #{service_instance.name} already exists in #{space.name}."
5959
error!(error_msg)
6060
end
6161

62-
if space.service_instances_shared_from_other_spaces.map(&:name).include?(service_instance.name)
62+
if space.service_instances_shared_from_other_spaces_dataset.where(name: service_instance.name).any?
6363
error_msg = "A service instance called #{service_instance.name} has already been shared with #{space.name}."
6464
error!(error_msg)
6565
end

app/controllers/v3/roles_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def destroy
7171

7272
if role.type == VCAP::CloudController::RoleTypes::ORGANIZATION_USER
7373
org = Organization.find(id: role.organization_id)
74-
no_space_role = Role.where(space_id: org.spaces.map(&:id), user_id: role.user_id).empty?
74+
no_space_role = Role.where(space_id: org.spaces_dataset.select(:id), user_id: role.user_id).empty?
7575
unprocessable!('Cannot delete organization_user role while user has roles in spaces in that organization.') unless no_space_role
7676
end
7777
end

app/controllers/v3/security_groups_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def delete_running_spaces
123123
unprocessable_space! unless space
124124
unauthorized! unless permission_queryer.can_update_active_space?(space.id, space.organization_id)
125125
suspended! unless permission_queryer.is_space_active?(space.id)
126-
unprocessable_space! unless security_group.spaces.include?(space)
126+
unprocessable_space! unless security_group.spaces_dataset.where(id: space.id).any?
127127

128128
SecurityGroupUnapply.unapply_running(security_group, space)
129129

@@ -140,7 +140,7 @@ def delete_staging_spaces
140140
unprocessable_space! unless space
141141
unauthorized! unless permission_queryer.can_update_active_space?(space.id, space.organization_id)
142142
suspended! unless permission_queryer.is_space_active?(space.id)
143-
unprocessable_space! unless security_group.staging_spaces.include?(space)
143+
unprocessable_space! unless security_group.staging_spaces_dataset.where(id: space.id).any?
144144

145145
SecurityGroupUnapply.unapply_staging(security_group, space)
146146

app/fetchers/route_fetcher.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def filter(message, dataset)
3333
end
3434

3535
if message.requested?(:organization_guids)
36-
space_ids = Organization.where(guid: message.organization_guids).map(&:spaces).flatten.map(&:id)
36+
space_ids = Space.
37+
join(:organizations, id: :organization_id).
38+
where(organizations__guid: message.organization_guids).
39+
select(:spaces__id)
3740
dataset = dataset.where(space_id: space_ids)
3841
end
3942

app/models/runtime/user.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ def validate
8282
end
8383

8484
def validate_organization(org)
85-
unless org && organizations.include?(org)
85+
unless org && organizations_dataset.where(id: org).any?
8686
raise InvalidOrganizationRelation.new("Cannot add role, user does not belong to Organization with guid #{org.guid}")
8787
end
8888
end
8989

9090
def validate_organization_roles(org)
91-
if org && (managed_organizations.include?(org) || billing_managed_organizations.include?(org) || audited_organizations.include?(org))
91+
if org && (managed_organizations_dataset.where(id: org.id).any? ||
92+
billing_managed_organizations_dataset.where(id: org.id).any? ||
93+
audited_organizations_dataset.where(id: org.id).any?)
9294
raise InvalidOrganizationRelation.new("Cannot remove user from Organization with guid #{org.guid} if the user has the OrgManager, BillingManager, or Auditor role")
9395
end
9496
end

app/presenters/v3/security_group_presenter.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def to_hash
2727
},
2828
relationships: {
2929
running_spaces: {
30-
data: space_guid_hash_for(security_group.spaces)
30+
data: space_guid_hash_for(security_group.spaces_dataset)
3131
},
3232
staging_spaces: {
33-
data: space_guid_hash_for(security_group.staging_spaces)
33+
data: space_guid_hash_for(security_group.staging_spaces_dataset)
3434
}
3535
},
3636
links: build_links,
@@ -43,13 +43,13 @@ def security_group
4343
@resource
4444
end
4545

46-
def space_guid_hash_for(spaces)
47-
visible_spaces = if @all_spaces_visible
48-
spaces
49-
else
50-
spaces.select { |space| @visible_space_guids.include? space.guid }
51-
end
52-
visible_spaces.map { |space| { guid: space.guid } }
46+
def space_guid_hash_for(spaces_dataset)
47+
visible_spaces_dataset = if @all_spaces_visible
48+
spaces_dataset
49+
else
50+
spaces_dataset.where(guid: @visible_space_guids)
51+
end
52+
visible_spaces_dataset.select_map(:guid).map { |guid| { guid: guid } }
5353
end
5454

5555
def build_links

app/presenters/v3/space_quota_presenter.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def space_quota
5656
end
5757

5858
def filtered_visible_spaces
59-
visible_spaces = if @all_spaces_visible
60-
space_quota.spaces
61-
else
62-
space_quota.spaces.select { |space| @visible_space_guids.include? space.guid }
63-
end
64-
visible_spaces.map { |space| { guid: space.guid } }
59+
visible_spaces_dataset = if @all_spaces_visible
60+
space_quota.spaces_dataset
61+
else
62+
space_quota.spaces_dataset.where(guid: @visible_space_guids)
63+
end
64+
visible_spaces_dataset.select_map(:guid).map { |guid| { guid: guid } }
6565
end
6666

6767
def build_links

lib/cloud_controller/permissions.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ def can_read_route?(space_id)
245245
space = VCAP::CloudController::Space.where(id: space_id).first
246246

247247
space.has_member?(@user) || space.has_supporter?(@user) ||
248-
@user.managed_organizations.map(&:id).include?(space.organization_id) ||
249-
@user.audited_organizations.map(&:id).include?(space.organization_id)
248+
@user.managed_organizations_dataset.where(id: space.organization_id).any? ||
249+
@user.audited_organizations_dataset.where(id: space.organization_id).any?
250250
end
251251

252252
def space_guids_with_readable_routes_query

0 commit comments

Comments
 (0)