Skip to content

Commit

Permalink
Merge pull request #49408 from nextcloud/refactor/files-cleanup
Browse files Browse the repository at this point in the history
refactor(files): Use functions for all in `occ files:cleanup`
  • Loading branch information
juliusknorr authored Nov 21, 2024
2 parents cbb937f + b9cc4ba commit 37936bc
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions apps/files/lib/Command/DeleteOrphanedFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected function configure(): void {
}

public function execute(InputInterface $input, OutputInterface $output): int {
$deletedEntries = 0;
$fileIdsByStorage = [];

$deletedStorages = array_diff($this->getReferencedStorages(), $this->getExistingStorages());
Expand All @@ -44,26 +43,17 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$fileIdsByStorage = $this->getFileIdsForStorages($deletedStorages);
}

$deleteQuery = $this->connection->getQueryBuilder();
$deleteQuery->delete('filecache')
->where($deleteQuery->expr()->in('storage', $deleteQuery->createParameter('storage_ids')));

$deletedStorageChunks = array_chunk($deletedStorages, self::CHUNK_SIZE);
foreach ($deletedStorageChunks as $deletedStorageChunk) {
$deleteQuery->setParameter('storage_ids', $deletedStorageChunk, IQueryBuilder::PARAM_INT_ARRAY);
$deletedEntries += $deleteQuery->executeStatement();
}

$deletedEntries = $this->cleanupOrphanedFileCache($deletedStorages);
$output->writeln("$deletedEntries orphaned file cache entries deleted");

if ($deleteExtended) {
$deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended($fileIdsByStorage);
$output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted");
}


$deletedMounts = $this->cleanupOrphanedMounts();
$output->writeln("$deletedMounts orphaned mount entries deleted");

return self::SUCCESS;
}

Expand Down Expand Up @@ -106,6 +96,22 @@ private function getFileIdsForStorages(array $storageIds): array {
return $result;
}

private function cleanupOrphanedFileCache(array $deletedStorages): int {
$deletedEntries = 0;

$deleteQuery = $this->connection->getQueryBuilder();
$deleteQuery->delete('filecache')
->where($deleteQuery->expr()->in('storage', $deleteQuery->createParameter('storage_ids')));

$deletedStorageChunks = array_chunk($deletedStorages, self::CHUNK_SIZE);
foreach ($deletedStorageChunks as $deletedStorageChunk) {
$deleteQuery->setParameter('storage_ids', $deletedStorageChunk, IQueryBuilder::PARAM_INT_ARRAY);
$deletedEntries += $deleteQuery->executeStatement();
}

return $deletedEntries;
}

/**
* @param array<int, int[]> $fileIdsByStorage
* @return int
Expand Down

0 comments on commit 37936bc

Please sign in to comment.