From 8d7d96bae86ba59fc270ce01b64320eeabaa043e Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 19 Nov 2010 14:51:14 +0000 Subject: move org.sonar.plugins.dbcleaner.util.PurgeUtils to org.sonar.plugins.dbcleaner.api --- .../sonar/plugins/dbcleaner/DbCleanerPlugin.java | 2 +- .../plugins/dbcleaner/api/DbCleanerConstants.java | 32 +++++ .../sonar/plugins/dbcleaner/api/PurgeUtils.java | 129 +++++++++++++++++++++ .../dbcleaner/period/DefaultPeriodCleaner.java | 2 +- .../sonar/plugins/dbcleaner/period/Periods.java | 2 +- .../dbcleaner/purges/PurgeDeletedResources.java | 2 +- .../dbcleaner/purges/PurgeDependencies.java | 2 +- .../dbcleaner/purges/PurgeDeprecatedLast.java | 2 +- .../dbcleaner/purges/PurgeDisabledResources.java | 2 +- .../plugins/dbcleaner/purges/PurgeEntities.java | 2 +- .../dbcleaner/purges/PurgeEventOrphans.java | 2 +- .../dbcleaner/purges/PurgeOrphanResources.java | 2 +- .../dbcleaner/purges/PurgePropertyOrphans.java | 2 +- .../dbcleaner/purges/PurgeResourceRoles.java | 2 +- .../dbcleaner/purges/PurgeRuleMeasures.java | 2 +- .../plugins/dbcleaner/purges/PurgeUnprocessed.java | 2 +- .../dbcleaner/purges/UnflagLastDoublons.java | 2 +- .../plugins/dbcleaner/util/DbCleanerConstants.java | 32 ----- .../sonar/plugins/dbcleaner/util/PurgeUtils.java | 129 --------------------- .../plugins/dbcleaner/api/PurgeUtilsTest.java | 55 +++++++++ .../plugins/dbcleaner/util/PurgeUtilsTest.java | 54 --------- .../api/PurgeUtilsTest/purgeSnapshots-result.xml | 111 ++++++++++++++++++ .../api/PurgeUtilsTest/purgeSnapshots.xml | 111 ++++++++++++++++++ .../util/PurgeUtilsTest/purgeSnapshots-result.xml | 111 ------------------ .../util/PurgeUtilsTest/purgeSnapshots.xml | 111 ------------------ 25 files changed, 453 insertions(+), 452 deletions(-) create mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java create mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java delete mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/DbCleanerConstants.java delete mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/PurgeUtils.java create mode 100644 plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest.java delete mode 100644 plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest.java create mode 100644 plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots-result.xml create mode 100644 plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots.xml delete mode 100644 plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots-result.xml delete mode 100644 plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots.xml (limited to 'plugins/sonar-dbcleaner-plugin/src') diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java index 41fa4916cf6..5e8a261d336 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java @@ -22,11 +22,11 @@ package org.sonar.plugins.dbcleaner; import org.sonar.api.Plugin; import org.sonar.api.Properties; import org.sonar.api.Property; +import org.sonar.plugins.dbcleaner.api.DbCleanerConstants; import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; import org.sonar.plugins.dbcleaner.period.PeriodPurge; import org.sonar.plugins.dbcleaner.purges.*; import org.sonar.plugins.dbcleaner.runner.PurgeRunner; -import org.sonar.plugins.dbcleaner.util.DbCleanerConstants; import java.util.Arrays; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java new file mode 100644 index 00000000000..d39e0ae0db1 --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java @@ -0,0 +1,32 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2010 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.dbcleaner.api; + +public interface DbCleanerConstants { + + String PLUGIN_KEY = "dbcleaner"; + String PLUGIN_NAME = "DbCleaner"; + String MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK = "sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByWeek"; + String MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH = "sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByMonth"; + String MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS = "sonar.dbcleaner.monthsBeforeDeletingAllSnapshots"; + String ONE_MONTH = "1"; + String ONE_YEAR = "12"; + String THREE_YEARS = "36"; +} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java new file mode 100644 index 00000000000..229bcce88be --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java @@ -0,0 +1,129 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.dbcleaner.api; + +import org.apache.commons.configuration.Configuration; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.model.*; +import org.sonar.api.design.DependencyDto; +import org.sonar.api.utils.TimeProfiler; + +import javax.persistence.Query; +import java.util.List; + +/** + * @since 2.5 + */ +public final class PurgeUtils { + + public static final int DEFAULT_MINIMUM_PERIOD_IN_HOURS = 12; + public static final String PROP_KEY_MINIMUM_PERIOD_IN_HOURS = "sonar.purge.minimumPeriodInHours"; + + /** + * Maximum elements in the SQL statement "IN" due to an Oracle limitation (see error ORA-01795) + */ + public static final int MAX_IN_ELEMENTS = 950; + + private PurgeUtils() { + // only static methods + } + + public static int getMinimumPeriodInHours(Configuration conf) { + int hours = DEFAULT_MINIMUM_PERIOD_IN_HOURS; + if (conf != null) { + hours = conf.getInt(PROP_KEY_MINIMUM_PERIOD_IN_HOURS, DEFAULT_MINIMUM_PERIOD_IN_HOURS); + } + return hours; + } + + public static void deleteSnapshotsData(DatabaseSession session, List snapshotIds) { + deleteMeasuresBySnapshotId(session, snapshotIds); + deleteSources(session, snapshotIds); + deleteViolations(session, snapshotIds); + deleteDependencies(session, snapshotIds); + deleteSnapshots(session, snapshotIds); + } + + public static void deleteDependencies(DatabaseSession session, List snapshotIds) { + executeQuery(session, "delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.fromSnapshotId in (:ids)"); + executeQuery(session, "delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.toSnapshotId in (:ids)"); + } + + /** + * Delete all measures, including MEASURE_DATA + */ + public static void deleteMeasuresBySnapshotId(DatabaseSession session, List snapshotIds) { + executeQuery(session, "delete measures by snapshot id", snapshotIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.snapshotId in (:ids)"); + executeQuery(session, "delete measures by snapshot id", snapshotIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.snapshotId in (:ids)"); + } + + /** + * Delete all measures, including MEASURE_DATA + */ + public static void deleteMeasuresById(DatabaseSession session, List measureIds) { + executeQuery(session, "delete measures by id", measureIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.measure.id in (:ids)"); + executeQuery(session, "delete measures by id", measureIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.id in (:ids)"); + } + + /** + * Delete SNAPSHOT_SOURCES table + */ + public static void deleteSources(DatabaseSession session, List snapshotIds) { + executeQuery(session, "delete sources", snapshotIds, "delete from " + SnapshotSource.class.getSimpleName() + " e where e.snapshotId in (:ids)"); + } + + /** + * Delete violations (RULE_FAILURES table) + */ + public static void deleteViolations(DatabaseSession session, List snapshotIds) { + executeQuery(session, "delete violations", snapshotIds, "delete from " + RuleFailureModel.class.getSimpleName() + " e where e.snapshotId in (:ids)"); + } + + /** + * Delete SNAPSHOTS table + */ + public static void deleteSnapshots(DatabaseSession session, List snapshotIds) { + executeQuery(session, "delete snapshots", snapshotIds, "delete from " + Snapshot.class.getSimpleName() + " s where s.id in (:ids)"); + } + + /** + * Paginate execution of SQL requests to avoid exceeding size of rollback segment + */ + public static void executeQuery(DatabaseSession session, String description, List ids, String hql) { + if (ids == null || ids.isEmpty()) { + return; + } + + TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description); + + int index = 0; + while (index < ids.size()) { + Query query = session.createQuery(hql); + List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS)); + query.setParameter("ids", paginedSids); + query.executeUpdate(); + index += MAX_IN_ELEMENTS; + session.commit(); + } + + profiler.stop(); + } + +} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java index cc02ae077e0..43b707bbd67 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java @@ -26,7 +26,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Project; import org.sonar.plugins.dbcleaner.api.PeriodCleaner; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.text.DateFormat; import java.util.Date; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java index 19b9ba349bf..19abce77db5 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Periods.java @@ -23,7 +23,7 @@ import org.apache.commons.configuration.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.resources.Project; -import org.sonar.plugins.dbcleaner.util.DbCleanerConstants; +import org.sonar.plugins.dbcleaner.api.DbCleanerConstants; import java.text.SimpleDateFormat; import java.util.Date; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeletedResources.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeletedResources.java index b0e3c3991f9..4e4708fa564 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeletedResources.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeletedResources.java @@ -24,7 +24,7 @@ import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDependencies.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDependencies.java index b3532177878..07f9e81efe8 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDependencies.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDependencies.java @@ -24,7 +24,7 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.design.DependencyDto; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import javax.persistence.Query; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeprecatedLast.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeprecatedLast.java index a08ba28718c..fb4d3782f03 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeprecatedLast.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDeprecatedLast.java @@ -23,7 +23,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResources.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResources.java index 27376509543..5ce0448833e 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResources.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResources.java @@ -25,7 +25,7 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEntities.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEntities.java index 98415599098..cff889cc37a 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEntities.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEntities.java @@ -27,7 +27,7 @@ import org.sonar.api.resources.Resource; import org.sonar.api.utils.Logs; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import javax.persistence.Query; import java.util.Date; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEventOrphans.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEventOrphans.java index 28dbc281293..16b01253ae9 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEventOrphans.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeEventOrphans.java @@ -24,7 +24,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResources.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResources.java index 24bfd0f4467..680faa6633b 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResources.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResources.java @@ -23,7 +23,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgePropertyOrphans.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgePropertyOrphans.java index 251e94a2b6b..485a036bedd 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgePropertyOrphans.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgePropertyOrphans.java @@ -25,7 +25,7 @@ import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.User; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeResourceRoles.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeResourceRoles.java index 87b3aa123a3..80f16aadb9b 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeResourceRoles.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeResourceRoles.java @@ -25,7 +25,7 @@ import org.sonar.api.security.GroupRole; import org.sonar.api.security.UserRole; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java index a67a0903f07..d61ad812499 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java @@ -24,7 +24,7 @@ import org.sonar.api.database.model.MeasureModel; import org.sonar.api.database.model.Snapshot; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeUnprocessed.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeUnprocessed.java index 4911b3fe45d..579cadf275d 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeUnprocessed.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeUnprocessed.java @@ -26,7 +26,7 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.utils.Logs; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import javax.persistence.Query; import java.util.Date; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/UnflagLastDoublons.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/UnflagLastDoublons.java index 0a1f030e466..bb309655a58 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/UnflagLastDoublons.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/UnflagLastDoublons.java @@ -23,7 +23,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.util.PurgeUtils; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; import java.util.List; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/DbCleanerConstants.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/DbCleanerConstants.java deleted file mode 100644 index 03a29472fb3..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/DbCleanerConstants.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2010 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.dbcleaner.util; - -public interface DbCleanerConstants { - - String PLUGIN_KEY = "dbcleaner"; - String PLUGIN_NAME = "DbCleaner"; - String MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK = "sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByWeek"; - String MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH = "sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByMonth"; - String MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS = "sonar.dbcleaner.monthsBeforeDeletingAllSnapshots"; - String ONE_MONTH = "1"; - String ONE_YEAR = "12"; - String THREE_YEARS = "36"; -} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/PurgeUtils.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/PurgeUtils.java deleted file mode 100644 index 1fc28b31b57..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/util/PurgeUtils.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.dbcleaner.util; - -import org.apache.commons.configuration.Configuration; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.model.*; -import org.sonar.api.design.DependencyDto; -import org.sonar.api.utils.TimeProfiler; - -import javax.persistence.Query; -import java.util.List; - -/** - * @since 2.5 - */ -public final class PurgeUtils { - - public static final int DEFAULT_MINIMUM_PERIOD_IN_HOURS = 12; - public static final String PROP_KEY_MINIMUM_PERIOD_IN_HOURS = "sonar.purge.minimumPeriodInHours"; - - /** - * Maximum elements in the SQL statement "IN" due to an Oracle limitation (see error ORA-01795) - */ - public static final int MAX_IN_ELEMENTS = 950; - - private PurgeUtils() { - // only static methods - } - - public static int getMinimumPeriodInHours(Configuration conf) { - int hours = DEFAULT_MINIMUM_PERIOD_IN_HOURS; - if (conf != null) { - hours = conf.getInt(PROP_KEY_MINIMUM_PERIOD_IN_HOURS, DEFAULT_MINIMUM_PERIOD_IN_HOURS); - } - return hours; - } - - public static void deleteSnapshotsData(DatabaseSession session, List snapshotIds) { - deleteMeasuresBySnapshotId(session, snapshotIds); - deleteSources(session, snapshotIds); - deleteViolations(session, snapshotIds); - deleteDependencies(session, snapshotIds); - deleteSnapshots(session, snapshotIds); - } - - public static void deleteDependencies(DatabaseSession session, List snapshotIds) { - executeQuery(session, "delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.fromSnapshotId in (:ids)"); - executeQuery(session, "delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.toSnapshotId in (:ids)"); - } - - /** - * Delete all measures, including MEASURE_DATA - */ - public static void deleteMeasuresBySnapshotId(DatabaseSession session, List snapshotIds) { - executeQuery(session, "delete measures by snapshot id", snapshotIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.snapshotId in (:ids)"); - executeQuery(session, "delete measures by snapshot id", snapshotIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.snapshotId in (:ids)"); - } - - /** - * Delete all measures, including MEASURE_DATA - */ - public static void deleteMeasuresById(DatabaseSession session, List measureIds) { - executeQuery(session, "delete measures by id", measureIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.measure.id in (:ids)"); - executeQuery(session, "delete measures by id", measureIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.id in (:ids)"); - } - - /** - * Delete SNAPSHOT_SOURCES table - */ - public static void deleteSources(DatabaseSession session, List snapshotIds) { - executeQuery(session, "delete sources", snapshotIds, "delete from " + SnapshotSource.class.getSimpleName() + " e where e.snapshotId in (:ids)"); - } - - /** - * Delete violations (RULE_FAILURES table) - */ - public static void deleteViolations(DatabaseSession session, List snapshotIds) { - executeQuery(session, "delete violations", snapshotIds, "delete from " + RuleFailureModel.class.getSimpleName() + " e where e.snapshotId in (:ids)"); - } - - /** - * Delete SNAPSHOTS table - */ - public static void deleteSnapshots(DatabaseSession session, List snapshotIds) { - executeQuery(session, "delete snapshots", snapshotIds, "delete from " + Snapshot.class.getSimpleName() + " s where s.id in (:ids)"); - } - - /** - * Paginate execution of SQL requests to avoid exceeding size of rollback segment - */ - public static void executeQuery(DatabaseSession session, String description, List ids, String hql) { - if (ids == null || ids.isEmpty()) { - return; - } - - TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description); - - int index = 0; - while (index < ids.size()) { - Query query = session.createQuery(hql); - List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS)); - query.setParameter("ids", paginedSids); - query.executeUpdate(); - index += MAX_IN_ELEMENTS; - session.commit(); - } - - profiler.stop(); - } - -} diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest.java new file mode 100644 index 00000000000..08bb8604fc2 --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest.java @@ -0,0 +1,55 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.dbcleaner.api; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.junit.Test; +import org.sonar.jpa.test.AbstractDbUnitTestCase; +import org.sonar.plugins.dbcleaner.api.PurgeUtils; + +import java.sql.SQLException; +import java.util.Arrays; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class PurgeUtilsTest extends AbstractDbUnitTestCase { + + @Test + public void shouldReturnDefaultMinimumPeriod() { + assertThat(PurgeUtils.getMinimumPeriodInHours(new PropertiesConfiguration()), is(PurgeUtils.DEFAULT_MINIMUM_PERIOD_IN_HOURS)); + } + + @Test + public void shouldReturnMinimumPeriod() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(PurgeUtils.PROP_KEY_MINIMUM_PERIOD_IN_HOURS, "9"); + assertThat(PurgeUtils.getMinimumPeriodInHours(conf), is(9)); + } + + @Test + public void purgeSnapshots() throws SQLException { + setupData("purgeSnapshots"); + + PurgeUtils.deleteSnapshotsData(getSession(), Arrays.asList(3, 4)); + + checkTables("purgeSnapshots", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "dependencies"); + } +} diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest.java deleted file mode 100644 index 31de15a3824..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.plugins.dbcleaner.util; - -import org.apache.commons.configuration.PropertiesConfiguration; -import org.junit.Test; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import java.sql.SQLException; -import java.util.Arrays; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -public class PurgeUtilsTest extends AbstractDbUnitTestCase { - - @Test - public void shouldReturnDefaultMinimumPeriod() { - assertThat(PurgeUtils.getMinimumPeriodInHours(new PropertiesConfiguration()), is(PurgeUtils.DEFAULT_MINIMUM_PERIOD_IN_HOURS)); - } - - @Test - public void shouldReturnMinimumPeriod() { - PropertiesConfiguration conf = new PropertiesConfiguration(); - conf.setProperty(PurgeUtils.PROP_KEY_MINIMUM_PERIOD_IN_HOURS, "9"); - assertThat(PurgeUtils.getMinimumPeriodInHours(conf), is(9)); - } - - @Test - public void purgeSnapshots() throws SQLException { - setupData("purgeSnapshots"); - - PurgeUtils.deleteSnapshotsData(getSession(), Arrays.asList(3, 4)); - - checkTables("purgeSnapshots", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "dependencies"); - } -} diff --git a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots-result.xml b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots-result.xml new file mode 100644 index 00000000000..f5039b0e8ec --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots-result.xml @@ -0,0 +1,111 @@ + + + + + \ No newline at end of file diff --git a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots.xml b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots.xml new file mode 100644 index 00000000000..93ef910270a --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots.xml @@ -0,0 +1,111 @@ + + + + + \ No newline at end of file diff --git a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots-result.xml b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots-result.xml deleted file mode 100644 index f5039b0e8ec..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots-result.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - \ No newline at end of file diff --git a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots.xml b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots.xml deleted file mode 100644 index 93ef910270a..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/util/PurgeUtilsTest/purgeSnapshots.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - \ No newline at end of file -- cgit v1.2.3