Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Experiment with optimizing moves #3834

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Products/CMFPlone/patches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
from . import publishing
from . import templatecookcheck # Make sure templates aren't re-read in
from . import z3c_form

from . import cmfcatalogaware

# production sites
89 changes: 89 additions & 0 deletions Products/CMFPlone/patches/cmfcatalogaware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import inspect
from Acquisition import aq_base
from OFS.interfaces import IObjectWillBeMovedEvent
from Products.CMFCore.CMFCatalogAware import handleContentishEvent as orig
from Products.CMFCore.indexing import getQueue
from Products.CMFCore.interfaces import ICatalogTool
from zope.container.interfaces import IObjectAddedEvent
from zope.container.interfaces import IObjectMovedEvent
from zope.lifecycleevent.interfaces import IObjectCopiedEvent
from zope.lifecycleevent.interfaces import IObjectCreatedEvent
from zope.component import queryUtility
from zope.component import ComponentLookupError


def handleContentishEvent(ob, event):
""" Event subscriber for (IContentish, IObjectEvent) events.
"""
if IObjectAddedEvent.providedBy(event):
ob.notifyWorkflowCreated()
ob.indexObject()

elif IObjectWillBeMovedEvent.providedBy(event):
# Move/Rename
if event.oldParent is not None and event.newParent is not None:
try:
catalog = queryUtility(ICatalogTool)
except ComponentLookupError:
# Happens when renaming a Plone Site in the ZMI.
# Then it is best to manually clear and rebuild
# the catalog later anyway.
# But for now do what would happen without our patch.
ob.unindexObject()
else:
ob_path = '/'.join(ob.getPhysicalPath())
rid = catalog._catalog.uids.get(ob_path)
if rid is not None:
setattr(ob, '__rid', rid)
else:
# This may happen if deferred indexing is active and an
# object is added and renamed/moved in the same transaction
# (e.g. moved in an IObjectAddedEvent handler)
return
elif event.oldParent is not None:
# Delete
ob.unindexObject()

elif IObjectMovedEvent.providedBy(event):
if event.newParent is not None:
rid = getattr(ob, '__rid', None)
if rid:
catalog = queryUtility(ICatalogTool)
_catalog = catalog._catalog

new_path = '/'.join(ob.getPhysicalPath())
old_path = _catalog.paths[rid]

# Make sure the queue is empty before we update catalog internals
getQueue().process()

del _catalog.uids[old_path]
_catalog.uids[new_path] = rid
_catalog.paths[rid] = new_path

ob.reindexObject(
idxs=["allowedRolesAndUsers", "path", "getId", "id"]
)

delattr(ob, '__rid')
else:
# This may happen if deferred indexing is active and an
# object is added and renamed/moved in the same transaction
# (e.g. moved in an IObjectAddedEvent handler)
ob.indexObject()

elif IObjectCopiedEvent.providedBy(event):
if hasattr(aq_base(ob), 'workflow_history'):
del ob.workflow_history

elif IObjectCreatedEvent.providedBy(event):
if hasattr(aq_base(ob), 'addCreator'):
ob.addCreator()


globals = orig.__globals__
globals.update({"getQueue": getQueue})
source = "\n" * (handleContentishEvent.__code__.co_firstlineno - 1) + inspect.getsource(handleContentishEvent)
code = compile(source, handleContentishEvent.__code__.co_filename, 'exec')
exec(code, globals)
orig.__code__ = globals["handleContentishEvent"].__code__