diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-12-20 21:32:31 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-12-20 21:32:31 +0100 |
commit | eae7223b4cdfbce089c4f30a31692ae8130df600 (patch) | |
tree | 936eb6cf351052b30e3bc735a4ea834fa38d712c /plugins/sonar-dbcleaner-plugin | |
parent | 3abd667d979c3899a4f529a6e0dbf0e0dddb8245 (diff) | |
download | sonarqube-eae7223b4cdfbce089c4f30a31692ae8130df600.tar.gz sonarqube-eae7223b4cdfbce089c4f30a31692ae8130df600.zip |
SONAR-983 resources are indexed during analysis
* Rename the column RESOURCE_INDEX.PROJECT_ID to ROOT_PROJECT_ID
* Increase the size of RESOURCE_INDEX.PROJECT_ID
* Clean resource index from dbcleaner plugin
* Experimental: indexes all the existing resources during upgrade from sonar < 2.13
Diffstat (limited to 'plugins/sonar-dbcleaner-plugin')
6 files changed, 108 insertions, 57 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 a02d8d0a308..f449b5c38e0 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 @@ -19,54 +19,45 @@ */ package org.sonar.plugins.dbcleaner; -import java.util.Arrays; -import java.util.List; - import org.sonar.api.Properties; import org.sonar.api.Property; import org.sonar.api.SonarPlugin; +import org.sonar.plugins.dbcleaner.api.DbCleanerCommands; 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.PurgeDeletedResources; -import org.sonar.plugins.dbcleaner.purges.PurgeDependencies; -import org.sonar.plugins.dbcleaner.purges.PurgeDeprecatedLast; -import org.sonar.plugins.dbcleaner.purges.PurgeDisabledResources; -import org.sonar.plugins.dbcleaner.purges.PurgeEntities; -import org.sonar.plugins.dbcleaner.purges.PurgeEventOrphans; -import org.sonar.plugins.dbcleaner.purges.PurgeOrphanResources; -import org.sonar.plugins.dbcleaner.purges.PurgeOrphanReviews; -import org.sonar.plugins.dbcleaner.purges.PurgePropertyOrphans; -import org.sonar.plugins.dbcleaner.purges.PurgeResourceRoles; -import org.sonar.plugins.dbcleaner.purges.PurgeRuleMeasures; -import org.sonar.plugins.dbcleaner.purges.PurgeUnprocessed; +import org.sonar.plugins.dbcleaner.purges.*; import org.sonar.plugins.dbcleaner.runner.PurgeRunner; +import java.util.Arrays; +import java.util.List; + @Properties({ - @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, defaultValue = DbCleanerConstants.ONE_MONTH, - name = "Number of months before starting to keep only one snapshot by week", - description = "After this number of months, if there are several snapshots during the same week, " - + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true), - @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, defaultValue = DbCleanerConstants.ONE_YEAR, - name = "Number of months before starting to keep only one snapshot by month", - description = "After this number of months, if there are several snapshots during the same month, " - + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true), - @Property(key = DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS, defaultValue = DbCleanerConstants.FIVE_YEARS, - name = "Number of months before starting to delete all remaining snapshots", - description = "After this number of months, all snapshots are fully deleted.", global = true, project = true) }) + @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, defaultValue = DbCleanerConstants.ONE_MONTH, + name = "Number of months before starting to keep only one snapshot by week", + description = "After this number of months, if there are several snapshots during the same week, " + + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true), + @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, defaultValue = DbCleanerConstants.ONE_YEAR, + name = "Number of months before starting to keep only one snapshot by month", + description = "After this number of months, if there are several snapshots during the same month, " + + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true), + @Property(key = DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS, defaultValue = DbCleanerConstants.FIVE_YEARS, + name = "Number of months before starting to delete all remaining snapshots", + description = "After this number of months, all snapshots are fully deleted.", global = true, project = true)}) public final class DbCleanerPlugin extends SonarPlugin { public List getExtensions() { return Arrays.asList( - // shared components - DefaultPeriodCleaner.class, + // shared components + DefaultPeriodCleaner.class, + DbCleanerCommands.class, - // purges - PurgeOrphanResources.class, PurgeEntities.class, PurgeRuleMeasures.class, PurgeUnprocessed.class, PurgeDeletedResources.class, - PurgeDeprecatedLast.class, PurgeDisabledResources.class, PurgeResourceRoles.class, PurgeEventOrphans.class, - PurgePropertyOrphans.class, PeriodPurge.class, PurgeDependencies.class, PurgeOrphanReviews.class, + // purges + PurgeOrphanResources.class, PurgeEntities.class, PurgeRuleMeasures.class, PurgeUnprocessed.class, PurgeDeletedResources.class, + PurgeDeprecatedLast.class, PurgeDisabledResources.class, PurgeResourceRoles.class, PurgeEventOrphans.class, + PurgePropertyOrphans.class, PeriodPurge.class, PurgeDependencies.class, PurgeOrphanReviews.class, - // post-job - PurgeRunner.class); + // post-job + PurgeRunner.class); } } diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerCommands.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerCommands.java new file mode 100644 index 00000000000..b010988a0cd --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/DbCleanerCommands.java @@ -0,0 +1,56 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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; + +import org.sonar.api.BatchExtension; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.model.ResourceModel; +import org.sonar.persistence.resource.ResourceIndexerDao; + +import java.util.List; + +/** + * @since 2.13 + */ +public class DbCleanerCommands implements BatchExtension { + + private DatabaseSession session; + private ResourceIndexerDao resourceIndexer; + + public DbCleanerCommands(DatabaseSession session, ResourceIndexerDao resourceIndexer) { + this.session = session; + this.resourceIndexer = resourceIndexer; + } + + public DbCleanerCommands deleteSnapshots(List<Integer> snapshotIds, boolean includeDependents) { + if (includeDependents) { + PurgeUtils.deleteSnapshotsData(session, snapshotIds); + } else { + PurgeUtils.deleteSnapshots(session, snapshotIds); + } + return this; + } + + public DbCleanerCommands deleteResources(List<Integer> resourceIds) { + PurgeUtils.executeQuery(session, "", resourceIds, "DELETE FROM " + ResourceModel.class.getSimpleName() + " WHERE id in (:ids)"); + resourceIndexer.delete(resourceIds); + return this; + } +} 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 267876d0928..dbf22a5004d 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 @@ -22,34 +22,28 @@ package org.sonar.plugins.dbcleaner.purges; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; - +import org.sonar.plugins.dbcleaner.api.DbCleanerCommands; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.api.PurgeUtils; - -import java.util.List; import javax.persistence.Query; +import java.util.List; /** * @since 1.11 */ public final class PurgeDisabledResources extends Purge { - public PurgeDisabledResources(DatabaseSession session) { + private DbCleanerCommands dbCleanerCommands; + + public PurgeDisabledResources(DatabaseSession session, DbCleanerCommands dbCleanerCommands) { super(session); + this.dbCleanerCommands = dbCleanerCommands; } public void purge(PurgeContext context) { - PurgeUtils.deleteSnapshotsData(getSession(), getSnapshotIds()); - deleteResources(); - } - - private void deleteResources() { - final List<Integer> resourceIds = getResourceIds(); - if ( !resourceIds.isEmpty()) { - PurgeUtils.executeQuery(getSession(), "", resourceIds, "delete from " + ResourceModel.class.getSimpleName() + " r where r.id in (:ids)"); - } + dbCleanerCommands.deleteSnapshots(getSnapshotIds(), true); + dbCleanerCommands.deleteResources(getResourceIds()); } private List<Integer> getResourceIds() { @@ -60,7 +54,7 @@ public final class PurgeDisabledResources extends Purge { private List<Integer> getSnapshotIds() { Query query = getSession().createQuery("SELECT s.id FROM " + Snapshot.class.getSimpleName() + " s WHERE " + - " EXISTS (FROM " + ResourceModel.class.getSimpleName() + " r WHERE r.id=s.resourceId AND r.enabled=:enabled)"); + " EXISTS (FROM " + ResourceModel.class.getSimpleName() + " r WHERE r.id=s.resourceId AND r.enabled=:enabled)"); query.setParameter("enabled", Boolean.FALSE); return query.getResultList(); } 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 9178b5ad85d..a38ead16fbb 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 @@ -21,29 +21,31 @@ package org.sonar.plugins.dbcleaner.purges; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; +import org.sonar.plugins.dbcleaner.api.DbCleanerCommands; import org.sonar.plugins.dbcleaner.api.Purge; import org.sonar.plugins.dbcleaner.api.PurgeContext; -import org.sonar.plugins.dbcleaner.api.PurgeUtils; - -import java.util.List; import javax.persistence.Query; +import java.util.List; /** * @since 2.1 */ public final class PurgeOrphanResources extends Purge { - public PurgeOrphanResources(DatabaseSession session) { + private DbCleanerCommands dbCleanerCommands; + + public PurgeOrphanResources(DatabaseSession session, DbCleanerCommands dbCleanerCommands) { super(session); + this.dbCleanerCommands = dbCleanerCommands; } public void purge(PurgeContext context) { Query query = getSession().createQuery("SELECT r1.id FROM " + ResourceModel.class.getSimpleName() + - " r1 WHERE r1.rootId IS NOT NULL AND NOT EXISTS(FROM " + ResourceModel.class.getSimpleName() + " r2 WHERE r1.rootId=r2.id)"); + " r1 WHERE r1.rootId IS NOT NULL AND NOT EXISTS(FROM " + ResourceModel.class.getSimpleName() + " r2 WHERE r1.rootId=r2.id)"); List<Integer> idsToDelete = query.getResultList(); if (!idsToDelete.isEmpty()) { - PurgeUtils.executeQuery(getSession(), "", idsToDelete, "DELETE FROM " + ResourceModel.class.getSimpleName() + " WHERE id in (:ids)"); + dbCleanerCommands.deleteResources(idsToDelete); } } } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResourcesTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResourcesTest.java index 8ff3d3d84c3..2e87714e2e8 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResourcesTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeDisabledResourcesTest.java @@ -21,9 +21,13 @@ package org.sonar.plugins.dbcleaner.purges; import org.junit.Test; import org.sonar.jpa.test.AbstractDbUnitTestCase; +import org.sonar.persistence.resource.ResourceIndexerDao; +import org.sonar.plugins.dbcleaner.api.DbCleanerCommands; import java.sql.SQLException; +import static org.mockito.Mockito.mock; + public class PurgeDisabledResourcesTest extends AbstractDbUnitTestCase { @Test @@ -43,7 +47,7 @@ public class PurgeDisabledResourcesTest extends AbstractDbUnitTestCase { private void assertPurge(String testName) { setupData("sharedFixture", testName); - new PurgeDisabledResources(getSession()).purge(null); + new PurgeDisabledResources(getSession(), new DbCleanerCommands(getSession(), mock(ResourceIndexerDao.class))).purge(null); checkTables(testName, "snapshots", "project_measures"); } } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResourcesTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResourcesTest.java index 87ba7fb9c8b..5f443998ce3 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResourcesTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanResourcesTest.java @@ -21,9 +21,13 @@ package org.sonar.plugins.dbcleaner.purges; import org.junit.Test; import org.sonar.jpa.test.AbstractDbUnitTestCase; +import org.sonar.persistence.resource.ResourceIndexerDao; +import org.sonar.plugins.dbcleaner.api.DbCleanerCommands; import java.sql.SQLException; +import static org.mockito.Mockito.mock; + public class PurgeOrphanResourcesTest extends AbstractDbUnitTestCase { @Test public void purgeOrphanResources() throws SQLException { @@ -32,7 +36,7 @@ public class PurgeOrphanResourcesTest extends AbstractDbUnitTestCase { private void assertPurge(String testName) { setupData(testName); - new PurgeOrphanResources(getSession()).purge(null); + new PurgeOrphanResources(getSession(), new DbCleanerCommands(getSession(), mock(ResourceIndexerDao.class))).purge(null); checkTables(testName, "projects"); } } |