3 * Copyright (C) 2009-2019 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.ce.task.projectanalysis.source;
22 import java.util.List;
23 import java.util.Optional;
24 import org.apache.commons.lang.StringUtils;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.core.hash.LineRange;
27 import org.sonar.core.hash.SourceLineHashesComputer;
28 import org.sonar.core.util.CloseableIterator;
29 import org.sonar.db.source.LineHashVersion;
31 public class SourceLinesHashRepositoryImpl implements SourceLinesHashRepository {
32 private final SourceLinesRepository sourceLinesRepository;
33 private final SignificantCodeRepository significantCodeRepository;
34 private final SourceLinesHashCache cache;
35 private final DbLineHashVersion dbLineHashesVersion;
37 public SourceLinesHashRepositoryImpl(SourceLinesRepository sourceLinesRepository, SignificantCodeRepository significantCodeRepository,
38 SourceLinesHashCache cache, DbLineHashVersion dbLineHashVersion) {
39 this.sourceLinesRepository = sourceLinesRepository;
40 this.significantCodeRepository = significantCodeRepository;
42 this.dbLineHashesVersion = dbLineHashVersion;
46 public List<String> getLineHashesMatchingDBVersion(Component component) {
47 return cache.computeIfAbsent(component, this::createLineHashesMatchingDBVersion);
51 public int getLineHashesVersion(Component component) {
52 if (significantCodeRepository.getRangesPerLine(component).isPresent()) {
53 return LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue();
55 return LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue();
60 public LineHashesComputer getLineHashesComputerToPersist(Component component) {
61 boolean cacheHit = cache.contains(component);
63 // check if line hashes are cached and if we can use it
64 if (cacheHit && dbLineHashesVersion.hasLineHashesWithSignificantCode(component)) {
65 return new CachedLineHashesComputer(cache.get(component));
68 Optional<LineRange[]> significantCodePerLine = significantCodeRepository.getRangesPerLine(component);
69 if (cacheHit && !significantCodePerLine.isPresent()) {
70 return new CachedLineHashesComputer(cache.get(component));
73 // Generate the line hashes taking into account significant code ranges
74 return createLineHashesProcessor(component.getFileAttributes().getLines(), significantCodePerLine);
77 private List<String> createLineHashesMatchingDBVersion(Component component) {
78 if (!dbLineHashesVersion.hasLineHashesWithSignificantCode(component)) {
79 return createLineHashes(component, Optional.empty());
82 Optional<LineRange[]> significantCodePerLine = significantCodeRepository.getRangesPerLine(component);
83 return createLineHashes(component, significantCodePerLine);
86 private List<String> createLineHashes(Component component, Optional<LineRange[]> significantCodePerLine) {
87 LineHashesComputer processor = createLineHashesProcessor(component.getFileAttributes().getLines(), significantCodePerLine);
88 try (CloseableIterator<String> lines = sourceLinesRepository.readLines(component)) {
89 while (lines.hasNext()) {
90 processor.addLine(lines.next());
92 return processor.getResult();
96 public interface LineHashesComputer {
97 void addLine(String line);
99 List<String> getResult();
102 private static LineHashesComputer createLineHashesProcessor(int numLines, Optional<LineRange[]> significantCodePerLine) {
103 if (significantCodePerLine.isPresent()) {
104 return new SignificantCodeLineHashesComputer(new SourceLineHashesComputer(numLines), significantCodePerLine.get());
106 return new SimpleLineHashesComputer(numLines);
110 static class CachedLineHashesComputer implements LineHashesComputer {
111 private final List<String> lineHashes;
113 public CachedLineHashesComputer(List<String> lineHashes) {
114 this.lineHashes = lineHashes;
118 public void addLine(String line) {
123 public List<String> getResult() {
128 static class SimpleLineHashesComputer implements LineHashesComputer {
129 private final SourceLineHashesComputer delegate;
131 public SimpleLineHashesComputer(int numLines) {
132 this.delegate = new SourceLineHashesComputer(numLines);
136 public void addLine(String line) {
137 delegate.addLine(line);
141 public List<String> getResult() {
142 return delegate.getLineHashes();
146 static class SignificantCodeLineHashesComputer implements LineHashesComputer {
147 private final SourceLineHashesComputer delegate;
148 private final LineRange[] rangesPerLine;
152 public SignificantCodeLineHashesComputer(SourceLineHashesComputer hashComputer, LineRange[] rangesPerLine) {
153 this.rangesPerLine = rangesPerLine;
154 this.delegate = hashComputer;
158 public void addLine(String line) {
159 LineRange range = null;
160 if (i < rangesPerLine.length) {
161 range = rangesPerLine[i];
165 delegate.addLine("");
167 delegate.addLine(StringUtils.substring(line, range.startOffset(), range.endOffset()));
173 public List<String> getResult() {
174 return delegate.getLineHashes();