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 try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
126 LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file)) {
127 ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), file.getFileAttributes().getLines());
128 ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute();
129 persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange());
130 } catch (Exception e) {
131 throw new IllegalStateException(String.format("Cannot persist sources of %s", file.getKey()), e);
135 private void persistSource(ComputeFileSourceData.Data fileSourceData, String componentUuid, @Nullable Changeset latestChange) {
136 DbFileSources.Data fileData = fileSourceData.getFileSourceData();
138 byte[] data = FileSourceDto.encodeSourceData(fileData);
139 String dataHash = DigestUtils.md5Hex(data);
140 String srcHash = fileSourceData.getSrcHash();
141 String lineHashes = fileSourceData.getLineHashes();
142 FileSourceDto previousDto = previousFileSourcesByUuid.get(componentUuid);
144 if (previousDto == null) {
145 FileSourceDto dto = new FileSourceDto()
146 .setProjectUuid(projectUuid)
147 .setFileUuid(componentUuid)
148 .setDataType(Type.SOURCE)
151 .setDataHash(dataHash)
152 .setLineHashes(lineHashes)
153 .setCreatedAt(system2.now())
154 .setUpdatedAt(system2.now())
155 .setRevision(computeRevision(latestChange));
156 dbClient.fileSourceDao().insert(session, dto);
159 // Update only if data_hash has changed or if src_hash is missing or revision is missing (progressive migration)
160 boolean binaryDataUpdated = !dataHash.equals(previousDto.getDataHash());
161 boolean srcHashUpdated = !srcHash.equals(previousDto.getSrcHash());
162 String revision = computeRevision(latestChange);
163 boolean revisionUpdated = !ObjectUtils.equals(revision, previousDto.getRevision());
164 if (binaryDataUpdated || srcHashUpdated || revisionUpdated) {
167 .setDataHash(dataHash)
169 .setLineHashes(lineHashes)
170 .setRevision(revision)
171 .setUpdatedAt(system2.now());
172 dbClient.fileSourceDao().update(session, previousDto);
179 private String computeRevision(@Nullable Changeset latestChange) {
180 if (latestChange == null) {
183 return latestChange.getRevision();
187 private static class LineReaders implements AutoCloseable {
188 private final List<LineReader> readers = new ArrayList<>();
189 private final List<CloseableIterator<?>> closeables = new ArrayList<>();
191 private final ScmLineReader scmLineReader;
193 LineReaders(BatchReportReader reportReader, ScmInfoRepository scmInfoRepository, DuplicationRepository duplicationRepository, Component component) {
194 int componentRef = component.getReportAttributes().getRef();
195 CloseableIterator<ScannerReport.LineCoverage> coverageIt = reportReader.readComponentCoverage(componentRef);
196 closeables.add(coverageIt);
197 readers.add(new CoverageLineReader(coverageIt));
199 Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
200 if (scmInfoOptional.isPresent()) {
201 this.scmLineReader = new ScmLineReader(scmInfoOptional.get());
202 readers.add(scmLineReader);
204 this.scmLineReader = null;
207 RangeOffsetConverter rangeOffsetConverter = new RangeOffsetConverter();
208 CloseableIterator<ScannerReport.SyntaxHighlightingRule> highlightingIt = reportReader.readComponentSyntaxHighlighting(componentRef);
209 closeables.add(highlightingIt);
210 readers.add(new HighlightingLineReader(component, highlightingIt, rangeOffsetConverter));
212 CloseableIterator<ScannerReport.Symbol> symbolsIt = reportReader.readComponentSymbols(componentRef);
213 closeables.add(symbolsIt);
214 readers.add(new SymbolsLineReader(component, symbolsIt, rangeOffsetConverter));
215 readers.add(new DuplicationLineReader(duplicationRepository.getDuplications(component)));
218 List<LineReader> readers() {
223 public void close() {
224 for (CloseableIterator<?> reportIterator : closeables) {
225 reportIterator.close();
230 public Changeset getLatestChange() {
231 if (scmLineReader == null) {
234 return scmLineReader.getLatestChange();
239 public String getDescription() {
240 return "Persist sources";