*/
package org.sonar.server.computation.task.projectanalysis.step;
-import java.util.List;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
-import org.sonar.server.computation.task.projectanalysis.component.PathAwareCrawler;
-import org.sonar.server.computation.task.projectanalysis.component.PathAwareVisitorAdapter;
+import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
+import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
import org.sonar.server.computation.task.projectanalysis.period.Period;
import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
import org.sonar.server.computation.task.step.ComputationStep;
public void execute() {
DbSession session = dbClient.openSession(false);
try {
- new PathAwareCrawler<>(
+ new DepthTraversalTypeAwareCrawler(
new PersistSnapshotsPathAwareVisitor(session, analysisMetadataHolder.getAnalysisDate()))
.visit(treeRootHolder.getRoot());
session.commit();
}
}
- private class PersistSnapshotsPathAwareVisitor extends PathAwareVisitorAdapter<SnapshotDtoHolder> {
+ private class PersistSnapshotsPathAwareVisitor extends TypeAwareVisitorAdapter {
private final DbSession dbSession;
private final long analysisDate;
public PersistSnapshotsPathAwareVisitor(DbSession dbSession, long analysisDate) {
- super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER, SnapshotDtoHolderFactory.INSTANCE);
+ super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER);
this.dbSession = dbSession;
this.analysisDate = analysisDate;
}
@Override
- public void visitProject(Component project, Path<SnapshotDtoHolder> path) {
+ public void visitProject(Component project) {
SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), project, true);
updateSnapshotPeriods(snapshot);
persist(snapshot, dbSession);
}
@Override
- public void visitView(Component view, Path<SnapshotDtoHolder> path) {
+ public void visitView(Component view) {
SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), view, false);
updateSnapshotPeriods(snapshot);
persist(snapshot, dbSession);
}
private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
- List<Period> periods = periodsHolder.getPeriods();
- if (periods.isEmpty()) {
+ if (!periodsHolder.hasPeriod()) {
return;
}
- Period period = periods.get(0);
+ Period period = periodsHolder.getPeriod();
snapshotDto.setPeriodMode(period.getMode());
snapshotDto.setPeriodParam(period.getModeParameter());
snapshotDto.setPeriodDate(period.getSnapshotDate());
}
}
- private static final class SnapshotDtoHolder {
- @CheckForNull
- private SnapshotDto snapshotDto;
-
- @CheckForNull
- public SnapshotDto getSnapshotDto() {
- return snapshotDto;
- }
-
- public void setSnapshotDto(@Nullable SnapshotDto snapshotDto) {
- this.snapshotDto = snapshotDto;
- }
- }
-
- /**
- * Factory of SnapshotDtoHolder.
- *
- * No need to create an instance for components of type FILE and PROJECT_VIEW, since they are the leaves of their
- * respective trees, no one will consume the value of the holder, so we save on creating useless objects.
- */
- private static class SnapshotDtoHolderFactory extends PathAwareVisitorAdapter.SimpleStackElementFactory<SnapshotDtoHolder> {
- public static final SnapshotDtoHolderFactory INSTANCE = new SnapshotDtoHolderFactory();
-
- @Override
- public SnapshotDtoHolder createForAny(Component component) {
- return new SnapshotDtoHolder();
- }
-
- @Override
- public SnapshotDtoHolder createForFile(Component file) {
- return null;
- }
-
- @Override
- public SnapshotDtoHolder createForProjectView(Component projectView) {
- return null;
- }
- }
-
@Override
public String getDescription() {
return "Persist analysis";
import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS;
-
public class ReportPersistAnalysisStepTest extends BaseStepTest {
private static final String PROJECT_KEY = "PROJECT_KEY";
@Rule
public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
- System2 system2 = mock(System2.class);
+ private System2 system2 = mock(System2.class);
- DbIdsRepositoryImpl dbIdsRepository;
+ private DbIdsRepositoryImpl dbIdsRepository;
- DbClient dbClient = dbTester.getDbClient();
+ private DbClient dbClient = dbTester.getDbClient();
- long analysisDate;
+ private long analysisDate;
- long now;
+ private long now;
PersistAnalysisStep underTest;
underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
// initialize PeriodHolder to empty by default
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
}
@Override
SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
dbTester.getSession().commit();
- periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
+ periodsHolder.setPeriod(new Period(TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
treeRootHolder.setRoot(project);
@Test
public void only_persist_snapshots_with_leak_period_on_project_and_module() {
- periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1"));
+ periodsHolder.setPeriod(new Period(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1"));
OrganizationDto organizationDto = dbTester.organizations().insert();
ComponentDto projectDto = ComponentTesting.newProjectDto(organizationDto, "ABCD").setKey(PROJECT_KEY).setName("Project");
@Rule
public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
- System2 system2 = mock(System2.class);
+ private System2 system2 = mock(System2.class);
- DbClient dbClient = dbTester.getDbClient();
+ private DbClient dbClient = dbTester.getDbClient();
- long analysisDate;
+ private long analysisDate;
- long now;
+ private long now;
- PersistAnalysisStep underTest;
+ private PersistAnalysisStep underTest;
@Before
public void setup() {
underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
// initialize PeriodHolder to empty by default
- periodsHolder.setPeriods();
+ periodsHolder.setPeriod(null);
}
@Override
Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
treeRootHolder.setRoot(view);
- periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
+ periodsHolder.setPeriod(new Period(TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
underTest.execute();