diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-09 12:15:07 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-09 12:15:07 +0200 |
commit | 7155123011053abae8646c6b8ac79bbe19d1355c (patch) | |
tree | 0f1d4215ff2899f0e1f1d421397367aff29294e3 /sonar-batch/src | |
parent | 9a934db810c4b91b13ff6fafa2bb50dcdd933d63 (diff) | |
download | sonarqube-7155123011053abae8646c6b8ac79bbe19d1355c.tar.gz sonarqube-7155123011053abae8646c6b8ac79bbe19d1355c.zip |
SONAR-6258 Read/write Highlighting using Streaming
Diffstat (limited to 'sonar-batch/src')
7 files changed, 67 insertions, 34 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java b/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java index 8a0184be3e3..8a1447acdee 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java @@ -21,6 +21,7 @@ package org.sonar.batch.index; import com.google.common.base.CharMatcher; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; import org.sonar.api.batch.fs.internal.DefaultInputFile; @@ -30,12 +31,13 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.utils.KeyValueFormat; import org.sonar.batch.duplication.DuplicationCache; +import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport.Range; import org.sonar.batch.protocol.output.BatchReport.Scm; import org.sonar.batch.protocol.output.BatchReport.Scm.Changeset; import org.sonar.batch.protocol.output.BatchReport.Symbols; -import org.sonar.batch.protocol.output.BatchReport.SyntaxHighlighting.HighlightingRule; -import org.sonar.batch.protocol.output.*; +import org.sonar.batch.protocol.output.BatchReport.SyntaxHighlighting; +import org.sonar.batch.protocol.output.BatchReportReader; import org.sonar.batch.report.BatchReportUtils; import org.sonar.batch.report.ReportPublisher; import org.sonar.batch.scan.measure.MeasureCache; @@ -43,7 +45,9 @@ import org.sonar.core.source.db.FileSourceDto; import org.sonar.server.source.db.FileSourceDb; import org.sonar.server.source.db.FileSourceDb.Data.Builder; +import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.*; /** @@ -196,20 +200,34 @@ public class SourceDataFactory implements BatchComponent { void applyHighlighting(DefaultInputFile inputFile, FileSourceDb.Data.Builder to) { BatchReportReader reader = new BatchReportReader(reportPublisher.getReportDir()); - List<HighlightingRule> highlightingRules = reader.readComponentSyntaxHighlighting(resourceCache.get(inputFile).batchId()); - if (highlightingRules.isEmpty()) { + File highlightingFile = reader.readComponentSyntaxHighlighting(resourceCache.get(inputFile).batchId()); + if (highlightingFile == null) { return; } StringBuilder[] highlightingPerLine = new StringBuilder[inputFile.lines()]; RuleItemWriter ruleItemWriter = new RuleItemWriter(); int currentLineIdx = 1; - for (HighlightingRule rule : highlightingRules) { - while (currentLineIdx < inputFile.lines() && rule.getRange().getStartLine() > currentLineIdx) { - // This rule starts on another line so advance - currentLineIdx++; + + InputStream inputStream = null; + try { + inputStream = FileUtils.openInputStream(highlightingFile); + BatchReport.SyntaxHighlighting rule = BatchReport.SyntaxHighlighting.PARSER.parseDelimitedFrom(inputStream); + while (rule != null) { + while (currentLineIdx < inputFile.lines() && rule.getRange().getStartLine() > currentLineIdx) { + // This rule starts on another line so advance + currentLineIdx++; + } + // Now we know current rule starts on current line + writeDataPerLine(inputFile.originalLineOffsets(), rule, rule.getRange(), highlightingPerLine, ruleItemWriter); + + // Get next element + rule = BatchReport.SyntaxHighlighting.PARSER.parseDelimitedFrom(inputStream); } - // Now we know current rule starts on current line - writeDataPerLine(inputFile.originalLineOffsets(), rule, rule.getRange(), highlightingPerLine, ruleItemWriter); + + } catch (Exception e) { + throw new IllegalStateException("Can't read syntax highlighting for " + inputFile.absolutePath()); + } finally { + IOUtils.closeQuietly(inputStream); } for (int i = 0; i < highlightingPerLine.length; i++) { StringBuilder sb = highlightingPerLine[i]; @@ -294,9 +312,9 @@ public class SourceDataFactory implements BatchComponent { void writeItem(StringBuilder currentLineSb, long startLineOffset, long endLineOffset, G item); } - private static class RuleItemWriter implements RangeItemWriter<HighlightingRule> { + private static class RuleItemWriter implements RangeItemWriter<SyntaxHighlighting> { @Override - public void writeItem(StringBuilder currentLineSb, long startLineOffset, long endLineOffset, HighlightingRule item) { + public void writeItem(StringBuilder currentLineSb, long startLineOffset, long endLineOffset, SyntaxHighlighting item) { if (currentLineSb.length() > 0) { currentLineSb.append(';'); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java index 423f430c14f..6dff11809b9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java @@ -20,6 +20,8 @@ package org.sonar.batch.mediumtest; import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,12 +42,12 @@ import org.sonar.batch.dependency.DependencyCache; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.index.Cache.Entry; import org.sonar.batch.issue.IssueCache; +import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport.Component; import org.sonar.batch.protocol.output.BatchReport.Metadata; import org.sonar.batch.protocol.output.BatchReport.Range; import org.sonar.batch.protocol.output.BatchReport.Symbols.Symbol; -import org.sonar.batch.protocol.output.BatchReport.SyntaxHighlighting.HighlightingRule; -import org.sonar.batch.protocol.output.*; +import org.sonar.batch.protocol.output.BatchReportReader; import org.sonar.batch.report.BatchReportUtils; import org.sonar.batch.report.ReportPublisher; import org.sonar.batch.scan.ProjectScanContainer; @@ -55,6 +57,8 @@ import org.sonar.batch.scan.measure.MeasureCache; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import java.io.File; +import java.io.InputStream; import java.io.Serializable; import java.util.*; @@ -189,21 +193,33 @@ public class TaskResult implements org.sonar.batch.mediumtest.ScanTaskObserver { /** * Get highlighting types at a given position in an inputfile - * @param charIndex 0-based offset in file + * @param lineOffset 0-based offset in file */ public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); - List<HighlightingRule> syntaxHighlightingRules = getReportReader().readComponentSyntaxHighlighting(ref); - if (syntaxHighlightingRules.isEmpty()) { + File highlightingFile = reader.readComponentSyntaxHighlighting(ref); + if (highlightingFile == null) { return Collections.emptyList(); } TextPointer pointer = file.newPointer(line, lineOffset); List<TypeOfText> result = new ArrayList<TypeOfText>(); - for (HighlightingRule sortedRule : syntaxHighlightingRules) { - TextRange ruleRange = toRange(file, sortedRule.getRange()); - if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { - result.add(BatchReportUtils.toBatchType(sortedRule.getType())); + InputStream inputStream = null; + try { + inputStream = FileUtils.openInputStream(highlightingFile); + BatchReport.SyntaxHighlighting rule = BatchReport.SyntaxHighlighting.PARSER.parseDelimitedFrom(inputStream); + while (rule != null) { + TextRange ruleRange = toRange(file, rule.getRange()); + if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { + result.add(BatchReportUtils.toBatchType(rule.getType())); + } + // Get next element + rule = BatchReport.SyntaxHighlighting.PARSER.parseDelimitedFrom(inputStream); } + + } catch (Exception e) { + throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath()); + } finally { + IOUtils.closeQuietly(inputStream); } return result; } @@ -214,8 +230,8 @@ public class TaskResult implements org.sonar.batch.mediumtest.ScanTaskObserver { /** * Get list of all start positions of a symbol in an inputfile - * @param symbolStartOffset 0-based start offset for the symbol in file - * @param symbolEndOffset 0-based end offset for the symbol in file + * @param symbolStartLine 0-based start offset for the symbol in file + * @param symbolStartLineOffset 0-based end offset for the symbol in file */ @CheckForNull public List<Range> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java index 6bc33a24c6e..617e4d1b943 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java @@ -91,7 +91,7 @@ public class CoveragePublisher implements ReportPublisherStep { } }); - writer.writeFileCoverage(resource.batchId(), Iterables.transform(coveragePerLine.values(), new Function<Coverage.Builder, Coverage>() { + writer.writeComponentCoverage(resource.batchId(), Iterables.transform(coveragePerLine.values(), new Function<Coverage.Builder, Coverage>() { @Override public Coverage apply(Builder input) { return input.build(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java index 5aec595a9f7..99cdf8bbb70 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java +++ b/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java @@ -58,7 +58,6 @@ import org.sonar.batch.index.ResourceCache; import org.sonar.batch.issue.ModuleIssues; import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport.Range; -import org.sonar.batch.protocol.output.BatchReport.SyntaxHighlighting.HighlightingRule; import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.batch.report.BatchReportUtils; import org.sonar.batch.report.ReportPublisher; @@ -226,12 +225,12 @@ public class DefaultSensorStorage implements SensorStorage { BatchReportWriter writer = reportPublisher.getWriter(); DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile(); writer.writeComponentSyntaxHighlighting(resourceCache.get(inputFile).batchId(), - Iterables.transform(highlighting.getSyntaxHighlightingRuleSet(), new Function<SyntaxHighlightingRule, HighlightingRule>() { - private HighlightingRule.Builder builder = HighlightingRule.newBuilder(); + Iterables.transform(highlighting.getSyntaxHighlightingRuleSet(), new Function<SyntaxHighlightingRule, BatchReport.SyntaxHighlighting>() { + private BatchReport.SyntaxHighlighting.Builder builder = BatchReport.SyntaxHighlighting.newBuilder(); private Range.Builder rangeBuilder = Range.newBuilder(); @Override - public HighlightingRule apply(SyntaxHighlightingRule input) { + public BatchReport.SyntaxHighlighting apply(SyntaxHighlightingRule input) { builder.clear(); rangeBuilder.clear(); builder.setRange(rangeBuilder.setStartLine(input.range().start().line()) diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java index 50a2554aa37..3d003ecc8a3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java @@ -35,11 +35,12 @@ import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.protocol.Constants.HighlightingType; -import org.sonar.batch.protocol.output.*; +import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport.Range; import org.sonar.batch.protocol.output.BatchReport.Scm; import org.sonar.batch.protocol.output.BatchReport.Scm.Changeset; -import org.sonar.batch.protocol.output.BatchReport.SyntaxHighlighting.HighlightingRule; +import org.sonar.batch.protocol.output.BatchReport.SyntaxHighlighting; +import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.batch.report.ReportPublisher; import org.sonar.batch.scan.measure.MeasureCache; import org.sonar.server.source.db.FileSourceDb; @@ -315,8 +316,8 @@ public class SourceDataFactoryTest { assertThat(data.getLines(2).getHighlighting()).isEqualTo("0,9,c;1,8,k"); } - private HighlightingRule newRule(int startLine, int startOffset, int endLine, int endOffset, HighlightingType type) { - return BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder() + private SyntaxHighlighting newRule(int startLine, int startOffset, int endLine, int endOffset, HighlightingType type) { + return BatchReport.SyntaxHighlighting.newBuilder() .setRange(Range.newBuilder().setStartLine(startLine).setStartOffset(startOffset).setEndLine(endLine).setEndOffset(endOffset).build()) .setType(type) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java index 96ce8e035e7..0455bf55dba 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java @@ -91,7 +91,6 @@ public class HighlightingMediumTest { assertThat(result.highlightingTypeFor(file, 1, 9)).containsExactly(TypeOfText.STRING); assertThat(result.highlightingTypeFor(file, 2, 0)).containsExactly(TypeOfText.KEYWORD); assertThat(result.highlightingTypeFor(file, 2, 8)).isEmpty(); - } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java index 3e3c4cf284d..f799843baba 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java @@ -95,7 +95,7 @@ public class CoveragePublisherTest { publisher.publish(writer); - try (InputStream inputStream = FileUtils.openInputStream(new BatchReportReader(outputDir).readFileCoverage(2))) { + try (InputStream inputStream = FileUtils.openInputStream(new BatchReportReader(outputDir).readComponentCoverage(2))) { assertThat(BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream)).isEqualTo(Coverage.newBuilder() .setLine(2) .setUtHits(true) |