Skip to content

Commit 189ff65

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 189ff65

5 files changed

Lines changed: 25 additions & 8 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/Controller/APIv1ControllerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\Activity\IExtension;
3838
use OCP\Activity\IManager;
3939
use OCP\AppFramework\Utility\ITimeFactory;
40+
use OCP\Files\IRootFolder;
4041
use OCP\IConfig;
4142
use OCP\IDBConnection;
4243
use OCP\IL10N;
@@ -117,6 +118,7 @@ protected function cleanUp(): void {
117118
Server::get(IDBConnection::class),
118119
$this->createMock(LoggerInterface::class),
119120
$this->createMock(IConfig::class),
121+
$this->createMock(IRootFolder::class),
120122
);
121123

122124
$this->deleteUser($data, 'activity-api-user1');
@@ -227,6 +229,7 @@ public function testGet(string $user, int $start, int $count, array $expected):
227229
Server::get(IDBConnection::class),
228230
$this->createMock(LoggerInterface::class),
229231
$this->createMock(IConfig::class),
232+
$this->createMock(IRootFolder::class),
230233
);
231234

232235
$controller = new APIv1Controller(

tests/DataDeleteActivitiesTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\BackgroundJob\IJobList;
3333
use OCP\DB\IPreparedStatement;
3434
use OCP\DB\QueryBuilder\IQueryBuilder;
35+
use OCP\Files\IRootFolder;
3536
use OCP\IConfig;
3637
use OCP\IDBConnection;
3738
use OCP\Server;
@@ -62,6 +63,7 @@ protected function setUp(): void {
6263
Server::get(IDBConnection::class),
6364
$this->createMock(LoggerInterface::class),
6465
$this->createMock(IConfig::class),
66+
$this->createMock(IRootFolder::class),
6567
);
6668
}
6769

@@ -154,6 +156,7 @@ public function testExpireActivitiesWithExcludedUsers(): void {
154156
Server::get(IDBConnection::class),
155157
$this->createMock(LoggerInterface::class),
156158
$config,
159+
$this->createMock(IRootFolder::class),
157160
);
158161

159162
$time = $this->createMock(ITimeFactory::class);

tests/DataTest.php

Lines changed: 12 additions & 2 deletions
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

@@ -516,7 +520,13 @@ private function getUnfilteredData(): Data {
516520
$activityManager->method('getFilterById')
517521
->willThrowException(new FilterNotFoundException('all'));
518522

519-
return new Data($activityManager, $this->dbConnection, $this->logger, $this->config);
523+
return new Data(
524+
$activityManager,
525+
$this->dbConnection,
526+
$this->logger,
527+
$this->config,
528+
$this->rootFolder,
529+
);
520530
}
521531

522532
private function getHistogramUserSettings(): UserSettings&MockObject {

0 commit comments

Comments
 (0)