DeleteOrphanedItems.php 5.4 KB

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