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

Speed up recursive item lists #1694

Merged
merged 1 commit into from
Oct 18, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 1.30.2

### Improvements

- Speed up recursive item lists ([#1694](../../pull/1694))

## 1.30.1

### Improvements
Expand Down
18 changes: 5 additions & 13 deletions girder/girder_large_image/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def _groupingPipeline(initialPipeline, cbase, grouping, sort=None):
{'keys': ['meta.dicom.PatientID'], 'counts': {
'meta.dicom.StudyInstanceUID': 'meta._count.studycount',
'meta.dicom.SeriesInstanceUID': 'meta._count.seriescount'}}
:param sort: an optional lost of (key, direction) tuples
:param sort: an optional list of (key, direction) tuples
"""
for gidx, gr in enumerate(grouping['keys']):
grsort = [(gr, 1)] + (sort or [])
grsort = [(gr, 1)] + (sort or []) + [('_id', 1)]
initialPipeline.extend([{
'$match': {gr: {'$exists': True}},
}, {
Expand Down Expand Up @@ -130,6 +130,7 @@ def _itemFindRecursive( # noqa
from bson.objectid import ObjectId

if folderId:
user = self.getCurrentUser()
if recurse:
pipeline = [
{'$match': {'_id': ObjectId(folderId)}},
Expand All @@ -141,6 +142,7 @@ def _itemFindRecursive( # noqa
'as': '_folder',
'startWith': '$_id',
}},
{'$match': Folder().permissionClauses(user, AccessType.READ, '_folder.')},
{'$group': {'_id': '$_folder._id'}},
]
children = [ObjectId(folderId)] + next(Folder().collection.aggregate(pipeline))['_id']
Expand All @@ -155,22 +157,13 @@ def _itemFindRecursive( # noqa
if name:
filters['name'] = name
filters['folderId'] = {'$in': children}
user = self.getCurrentUser()
if isinstance(sort, list):
sort.append(('parentId', 1))

# This is taken from girder.utility.acl_mixin.findWithPermissions,
# except it adds a grouping stage
initialPipeline = [
{'$match': filters},
{'$lookup': {
'from': 'folder',
'localField': Item().resourceParent,
'foreignField': '_id',
'as': '__parent',
}},
{'$match': Item().permissionClauses(user, AccessType.READ, '__parent.')},
{'$project': {'__parent': False}},
]
if group is not None:
if not isinstance(group, list):
Expand Down Expand Up @@ -200,6 +193,7 @@ def _itemFindRecursive( # noqa
if offset:
fullPipeline.append({'$skip': offset})

print(fullPipeline)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realize this is merged but this print line should probably be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Follow on PR coming.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought my pre-commit rules would have stopped me.

logger.debug('Find item pipeline %r', fullPipeline)

options = {
Expand All @@ -220,8 +214,6 @@ def count():
result.count = count
result.fromAggregate = True
return result

return Item().findWithPermissions(filters, offset, limit, sort=sort, user=user)
return origItemFind(folderId, text, name, limit, offset, sort, filters)


Expand Down