diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2016-02-18 17:49:46 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2016-02-22 10:57:25 +0100 |
commit | cef567021858b0e7239ac80d4512172ef7b70dbd (patch) | |
tree | dea220deefe5504de43689cc00f0ca3e539ee4f4 | |
parent | cc00abdb06af7466b4aedbaa7159d0844f465e96 (diff) | |
download | sonarqube-cef567021858b0e7239ac80d4512172ef7b70dbd.tar.gz sonarqube-cef567021858b0e7239ac80d4512172ef7b70dbd.zip |
No need to take into account personId in measure cache (devcockpit moved to CE)
4 files changed, 25 insertions, 39 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 7cfeac5697f..d5f1a561729 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -151,20 +151,19 @@ public class DefaultIndex extends SonarIndex { if (indexedResource == null) { return null; } - Iterable<Measure> unfiltered; + Collection<Measure> unfiltered = new ArrayList<>(); if (filter instanceof MeasuresFilters.MetricFilter) { // optimization - unfiltered = measureCache.byMetric(indexedResource, ((MeasuresFilters.MetricFilter<M>) filter).filterOnMetricKey()); + Measure byMetric = measureCache.byMetric(indexedResource, ((MeasuresFilters.MetricFilter<M>) filter).filterOnMetricKey()); + if (byMetric != null) { + unfiltered.add(byMetric); + } } else { - unfiltered = measureCache.byResource(indexedResource); - } - Collection<Measure> all = new ArrayList<>(); - if (unfiltered != null) { - for (Measure measure : unfiltered) { - all.add(measure); + for (Measure measure : measureCache.byResource(indexedResource)) { + unfiltered.add(measure); } } - return filter.filter(all); + return filter.filter(unfiltered); } @Override 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 c1b8b477945..dbfe3b4d03b 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 @@ -102,9 +102,8 @@ public class CoveragePublisher implements ReportPublisherStep { } void applyLineMeasure(String inputFileKey, int lineCount, String metricKey, Map<Integer, Coverage.Builder> coveragePerLine, MeasureOperation op) { - Iterable<Measure> measures = measureCache.byMetric(inputFileKey, metricKey); - if (measures.iterator().hasNext()) { - Measure measure = measures.iterator().next(); + Measure measure = measureCache.byMetric(inputFileKey, metricKey); + if (measure != null) { Map<Integer, String> lineMeasures = KeyValueFormat.parseIntString((String) measure.value()); for (Map.Entry<Integer, String> lineMeasure : lineMeasures.entrySet()) { int lineIdx = lineMeasure.getKey(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java index f262669ac3b..c0adb26f4b6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java @@ -20,6 +20,7 @@ package org.sonar.batch.scan.measure; import com.google.common.base.Preconditions; +import javax.annotation.CheckForNull; import org.sonar.api.batch.BatchSide; import org.sonar.api.batch.measure.MetricFinder; import org.sonar.api.measures.Measure; @@ -53,38 +54,27 @@ public class MeasureCache { return cache.values(r.getEffectiveKey()); } - public Iterable<Measure> byMetric(Resource r, String metricKey) { + @CheckForNull + public Measure byMetric(Resource r, String metricKey) { return byMetric(r.getEffectiveKey(), metricKey); } - public Iterable<Measure> byMetric(String resourceKey, String metricKey) { - return cache.values(resourceKey, metricKey); + @CheckForNull + public Measure byMetric(String resourceKey, String metricKey) { + return cache.get(resourceKey, metricKey); } public MeasureCache put(Resource resource, Measure measure) { Preconditions.checkNotNull(resource.getEffectiveKey()); Preconditions.checkNotNull(measure.getMetricKey()); - cache.put(resource.getEffectiveKey(), measure.getMetricKey(), computeMeasureKey(measure), measure); + cache.put(resource.getEffectiveKey(), measure.getMetricKey(), measure); return this; } public boolean contains(Resource resource, Measure measure) { Preconditions.checkNotNull(resource.getEffectiveKey()); Preconditions.checkNotNull(measure.getMetricKey()); - return cache.containsKey(resource.getEffectiveKey(), measure.getMetricKey(), computeMeasureKey(measure)); - } - - private static String computeMeasureKey(Measure m) { - StringBuilder sb = new StringBuilder(); - if (m.getMetricKey() != null) { - sb.append(m.getMetricKey()); - } - sb.append("|"); - Integer personId = m.getPersonId(); - if (personId != null) { - sb.append(personId); - } - return sb.toString(); + return cache.containsKey(resource.getEffectiveKey(), measure.getMetricKey()); } } 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 8ab2fb9b9df..07952274c45 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 @@ -20,8 +20,6 @@ package org.sonar.batch.report; import java.io.File; -import java.util.Arrays; -import java.util.Collections; import java.util.Date; import org.junit.Before; import org.junit.Rule; @@ -62,7 +60,7 @@ public class CoveragePublisherTest { resourceCache.add(p, null).setInputComponent(new DefaultInputModule("foo")); resourceCache.add(sampleFile, null).setInputComponent(new DefaultInputFile("foo", "src/Foo.php").setLines(5)); measureCache = mock(MeasureCache.class); - when(measureCache.byMetric(anyString(), anyString())).thenReturn(Collections.<Measure>emptyList()); + when(measureCache.byMetric(anyString(), anyString())).thenReturn(null); publisher = new CoveragePublisher(resourceCache, measureCache); } @@ -70,22 +68,22 @@ public class CoveragePublisherTest { public void publishCoverage() throws Exception { Measure utLineHits = new Measure<>(CoreMetrics.COVERAGE_LINE_HITS_DATA).setData("2=1;3=1;5=0;6=3"); - when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(Arrays.asList(utLineHits)); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(utLineHits); Measure conditionsByLine = new Measure<>(CoreMetrics.CONDITIONS_BY_LINE).setData("3=4"); - when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.CONDITIONS_BY_LINE_KEY)).thenReturn(Arrays.asList(conditionsByLine)); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.CONDITIONS_BY_LINE_KEY)).thenReturn(conditionsByLine); Measure coveredConditionsByUts = new Measure<>(CoreMetrics.COVERED_CONDITIONS_BY_LINE).setData("3=2"); - when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(Arrays.asList(coveredConditionsByUts)); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(coveredConditionsByUts); Measure itLineHits = new Measure<>(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA).setData("2=0;3=0;5=1"); - when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(Arrays.asList(itLineHits)); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY)).thenReturn(itLineHits); Measure coveredConditionsByIts = new Measure<>(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE).setData("3=1"); - when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(Arrays.asList(coveredConditionsByIts)); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(coveredConditionsByIts); Measure overallCoveredConditions = new Measure<>(CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE).setData("3=2"); - when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(Arrays.asList(overallCoveredConditions)); + when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn(overallCoveredConditions); File outputDir = temp.newFolder(); BatchReportWriter writer = new BatchReportWriter(outputDir); |