From 1be37880a5ecfb86bd9fb326d42bbb5c5bfeaac6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 26 Jan 2012 17:16:49 +0100 Subject: SONAR-2807 + SONAR-3219 : improve the DBCleaner plugin * Delete all the wasted data. Some snapshots have been ignored * Keep a single snapshot per day --- .../java/org/sonar/core/persistence/MyBatis.java | 2 + .../main/java/org/sonar/core/purge/PurgeDao.java | 18 +++++ .../java/org/sonar/core/purge/PurgeMapper.java | 4 + .../org/sonar/core/purge/PurgeSnapshotQuery.java | 10 +++ .../org/sonar/core/purge/PurgeableSnapshotDto.java | 93 ++++++++++++++++++++++ .../resources/org/sonar/core/purge/PurgeMapper.xml | 38 ++++++--- 6 files changed, 155 insertions(+), 10 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java (limited to 'sonar-core/src/main') diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java index 8837db3c6d7..f74d34fed79 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java @@ -33,6 +33,7 @@ import org.sonar.core.dashboard.*; import org.sonar.core.duplication.DuplicationMapper; import org.sonar.core.duplication.DuplicationUnitDto; import org.sonar.core.purge.PurgeMapper; +import org.sonar.core.purge.PurgeableSnapshotDto; import org.sonar.core.resource.*; import org.sonar.core.review.ReviewDto; import org.sonar.core.review.ReviewMapper; @@ -66,6 +67,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "Dashboard", DashboardDto.class); loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class); loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class); + loadAlias(conf, "PurgeableSnapshot", PurgeableSnapshotDto.class); loadAlias(conf, "Review", ReviewDto.class); loadAlias(conf, "Resource", ResourceDto.class); loadAlias(conf, "ResourceIndex", ResourceIndexDto.class); diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java index 8bbe33b44b8..cd38614b5e7 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java @@ -20,6 +20,8 @@ package org.sonar.core.purge; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ResultContext; import org.apache.ibatis.session.ResultHandler; @@ -28,7 +30,9 @@ import org.sonar.core.persistence.BatchSession; import org.sonar.core.persistence.MyBatis; import org.sonar.core.resource.ResourceDao; +import java.util.Collections; import java.util.List; +import java.util.SortedSet; public class PurgeDao { private final MyBatis mybatis; @@ -84,6 +88,20 @@ public class PurgeDao { session.commit(); } + public List selectPurgeableSnapshots(long resourceId) { + SqlSession session = mybatis.openSession(ExecutorType.REUSE); + try { + PurgeMapper mapper = session.getMapper(PurgeMapper.class); + List result = Lists.newArrayList(); + result.addAll(mapper.selectPurgeableSnapshotsWithVersionEvent(resourceId)); + result.addAll(mapper.selectPurgeableSnapshotsWithoutVersionEvent(resourceId)); + Collections.sort(result);// sort by date + return result; + } finally { + MyBatis.closeQuietly(session); + } + } + public PurgeDao deleteProject(long rootProjectId) { final BatchSession session = mybatis.openBatchSession(); try { diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java index 38cccfe0e60..31993b1896f 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java @@ -70,4 +70,8 @@ public interface PurgeMapper { void deleteResourceEvents(long resourceId); void closeResourceReviews(long resourceId); + + List selectPurgeableSnapshotsWithVersionEvent(long resourceId); + + List selectPurgeableSnapshotsWithoutVersionEvent(long resourceId); } diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java index 95dadd2f03c..5c9c3eccdf0 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java @@ -31,6 +31,7 @@ public final class PurgeSnapshotQuery { private String[] status; private Boolean islast; private Boolean notPurged; + private Boolean withVersionEvent; private PurgeSnapshotQuery() { } @@ -119,4 +120,13 @@ public final class PurgeSnapshotQuery { this.resourceId = l; return this; } + + public Boolean getWithVersionEvent() { + return withVersionEvent; + } + + public PurgeSnapshotQuery setWithVersionEvent(Boolean withVersionEvent) { + this.withVersionEvent = withVersionEvent; + return this; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java new file mode 100644 index 00000000000..5579bfa7c06 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java @@ -0,0 +1,93 @@ +/* + * 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.core.purge; + +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + +import java.util.Date; + +public class PurgeableSnapshotDto implements Comparable { + private Date date; + private long snapshotId; + private boolean hasVersionEvent; + private boolean isLast; + + public Date getDate() { + return date; + } + + public long getSnapshotId() { + return snapshotId; + } + + public boolean hasVersionEvent() { + return hasVersionEvent; + } + + public boolean isLast() { + return isLast; + } + + public void setDate(Date date) { + this.date = date; + } + + public PurgeableSnapshotDto setSnapshotId(long snapshotId) { + this.snapshotId = snapshotId; + return this; + } + + public PurgeableSnapshotDto setHasVersionEvent(boolean hasVersionEvent) { + this.hasVersionEvent = hasVersionEvent; + return this; + } + + public PurgeableSnapshotDto setLast(boolean last) { + isLast = last; + return this; + } + + public int compareTo(PurgeableSnapshotDto other) { + return date.compareTo(other.date); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PurgeableSnapshotDto that = (PurgeableSnapshotDto) o; + return snapshotId == that.snapshotId; + } + + @Override + public int hashCode() { + return (int)snapshotId; + } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); + } +} diff --git a/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml b/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml index 3f942d66c6b..e0a7f656c3d 100644 --- a/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml @@ -4,41 +4,59 @@ + + + +