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 com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableMap;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import javax.annotation.CheckForNull;
29 import javax.annotation.Nullable;
30 import org.apache.commons.codec.digest.DigestUtils;
31 import org.apache.commons.lang.ObjectUtils;
32 import org.sonar.api.utils.System2;
33 import org.sonar.core.util.CloseableIterator;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.protobuf.DbFileSources;
37 import org.sonar.db.source.FileSourceDto;
38 import org.sonar.db.source.FileSourceDto.Type;
39 import org.sonar.scanner.protocol.output.ScannerReport;
40 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
41 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
42 import org.sonar.server.computation.task.projectanalysis.component.Component;
43 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
44 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
45 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
46 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
47 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
48 import org.sonar.server.computation.task.projectanalysis.duplication.DuplicationRepository;
49 import org.sonar.server.computation.task.projectanalysis.scm.Changeset;
50 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfo;
51 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepository;
52 import org.sonar.server.computation.task.projectanalysis.source.ComputeFileSourceData;
53 import org.sonar.server.computation.task.projectanalysis.source.CoverageLineReader;
54 import org.sonar.server.computation.task.projectanalysis.source.DuplicationLineReader;
55 import org.sonar.server.computation.task.projectanalysis.source.HighlightingLineReader;
56 import org.sonar.server.computation.task.projectanalysis.source.LineReader;
57 import org.sonar.server.computation.task.projectanalysis.source.RangeOffsetConverter;
58 import org.sonar.server.computation.task.projectanalysis.source.ScmLineReader;
59 import org.sonar.server.computation.task.projectanalysis.source.SourceLinesRepository;
60 import org.sonar.server.computation.task.projectanalysis.source.SymbolsLineReader;
61 import org.sonar.server.computation.task.step.ComputationStep;
63 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
65 public class PersistFileSourcesStep implements ComputationStep {
67 private final DbClient dbClient;
68 private final System2 system2;
69 private final TreeRootHolder treeRootHolder;
70 private final BatchReportReader reportReader;
71 private final SourceLinesRepository sourceLinesRepository;
72 private final ScmInfoRepository scmInfoRepository;
73 private final DuplicationRepository duplicationRepository;
74 private final AnalysisMetadataHolder analysisMetadataHolder;
76 public PersistFileSourcesStep(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, BatchReportReader reportReader, SourceLinesRepository sourceLinesRepository,
77 ScmInfoRepository scmInfoRepository, DuplicationRepository duplicationRepository, AnalysisMetadataHolder analysisMetadataHolder) {
78 this.dbClient = dbClient;
79 this.system2 = system2;
80 this.treeRootHolder = treeRootHolder;
81 this.reportReader = reportReader;
82 this.sourceLinesRepository = sourceLinesRepository;
83 this.scmInfoRepository = scmInfoRepository;
84 this.duplicationRepository = duplicationRepository;
85 this.analysisMetadataHolder = analysisMetadataHolder;
89 public void execute() {
90 // Don't use batch insert for file_sources since keeping all data in memory can produce OOM for big files
91 try (DbSession dbSession = dbClient.openSession(false)) {
92 new DepthTraversalTypeAwareCrawler(new FileSourceVisitor(dbSession))
93 .visit(treeRootHolder.getRoot());
97 private class FileSourceVisitor extends TypeAwareVisitorAdapter {
99 private final DbSession session;
101 private Map<String, FileSourceDto> previousFileSourcesByUuid = new HashMap<>();
102 private String projectUuid;
104 private FileSourceVisitor(DbSession session) {
105 super(CrawlerDepthLimit.FILE, PRE_ORDER);
106 this.session = session;
110 public void visitProject(Component project) {
111 this.projectUuid = project.getUuid();
112 session.select("org.sonar.db.source.FileSourceMapper.selectHashesForProject", ImmutableMap.of("projectUuid", projectUuid, "dataType", Type.SOURCE),
114 FileSourceDto dto = (FileSourceDto) context.getResultObject();
115 previousFileSourcesByUuid.put(dto.getFileUuid(), dto);
120 public void visitFile(Component file) {
121 if (analysisMetadataHolder.isIncrementalAnalysis() && file.getStatus() == Status.SAME) {
125 CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
126 LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file);
128 ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), file.getFileAttributes().getLines());
129 ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute();
130 persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange());
131 } catch (Exception e) {
132 throw new IllegalStateException(String.format("Cannot persist sources of %s", file.getKey()), e);
134 linesIterator.close();
139 private void persistSource(ComputeFileSourceData.Data fileSourceData, String componentUuid, @Nullable Changeset latestChange) {
140 DbFileSources.Data fileData = fileSourceData.getFileSourceData();
142 byte[] data = FileSourceDto.encodeSourceData(fileData);
143 String dataHash = DigestUtils.md5Hex(data);
144 String srcHash = fileSourceData.getSrcHash();
145 String lineHashes = fileSourceData.getLineHashes();
146 FileSourceDto previousDto = previousFileSourcesByUuid.get(componentUuid);
148 if (previousDto == null) {
149 FileSourceDto dto = new FileSourceDto()
150 .setProjectUuid(projectUuid)
151 .setFileUuid(componentUuid)
152 .setDataType(Type.SOURCE)
155 .setDataHash(dataHash)
156 .setLineHashes(lineHashes)
157 .setCreatedAt(system2.now())
158 .setUpdatedAt(system2.now())
159 .setRevision(computeRevision(latestChange));
160 dbClient.fileSourceDao().insert(session, dto);
163 // Update only if data_hash has changed or if src_hash is missing or revision is missing (progressive migration)
164 boolean binaryDataUpdated = !dataHash.equals(previousDto.getDataHash());
165 boolean srcHashUpdated = !srcHash.equals(previousDto.getSrcHash());
166 String revision = computeRevision(latestChange);
167 boolean revisionUpdated = !ObjectUtils.equals(revision, previousDto.getRevision());
168 if (binaryDataUpdated || srcHashUpdated || revisionUpdated) {
171 .setDataHash(dataHash)
173 .setLineHashes(lineHashes)
174 .setRevision(revision)
175 .setUpdatedAt(system2.now());
176 dbClient.fileSourceDao().update(session, previousDto);
183 private String computeRevision(@Nullable Changeset latestChange) {
184 if (latestChange == null) {
187 return latestChange.getRevision();
191 private static class LineReaders {
192 private final List<LineReader> readers = new ArrayList<>();
193 private final List<CloseableIterator<?>> closeables = new ArrayList<>();
195 private final ScmLineReader scmLineReader;
197 LineReaders(BatchReportReader reportReader, ScmInfoRepository scmInfoRepository, DuplicationRepository duplicationRepository, Component component) {
198 int componentRef = component.getReportAttributes().getRef();
199 CloseableIterator<ScannerReport.LineCoverage> coverageIt = reportReader.readComponentCoverage(componentRef);
200 closeables.add(coverageIt);
201 readers.add(new CoverageLineReader(coverageIt));
203 Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
204 if (scmInfoOptional.isPresent()) {
205 this.scmLineReader = new ScmLineReader(scmInfoOptional.get());
206 readers.add(scmLineReader);
208 this.scmLineReader = null;
211 RangeOffsetConverter rangeOffsetConverter = new RangeOffsetConverter();
212 CloseableIterator<ScannerReport.SyntaxHighlightingRule> highlightingIt = reportReader.readComponentSyntaxHighlighting(componentRef);
213 closeables.add(highlightingIt);
214 readers.add(new HighlightingLineReader(component, highlightingIt, rangeOffsetConverter));
216 CloseableIterator<ScannerReport.Symbol> symbolsIt = reportReader.readComponentSymbols(componentRef);
217 closeables.add(symbolsIt);
218 readers.add(new SymbolsLineReader(component, symbolsIt, rangeOffsetConverter));
219 readers.add(new DuplicationLineReader(duplicationRepository.getDuplications(component)));
222 List<LineReader> readers() {
227 for (CloseableIterator<?> reportIterator : closeables) {
228 reportIterator.close();
233 public Changeset getLatestChange() {
234 if (scmLineReader == null) {
237 return scmLineReader.getLatestChange();
242 public String getDescription() {
243 return "Persist sources";