Removing duplicates from columns_to_join list

columns_to_join list contains duplicate values as we keep
extending expected_attrs list in api level.
This commit involves removing duplicates from columns_to_join
attribute of instance model to avoid unnecessary joins in
instance_get_all_by_filters method.
Sorting resultant set based on list index is required to maintain
the sequence.

Change-Id: I2c93cef0babad23fa456bf10ba2ccaec7844b4cd
Closes-Bug: #1599256
This commit is contained in:
Pushkar Umaranikar 2016-07-14 17:32:02 +00:00
parent 9adb28bfc5
commit dc7c21f3cf
2 changed files with 19 additions and 1 deletions
nova
objects
tests/unit/objects

@ -88,7 +88,13 @@ def _expected_cols(expected_attrs):
if complex_cols:
simple_cols.append('extra')
simple_cols = [x for x in simple_cols if x not in _INSTANCE_EXTRA_FIELDS]
return simple_cols + complex_cols
expected_cols = simple_cols + complex_cols
# NOTE(pumaranikar): expected_cols list can contain duplicates since
# caller appends column attributes to expected_attr without checking if
# it is already present in the list or not. Hence, we remove duplicates
# here, if any. The resultant list is sorted based on list index to
# maintain the insertion order.
return sorted(list(set(expected_cols)), key=expected_cols.index)
_NO_DATA_SENTINEL = object()

@ -1877,6 +1877,18 @@ class TestInstanceObjectMisc(test.TestCase):
instance._expected_cols(['metadata',
'numa_topology']))
def test_expected_cols_no_duplicates(self):
expected_attr = ['metadata', 'system_metadata', 'info_cache',
'security_groups', 'info_cache', 'metadata',
'pci_devices', 'tags', 'extra', 'flavor']
result_list = instance._expected_cols(expected_attr)
self.assertEqual(len(result_list), len(set(expected_attr)))
self.assertEqual(['metadata', 'system_metadata', 'info_cache',
'security_groups', 'pci_devices', 'tags', 'extra',
'extra.flavor'], result_list)
def test_migrate_instance_keypairs(self):
ctxt = context.RequestContext('foo', 'bar')
key = objects.KeyPair(context=ctxt,