Improve "objects.db.api.count" method

Two improvements are implemented in this method:
- Add a query limit number parameter. This parameter is used by
  "NeutronDbObject.objects_exist" to limit the number of registers
  retrieved to 1.
- Add a query field parameter. This is the name (string) of the
  "obj_cls.fields" list. That will reduce the columns to be
  retrieved to only one. To check the existence of a DB register,
  there is not need to retrieve the full DB model including the back
  references.
  In case of not passing any value, the first "obj_cls.primary_keys"
  value will be used instead.

Closes-Bug: #1925528

Change-Id: I9fd5e306e293102c366d89c01bbe8b13721d59b0
This commit is contained in:
Rodolfo Alonso Hernandez 2021-04-23 13:23:28 +00:00
parent cbcb1ef992
commit 9911d414c6
4 changed files with 14 additions and 7 deletions

View File

@ -50,7 +50,7 @@ msgpack-python==0.4.0
munch==2.1.0 munch==2.1.0
netaddr==0.7.18 netaddr==0.7.18
netifaces==0.10.4 netifaces==0.10.4
neutron-lib==2.10.1 neutron-lib==2.11.0
openstacksdk==0.31.2 openstacksdk==0.31.2
os-client-config==1.28.0 os-client-config==1.28.0
os-ken==0.3.0 os-ken==0.3.0

View File

@ -933,5 +933,5 @@ class NeutronDbObject(NeutronObject, metaclass=DeclarativeObject):
cls.validate_filters(**kwargs) cls.validate_filters(**kwargs)
# Succeed if at least a single object matches; no need to fetch more # Succeed if at least a single object matches; no need to fetch more
return bool(obj_db_api.count( return bool(obj_db_api.count(
cls, context, **cls.modify_fields_to_db(kwargs)) cls, context, query_limit=1, **cls.modify_fields_to_db(kwargs))
) )

View File

@ -20,11 +20,13 @@ from oslo_utils import uuidutils
# Common database operation implementations # Common database operation implementations
def _get_filter_query(obj_cls, context, **kwargs): def _get_filter_query(obj_cls, context, query_field=None, query_limit=None,
**kwargs):
with obj_cls.db_context_reader(context): with obj_cls.db_context_reader(context):
filters = _kwargs_to_filters(**kwargs) filters = _kwargs_to_filters(**kwargs)
query = model_query.get_collection_query( query = model_query.get_collection_query(
context, obj_cls.db_model, filters) context, obj_cls.db_model, filters, limit=query_limit,
field=query_field)
return query return query
@ -32,8 +34,13 @@ def get_object(obj_cls, context, **kwargs):
return _get_filter_query(obj_cls, context, **kwargs).first() return _get_filter_query(obj_cls, context, **kwargs).first()
def count(obj_cls, context, **kwargs): def count(obj_cls, context, query_field=None, query_limit=None, **kwargs):
return _get_filter_query(obj_cls, context, **kwargs).count() if not query_field and obj_cls.primary_keys:
query_field = obj_cls.primary_keys[0]
if query_field in obj_cls.fields_need_translation:
query_field = obj_cls.fields_need_translation[query_field]
return _get_filter_query(obj_cls, context, query_field=query_field,
query_limit=query_limit, **kwargs).count()
def _kwargs_to_filters(**kwargs): def _kwargs_to_filters(**kwargs):

View File

@ -16,7 +16,7 @@ Jinja2>=2.10 # BSD License (3 clause)
keystonemiddleware>=5.1.0 # Apache-2.0 keystonemiddleware>=5.1.0 # Apache-2.0
netaddr>=0.7.18 # BSD netaddr>=0.7.18 # BSD
netifaces>=0.10.4 # MIT netifaces>=0.10.4 # MIT
neutron-lib>=2.10.1 # Apache-2.0 neutron-lib>=2.11.0 # Apache-2.0
python-neutronclient>=6.7.0 # Apache-2.0 python-neutronclient>=6.7.0 # Apache-2.0
tenacity>=6.0.0 # Apache-2.0 tenacity>=6.0.0 # Apache-2.0
SQLAlchemy>=1.2.0 # MIT SQLAlchemy>=1.2.0 # MIT