aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-dbcleaner-plugin
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-01-25 17:38:50 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-01-25 17:39:14 +0100
commit8c6df4977b90cfce0cc6797e3b1ba23b1253a62c (patch)
treebf1af9f518adeebd8e7ff9434a7c1c0e13835392 /plugins/sonar-dbcleaner-plugin
parent319a66df83a3aa19eea3612901626a6223f4e1d4 (diff)
downloadsonarqube-8c6df4977b90cfce0cc6797e3b1ba23b1253a62c.tar.gz
sonarqube-8c6df4977b90cfce0cc6797e3b1ba23b1253a62c.zip
SONAR-2757 Refactor the purge mechanisms to prevent any fullscan SQL requests on snapshots table
Diffstat (limited to 'plugins/sonar-dbcleaner-plugin')
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java15
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerConstants.java1
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteAbortedBuilds.java43
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteDirectoryHistory.java51
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteFileHistory.java41
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurge.java28
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeContext.java28
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeTask.java57
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/ProjectPurgePostJob.java71
-rw-r--r--plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java2
10 files changed, 56 insertions, 281 deletions
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 47a872a2fd6..0eb4d380281 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
@@ -25,10 +25,6 @@ import org.sonar.api.SonarPlugin;
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.DeleteAbortedBuilds;
-import org.sonar.plugins.dbcleaner.purges.DeleteDirectoryHistory;
-import org.sonar.plugins.dbcleaner.purges.DeleteFileHistory;
-import org.sonar.plugins.dbcleaner.purges.ProjectPurgeTask;
import org.sonar.plugins.dbcleaner.runner.DeprecatedPurgePostJob;
import org.sonar.plugins.dbcleaner.runner.ProjectPurgePostJob;
@@ -59,13 +55,10 @@ public final class DbCleanerPlugin extends SonarPlugin {
public List getExtensions() {
return Arrays.asList(
- // shared components
- DefaultPeriodCleaner.class, ProjectPurgeTask.class,
+ DefaultPeriodCleaner.class,
+ PeriodPurge.class,
- // purges
- PeriodPurge.class, DeleteFileHistory.class, DeleteDirectoryHistory.class, DeleteAbortedBuilds.class, ProjectPurgePostJob.class,
-
- // post-job
- DeprecatedPurgePostJob.class);
+ // post-jobs
+ ProjectPurgePostJob.class, DeprecatedPurgePostJob.class);
}
}
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
index f003ac5e16e..a1e4bd8aabb 100644
--- 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
@@ -23,6 +23,7 @@ public interface DbCleanerConstants {
String PLUGIN_KEY = "dbcleaner";
String PLUGIN_NAME = "DbCleaner";
+ String PROPERTY_CLEAN_DIRECTORY = "sonar.dbcleaner.cleanDirectory";
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";
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteAbortedBuilds.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteAbortedBuilds.java
deleted file mode 100644
index 464fd4b2714..00000000000
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteAbortedBuilds.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.purges;
-
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeSnapshotQuery;
-
-/**
- * @since 2.14
- */
-public class DeleteAbortedBuilds extends ProjectPurge {
- private PurgeDao purgeDao;
-
- public DeleteAbortedBuilds(PurgeDao purgeDao) {
- this.purgeDao = purgeDao;
- }
-
- @Override
- public void execute(ProjectPurgeContext context) {
- PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
- .setBeforeBuildDate(context.getBeforeBuildDate())
- .setRootProjectId(context.getRootProjectId())
- .setStatus(new String[]{"U"});
- purgeDao.deleteSnapshots(query);
- }
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteDirectoryHistory.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteDirectoryHistory.java
deleted file mode 100644
index dfaa68fd1f4..00000000000
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteDirectoryHistory.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.purges;
-
-import org.sonar.api.Properties;
-import org.sonar.api.Property;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Scopes;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeSnapshotQuery;
-
-@Properties({
- @Property(key = "sonar.dbcleaner.cleanDirectory", defaultValue = "false", name = "Clean history data of directories/packages")
-})
-public class DeleteDirectoryHistory extends ProjectPurge {
- private PurgeDao purgeDao;
- private Settings settings;
-
- public DeleteDirectoryHistory(PurgeDao purgeDao, Settings settings) {
- this.purgeDao = purgeDao;
- this.settings = settings;
- }
-
- @Override
- public void execute(ProjectPurgeContext context) {
- if (settings.getBoolean("sonar.dbcleaner.cleanDirectoryHistory")) {
- PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
- .setBeforeBuildDate(context.getBeforeBuildDate())
- .setRootProjectId(context.getRootProjectId())
- .setScopes(new String[]{Scopes.DIRECTORY});
- purgeDao.deleteSnapshots(query);
- }
- }
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteFileHistory.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteFileHistory.java
deleted file mode 100644
index b0f48d93239..00000000000
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/DeleteFileHistory.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.purges;
-
-import org.sonar.api.resources.Scopes;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeSnapshotQuery;
-
-public class DeleteFileHistory extends ProjectPurge {
- private PurgeDao purgeDao;
-
- public DeleteFileHistory(PurgeDao purgeDao) {
- this.purgeDao = purgeDao;
- }
-
- @Override
- public void execute(ProjectPurgeContext context) {
- PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
- .setBeforeBuildDate(context.getBeforeBuildDate())
- .setRootProjectId(context.getRootProjectId())
- .setScopes(new String[]{Scopes.FILE});
- purgeDao.deleteSnapshots(query);
- }
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurge.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurge.java
deleted file mode 100644
index 41ecd85098f..00000000000
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurge.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.purges;
-
-import org.sonar.api.BatchExtension;
-
-public abstract class ProjectPurge implements BatchExtension {
-
- public abstract void execute(ProjectPurgeContext context);
-
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeContext.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeContext.java
deleted file mode 100644
index e01330a0598..00000000000
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeContext.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.purges;
-
-import java.util.Date;
-
-public interface ProjectPurgeContext {
- Long getRootProjectId();
-
- Date getBeforeBuildDate();
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeTask.java
deleted file mode 100644
index de721ba1891..00000000000
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/ProjectPurgeTask.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 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.purges;
-
-import org.slf4j.LoggerFactory;
-import org.sonar.api.BatchExtension;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeSnapshotQuery;
-
-public class ProjectPurgeTask implements BatchExtension {
-
- private ProjectPurge[] purges;
- private PurgeDao purgeDao;
-
- public ProjectPurgeTask(ProjectPurge[] purges, PurgeDao purgeDao) {
- this.purges = purges;
- this.purgeDao = purgeDao;
- }
-
- public ProjectPurgeTask execute(ProjectPurgeContext context) {
- purgeProject(context);
- purgeSnapshots(context);
- return this;
- }
-
- private void purgeProject(ProjectPurgeContext purgeContext) {
- for (ProjectPurge purge : purges) {
- LoggerFactory.getLogger(getClass()).debug("Executing purge " + purge);
- purge.execute(purgeContext);
- }
- }
-
- private void purgeSnapshots(ProjectPurgeContext context) {
- LoggerFactory.getLogger(getClass()).debug("Purging snapshots");
- PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
- .setBeforeBuildDate(context.getBeforeBuildDate())
- .setRootProjectId(context.getRootProjectId());
- purgeDao.purgeSnapshots(query);
- }
-}
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/ProjectPurgePostJob.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/ProjectPurgePostJob.java
index 6dccaef73be..5a5646e4187 100644
--- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/ProjectPurgePostJob.java
+++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/runner/ProjectPurgePostJob.java
@@ -19,41 +19,70 @@
*/
package org.sonar.plugins.dbcleaner.runner;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.sonar.api.Properties;
+import org.sonar.api.Property;
import org.sonar.api.batch.PostJob;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
import org.sonar.core.NotDryRun;
-import org.sonar.plugins.dbcleaner.purges.ProjectPurgeContext;
-import org.sonar.plugins.dbcleaner.purges.ProjectPurgeTask;
-
-import java.util.Date;
+import org.sonar.core.purge.PurgeDao;
+import org.sonar.core.purge.PurgeSnapshotQuery;
+import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
+@Properties({
+ @Property(
+ key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY,
+ defaultValue = "false",
+ name = "Clean history data of directories/packages")
+})
@NotDryRun
public class ProjectPurgePostJob implements PostJob {
- private ProjectPurgeTask task;
+ private PurgeDao purgeDao;
+ private Settings settings;
- public ProjectPurgePostJob(ProjectPurgeTask task) {
- this.task = task;
+ public ProjectPurgePostJob(PurgeDao purgeDao, Settings settings) {
+ this.purgeDao = purgeDao;
+ this.settings = settings;
}
public void executeOn(final Project project, SensorContext context) {
- final Date beforeBuildDate = new Date();
+ long projectId = (long) project.getId();
+ deleteAbortedBuilds(projectId);
+ deleteFileHistory(projectId);
+ if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) {
+ deleteDirectoryHistory(projectId);
+ }
+ purgeProject(projectId);
+ }
+
+ private void purgeProject(long projectId) {
+ purgeDao.purgeProject(projectId);
+ }
- ProjectPurgeContext purgeContext = new ProjectPurgeContext() {
- public Long getRootProjectId() {
- return new Long(project.getId());
- }
+ private void deleteDirectoryHistory(long projectId) {
+ PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
+ .setRootProjectId(projectId)
+ .setIslast(false)
+ .setScopes(new String[]{Scopes.DIRECTORY});
+ purgeDao.deleteSnapshots(query);
+ }
- public Date getBeforeBuildDate() {
- return beforeBuildDate;
- }
- };
+ private void deleteFileHistory(long projectId) {
+ PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
+ .setRootProjectId(projectId)
+ .setIslast(false)
+ .setScopes(new String[]{Scopes.FILE});
+ purgeDao.deleteSnapshots(query);
+ }
- Logger logger = LoggerFactory.getLogger(getClass());
- logger.info("Optimizing project");
- task.execute(purgeContext);
+ private void deleteAbortedBuilds(long projectId) {
+ PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
+ .setRootProjectId(projectId)
+ .setIslast(false)
+ .setStatus(new String[]{"U"});
+ purgeDao.deleteSnapshots(query);
}
}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java
index 9154cce3a28..f08ebf1f690 100644
--- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java
+++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DbCleanerPluginTest.java
@@ -28,6 +28,6 @@ public class DbCleanerPluginTest {
@Test
public void shouldGetExtensions() {
- assertThat(new DbCleanerPlugin().getExtensions().size(), greaterThan(5));
+ assertThat(new DbCleanerPlugin().getExtensions().size(), greaterThan(2));
}
}