From fc6b67181d3f7affaa9ea3ba14cb33650bcdec70 Mon Sep 17 00:00:00 2001 From: Julius Knorr Date: Mon, 28 Oct 2024 14:37:48 +0100 Subject: [PATCH] perf(dashboard): Use storage id for more performant index usage on dashboard query Signed-off-by: Julius Knorr --- lib/Service/RecentPagesService.php | 6 +++++- tests/Unit/Service/RecentPagesServiceTest.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Service/RecentPagesService.php b/lib/Service/RecentPagesService.php index 5a7557388..b72dbe13f 100644 --- a/lib/Service/RecentPagesService.php +++ b/lib/Service/RecentPagesService.php @@ -14,6 +14,7 @@ use OCP\DB\Exception; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\IMimeTypeLoader; +use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; @@ -30,6 +31,7 @@ public function __construct( protected IMimeTypeLoader $mimeTypeLoader, protected IURLGenerator $urlGenerator, protected IL10N $l10n, + protected IRootFolder $rootFolder, ) { } @@ -50,6 +52,7 @@ public function forUser(IUser $user, int $limit = 10): array { $qb = $this->dbc->getQueryBuilder(); $appData = $this->getAppDataFolderName(); + $storageId = $this->rootFolder->get($appData)->getStorage()->getCache()->getNumericStorageId(); $mimeTypeMd = $this->mimeTypeLoader->getId('text/markdown'); $expressions = []; @@ -64,7 +67,8 @@ public function forUser(IUser $user, int $limit = 10): array { $qb->select('p.*', 'f.mtime as timestamp', 'f.name as filename', 'f.path as path') ->from('filecache', 'f') ->leftJoin('f', 'collectives_pages', 'p', $qb->expr()->eq('f.fileid', 'p.file_id')) - ->where($qb->expr()->orX(...$expressions)) + ->where($qb->expr()->eq('f.storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_STR))) + ->andWhere($qb->expr()->orX(...$expressions)) ->andWhere($qb->expr()->eq('f.mimetype', $qb->createNamedParameter($mimeTypeMd, IQueryBuilder::PARAM_INT))) ->orderBy('f.mtime', 'DESC') ->setMaxResults($limit); diff --git a/tests/Unit/Service/RecentPagesServiceTest.php b/tests/Unit/Service/RecentPagesServiceTest.php index 5c178a05a..272d4e3d8 100644 --- a/tests/Unit/Service/RecentPagesServiceTest.php +++ b/tests/Unit/Service/RecentPagesServiceTest.php @@ -13,6 +13,7 @@ use OCA\Collectives\Service\NotFoundException; use OCA\Collectives\Service\RecentPagesService; use OCP\Files\IMimeTypeLoader; +use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; @@ -32,6 +33,7 @@ protected function setUp(): void { $mimeTypeLoader = $this->createMock(IMimeTypeLoader::class); $urlGenerator = $this->createMock(IURLGenerator::class); $l10n = $this->createMock(IL10N::class); + $rootFolder = $this->createMock(IRootFolder::class); $this->service = new RecentPagesService( $this->collectiveService, @@ -39,7 +41,9 @@ protected function setUp(): void { $config, $mimeTypeLoader, $urlGenerator, - $l10n); + $l10n, + $rootFolder + ); $this->user = $this->createMock(IUser::class); $this->user->method('getUID')->willReturn('user');