Skip to content

Commit

Permalink
Remove more inefficient counting (cloudfoundry#3067)
Browse files Browse the repository at this point in the history
  • Loading branch information
will-gant committed Dec 16, 2022
1 parent 7d54739 commit b82e3ed
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/access/base_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def has_read_scope?
end

def object_is_visible_to_user?(object, user)
object.class.user_visible(user, false).where(guid: object.guid).count > 0
object.class.user_visible(user, false).where(guid: object.guid).any?
end

def admin_user?
Expand Down
2 changes: 1 addition & 1 deletion app/access/managed_service_instance_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def allowed?(service_instance)
return true if admin_user?

if service_instance.new? || service_instance.changed_columns.include?(:service_plan_id)
return ServicePlan.user_visible(context.user, admin_user?).filter(guid: service_instance.service_plan.guid).count > 0
return ServicePlan.user_visible(context.user, admin_user?).filter(guid: service_instance.service_plan.guid).any?
end

true
Expand Down
2 changes: 1 addition & 1 deletion app/access/service_plan_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def index_with_token?(_)
end

def object_is_visible_to_user?(service_plan, user)
VCAP::CloudController::ServicePlan.user_visible(user, false, :read).where(guid: service_plan.guid).count > 0
VCAP::CloudController::ServicePlan.user_visible(user, false, :read).where(guid: service_plan.guid).any?
end
end
end
2 changes: 1 addition & 1 deletion app/actions/service_offering_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ServiceOfferingDelete
class AssociationNotEmptyError < StandardError; end

def delete(service_offering_model)
association_not_empty! unless service_offering_model.service_plans.empty?
association_not_empty! if ServicePlan.where(service_id: service_offering_model.id).any?
service_offering_model.destroy
end

Expand Down
8 changes: 7 additions & 1 deletion app/actions/space_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ def delete_apps(space_model)
def delete_service_brokers(space_model)
broker_remover = service_broker_remover(@services_event_repository)
private_service_brokers = space_model.service_brokers
deletable_brokers = private_service_brokers.reject { |broker| broker.service_plans.map(&:service_instances).flatten.any? }
deletable_brokers = private_service_brokers.reject do |broker|
ServiceInstance.
join(:service_plans, id: :service_instances__service_plan_id).
join(:services, id: :service_plans__service_id).
where(services__service_broker_id: broker.id).
any?
end

deletable_brokers.each do |broker|
broker_remover.remove(broker)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/runtime/routes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def check_route_reserved(domain_guid, host, path, port)
routes = routes.where(path: path) if path
routes = routes.where(port: port) if port

return [HTTP::NO_CONTENT, nil] if routes.count > 0
return [HTTP::NO_CONTENT, nil] if routes.any?
end
[HTTP::NOT_FOUND, nil]
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/runtime/app_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def database_uri
end

def staging_in_progress?
builds.any?(&:staging?)
builds_dataset.where(state: BuildModel::STAGING_STATE).any?
end

def docker?
Expand All @@ -122,7 +122,7 @@ def stopped?
end

def deploying?
deployments.any?(&:deploying?)
deployments_dataset.where(state: DeploymentModel::DEPLOYING_STATE).any?
end

def self.user_visibility_filter(user)
Expand Down
2 changes: 1 addition & 1 deletion lib/database/batch_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def delete

loop do
set = dataset.limit(amount)
break if set.count == 0
break if set.empty?

total_count += delete_batch(set)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/services/service_brokers/v2/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def taken_names(new_services_names)
end

def can_delete_service?(service)
service.service_plans_dataset.map(&:service_instances_dataset).map(&:count).all?(0)
VCAP::CloudController::ServiceInstance.
join(:service_plans, id: :service_instances__service_plan_id).
where(service_plans__service_id: service.id).
empty?
end

def updating_service?(new_service, old_service)
Expand Down

0 comments on commit b82e3ed

Please sign in to comment.