3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import java.util.List;
23 import javax.annotation.Nullable;
24 import org.sonar.core.util.CloseableIterator;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.duplication.DuplicationUnitDto;
28 import org.sonar.scanner.protocol.output.ScannerReport;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
33 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
34 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
35 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
36 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
37 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
38 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
39 import org.sonar.server.computation.task.step.ComputationStep;
41 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
44 * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
46 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
48 private final DbClient dbClient;
49 private final TreeRootHolder treeRootHolder;
50 private final AnalysisMetadataHolder analysisMetadataHolder;
51 private final BatchReportReader reportReader;
52 private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
54 public PersistCrossProjectDuplicationIndexStep(CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder, DbClient dbClient,
55 TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
56 BatchReportReader reportReader) {
57 this.dbClient = dbClient;
58 this.treeRootHolder = treeRootHolder;
59 this.analysisMetadataHolder = analysisMetadataHolder;
60 this.reportReader = reportReader;
61 this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
65 public void execute() {
66 if (!crossProjectDuplicationStatusHolder.isEnabled()) {
70 try (DbSession dbSession = dbClient.openSession(true)) {
71 Component project = treeRootHolder.getRoot();
72 Analysis baseAnalysis = analysisMetadataHolder.getBaseAnalysis();
73 String lastAnalysysUuid = (baseAnalysis != null) ? baseAnalysis.getUuid() : null;
74 new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(dbSession, analysisMetadataHolder.getUuid(), lastAnalysysUuid)).visit(project);
79 private class DuplicationVisitor extends TypeAwareVisitorAdapter {
81 private final DbSession session;
82 private final String analysisUuid;
83 private final String lastAnalysisUuid;
85 private DuplicationVisitor(DbSession session, String analysisUuid, @Nullable String lastAnalysisUuid) {
86 super(CrawlerDepthLimit.FILE, PRE_ORDER);
87 this.session = session;
88 this.analysisUuid = analysisUuid;
89 this.lastAnalysisUuid = lastAnalysisUuid;
93 public void visitFile(Component file) {
97 private void visitComponent(Component component) {
98 if (analysisMetadataHolder.isIncrementalAnalysis() && component.getStatus() == Status.SAME) {
99 readFromDb(component);
101 readFromReport(component);
105 private void readFromReport(Component component) {
107 try (CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) {
108 while (blocks.hasNext()) {
109 ScannerReport.CpdTextBlock block = blocks.next();
110 dbClient.duplicationDao().insert(
112 new DuplicationUnitDto()
113 .setHash(block.getHash())
114 .setStartLine(block.getStartLine())
115 .setEndLine(block.getEndLine())
116 .setIndexInFile(indexInFile)
117 .setAnalysisUuid(analysisUuid)
118 .setComponentUuid(component.getUuid()));
124 private void readFromDb(Component component) {
126 List<DuplicationUnitDto> units = dbClient.duplicationDao().selectComponent(session, component.getUuid(), lastAnalysisUuid);
127 for (DuplicationUnitDto unit : units) {
128 unit.setAnalysisUuid(analysisUuid);
129 unit.setIndexInFile(indexInFile);
130 dbClient.duplicationDao().insert(session, unit);
137 public String getDescription() {
138 return "Persist cross project duplications index";