You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultPeriodCleaner.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.purge.period;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.stream.Collectors;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.sonar.api.config.Configuration;
  28. import org.sonar.api.utils.DateUtils;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.purge.PurgeDao;
  31. import org.sonar.db.purge.PurgeProfiler;
  32. import org.sonar.db.purge.PurgeableAnalysisDto;
  33. public class DefaultPeriodCleaner {
  34. private static final Logger LOG = LoggerFactory.getLogger(DefaultPeriodCleaner.class);
  35. private final PurgeDao purgeDao;
  36. private final PurgeProfiler profiler;
  37. public DefaultPeriodCleaner(PurgeDao purgeDao, PurgeProfiler profiler) {
  38. this.purgeDao = purgeDao;
  39. this.profiler = profiler;
  40. }
  41. public void clean(DbSession session, String rootUuid, Configuration config) {
  42. doClean(rootUuid, new Filters(config).all(), session);
  43. }
  44. @VisibleForTesting
  45. void doClean(String rootUuid, List<Filter> filters, DbSession session) {
  46. List<PurgeableAnalysisDto> history = new ArrayList<>(selectAnalysesOfComponent(rootUuid, session));
  47. for (Filter filter : filters) {
  48. filter.log();
  49. List<PurgeableAnalysisDto> toDelete = filter.filter(history);
  50. List<PurgeableAnalysisDto> deleted = delete(rootUuid, toDelete, session);
  51. history.removeAll(deleted);
  52. }
  53. }
  54. private List<PurgeableAnalysisDto> delete(String rootUuid, List<PurgeableAnalysisDto> snapshots, DbSession session) {
  55. if (LOG.isDebugEnabled()) {
  56. LOG.debug("<- Delete analyses of component {}: {}",
  57. rootUuid,
  58. snapshots.stream().map(snapshot -> snapshot.getAnalysisUuid() + "@" + DateUtils.formatDateTime(snapshot.getDate()))
  59. .collect(Collectors.joining(", ")));
  60. }
  61. purgeDao.deleteAnalyses(
  62. session, profiler,
  63. snapshots.stream().map(PurgeableAnalysisDto::getAnalysisUuid).toList());
  64. return snapshots;
  65. }
  66. private List<PurgeableAnalysisDto> selectAnalysesOfComponent(String componentUuid, DbSession session) {
  67. return purgeDao.selectPurgeableAnalyses(componentUuid, session);
  68. }
  69. }