From f2df9622b581fa34da9265f01dbbb73375818739 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 15 Mar 2013 20:23:21 +0100 Subject: [PATCH] Remove deprecated and unused API of dbcleaner plugin --- .../plugins/dbcleaner/api/PeriodCleaner.java | 32 ---- .../sonar/plugins/dbcleaner/api/Purge.java | 55 ------ .../plugins/dbcleaner/api/PurgeContext.java | 42 ---- .../plugins/dbcleaner/api/PurgeUtils.java | 181 ------------------ .../period/DefaultPeriodCleaner.java | 3 +- .../plugins/dbcleaner/api/PurgeUtilsTest.java | 87 --------- .../PurgeUtilsTest/purgeSnapshots-result.xml | 127 ------------ .../api/PurgeUtilsTest/purgeSnapshots.xml | 133 ------------- 8 files changed, 1 insertion(+), 659 deletions(-) delete mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java delete mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/Purge.java delete mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java delete mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java delete 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/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots-result.xml delete mode 100644 plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots.xml diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java deleted file mode 100644 index 44c02d9a503..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PeriodCleaner.java +++ /dev/null @@ -1,32 +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.api; - -import org.sonar.api.task.TaskExtension; - -import org.sonar.api.resources.Project; - -/** - * @deprecated in 2.14 - */ -@Deprecated -public interface PeriodCleaner extends TaskExtension { - void purge(Project project, int projectSnapshotId); -} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/Purge.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/Purge.java deleted file mode 100644 index 5a147f72028..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/Purge.java +++ /dev/null @@ -1,55 +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.api; - -import org.sonar.api.BatchExtension; -import org.sonar.api.database.DatabaseSession; - -/** - * Implement this component in order to define your own rules to cleanup database. - * - * @since 2.5 - * @deprecated in 2.14 - */ -@Deprecated -public abstract class Purge implements BatchExtension { - - private DatabaseSession session; - - protected Purge(DatabaseSession session) { - this.session = session; - } - - protected final DatabaseSession getSession() { - return session; - } - - /** - * Snapshots include the current snapshot (flagged as last) and optionally the penultimate one. - * - * @snapshots never null. - */ - public abstract void purge(PurgeContext context); - - @Override - public String toString() { - return getClass().getName(); - } -} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java deleted file mode 100644 index 544949a3d7e..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeContext.java +++ /dev/null @@ -1,42 +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.api; - -import org.sonar.api.resources.Project; - -/** - * @since 2.5 - * @deprecated in 2.14 - */ -@Deprecated -public interface PurgeContext { - - Project getProject(); - - /** - * @return the snapshot id of the current project - */ - Integer getSnapshotId(); - - /** - * Can be null - */ - Integer getPreviousSnapshotId(); -} 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 deleted file mode 100644 index e84b513e27d..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java +++ /dev/null @@ -1,181 +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.api; - -import com.google.common.annotations.VisibleForTesting; -import org.apache.commons.configuration.Configuration; -import org.sonar.api.batch.Event; -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 - * @deprecated Useless since version 2.14 because of the refactoring of the cleanup mechanism (see SONAR-2757). - */ -@Deprecated -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); - deleteDuplicationBlocks(session, snapshotIds); - deleteEvents(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 DUPLICATIONS_INDEX table - * - * @since 2.11 - */ - private static void deleteDuplicationBlocks(DatabaseSession session, List snapshotIds) { - executeNativeQuery(session, "delete duplication blocks", snapshotIds, "delete from duplications_index where snapshot_id in (:ids)"); - } - - /** - * Delete EVENTS table - */ - public static void deleteEvents(DatabaseSession session, List snapshotIds) { - executeQuery(session, "delete events", snapshotIds, "delete from " + Event.class.getSimpleName() + " e where e.snapshot.id 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)"); - } - - public static void deleteResources(DatabaseSession session, List ids) { - executeQuery(session, "", ids, "DELETE FROM " + ResourceModel.class.getSimpleName() + " WHERE id in (:ids)"); - deleteResourceIndex(session, ids); - } - - /** - * Delete RESOURCE_INDEX table - */ - public static void deleteResourceIndex(DatabaseSession session, List resourceIds) { - executeNativeQuery(session, "delete resource_index", resourceIds, "delete from resource_index where resource_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()) { - List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS)); - Query query = session.createQuery(hql); - query.setParameter("ids", paginedSids); - query.executeUpdate(); - index += MAX_IN_ELEMENTS; - session.commit(); - } - profiler.stop(); - } - - /** - * @since 2.13 - */ - @VisibleForTesting - static void executeNativeQuery(DatabaseSession session, String description, List ids, String sql) { - if (ids == null || ids.isEmpty()) { - return; - } - TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description); - int index = 0; - while (index < ids.size()) { - List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS)); - Query query = session.createNativeQuery(sql); - 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 016e11eeceb..d4339204747 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 @@ -28,11 +28,10 @@ import org.sonar.api.utils.DateUtils; import org.sonar.core.purge.PurgeDao; import org.sonar.core.purge.PurgeSnapshotQuery; import org.sonar.core.purge.PurgeableSnapshotDto; -import org.sonar.plugins.dbcleaner.api.PeriodCleaner; import java.util.List; -public class DefaultPeriodCleaner implements PeriodCleaner { +public class DefaultPeriodCleaner { private static final Logger LOG = LoggerFactory.getLogger(DefaultPeriodCleaner.class); private PurgeDao purgeDao; 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 deleted file mode 100644 index bea6ed413b6..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest.java +++ /dev/null @@ -1,87 +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.api; - -import com.google.common.collect.Lists; -import org.apache.commons.configuration.PropertiesConfiguration; -import org.junit.Test; -import org.sonar.api.database.DatabaseSession; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import javax.persistence.Query; - -import java.util.Arrays; -import java.util.List; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -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() { - setupData("purgeSnapshots"); - - PurgeUtils.deleteSnapshotsData(getSession(), Arrays.asList(3, 4)); - - checkTables("purgeSnapshots", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "dependencies", "events", "duplications_index"); - } - - @Test - public void shouldPaginate() throws Exception { - DatabaseSession session = mock(DatabaseSession.class); - Query query = mock(Query.class); - when(session.createQuery(anyString())).thenReturn(query); - when(session.createNativeQuery(anyString())).thenReturn(query); - - List ids = Lists.newArrayList(); - for (int i = 0; i < (PurgeUtils.MAX_IN_ELEMENTS + 1); i++) { - ids.add(i); - } - - // createQuery() and createNativeQuery() should be invoked as many times as commit(), because it starts new transaction - - PurgeUtils.executeQuery(session, "description", ids, "hql"); - verify(session, times(2)).createQuery(anyString()); - verify(session, times(2)).commit(); - - PurgeUtils.executeNativeQuery(session, "description", ids, "sql"); - verify(session, times(2)).createNativeQuery(anyString()); - verify(session, times(4)).commit(); - } - -} 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 deleted file mode 100644 index 4818b2d8b75..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots-result.xml +++ /dev/null @@ -1,127 +0,0 @@ - - - - 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 deleted file mode 100644 index 7c719cc0df3..00000000000 --- a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/api/PurgeUtilsTest/purgeSnapshots.xml +++ /dev/null @@ -1,133 +0,0 @@ - - - - -- 2.39.5