From 2f6cc924914e52841da58866c93b5ab0546ce72f Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Wed, 21 Jun 2023 16:34:44 +0200 Subject: [PATCH 1/2] feat: remove orphaned entries from filecache_extended Signed-off-by: Daniel Kesselberg --- .../files/lib/Command/DeleteOrphanedFiles.php | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/files/lib/Command/DeleteOrphanedFiles.php b/apps/files/lib/Command/DeleteOrphanedFiles.php index 4b7179271f584..1af8ad64a37f2 100644 --- a/apps/files/lib/Command/DeleteOrphanedFiles.php +++ b/apps/files/lib/Command/DeleteOrphanedFiles.php @@ -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; /** @@ -44,7 +46,8 @@ public function __construct( protected function configure(): void { $this ->setName('files:cleanup') - ->setDescription('cleanup filecache'); + ->setDescription('cleanup filecache') + ->addOption('filecache-extended', null, InputOption::VALUE_NONE, 'remove orphaned entries from filecache_extended'); } public function execute(InputInterface $input, OutputInterface $output): int { @@ -75,11 +78,44 @@ public function execute(InputInterface $input, OutputInterface $output): int { $output->writeln("$deletedEntries orphaned file cache entries deleted"); + if ($input->getOption('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; From 1d34f0a824e15ad5ed685d05dc46910362e99404 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 9 Feb 2024 14:49:56 +0100 Subject: [PATCH 2/2] feat: cleanup filecache_extended items by default Signed-off-by: Robin Appelman Signed-off-by: Daniel Kesselberg --- apps/files/lib/Command/DeleteOrphanedFiles.php | 4 ++-- apps/files/tests/Command/DeleteOrphanedFilesTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/files/lib/Command/DeleteOrphanedFiles.php b/apps/files/lib/Command/DeleteOrphanedFiles.php index 1af8ad64a37f2..1b5727ae546c0 100644 --- a/apps/files/lib/Command/DeleteOrphanedFiles.php +++ b/apps/files/lib/Command/DeleteOrphanedFiles.php @@ -47,7 +47,7 @@ protected function configure(): void { $this ->setName('files:cleanup') ->setDescription('cleanup filecache') - ->addOption('filecache-extended', null, InputOption::VALUE_NONE, 'remove orphaned entries from filecache_extended'); + ->addOption('skip-filecache-extended', null, InputOption::VALUE_NONE, 'don\'t remove orphaned entries from filecache_extended'); } public function execute(InputInterface $input, OutputInterface $output): int { @@ -78,7 +78,7 @@ public function execute(InputInterface $input, OutputInterface $output): int { $output->writeln("$deletedEntries orphaned file cache entries deleted"); - if ($input->getOption('filecache-extended')) { + if (!$input->getOption('skip-filecache-extended')) { $deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended(); $output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted"); } diff --git a/apps/files/tests/Command/DeleteOrphanedFilesTest.php b/apps/files/tests/Command/DeleteOrphanedFilesTest.php index 520d3278e6435..ee1144963ac32 100644 --- a/apps/files/tests/Command/DeleteOrphanedFilesTest.php +++ b/apps/files/tests/Command/DeleteOrphanedFilesTest.php @@ -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'], );