From 4bfd79e52cea465f226c78abb253c2073f4c1d4c Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Thu, 18 Nov 2010 15:02:54 +0000 Subject: SONAR-1663 include the dbcleaner plugin + move all purges in this plugin --- .../java/org/sonar/core/purge/AbstractPurge.java | 137 --------------------- .../org/sonar/core/purge/DefaultPurgeContext.java | 86 ------------- .../org/sonar/core/purge/AbstractPurgeTest.java | 51 -------- .../AbstractPurgeTest/purgeSnapshots-result.xml | 111 ----------------- .../purge/AbstractPurgeTest/purgeSnapshots.xml | 111 ----------------- 5 files changed, 496 deletions(-) delete mode 100644 sonar-core/src/main/java/org/sonar/core/purge/AbstractPurge.java delete mode 100644 sonar-core/src/main/java/org/sonar/core/purge/DefaultPurgeContext.java delete mode 100644 sonar-core/src/test/java/org/sonar/core/purge/AbstractPurgeTest.java delete mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots-result.xml delete mode 100644 sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots.xml (limited to 'sonar-core') diff --git a/sonar-core/src/main/java/org/sonar/core/purge/AbstractPurge.java b/sonar-core/src/main/java/org/sonar/core/purge/AbstractPurge.java deleted file mode 100644 index a0f46d286d1..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/purge/AbstractPurge.java +++ /dev/null @@ -1,137 +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.core.purge; - -import org.sonar.api.batch.Purge; -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; - -public abstract class AbstractPurge implements Purge { - - private static final int MAX_IN_ELEMENTS = 950; - - private int sqlInPageSize = MAX_IN_ELEMENTS; - private DatabaseSession session; - private TimeProfiler profiler = new TimeProfiler().setLevelToDebug(); - - public AbstractPurge(DatabaseSession session) { - this.session = session; - } - - protected DatabaseSession getSession() { - return session; - } - - /** - * Delete SNAPSHOTS and all dependent tables (MEASURES, ...) - */ - protected void deleteSnapshotData(List snapshotIds) { - deleteMeasuresBySnapshotId(snapshotIds); - deleteSources(snapshotIds); - deleteViolations(snapshotIds); - deleteDependencies(snapshotIds); - deleteSnapshots(snapshotIds); - } - - protected void deleteDependencies(List snapshotIds) { - executeQuery("delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.fromSnapshotId in (:ids)"); - executeQuery("delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.toSnapshotId in (:ids)"); - } - - /** - * Delete all measures, including MEASURE_DATA - */ - protected void deleteMeasuresBySnapshotId(List snapshotIds) { - executeQuery("delete measures by snapshot id", snapshotIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.snapshotId in (:ids)"); - executeQuery("delete measures by snapshot id", snapshotIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.snapshotId in (:ids)"); - } - - /** - * Delete all measures, including MEASURE_DATA - */ - protected void deleteMeasuresById(List measureIds) { - executeQuery("delete measures by id", measureIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.measure.id in (:ids)"); - executeQuery("delete measures by id", measureIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.id in (:ids)"); - } - - /** - * Delete SNAPSHOT_SOURCES table - */ - protected void deleteSources(List snapshotIds) { - executeQuery("delete sources", snapshotIds, "delete from " + SnapshotSource.class.getSimpleName() + " e where e.snapshotId in (:ids)"); - } - - /** - * Delete violations (RULE_FAILURES table) - */ - protected void deleteViolations(List snapshotIds) { - executeQuery("delete violations", snapshotIds, "delete from " + RuleFailureModel.class.getSimpleName() + " e where e.snapshotId in (:ids)"); - } - - /** - * Delete SNAPSHOTS table - */ - protected void deleteSnapshots(List snapshotIds) { - executeQuery("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 - */ - private void executeQuery(String name, List ids, String hql) { - if (ids == null || ids.isEmpty()) { - return; - } - - TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + name); - - int page = 1; - int index = 0; - while (index < ids.size()) { - TimeProfiler pageProfiler = new TimeProfiler().setLevelToDebug().start("Execute " + name + " " + page + " page"); - Query query = session.createQuery(hql); - List paginedSids = ids.subList(index, Math.min(ids.size(), index + sqlInPageSize)); - query.setParameter("ids", paginedSids); - query.executeUpdate(); - index += sqlInPageSize; - page++; - session.commit(); - pageProfiler.stop(); - } - - profiler.stop(); - } - - protected void executeQuery(List ids, String hql) { - executeQuery("delete for " + getClass().getSimpleName(), ids, hql); - } - - protected List selectIds(Query query) { - profiler.start("Select IDs for " + getClass().getSimpleName()); - List result = query.getResultList(); - profiler.stop(); - return result; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/purge/DefaultPurgeContext.java b/sonar-core/src/main/java/org/sonar/core/purge/DefaultPurgeContext.java deleted file mode 100644 index f2106a6bd58..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/purge/DefaultPurgeContext.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.sonar.core.purge; - -import org.apache.commons.lang.builder.ToStringBuilder; -import org.sonar.api.batch.PurgeContext; -import org.sonar.api.database.model.Snapshot; - -public class DefaultPurgeContext implements PurgeContext { - - private Integer currentSid; - private Integer lastSid; - - public DefaultPurgeContext() { - } - - public DefaultPurgeContext(Snapshot currentSnapshot) { - this(currentSnapshot, null); - } - - public DefaultPurgeContext(Snapshot currentSnapshot, Snapshot lastSnapshot) { - if (currentSnapshot != null) { - currentSid = currentSnapshot.getId(); - } - if (lastSnapshot != null) { - lastSid = lastSnapshot.getId(); - } - } - - public DefaultPurgeContext(Integer currentSid, Integer lastSid) { - this.currentSid = currentSid; - this.lastSid = lastSid; - } - - public DefaultPurgeContext setLastSnapshotId(Integer lastSid) { - this.lastSid = lastSid; - return this; - } - - public DefaultPurgeContext setCurrentSnapshotId(Integer currentSid) { - this.currentSid = currentSid; - return this; - } - - public Integer getPreviousSnapshotId() { - return lastSid; - } - - public Integer getLastSnapshotId() { - return currentSid; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - DefaultPurgeContext context = (DefaultPurgeContext) o; - - if (!currentSid.equals(context.currentSid)) { - return false; - } - if (lastSid != null ? !lastSid.equals(context.lastSid) : context.lastSid != null) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - int result = lastSid != null ? lastSid.hashCode() : 0; - result = 31 * result + currentSid.hashCode(); - return result; - } - - @Override - public String toString() { - return new ToStringBuilder(this) - .append("currentSid", currentSid) - .append("lastSid", lastSid) - .toString(); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/purge/AbstractPurgeTest.java b/sonar-core/src/test/java/org/sonar/core/purge/AbstractPurgeTest.java deleted file mode 100644 index c1b8d49d975..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/purge/AbstractPurgeTest.java +++ /dev/null @@ -1,51 +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.core.purge; - -import org.junit.Test; -import org.sonar.api.batch.PurgeContext; -import org.sonar.api.database.DatabaseSession; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import java.sql.SQLException; -import java.util.Arrays; - -public class AbstractPurgeTest extends AbstractDbUnitTestCase { - - @Test - public void purgeSnapshots() throws SQLException { - setupData("purgeSnapshots"); - - final FakePurge purge = new FakePurge(getSession()); - purge.purge(null); - - checkTables("purgeSnapshots", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources", "dependencies"); - } -} - -class FakePurge extends AbstractPurge { - public FakePurge(DatabaseSession session) { - super(session); - } - - public void purge(PurgeContext context) { - deleteSnapshotData(Arrays.asList(3, 4)); - } -} \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots-result.xml b/sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots-result.xml deleted file mode 100644 index f5039b0e8ec..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots-result.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots.xml b/sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots.xml deleted file mode 100644 index 93ef910270a..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/purge/AbstractPurgeTest/purgeSnapshots.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - \ No newline at end of file -- cgit v1.2.3