From 430baa37a5ea8cd263bb9b3cee331e636e61438a Mon Sep 17 00:00:00 2001 From: Dave Brondsema Date: Wed, 21 Feb 2024 12:06:19 -0500 Subject: [PATCH] use $regex instead of re.compile in mongo queries, so it uses indexes properly. Maybe fixed in current mongo versions https://jira.mongodb.org/browse/SERVER-26991 --- Allura/allura/controllers/project.py | 2 +- Allura/allura/controllers/site_admin.py | 4 ++-- Allura/allura/controllers/trovecategories.py | 6 +++--- Allura/allura/ext/admin/admin_main.py | 4 ++-- Allura/allura/lib/plugin.py | 4 ++-- Allura/allura/model/project.py | 2 +- ForgeDiscussion/forgediscussion/controllers/forum.py | 2 +- ForgeUserStats/forgeuserstats/controllers/userstats.py | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Allura/allura/controllers/project.py b/Allura/allura/controllers/project.py index 8ddb21da81..f4d8c9865e 100644 --- a/Allura/allura/controllers/project.py +++ b/Allura/allura/controllers/project.py @@ -687,7 +687,7 @@ def update(self, name=None, css=None, homepage=None, project_template=None, icon if icon is not None and icon != b'': if self.neighborhood.icon: self.neighborhood.icon.delete() - M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon'))) + M.ProjectFile.query.remove(dict(project_id=c.project._id, category={'$regex': r'^icon'})) save_icon = c.project.save_icon(icon.filename, icon.file, content_type=icon.type) if save_icon: M.AuditLog.log('update neighborhood icon') diff --git a/Allura/allura/controllers/site_admin.py b/Allura/allura/controllers/site_admin.py index 5b41bc94a2..07009028b4 100644 --- a/Allura/allura/controllers/site_admin.py +++ b/Allura/allura/controllers/site_admin.py @@ -542,9 +542,9 @@ def index(self, page_num=1, minutes=10, state=None, task_name=None, host=None, * if state: query['state'] = state if task_name: - query['task_name'] = re.compile(re.escape(task_name)) + query['task_name'] = {'$regex': re.escape(task_name)} if host: - query['process'] = re.compile(re.escape(host)) + query['process'] = {'$regex': re.escape(host)} tasks = list(M.monq_model.MonQTask.query.find(query).sort('_id', -1)) for task in tasks: diff --git a/Allura/allura/controllers/trovecategories.py b/Allura/allura/controllers/trovecategories.py index 1c0c14a890..d94d60e424 100644 --- a/Allura/allura/controllers/trovecategories.py +++ b/Allura/allura/controllers/trovecategories.py @@ -131,11 +131,11 @@ def _create(cls, name, upper_id, shortname): if upper: trove_type = upper.fullpath.split(' :: ')[0] - fullpath_re = re.compile(fr'^{re.escape(trove_type)} :: ') # e.g. scope within "Topic :: " + fullpath_re = fr'^{re.escape(trove_type)} :: ' # e.g. scope within "Topic :: " else: # no parent, so making a top-level. Don't limit fullpath_re, so enforcing global uniqueness - fullpath_re = re.compile(r'') - oldcat = M.TroveCategory.query.get(shortname=shortname, fullpath=fullpath_re) + fullpath_re = r'' + oldcat = M.TroveCategory.query.get(shortname=shortname, fullpath={'$regex': fullpath_re}) if oldcat: raise TroveAdminException( diff --git a/Allura/allura/ext/admin/admin_main.py b/Allura/allura/ext/admin/admin_main.py index fc2f2ac531..6e8232fc60 100644 --- a/Allura/allura/ext/admin/admin_main.py +++ b/Allura/allura/ext/admin/admin_main.py @@ -336,7 +336,7 @@ def update(self, name=None, c.project.removal = removal c.project.removal_changed_date = datetime.utcnow() if 'delete_icon' in kw: - M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon'))) + M.ProjectFile.query.remove(dict(project_id=c.project._id, category={'$regex': r'^icon'})) c.project.set_tool_data('allura', icon_original_size=None, icon_sha256=None) M.AuditLog.log('remove project icon') g.post_event('project_updated') @@ -415,7 +415,7 @@ def update(self, name=None, if icon is not None and icon != b'': if c.project.icon: - M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon'))) + M.ProjectFile.query.remove(dict(project_id=c.project._id, category={'$regex': r'^icon'})) save_icon = c.project.save_icon(icon.filename, icon.file, content_type=icon.type) if not save_icon: M.AuditLog.log('could not update project icon') diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py index 47defa5db6..d881632845 100644 --- a/Allura/allura/lib/plugin.py +++ b/Allura/allura/lib/plugin.py @@ -567,8 +567,8 @@ def by_username(self, username): escaped_underscore = re.escape('_') # changes in py3.x versions # https://docs.python.org/3/library/re.html#re.escape un = un.replace(escaped_underscore, '[-_]') un = un.replace(r'\-', '[-_]') - rex = re.compile('^' + un + '$') - return M.User.query.get(username=rex, disabled=False, pending=False) + rex = r'^' + un + '$' + return M.User.query.get(username={'$regex': rex}, disabled=False, pending=False) def set_password(self, user, old_password, new_password): if old_password is not None and not self.validate_password(user, old_password): diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py index 33a675e8a1..c302e2487b 100644 --- a/Allura/allura/model/project.py +++ b/Allura/allura/model/project.py @@ -161,7 +161,7 @@ def subcategories(self): @property def children(self): - return sorted(self.query.find({'fullpath': re.compile('^' + re.escape(self.fullpath) + ' ::')}).all(), + return sorted(self.query.find({'fullpath': {'$regex': '^' + re.escape(self.fullpath) + ' ::'}}).all(), key=lambda t: t.fullpath.lower()) @property diff --git a/ForgeDiscussion/forgediscussion/controllers/forum.py b/ForgeDiscussion/forgediscussion/controllers/forum.py index 953d40dd01..064135ce0c 100644 --- a/ForgeDiscussion/forgediscussion/controllers/forum.py +++ b/ForgeDiscussion/forgediscussion/controllers/forum.py @@ -136,7 +136,7 @@ def subscribe_to_forum(self, subscribe=None, unsubscribe=None, shortname=None, * user_id=c.user._id, project_id=c.project._id, app_config_id=c.app.config._id, - artifact_index_id=re.compile('^' + re.escape(forumthread_index_prefix)), + artifact_index_id={'$regex': '^' + re.escape(forumthread_index_prefix)}, )).all() # get the ForumThread objects from the subscriptions thread_index_ids = [mbox.artifact_index_id for mbox in thread_mboxes] diff --git a/ForgeUserStats/forgeuserstats/controllers/userstats.py b/ForgeUserStats/forgeuserstats/controllers/userstats.py index 7efcb512b3..7df06f51fe 100644 --- a/ForgeUserStats/forgeuserstats/controllers/userstats.py +++ b/ForgeUserStats/forgeuserstats/controllers/userstats.py @@ -38,7 +38,7 @@ class ForgeUserStatsCatController(BaseController): @expose() def _lookup(self, category, *remainder): - cat = M.TroveCategory.query.get(shortname=category, fullpath=re.compile(r'^Topic :: ')) + cat = M.TroveCategory.query.get(shortname=category, fullpath={'$regex': r'^Topic :: '}) if not cat: raise exc.HTTPNotFound return ForgeUserStatsCatController(category=cat), remainder