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

feat: remove orphaned entries from filecache_extended #38933

Merged
merged 2 commits into from
Apr 15, 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
38 changes: 37 additions & 1 deletion apps/files/lib/Command/DeleteOrphanedFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
*/
namespace OCA\Files\Command;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -44,7 +46,8 @@ public function __construct(
protected function configure(): void {
$this
->setName('files:cleanup')
->setDescription('cleanup filecache');
->setDescription('cleanup filecache')
->addOption('skip-filecache-extended', null, InputOption::VALUE_NONE, 'don\'t remove orphaned entries from filecache_extended');
}

public function execute(InputInterface $input, OutputInterface $output): int {
Expand Down Expand Up @@ -75,11 +78,44 @@ public function execute(InputInterface $input, OutputInterface $output): int {

$output->writeln("$deletedEntries orphaned file cache entries deleted");

if (!$input->getOption('skip-filecache-extended')) {
$deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended();
$output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted");
}


$deletedMounts = $this->cleanupOrphanedMounts();
$output->writeln("$deletedMounts orphaned mount entries deleted");
return self::SUCCESS;
}

private function cleanupOrphanedFileCacheExtended(): int {
$deletedEntries = 0;

$query = $this->connection->getQueryBuilder();
$query->select('fce.fileid')
->from('filecache_extended', 'fce')
->leftJoin('fce', 'filecache', 'fc', $query->expr()->eq('fce.fileid', 'fc.fileid'))
->where($query->expr()->isNull('fc.fileid'))
->setMaxResults(self::CHUNK_SIZE);

$deleteQuery = $this->connection->getQueryBuilder();
$deleteQuery->delete('filecache_extended')
->where($deleteQuery->expr()->in('fileid', $deleteQuery->createParameter('idsToDelete')));

$result = $query->executeQuery();
while ($result->rowCount() > 0) {
$idsToDelete = $result->fetchAll(\PDO::FETCH_COLUMN);

$deleteQuery->setParameter('idsToDelete', $idsToDelete, IQueryBuilder::PARAM_INT_ARRAY);
$deletedEntries += $deleteQuery->executeStatement();

$result = $query->executeQuery();
}

return $deletedEntries;
}

private function cleanupOrphanedMounts(): int {
$deletedEntries = 0;

Expand Down
3 changes: 2 additions & 1 deletion apps/files/tests/Command/DeleteOrphanedFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ public function testClearFiles() {

// parent folder, `files`, ´test` and `welcome.txt` => 4 elements
$output
->expects($this->exactly(2))
->expects($this->exactly(3))
->method('writeln')
->withConsecutive(
['3 orphaned file cache entries deleted'],
['0 orphaned file cache extended entries deleted'],
['1 orphaned mount entries deleted'],
);

Expand Down
Loading