2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar 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.
11 * Sonar 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.dbcleaner.period;
22 import com.google.common.collect.Lists;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.sonar.api.database.DatabaseSession;
26 import org.sonar.api.database.model.Snapshot;
27 import org.sonar.api.resources.Project;
28 import org.sonar.plugins.dbcleaner.api.PeriodCleaner;
29 import org.sonar.plugins.dbcleaner.api.PurgeUtils;
31 import java.text.DateFormat;
32 import java.util.Date;
33 import java.util.GregorianCalendar;
34 import java.util.List;
36 public final class DefaultPeriodCleaner implements PeriodCleaner {
38 private static final Logger LOG = LoggerFactory.getLogger(DefaultPeriodCleaner.class);
39 private final SQLRequests sql;
40 private DatabaseSession session;
42 public DefaultPeriodCleaner(DatabaseSession session) {
43 this.session = session;
44 this.sql = new SQLRequests(session);
47 public void purge(Project project, int projectSnapshotId) {
48 Periods periods = new Periods(project);
50 purge(project, projectSnapshotId, periods);
53 void purge(Project project, int projectSnapshotId, Periods periods) {
54 List<SnapshotFilter> filters = newFilters(periods);
55 List<Snapshot> snapshotHistory = selectProjectSnapshots(project, projectSnapshotId);
56 applyFilters(snapshotHistory, filters);
57 deleteSnapshotsAndAllRelatedData(snapshotHistory);
60 private List<Snapshot> selectProjectSnapshots(Project project, int snapshotId) {
61 List<Snapshot> snapshotHistory = Lists.newLinkedList(sql.getProjectSnapshotsOrderedByCreatedAt(snapshotId));
62 LOG.debug("The project '" + project.getName() + "' has " + snapshotHistory.size() + " snapshots.");
63 return snapshotHistory;
66 private void deleteSnapshotsAndAllRelatedData(List<Snapshot> snapshotHistory) {
67 if (snapshotHistory.isEmpty()) {
68 LOG.info("There are no snapshots to purge");
72 List<Integer> ids = Lists.newArrayList();
73 for (Snapshot snapshot : snapshotHistory) {
74 ids.addAll(sql.getChildIds(snapshot));
76 LOG.info("There are " + snapshotHistory.size() + " snapshots and " + (ids.size() - snapshotHistory.size())
77 + " children snapshots which are obsolete and are going to be deleted.");
78 if (LOG.isDebugEnabled()) {
79 DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
80 for (Snapshot snapshot : snapshotHistory) {
81 LOG.debug("Delete snapshot created at " + format.format(snapshot.getCreatedAt()));
84 PurgeUtils.deleteSnapshotsData(session, ids);
87 private void applyFilters(List<Snapshot> snapshotHistory, List<SnapshotFilter> filters) {
88 for (SnapshotFilter filter : filters) {
89 filter.filter(snapshotHistory);
93 private List<SnapshotFilter> newFilters(Periods periods) {
94 List<SnapshotFilter> filters = Lists.newArrayList();
95 filters.add(new KeepLibrarySnapshotFilter());
96 filters.add(new KeepSnapshotsBetweenTwoDatesFilter(new Date(), periods.dateToStartKeepingOneSnapshotByWeek));
97 filters.add(new KeepOneSnapshotByPeriodBetweenTwoDatesFilter(GregorianCalendar.WEEK_OF_YEAR,
98 periods.dateToStartKeepingOneSnapshotByWeek,
99 periods.dateToStartKeepingOneSnapshotByMonth));
100 filters.add(new KeepOneSnapshotByPeriodBetweenTwoDatesFilter(GregorianCalendar.MONTH,
101 periods.dateToStartKeepingOneSnapshotByMonth,
102 periods.dateToStartDeletingAllSnapshots));
103 filters.add(new KeepLastSnapshotFilter());