Skip to content

Commit

Permalink
Reducing queries in ScanFiles.php
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoHeichou <federicoheichou@gmail.com>
  • Loading branch information
FedericoHeichou authored Aug 23, 2023
1 parent 590b1d9 commit b5f21ef
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions apps/files/lib/BackgroundJob/ScanFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,6 @@ protected function runScanner(string $user): void {
\OC_Util::tearDownFS();
}

/**
* Find a storage which have unindexed files and return a user with access to the storage
*
* @return string|false
*/
private function getUserToScan() {
$query = $this->connection->getQueryBuilder();
$query->select('user_id')
->from('filecache', 'f')
->innerJoin('f', 'mounts', 'm', $query->expr()->eq('storage_id', 'storage'))
->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
->setMaxResults(1);

return $query->executeQuery()->fetchOne();
}

/**
* @param $argument
* @throws \Exception
Expand All @@ -106,18 +89,59 @@ protected function run($argument) {
return;
}

$usersScanned = 0;
$lastUser = '';
$user = $this->getUserToScan();
while ($user && $usersScanned < self::USERS_PER_SESSION && $lastUser !== $user) {
$this->runScanner($user);
$lastUser = $user;
$user = $this->getUserToScan();
$usersScanned += 1;
$usersScanned = [];
$storageScanned = [];

$query = $this->connection->getQueryBuilder();
$query->select('storage_id', 'user_id')->from('mounts');
$storageUsers = $query->executeQuery()->fetchAll();
$storageUsers = array_column($storageUsers, 'user_id', 'storage_id');
$mountedStorageIds = array_keys($storageUsers);

$query = $this->connection->getQueryBuilder();
$query->selectDistinct('storage')->from('filecache', 'f')
->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));
$storageIds = $query->executeQuery()->fetchAll();
$storageIds = array_column($storageIds, 'storage');

$scanningStrageIds = array_intersect($mountedStorageIds, $storageIds);

foreach ($scanningStrageIds as $scanningStrageId) {
if (count($usersScanned) >= self::USERS_PER_SESSION) {
break;
}
$storageScanned[] = $scanningStrageId;
if (in_array($storageUsers[$scanningStrageId], $usersScanned)) {
continue;
}
$this->runScanner($storageUsers[$scanningStrageId]);
$usersScanned[] = $storageUsers[$scanningStrageId];
}

if ($lastUser === $user) {
$this->logger->warning("User $user still has unscanned files after running background scan, background scan might be stopped prematurely");
if ($this->config->getSystemValue('loglevel') > \OC\Log::WARN) {

Check notice

Code scanning / Psalm

DeprecatedConstant Note

Constant OC\Log::WARN is deprecated
return;
}

$query = $this->connection->getQueryBuilder();
$query->selectDistinct('storage')->from('filecache', 'f')
->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->in('storage', $query->createNamedParameter($storageScanned, IQueryBuilder::PARAM_INT_ARRAY)));
$unscannedStorageIds = $query->executeQuery()->fetchAll();
$unscannedStorageIds = array_column($unscannedStorageIds, 'storage');

foreach ($unscannedStorageIds as $unscannedStorageId) {
if (!isset($storageUsers[$unscannedStorageId])) {
continue;
}
$user = $storageUsers[$unscannedStorageId];
$userIndex = array_search($user, $usersScanned);
if ($userIndex !== false) {
unset($usersScanned[$userIndex]);
$this->logger->warning("User $user still has unscanned files after running background scan, background scan might be stopped prematurely");
}
}

}
}

0 comments on commit b5f21ef

Please sign in to comment.