You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SourceLinesHashRepositoryImpl.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info 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.ce.task.projectanalysis.source;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.sonar.ce.task.projectanalysis.component.Component;
  25. import org.sonar.core.hash.LineRange;
  26. import org.sonar.core.hash.SourceLineHashesComputer;
  27. import org.sonar.core.util.CloseableIterator;
  28. import org.sonar.db.source.LineHashVersion;
  29. public class SourceLinesHashRepositoryImpl implements SourceLinesHashRepository {
  30. private final SourceLinesRepository sourceLinesRepository;
  31. private final SignificantCodeRepository significantCodeRepository;
  32. private final SourceLinesHashCache cache;
  33. private final DbLineHashVersion dbLineHashesVersion;
  34. public SourceLinesHashRepositoryImpl(SourceLinesRepository sourceLinesRepository, SignificantCodeRepository significantCodeRepository,
  35. SourceLinesHashCache cache, DbLineHashVersion dbLineHashVersion) {
  36. this.sourceLinesRepository = sourceLinesRepository;
  37. this.significantCodeRepository = significantCodeRepository;
  38. this.cache = cache;
  39. this.dbLineHashesVersion = dbLineHashVersion;
  40. }
  41. @Override
  42. public List<String> getLineHashesMatchingDBVersion(Component component) {
  43. return cache.computeIfAbsent(component, this::createLineHashesMatchingDBVersion);
  44. }
  45. @Override
  46. public int getLineHashesVersion(Component component) {
  47. if (significantCodeRepository.getRangesPerLine(component).isPresent()) {
  48. return LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue();
  49. } else {
  50. return LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue();
  51. }
  52. }
  53. @Override
  54. public LineHashesComputer getLineHashesComputerToPersist(Component component) {
  55. boolean cacheHit = cache.contains(component);
  56. // check if line hashes are cached and if we can use it
  57. if (cacheHit && !dbLineHashesVersion.hasLineHashesWithoutSignificantCode(component)) {
  58. return new CachedLineHashesComputer(cache.get(component));
  59. }
  60. Optional<LineRange[]> significantCodePerLine = significantCodeRepository.getRangesPerLine(component);
  61. if (cacheHit && !significantCodePerLine.isPresent()) {
  62. return new CachedLineHashesComputer(cache.get(component));
  63. }
  64. // Generate the line hashes taking into account significant code ranges
  65. return createLineHashesProcessor(component.getFileAttributes().getLines(), significantCodePerLine);
  66. }
  67. private List<String> createLineHashesMatchingDBVersion(Component component) {
  68. if (dbLineHashesVersion.hasLineHashesWithoutSignificantCode(component)) {
  69. return createLineHashes(component, Optional.empty());
  70. }
  71. // if the file is not in the DB, this will be used too
  72. Optional<LineRange[]> significantCodePerLine = significantCodeRepository.getRangesPerLine(component);
  73. return createLineHashes(component, significantCodePerLine);
  74. }
  75. private List<String> createLineHashes(Component component, Optional<LineRange[]> significantCodePerLine) {
  76. LineHashesComputer processor = createLineHashesProcessor(component.getFileAttributes().getLines(), significantCodePerLine);
  77. try (CloseableIterator<String> lines = sourceLinesRepository.readLines(component)) {
  78. while (lines.hasNext()) {
  79. processor.addLine(lines.next());
  80. }
  81. return processor.getResult();
  82. }
  83. }
  84. public interface LineHashesComputer {
  85. void addLine(String line);
  86. List<String> getResult();
  87. }
  88. private static LineHashesComputer createLineHashesProcessor(int numLines, Optional<LineRange[]> significantCodePerLine) {
  89. if (significantCodePerLine.isPresent()) {
  90. return new SignificantCodeLineHashesComputer(new SourceLineHashesComputer(numLines), significantCodePerLine.get());
  91. } else {
  92. return new SimpleLineHashesComputer(numLines);
  93. }
  94. }
  95. static class CachedLineHashesComputer implements LineHashesComputer {
  96. private final List<String> lineHashes;
  97. public CachedLineHashesComputer(List<String> lineHashes) {
  98. this.lineHashes = lineHashes;
  99. }
  100. @Override
  101. public void addLine(String line) {
  102. // no op
  103. }
  104. @Override
  105. public List<String> getResult() {
  106. return lineHashes;
  107. }
  108. }
  109. static class SimpleLineHashesComputer implements LineHashesComputer {
  110. private final SourceLineHashesComputer delegate;
  111. public SimpleLineHashesComputer(int numLines) {
  112. this.delegate = new SourceLineHashesComputer(numLines);
  113. }
  114. @Override
  115. public void addLine(String line) {
  116. delegate.addLine(line);
  117. }
  118. @Override
  119. public List<String> getResult() {
  120. return delegate.getLineHashes();
  121. }
  122. }
  123. static class SignificantCodeLineHashesComputer implements LineHashesComputer {
  124. private final SourceLineHashesComputer delegate;
  125. private final LineRange[] rangesPerLine;
  126. private int i = 0;
  127. public SignificantCodeLineHashesComputer(SourceLineHashesComputer hashComputer, LineRange[] rangesPerLine) {
  128. this.rangesPerLine = rangesPerLine;
  129. this.delegate = hashComputer;
  130. }
  131. @Override
  132. public void addLine(String line) {
  133. LineRange range = null;
  134. if (i < rangesPerLine.length) {
  135. range = rangesPerLine[i];
  136. }
  137. if (range == null) {
  138. delegate.addLine("");
  139. } else {
  140. delegate.addLine(StringUtils.substring(line, range.startOffset(), range.endOffset()));
  141. }
  142. i++;
  143. }
  144. @Override
  145. public List<String> getResult() {
  146. return delegate.getLineHashes();
  147. }
  148. }
  149. }