Skip to content

Commit

Permalink
use $regex instead of re.compile in mongo queries, so it uses indexes…
Browse files Browse the repository at this point in the history
… properly. Maybe fixed in current mongo versions https://jira.mongodb.org/browse/SERVER-26991
  • Loading branch information
brondsem committed Feb 21, 2024
1 parent 573e81e commit 430baa3
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Allura/allura/controllers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions Allura/allura/controllers/site_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions Allura/allura/controllers/trovecategories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions Allura/allura/ext/admin/admin_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions Allura/allura/lib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion Allura/allura/model/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ForgeDiscussion/forgediscussion/controllers/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion ForgeUserStats/forgeuserstats/controllers/userstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 430baa3

Please sign in to comment.