DeleteOrphanedItems.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\BackgroundJob;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\TimedJob;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Delete all share entries that have no matching entries in the file cache table.
  15. */
  16. class DeleteOrphanedItems extends TimedJob {
  17. public const CHUNK_SIZE = 200;
  18. protected $defaultIntervalMin = 60;
  19. /**
  20. * sets the correct interval for this timed job
  21. */
  22. public function __construct(
  23. ITimeFactory $time,
  24. protected IDBConnection $connection,
  25. protected LoggerInterface $logger,
  26. ) {
  27. parent::__construct($time);
  28. $this->interval = $this->defaultIntervalMin * 60;
  29. }
  30. /**
  31. * Makes the background job do its work
  32. *
  33. * @param array $argument unused argument
  34. */
  35. public function run($argument) {
  36. $this->cleanSystemTags();
  37. $this->cleanUserTags();
  38. $this->cleanComments();
  39. $this->cleanCommentMarkers();
  40. }
  41. /**
  42. * Deleting orphaned system tag mappings
  43. *
  44. * @param string $table
  45. * @param string $idCol
  46. * @param string $typeCol
  47. * @return int Number of deleted entries
  48. */
  49. protected function cleanUp(string $table, string $idCol, string $typeCol): int {
  50. $deletedEntries = 0;
  51. $deleteQuery = $this->connection->getQueryBuilder();
  52. $deleteQuery->delete($table)
  53. ->where($deleteQuery->expr()->eq($idCol, $deleteQuery->createParameter('objectid')));
  54. if ($this->connection->getShardDefinition('filecache')) {
  55. $sourceIdChunks = $this->getItemIds($table, $idCol, $typeCol, 1000);
  56. foreach ($sourceIdChunks as $sourceIdChunk) {
  57. $deletedSources = $this->findMissingSources($sourceIdChunk);
  58. $deleteQuery->setParameter('objectid', $deletedSources, IQueryBuilder::PARAM_INT_ARRAY);
  59. $deletedEntries += $deleteQuery->executeStatement();
  60. }
  61. } else {
  62. $query = $this->connection->getQueryBuilder();
  63. $query->select('t1.' . $idCol)
  64. ->from($table, 't1')
  65. ->where($query->expr()->eq($typeCol, $query->expr()->literal('files')))
  66. ->leftJoin('t1', 'filecache', 't2', $query->expr()->eq($query->expr()->castColumn('t1.' . $idCol, IQueryBuilder::PARAM_INT), 't2.fileid'))
  67. ->andWhere($query->expr()->isNull('t2.fileid'))
  68. ->groupBy('t1.' . $idCol)
  69. ->setMaxResults(self::CHUNK_SIZE);
  70. $deleteQuery = $this->connection->getQueryBuilder();
  71. $deleteQuery->delete($table)
  72. ->where($deleteQuery->expr()->in($idCol, $deleteQuery->createParameter('objectid')));
  73. $deletedInLastChunk = self::CHUNK_SIZE;
  74. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  75. $chunk = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  76. $deletedInLastChunk = count($chunk);
  77. $deleteQuery->setParameter('objectid', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
  78. $deletedEntries += $deleteQuery->executeStatement();
  79. }
  80. }
  81. return $deletedEntries;
  82. }
  83. /**
  84. * @param string $table
  85. * @param string $idCol
  86. * @param string $typeCol
  87. * @param int $chunkSize
  88. * @return \Iterator<int[]>
  89. * @throws \OCP\DB\Exception
  90. */
  91. private function getItemIds(string $table, string $idCol, string $typeCol, int $chunkSize): \Iterator {
  92. $query = $this->connection->getQueryBuilder();
  93. $query->select($idCol)
  94. ->from($table)
  95. ->where($query->expr()->eq($typeCol, $query->expr()->literal('files')))
  96. ->groupBy($idCol)
  97. ->andWhere($query->expr()->gt($idCol, $query->createParameter('min_id')))
  98. ->setMaxResults($chunkSize);
  99. $minId = 0;
  100. while (true) {
  101. $query->setParameter('min_id', $minId);
  102. $rows = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  103. if (count($rows) > 0) {
  104. $minId = $rows[count($rows) - 1];
  105. yield $rows;
  106. } else {
  107. break;
  108. }
  109. }
  110. }
  111. private function findMissingSources(array $ids): array {
  112. $qb = $this->connection->getQueryBuilder();
  113. $qb->select('fileid')
  114. ->from('filecache')
  115. ->where($qb->expr()->in('fileid', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  116. $found = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  117. return array_diff($ids, $found);
  118. }
  119. /**
  120. * Deleting orphaned system tag mappings
  121. *
  122. * @return int Number of deleted entries
  123. */
  124. protected function cleanSystemTags() {
  125. $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype');
  126. $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  127. return $deletedEntries;
  128. }
  129. /**
  130. * Deleting orphaned user tag mappings
  131. *
  132. * @return int Number of deleted entries
  133. */
  134. protected function cleanUserTags() {
  135. $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type');
  136. $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  137. return $deletedEntries;
  138. }
  139. /**
  140. * Deleting orphaned comments
  141. *
  142. * @return int Number of deleted entries
  143. */
  144. protected function cleanComments() {
  145. $deletedEntries = $this->cleanUp('comments', 'object_id', 'object_type');
  146. $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']);
  147. return $deletedEntries;
  148. }
  149. /**
  150. * Deleting orphaned comment read markers
  151. *
  152. * @return int Number of deleted entries
  153. */
  154. protected function cleanCommentMarkers() {
  155. $deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type');
  156. $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']);
  157. return $deletedEntries;
  158. }
  159. }