Skip to content

Commit

Permalink
feat: remove orphaned entries from filecache_extended
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Jun 21, 2023
1 parent 32bbe3d commit 4c3a6c7
Showing 1 changed file with 37 additions and 1 deletion.
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 @@ -48,7 +50,8 @@ public function __construct(IDBConnection $connection) {
protected function configure() {
$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 {
Expand Down Expand Up @@ -79,11 +82,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 0;
}

private function cleanupOrphanedFileCacheExtended() {
$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() {
$deletedEntries = 0;

Expand Down

0 comments on commit 4c3a6c7

Please sign in to comment.