From 0e8f7e1712b2bd92725622568b99901e0bcda59a Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Sat, 21 Mar 2015 09:10:25 -0700 Subject: [PATCH] Remove unnecessary 'IN vs ==' sql query branches Removes some branches in the codebase that switch queries depending on whether a WHERE match is against a single criteria or multiple criteria. For multiple options an 'IN' statement was used and for a single option an '==' was used. This is completely unnecessary complexity and brancing in our codebase because the 'col IN items' statement is just a nice syntax offered by SQL that gets converted into 'col==item1 OR col==item2 OR col==item3...' statements under the hood. So in the case of one item, 'WHERE col IN "F"' is the same as 'WHERE col = "F"'. Change-Id: I8bee8c49d72958f5ae424f87c9dc98b8abe6f579 --- neutron/db/l3_agentschedulers_db.py | 15 +++------------ neutron/db/portbindings_db.py | 5 +---- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/neutron/db/l3_agentschedulers_db.py b/neutron/db/l3_agentschedulers_db.py index c0c2052ea..044254850 100644 --- a/neutron/db/l3_agentschedulers_db.py +++ b/neutron/db/l3_agentschedulers_db.py @@ -312,12 +312,7 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase, query = query.options(orm.contains_eager( RouterL3AgentBinding.l3_agent)) query = query.join(RouterL3AgentBinding.l3_agent) - if len(router_ids) > 1: - query = query.filter( - RouterL3AgentBinding.router_id.in_(router_ids)) - else: - query = query.filter( - RouterL3AgentBinding.router_id == router_ids[0]) + query = query.filter(RouterL3AgentBinding.router_id.in_(router_ids)) if admin_state_up is not None: query = (query.filter(agents_db.Agent.admin_state_up == admin_state_up)) @@ -333,12 +328,8 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase, if not router_ids: return [] query = context.session.query(RouterL3AgentBinding) - if len(router_ids) > 1: - query = query.options(joinedload('l3_agent')).filter( - RouterL3AgentBinding.router_id.in_(router_ids)) - else: - query = query.options(joinedload('l3_agent')).filter( - RouterL3AgentBinding.router_id == router_ids[0]) + query = query.options(joinedload('l3_agent')).filter( + RouterL3AgentBinding.router_id.in_(router_ids)) return query.all() def list_l3_agents_hosting_router(self, context, router_id): diff --git a/neutron/db/portbindings_db.py b/neutron/db/portbindings_db.py index c8ab65c59..a8feddcef 100644 --- a/neutron/db/portbindings_db.py +++ b/neutron/db/portbindings_db.py @@ -49,10 +49,7 @@ class PortBindingMixin(portbindings_base.PortBindingBaseMixin): values = filters and filters.get(portbindings.HOST_ID, []) if not values: return query - if len(values) == 1: - query = query.filter(PortBindingPort.host == values[0]) - else: - query = query.filter(PortBindingPort.host.in_(values)) + query = query.filter(PortBindingPort.host.in_(values)) return query db_base_plugin_v2.NeutronDbPluginV2.register_model_query_hook( -- 2.45.2