Skip to content

Commit

Permalink
remove ids from PurgeSnapshotQuery
Browse files Browse the repository at this point in the history
in addition, PurgeDao#selectPurgeableSnapshots renamed to selectPurgeableAnalyses and it returns only root snapshots (ie. analyses)
  • Loading branch information
sns-seb committed Jul 4, 2016
1 parent b2e32e0 commit 5da7d29
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 142 deletions.
4 changes: 2 additions & 2 deletions sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java
Expand Up @@ -50,8 +50,8 @@ class PurgeCommands {
this(session, session.getMapper(PurgeMapper.class), profiler);
}

List<Long> selectSnapshotIds(PurgeSnapshotQuery query) {
return purgeMapper.selectSnapshotIdsAndUuids(query).stream().map(IdUuidPair::getId).collect(Collectors.toList());
List<String> selectSnapshotUuids(PurgeSnapshotQuery query) {
return purgeMapper.selectSnapshotIdsAndUuids(query).stream().map(IdUuidPair::getUuid).collect(Collectors.toList());
}

void deleteComponents(List<IdUuidPair> componentIdUuids) {
Expand Down
31 changes: 11 additions & 20 deletions sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java
Expand Up @@ -108,25 +108,25 @@ private static void deleteAbortedBuilds(ResourceDto project, PurgeCommands comma
}

private static void purge(ResourceDto project, String[] scopesWithoutHistoricalData, PurgeCommands purgeCommands) {
List<Long> projectSnapshotIds = purgeCommands.selectSnapshotIds(
PurgeSnapshotQuery.create()
.setComponentUuid(project.getUuid())
.setIslast(false)
.setNotPurged(true));
for (Long projectSnapshotId : projectSnapshotIds) {
LOG.debug("<- Clean snapshot " + projectSnapshotId);
List<String> projectSnapshotIds = purgeCommands.selectSnapshotUuids(
PurgeSnapshotQuery.create()
.setComponentUuid(project.getUuid())
.setIslast(false)
.setNotPurged(true));
for (String analysisUuid : projectSnapshotIds) {
LOG.debug("<- Clean analysis " + analysisUuid);
if (!ArrayUtils.isEmpty(scopesWithoutHistoricalData)) {
PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
.setIslast(false)
.setScopes(scopesWithoutHistoricalData)
.setRootSnapshotId(projectSnapshotId);
.setAnalysisUuid(analysisUuid);
purgeCommands.deleteSnapshots(query);
}

// must be executed at the end for reentrance
purgeCommands.purgeSnapshots(
PurgeSnapshotQuery.create().setRootSnapshotId(projectSnapshotId).setNotPurged(true),
PurgeSnapshotQuery.create().setId(projectSnapshotId).setNotPurged(true));
PurgeSnapshotQuery.create().setAnalysisUuid(analysisUuid).setNotPurged(true),
PurgeSnapshotQuery.create().setSnapshotUuid(analysisUuid).setNotPurged(true));
}
}

Expand All @@ -148,16 +148,7 @@ private void disableOrphanResources(final ResourceDto project, final SqlSession
session.commit();
}

public List<PurgeableAnalysisDto> selectPurgeableSnapshots(String componentUuid) {
DbSession session = mybatis.openSession(true);
try {
return selectPurgeableSnapshots(componentUuid, session);
} finally {
MyBatis.closeQuietly(session);
}
}

public List<PurgeableAnalysisDto> selectPurgeableSnapshots(String componentUuid, DbSession session) {
public List<PurgeableAnalysisDto> selectPurgeableAnalyses(String componentUuid, DbSession session) {
List<PurgeableAnalysisDto> result = Lists.newArrayList();
result.addAll(mapper(session).selectPurgeableAnalysesWithEvents(componentUuid));
result.addAll(mapper(session).selectPurgeableAnalysesWithoutEvents(componentUuid));
Expand Down
30 changes: 15 additions & 15 deletions sonar-db/src/main/java/org/sonar/db/purge/PurgeSnapshotQuery.java
Expand Up @@ -20,9 +20,9 @@
package org.sonar.db.purge;

public final class PurgeSnapshotQuery {
private Long id;
private String snapshotUuid;
private String analysisUuid;
private String rootComponentUuid;
private Long rootSnapshotId;
private String componentUuid;
private String[] scopes;
private String[] status;
Expand All @@ -37,12 +37,21 @@ public static PurgeSnapshotQuery create() {
return new PurgeSnapshotQuery();
}

public Long getId() {
return id;
public String getSnapshotUuid() {
return snapshotUuid;
}

public PurgeSnapshotQuery setId(Long l) {
this.id = l;
public PurgeSnapshotQuery setSnapshotUuid(String snapshotUuid) {
this.snapshotUuid = snapshotUuid;
return this;
}

public String getAnalysisUuid() {
return analysisUuid;
}

public PurgeSnapshotQuery setAnalysisUuid(String analysisUuid) {
this.analysisUuid = analysisUuid;
return this;
}

Expand Down Expand Up @@ -91,15 +100,6 @@ public PurgeSnapshotQuery setNotPurged(Boolean notPurged) {
return this;
}

public Long getRootSnapshotId() {
return rootSnapshotId;
}

public PurgeSnapshotQuery setRootSnapshotId(Long rootSnapshotId) {
this.rootSnapshotId = rootSnapshotId;
return this;
}

public String getComponentUuid() {
return componentUuid;
}
Expand Down
Expand Up @@ -23,10 +23,11 @@
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
* Represents an analysis, aka. root snapshot, aka. snapshot of a project, developer or view
*/
public class PurgeableAnalysisDto implements Comparable<PurgeableAnalysisDto> {
private Date date;
// FIXME this property will be useless once we refactor table SNAPSHOTS into ANALYSES
private long analysisId;
private String analysisUuid;
private boolean hasEvents;
private boolean isLast;
Expand All @@ -40,15 +41,6 @@ public PurgeableAnalysisDto setDate(Long aLong) {
return this;
}

public long getAnalysisId() {
return analysisId;
}

public PurgeableAnalysisDto setAnalysisId(long analysisId) {
this.analysisId = analysisId;
return this;
}

public String getAnalysisUuid() {
return analysisUuid;
}
Expand Down Expand Up @@ -90,12 +82,12 @@ public boolean equals(Object o) {
return false;
}
PurgeableAnalysisDto that = (PurgeableAnalysisDto) o;
return analysisId == that.analysisId;
return analysisUuid.equals(that.analysisUuid);
}

@Override
public int hashCode() {
return (int) analysisId;
return analysisUuid.hashCode();
}

@Override
Expand Down
Expand Up @@ -48,7 +48,7 @@ public void clean(DbSession session, String componentUuid, Settings settings) {

@VisibleForTesting
void doClean(String componentUuid, List<Filter> filters, DbSession session) {
List<PurgeableAnalysisDto> history = selectProjectSnapshots(componentUuid, session);
List<PurgeableAnalysisDto> history = selectAnalysesOfComponent(componentUuid, session);
for (Filter filter : filters) {
filter.log();
delete(filter.filter(history), session);
Expand All @@ -60,12 +60,12 @@ private void delete(List<PurgeableAnalysisDto> snapshots, DbSession session) {
LOG.debug("<- Delete snapshot: {} [{}]", DateUtils.formatDateTime(snapshot.getDate()), snapshot.getAnalysisUuid());
purgeDao.deleteSnapshots(
session, profiler,
PurgeSnapshotQuery.create().setRootSnapshotId(snapshot.getAnalysisId()),
PurgeSnapshotQuery.create().setId(snapshot.getAnalysisId()));
PurgeSnapshotQuery.create().setAnalysisUuid(snapshot.getAnalysisUuid()),
PurgeSnapshotQuery.create().setSnapshotUuid(snapshot.getAnalysisUuid()));
}
}

private List<PurgeableAnalysisDto> selectProjectSnapshots(String componentUuid, DbSession session) {
return purgeDao.selectPurgeableSnapshots(componentUuid, session);
private List<PurgeableAnalysisDto> selectAnalysesOfComponent(String componentUuid, DbSession session) {
return purgeDao.selectPurgeableAnalyses(componentUuid, session);
}
}
45 changes: 30 additions & 15 deletions sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
Expand Up @@ -4,20 +4,23 @@
<mapper namespace="org.sonar.db.purge.PurgeMapper">

<select id="selectSnapshotIdsAndUuids" parameterType="map" resultType="IdUuidPair">
select s.id as id, s.uuid as uuid from snapshots s
select
s.id as id, s.uuid as uuid
from
snapshots s
<if test="analysisUuid != null">
join snapshots analysis on analysis.uuid=#{analysisUuid} and s.root_snapshot_id=analysis.id
</if>
<where>
<if test="snapshotUuid != null">
and s.uuid=#{snapshotUuid}
</if>
<if test="islast != null">
and s.islast=#{islast}
</if>
<if test="notPurged != null and notPurged">
and (s.purge_status is null or s.purge_status=0)
</if>
<if test="rootSnapshotId != null">
and s.root_snapshot_id=#{rootSnapshotId}
</if>
<if test="id != null">
and s.id=#{id}
</if>
<if test="rootComponentUuid != null">
and s.root_component_uuid=#{rootComponentUuid}
</if>
Expand Down Expand Up @@ -54,17 +57,29 @@
</select>

<select id="selectPurgeableAnalysesWithEvents" parameterType="String" resultType="PurgeableAnalysis">
select s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast" from
snapshots s where
s.component_uuid=#{componentUuid} and s.status='P' and s.qualifier &lt;&gt; 'LIB' and
exists(select e.id from events e where e.analysis_uuid=s.uuid)
select
s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast"
from
snapshots s
where
s.component_uuid=#{componentUuid}
and s.status='P'
and s.qualifier &lt;&gt; 'LIB'
and exists(select e.id from events e where e.analysis_uuid=s.uuid)
and parent_snapshot_id is null
</select>

<select id="selectPurgeableAnalysesWithoutEvents" parameterType="String" resultType="PurgeableAnalysis">
select s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_false} as "hasEvents", islast as "isLast" from
snapshots s where
s.component_uuid=#{componentUuid} and s.status='P' and s.qualifier &lt;&gt; 'LIB' and
not exists(select e.id from events e where e.analysis_uuid=s.uuid)
select
s.uuid as "analysisUuid", s.created_at as "date", ${_false} as "hasEvents", islast as "isLast"
from
snapshots s
where
s.component_uuid=#{componentUuid}
and s.status='P'
and s.qualifier &lt;&gt; 'LIB'
and not exists(select e.id from events e where e.analysis_uuid=s.uuid)
and parent_snapshot_id is null
</select>

<select id="selectComponentIdUuidsToDisable" resultType="org.sonar.db.purge.IdUuidPair" parameterType="String">
Expand Down
Expand Up @@ -26,16 +26,16 @@ public final class DbCleanerTestUtils {
private DbCleanerTestUtils() {
}

public static PurgeableAnalysisDto createSnapshotWithDate(long snapshotId, String date) {
public static PurgeableAnalysisDto createAnalysisWithDate(String analysisUuid, String date) {
PurgeableAnalysisDto snapshot = new PurgeableAnalysisDto();
snapshot.setAnalysisId(snapshotId);
snapshot.setAnalysisUuid(analysisUuid);
snapshot.setDate(DateUtils.parseDate(date).getTime());
return snapshot;
}

public static PurgeableAnalysisDto createSnapshotWithDateTime(long snapshotId, String datetime) {
public static PurgeableAnalysisDto createAnalysisWithDateTime(String analysisUuid, String datetime) {
PurgeableAnalysisDto snapshot = new PurgeableAnalysisDto();
snapshot.setAnalysisId(snapshotId);
snapshot.setAnalysisUuid(analysisUuid);
snapshot.setDate(DateUtils.parseDateTime(datetime).getTime());
return snapshot;
}
Expand Down
Expand Up @@ -43,7 +43,7 @@ public class PurgeCommandsTest {
public void shouldDeleteSnapshot() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteSnapshot.xml");

new PurgeCommands(dbTester.getSession(), profiler).deleteSnapshots(PurgeSnapshotQuery.create().setId(5L));
new PurgeCommands(dbTester.getSession(), profiler).deleteSnapshots(PurgeSnapshotQuery.create().setSnapshotUuid("u5"));

dbTester.assertDbUnit(getClass(), "shouldDeleteSnapshot-result.xml", "snapshots", "project_measures", "duplications_index", "events");
}
Expand All @@ -64,7 +64,7 @@ public void should_not_fail_when_deleting_huge_number_of_snapshots() {
public void shouldPurgeSnapshot() {
dbTester.prepareDbUnit(getClass(), "shouldPurgeSnapshot.xml");

new PurgeCommands(dbTester.getSession(), profiler).purgeSnapshots(PurgeSnapshotQuery.create().setId(1L));
new PurgeCommands(dbTester.getSession(), profiler).purgeSnapshots(PurgeSnapshotQuery.create().setSnapshotUuid("u1"));

dbTester.assertDbUnit(getClass(), "shouldPurgeSnapshot-result.xml", "snapshots", "project_measures", "duplications_index", "events");
}
Expand All @@ -73,7 +73,7 @@ public void shouldPurgeSnapshot() {
public void delete_wasted_measures_when_purging_snapshot() {
dbTester.prepareDbUnit(getClass(), "shouldDeleteWastedMeasuresWhenPurgingSnapshot.xml");

new PurgeCommands(dbTester.getSession(), profiler).purgeSnapshots(PurgeSnapshotQuery.create().setId(1L));
new PurgeCommands(dbTester.getSession(), profiler).purgeSnapshots(PurgeSnapshotQuery.create().setSnapshotUuid("u1"));

dbTester.assertDbUnit(getClass(), "shouldDeleteWastedMeasuresWhenPurgingSnapshot-result.xml", "project_measures");
}
Expand Down
29 changes: 13 additions & 16 deletions sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
Expand Up @@ -41,7 +41,6 @@
import static org.mockito.Mockito.when;
import static org.sonar.db.ce.CeTaskTypes.REPORT;


public class PurgeDaoTest {

private static final String THE_PROJECT_UUID = "P1";
Expand Down Expand Up @@ -105,15 +104,15 @@ public void shouldDeleteSnapshots() {
@Test
public void shouldSelectPurgeableSnapshots() {
dbTester.prepareDbUnit(getClass(), "shouldSelectPurgeableSnapshots.xml");
List<PurgeableAnalysisDto> snapshots = underTest.selectPurgeableSnapshots(THE_PROJECT_UUID);
List<PurgeableAnalysisDto> snapshots = underTest.selectPurgeableAnalyses(THE_PROJECT_UUID, dbSession);

assertThat(snapshots).hasSize(3);
assertThat(getById(snapshots, THE_PROJECT_ID).isLast()).isTrue();
assertThat(getById(snapshots, THE_PROJECT_ID).hasEvents()).isFalse();
assertThat(getById(snapshots, 4L).isLast()).isFalse();
assertThat(getById(snapshots, 4L).hasEvents()).isFalse();
assertThat(getById(snapshots, 5L).isLast()).isFalse();
assertThat(getById(snapshots, 5L).hasEvents()).isTrue();
assertThat(getById(snapshots, "u1").isLast()).isTrue();
assertThat(getById(snapshots, "u1").hasEvents()).isFalse();
assertThat(getById(snapshots, "u4").isLast()).isFalse();
assertThat(getById(snapshots, "u4").hasEvents()).isFalse();
assertThat(getById(snapshots, "u5").isLast()).isFalse();
assertThat(getById(snapshots, "u5").hasEvents()).isTrue();
}

@Test
Expand Down Expand Up @@ -184,7 +183,7 @@ public void should_delete_old_closed_issues() {

dbTester.assertDbUnit(getClass(), "should_delete_old_closed_issues-result.xml", "issues", "issue_changes");

Class<ArrayList<String>> listClass = (Class<ArrayList<String>>)(Class)ArrayList.class;
Class<ArrayList<String>> listClass = (Class<ArrayList<String>>) (Class) ArrayList.class;
ArgumentCaptor<ArrayList<String>> issueKeys = ArgumentCaptor.forClass(listClass);
ArgumentCaptor<String> projectUuid = ArgumentCaptor.forClass(String.class);

Expand Down Expand Up @@ -217,13 +216,11 @@ private CeActivityDto insertCeActivity(String componentUuid) {
return dto;
}

private static PurgeableAnalysisDto getById(List<PurgeableAnalysisDto> snapshots, long id) {
for (PurgeableAnalysisDto snapshot : snapshots) {
if (snapshot.getAnalysisId() == id) {
return snapshot;
}
}
return null;
private static PurgeableAnalysisDto getById(List<PurgeableAnalysisDto> snapshots, String uuid) {
return snapshots.stream()
.filter(snapshot -> uuid.equals(snapshot.getAnalysisUuid()))
.findFirst()
.orElse(null);
}

private static PurgeConfiguration newConfigurationWith30Days() {
Expand Down

0 comments on commit 5da7d29

Please sign in to comment.