]> source.dussan.org Git - sonarqube.git/blob
a59c2af1306a6b12d4ce8bf6430ae1593ec38589
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.step;
21
22 import org.sonar.core.util.CloseableIterator;
23 import org.sonar.db.DbClient;
24 import org.sonar.db.DbSession;
25 import org.sonar.db.duplication.DuplicationUnitDto;
26 import org.sonar.scanner.protocol.output.ScannerReport;
27 import org.sonar.server.computation.batch.BatchReportReader;
28 import org.sonar.server.computation.component.Component;
29 import org.sonar.server.computation.component.CrawlerDepthLimit;
30 import org.sonar.server.computation.component.DbIdsRepository;
31 import org.sonar.server.computation.component.DepthTraversalTypeAwareCrawler;
32 import org.sonar.server.computation.component.TreeRootHolder;
33 import org.sonar.server.computation.component.TypeAwareVisitorAdapter;
34 import org.sonar.server.computation.duplication.CrossProjectDuplicationStatusHolder;
35
36 import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
37
38 /**
39  * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
40  */
41 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
42
43   private final DbClient dbClient;
44   private final TreeRootHolder treeRootHolder;
45   private final BatchReportReader reportReader;
46   private final DbIdsRepository dbIdsRepository;
47   private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
48
49   public PersistCrossProjectDuplicationIndexStep(DbClient dbClient, DbIdsRepository dbIdsRepository, TreeRootHolder treeRootHolder, BatchReportReader reportReader,
50     CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder) {
51     this.dbClient = dbClient;
52     this.treeRootHolder = treeRootHolder;
53     this.reportReader = reportReader;
54     this.dbIdsRepository = dbIdsRepository;
55     this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
56   }
57
58   @Override
59   public void execute() {
60     DbSession session = dbClient.openSession(true);
61     try {
62       if (crossProjectDuplicationStatusHolder.isEnabled()) {
63         Component project = treeRootHolder.getRoot();
64         long projectSnapshotId = dbIdsRepository.getSnapshotId(project);
65         new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(session, projectSnapshotId)).visit(project);
66       }
67       session.commit();
68     } finally {
69       dbClient.closeSession(session);
70     }
71   }
72
73   private class DuplicationVisitor extends TypeAwareVisitorAdapter {
74
75     private final DbSession session;
76     private final long projectSnapshotId;
77
78     private DuplicationVisitor(DbSession session, long projectSnapshotId) {
79       super(CrawlerDepthLimit.FILE, PRE_ORDER);
80       this.session = session;
81       this.projectSnapshotId = projectSnapshotId;
82     }
83
84     @Override
85     public void visitFile(Component file) {
86       visitComponent(file);
87     }
88
89     private void visitComponent(Component component) {
90       int indexInFile = 0;
91       CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef());
92       try {
93         while (blocks.hasNext()) {
94           ScannerReport.CpdTextBlock block = blocks.next();
95           dbClient.duplicationDao().insert(
96             session,
97             new DuplicationUnitDto()
98               .setHash(block.getHash())
99               .setStartLine(block.getStartLine())
100               .setEndLine(block.getEndLine())
101               .setIndexInFile(indexInFile)
102               .setSnapshotId(dbIdsRepository.getSnapshotId(component))
103               .setComponentUuid(component.getUuid())
104               .setProjectSnapshotId(projectSnapshotId));
105           indexInFile++;
106         }
107       } finally {
108         blocks.close();
109       }
110     }
111   }
112
113   @Override
114   public String getDescription() {
115     return "Persist cross project duplications index";
116   }
117
118 }