Skip to content

Commit a3aba31

Browse files
committed
fix: Fix focused filter performance issues
There can be several mounts for a same storage id, so the left join on oc_mounts was blowing up on a big instance. As we are only intested in files from the user’s home, we can compute the storage id beforehand. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 34d359f commit a3aba31

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\AppFramework\Bootstrap\IBootstrap;
3333
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3434
use OCP\DB\Events\AddMissingIndicesEvent;
35+
use OCP\Files\IRootFolder;
3536
use OCP\IAppConfig;
3637
use OCP\IConfig;
3738
use OCP\IDateTimeFormatter;
@@ -106,6 +107,7 @@ public function register(IRegistrationContext $context): void {
106107
$c->get('ActivityConnectionAdapter'),
107108
$c->get(LoggerInterface::class),
108109
$c->get(IConfig::class),
110+
$c->get(IRootFolder::class),
109111
);
110112
});
111113

lib/Data.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\Activity\IManager;
1818
use OCP\DB\Exception;
1919
use OCP\DB\QueryBuilder\IQueryBuilder;
20+
use OCP\Files\IRootFolder;
2021
use OCP\IConfig;
2122
use OCP\IDBConnection;
2223
use Psr\Log\LoggerInterface;
@@ -43,6 +44,7 @@ public function __construct(
4344
protected IDBConnection $connection,
4445
protected LoggerInterface $logger,
4546
protected IConfig $config,
47+
protected IRootFolder $rootFolder,
4648
) {
4749
}
4850

@@ -363,23 +365,20 @@ private function applyStreamConditions(
363365
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType)));
364366
$query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
365367
} elseif ($filter === 'focused') {
368+
// Get home storage id
369+
$storageId = $this->rootFolder->getUserFolder($user)->getMountPoint()->getNumericStorageId();
366370
// Only activity related to files owned by the user
367371
$query->leftJoin('a', 'filecache', 'f',
368372
$query->expr()->andX(
369373
$query->expr()->eq('a.object_type', $query->createNamedParameter('files')),
370374
$query->expr()->eq('a.object_id', 'f.fileid'),
371375
));
372-
$query->leftJoin('f', 'mounts', 'm',
373-
$query->expr()->andX(
374-
$query->expr()->eq('a.object_type', $query->createNamedParameter('files')),
375-
$query->expr()->eq('f.storage', 'm.storage_id'),
376-
));
377376
// Filter out our own activity
378377
$query->andWhere($query->expr()->neq('user', $query->createNamedParameter($user)));
379378
$query->andWhere(
380379
$query->expr()->orX(
381380
// Affected object is one of our files
382-
$query->expr()->eq('m.mount_point', $query->createNamedParameter('/' . $user . '/')),
381+
$query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)),
383382
// There is no affected object, so the activity affects our user directly
384383
$query->expr()->eq('object_type', $query->createNamedParameter('')),
385384
// Show all sharing related events

tests/DataTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\Activity\Exceptions\FilterNotFoundException;
3131
use OCP\Activity\IManager;
3232
use OCP\DB\QueryBuilder\IQueryBuilder;
33+
use OCP\Files\IRootFolder;
3334
use OCP\IConfig;
3435
use OCP\IDBConnection;
3536
use OCP\IL10N;
@@ -52,6 +53,7 @@ class DataTest extends TestCase {
5253
protected IManager $realActivityManager;
5354
protected NullLogger $logger;
5455
protected IConfig&MockObject $config;
56+
protected IRootFolder&MockObject $rootFolder;
5557

5658
protected function setUp(): void {
5759
parent::setUp();
@@ -63,12 +65,14 @@ protected function setUp(): void {
6365

6466
$activityManager = $this->createMock(IManager::class);
6567
$this->config = $this->createMock(IConfig::class);
68+
$this->rootFolder = $this->createMock(IRootFolder::class);
6669

6770
$this->data = new Data(
6871
$activityManager,
6972
$this->dbConnection,
7073
$this->logger,
71-
$this->config
74+
$this->config,
75+
$this->rootFolder,
7276
);
7377
}
7478

0 commit comments

Comments
 (0)