diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-26 17:16:49 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-26 17:18:06 +0100 |
commit | 1be37880a5ecfb86bd9fb326d42bbb5c5bfeaac6 (patch) | |
tree | 6c61db291fcea530b2c51752d8bd4b5259858a8f /sonar-core/src/main | |
parent | 0a59093da6b70ea04ce1a790ca4adb1f3d7c7e63 (diff) | |
download | sonarqube-1be37880a5ecfb86bd9fb326d42bbb5c5bfeaac6.tar.gz sonarqube-1be37880a5ecfb86bd9fb326d42bbb5c5bfeaac6.zip |
SONAR-2807 + SONAR-3219 : improve the DBCleaner plugin
* Delete all the wasted data. Some snapshots have been ignored
* Keep a single snapshot per day
Diffstat (limited to 'sonar-core/src/main')
6 files changed, 155 insertions, 10 deletions
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<PurgeableSnapshotDto> selectPurgeableSnapshots(long resourceId) { + SqlSession session = mybatis.openSession(ExecutorType.REUSE); + try { + PurgeMapper mapper = session.getMapper(PurgeMapper.class); + List<PurgeableSnapshotDto> 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<PurgeableSnapshotDto> selectPurgeableSnapshotsWithVersionEvent(long resourceId); + + List<PurgeableSnapshotDto> 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<PurgeableSnapshotDto> { + 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 @@ <mapper namespace="org.sonar.core.purge.PurgeMapper"> <select id="selectSnapshotIds" parameterType="map" resultType="long"> - select id from snapshots + select s.id from snapshots s <where> <if test="islast != null"> - and islast=#{islast} + and s.islast=#{islast} </if> <if test="notPurged != null and notPurged"> - and (purge_status is null or purge_status=0) + and (s.purge_status is null or s.purge_status=0) </if> <if test="rootSnapshotId != null"> - and root_snapshot_id=#{rootSnapshotId} + and (s.root_snapshot_id=#{rootSnapshotId} or s.id=#{rootSnapshotId}) </if> <if test="rootProjectId != null"> - and root_project_id=#{rootProjectId} + and s.root_project_id=#{rootProjectId} </if> <if test="resourceId != null"> - and project_id=#{resourceId} + and s.project_id=#{resourceId} </if> <if test="beforeBuildDate != null"> - and build_date <= #{beforeBuildDate} + and s.build_date <= #{beforeBuildDate} </if> <if test="status != null"> - and status in + and s.status in <foreach item="s" index="index" collection="status" open="(" separator="," close=")">#{s}</foreach> </if> <if test="scopes != null"> - and scope in + and s.scope in <foreach item="scope" index="index" collection="scopes" open="(" separator="," close=")">#{scope}</foreach> </if> <if test="qualifiers != null"> - and qualifier in + and s.qualifier in <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach> </if> + <if test="withVersionEvent != null"> + <if test="withVersionEvent"> + and exists(select e.id from events e where e.snapshot_id=s.id and e.category='Version') + </if> + <if test="!withVersionEvent"> + and not exists(select e.id from events e where e.snapshot_id=s.id and e.category='Version') + </if> + </if> </where> </select> + <select id="selectPurgeableSnapshotsWithVersionEvent" parameterType="long" resultType="PurgeableSnapshot"> + select s.id as "snapshotId", s.created_at as "date", ${_true} as "hasVersionEvent", islast as "isLast" from snapshots s + where s.project_id=#{id} and s.status='P' and s.qualifier <> 'LIB' and exists(select e.id from events e where e.snapshot_id=s.id and e.category='Version') + </select> + + <select id="selectPurgeableSnapshotsWithoutVersionEvent" parameterType="long" resultType="PurgeableSnapshot"> + select s.id as "snapshotId", s.created_at as "date", ${_false} as "hasVersionEvent", islast as "isLast" from snapshots s + where s.project_id=#{id} and s.status='P' and s.qualifier <> 'LIB' and not exists(select e.id from events e where e.snapshot_id=s.id and e.category='Version') + </select> + <select id="selectResourceIdsToDisable" resultType="long" parameterType="long"> select p.id from projects p where (p.id=#{id} or p.root_id=#{id}) and p.enabled=${_true} |