From b88c6ccdd30dc88b0ee6a8c666c34da02b60e4b9 Mon Sep 17 00:00:00 2001 From: David Glick Date: Sat, 16 Jul 2022 09:15:47 -0700 Subject: [PATCH] Optimization to only reindex cmf_uid after a UID is set --- CHANGES.rst | 3 ++- src/Products/CMFUid/UniqueIdHandlerTool.py | 12 ++++++------ src/Products/CMFUid/tests/test_uidhandling.py | 7 +++++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index fff4f2f..2d76a8d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,8 @@ Products.CMFUid Changelog 3.4 (unreleased) ---------------- -- Nothing changed yet. +- When an object is reindexed after its UID is set, + only reindex the ``cmf_uid`` index rather than all indexes. 3.3 (2022-07-13) diff --git a/src/Products/CMFUid/UniqueIdHandlerTool.py b/src/Products/CMFUid/UniqueIdHandlerTool.py index 38efcb7..0be474d 100644 --- a/src/Products/CMFUid/UniqueIdHandlerTool.py +++ b/src/Products/CMFUid/UniqueIdHandlerTool.py @@ -70,8 +70,11 @@ class UniqueIdHandlerTool(UniqueObject, SimpleItem): security = ClassSecurityInfo() def _reindexObject(self, obj): - ctool = getToolByName(self, 'portal_catalog') - ctool.reindexObject(obj, idxs=[self.UID_ATTRIBUTE_NAME]) + try: + obj.reindexObject(idxs=[self.UID_ATTRIBUTE_NAME]) + except AttributeError: + ctool = getToolByName(self, 'portal_catalog') + ctool.reindexObject(obj, idxs=[self.UID_ATTRIBUTE_NAME]) def _setUid(self, obj, uid): """Attaches a unique id to the object and does reindexing. @@ -82,10 +85,7 @@ def _setUid(self, obj, uid): annotation.setUid(uid) # reindex the object - try: - obj.reindexObject() - except AttributeError: - self._reindexObject(obj) + self._reindexObject(obj) security.declarePublic('register') diff --git a/src/Products/CMFUid/tests/test_uidhandling.py b/src/Products/CMFUid/tests/test_uidhandling.py index c2f316a..cb4f3c4 100644 --- a/src/Products/CMFUid/tests/test_uidhandling.py +++ b/src/Products/CMFUid/tests/test_uidhandling.py @@ -291,8 +291,11 @@ def test_UidCatalogingDoesNotCatalogPortalRoot(self): catalog = self.ctool dummy = self.app.dummy - # mock the portal root, which has empty indexing attributes - dummy.reindexObject = lambda: None + # An object like the portal root can override + # reindexObject to suppress indexing. + def reindexObject(idxs=[]): + pass + dummy.reindexObject = reindexObject uid = handler.register(dummy) brains = catalog(cmf_uid=uid)