]> source.dussan.org Git - sonarqube.git/commitdiff
Optimize copy of scanner measures
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 28 Jul 2017 15:36:36 +0000 (17:36 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 7 Aug 2017 09:44:06 +0000 (11:44 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureDao.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MapBasedRawMeasureRepository.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureRepository.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureRepositoryImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/validation/package-info.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/measure/MeasureRepositoryRule.java

index 419c4cd74e5cb9def2b262859b00b23f79c60481..1656c3229a22c053b42dadb40aa1116b47b91313 100644 (file)
@@ -116,7 +116,7 @@ public class MeasureDao implements Dao {
       ids -> mapper(dbSession).selectProjectMeasuresOfDeveloper(developerId, metricIds));
   }
 
-  public List<MeasureDto> selectByComponentsAndMetrics(DbSession dbSession, List<String> componentUuids, List<Integer> metricIds) {
+  public List<MeasureDto> selectByComponentsAndMetrics(DbSession dbSession, Collection<String> componentUuids, Collection<Integer> metricIds) {
     return executeLargeInputs(componentUuids, partitionComponentUuids -> mapper(dbSession).selectByComponentsAndMetrics(partitionComponentUuids, metricIds));
   }
 
index 0424109ec7ead503d50329d5ca1c00489ebc6893..7392ff616f819a1b656a9e9a0ae7718aacbc4f06 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.measure;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableSetMultimap;
-import com.google.common.collect.SetMultimap;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.collect.FluentIterable.from;
+import static java.lang.String.format;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+
 import org.sonar.server.computation.task.projectanalysis.component.Component;
 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.collect.FluentIterable.from;
-import static java.lang.String.format;
-import static java.util.Objects.requireNonNull;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableSetMultimap;
+import com.google.common.collect.SetMultimap;
 
 /**
  * Map based implementation of MeasureRepository which supports only raw measures.
@@ -59,6 +63,11 @@ public final class MapBasedRawMeasureRepository<T> implements MeasureRepository
     throw new UnsupportedOperationException("This implementation of MeasureRepository supports only raw measures");
   }
 
+  @Override
+  public int loadAsRawMeasures(Collection<Component> components, Collection<Metric> metrics) {
+    throw new UnsupportedOperationException("This implementation of MeasureRepository supports only raw measures");
+  }
+
   @Override
   public Optional<Measure> getRawMeasure(final Component component, final Metric metric) {
     // fail fast
index c5acb1d03c261a2d18673b2c5a0b775f55b58ff5..ecc86ce8c5e82eef21dd406f9f6976365d8e0aee 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.measure;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.SetMultimap;
+import java.util.Collection;
 import java.util.Set;
+
 import org.sonar.server.computation.task.projectanalysis.component.Component;
 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.SetMultimap;
+
 public interface MeasureRepository {
 
   /**
@@ -40,6 +43,8 @@ public interface MeasureRepository {
    */
   Optional<Measure> getBaseMeasure(Component component, Metric metric);
 
+  int loadAsRawMeasures(Collection<Component> components, Collection<Metric> metrics);
+
   /**
    * Retrieves the measure created during the current analysis for the specified {@link Component} for the specified
    * {@link Metric} if it exists (ie. one created by the Compute Engine or the Batch) and which is <strong>not</strong>
index 87d3a0692658c0096fd6b711a67ed22d50b91967..9a0942003b3c77b001fa69cd499262e6c0603954 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.measure;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.SetMultimap;
+import static java.util.Objects.requireNonNull;
+import static org.sonar.server.computation.task.projectanalysis.component.ComponentFunctions.toReportRef;
+
+import java.util.Collection;
 import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
+
 import org.sonar.core.util.CloseableIterator;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -36,8 +42,8 @@ import org.sonar.server.computation.task.projectanalysis.metric.Metric;
 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
 import org.sonar.server.computation.task.projectanalysis.metric.ReportMetricValidator;
 
-import static java.util.Objects.requireNonNull;
-import static org.sonar.server.computation.task.projectanalysis.component.ComponentFunctions.toReportRef;
+import com.google.common.base.Optional;
+import com.google.common.collect.SetMultimap;
 
 public class MeasureRepositoryImpl implements MeasureRepository {
   private final MapBasedRawMeasureRepository<Integer> delegate = new MapBasedRawMeasureRepository<>(toReportRef());
@@ -47,10 +53,10 @@ public class MeasureRepositoryImpl implements MeasureRepository {
   private final MetricRepository metricRepository;
   private final ReportMetricValidator reportMetricValidator;
 
-  private MeasureDtoToMeasure underTest = new MeasureDtoToMeasure();
+  private MeasureDtoToMeasure measureTransformer = new MeasureDtoToMeasure();
   private final Set<Integer> loadedComponents = new HashSet<>();
 
-  public MeasureRepositoryImpl(DbClient dbClient, BatchReportReader reportReader, MetricRepository metricRepository, 
+  public MeasureRepositoryImpl(DbClient dbClient, BatchReportReader reportReader, MetricRepository metricRepository,
     ReportMetricValidator reportMetricValidator) {
     this.dbClient = dbClient;
     this.reportReader = reportReader;
@@ -69,12 +75,38 @@ public class MeasureRepositoryImpl implements MeasureRepository {
       MeasureQuery query = MeasureQuery.builder().setComponentUuid(component.getUuid()).setMetricKey(metric.getKey()).build();
       java.util.Optional<MeasureDto> measureDto = dbClient.measureDao().selectSingle(dbSession, query);
       if (measureDto.isPresent()) {
-        return underTest.toMeasure(measureDto.get(), metric);
+        return measureTransformer.toMeasure(measureDto.get(), metric);
       }
       return Optional.absent();
     }
   }
 
+  @Override
+  public int loadAsRawMeasures(Collection<Component> components, Collection<Metric> metrics) {
+    requireNonNull(components);
+    requireNonNull(metrics);
+
+    Map<String, Component> componentsByUuid = components.stream()
+      .collect(Collectors.toMap(Component::getUuid, c -> c));
+    Map<Integer, Metric> metricsById = metrics.stream()
+      .collect(Collectors.toMap(Metric::getId, m -> m));
+
+    List<MeasureDto> measuresDto;
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      measuresDto = dbClient.measureDao().selectByComponentsAndMetrics(dbSession, componentsByUuid.keySet(), metricsById.keySet());
+    }
+    
+    for (MeasureDto dto : measuresDto) {
+
+      Metric metric = metricsById.get(dto.getMetricId());
+      Component component = componentsByUuid.get(dto.getComponentUuid());
+      Measure measure = measureTransformer.toMeasure(dto, metric).get();
+
+      delegate.add(component, metric, measure);
+    }
+    return measuresDto.size();
+  }
+
   @Override
   public Optional<Measure> getRawMeasure(Component component, Metric metric) {
     Optional<Measure> local = delegate.getRawMeasure(component, metric);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/validation/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/validation/package-info.java
new file mode 100644 (file)
index 0000000..ad6b743
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.computation.task.projectanalysis.validation;
+
+import javax.annotation.ParametersAreNonnullByDefault;
index c50ea46d20a0363c1e46db331a5f0503e8586526..976ea71ae0754c99e1ad3949bf1d2910811ffdb0 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.measure;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableSetMultimap;
-import com.google.common.collect.SetMultimap;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.collect.FluentIterable.from;
+import static com.google.common.collect.Maps.filterKeys;
+import static java.lang.String.format;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+
 import org.junit.rules.ExternalResource;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
 import org.sonar.server.computation.task.projectanalysis.component.ComponentProvider;
@@ -43,12 +48,11 @@ import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolde
 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
-import static com.google.common.collect.FluentIterable.from;
-import static com.google.common.collect.Maps.filterKeys;
-import static java.lang.String.format;
-import static java.util.Objects.requireNonNull;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableSetMultimap;
+import com.google.common.collect.SetMultimap;
 
 /**
  * An implementation of MeasureRepository as a JUnit rule which provides add methods for raw measures and extra add
@@ -62,6 +66,8 @@ public class MeasureRepositoryRule extends ExternalResource implements MeasureRe
   private final Map<InternalKey, Measure> baseMeasures = new HashMap<>();
   private final Map<InternalKey, Measure> rawMeasures = new HashMap<>();
   private final Map<InternalKey, Measure> initialRawMeasures = new HashMap<>();
+  private Collection<Component> loadedAsRawComponents;
+  private Collection<Metric> loadedAsRawMetrics;
   private final Predicate<Map.Entry<InternalKey, Measure>> isAddedMeasure = new Predicate<Map.Entry<InternalKey, Measure>>() {
     @Override
     public boolean apply(@Nonnull Map.Entry<InternalKey, Measure> input) {
@@ -181,12 +187,26 @@ public class MeasureRepositoryRule extends ExternalResource implements MeasureRe
 
     return this;
   }
-
   @Override
   public Optional<Measure> getBaseMeasure(Component component, Metric metric) {
     return Optional.fromNullable(baseMeasures.get(new InternalKey(component, metric)));
   }
 
+  @Override
+  public int loadAsRawMeasures(Collection<Component> components, Collection<Metric> metrics) {
+    this.loadedAsRawComponents = components;
+    this.loadedAsRawMetrics = metrics;
+    return 0;
+  }
+  
+  public Collection<Component> getComponentsLoadedAsRaw() {
+    return loadedAsRawComponents;
+  }
+  
+  public Collection<Metric> getMetricsLoadedAsRaw() {
+    return loadedAsRawMetrics;
+  }
+
   @Override
   public Optional<Measure> getRawMeasure(Component component, Metric metric) {
     return Optional.fromNullable(rawMeasures.get(new InternalKey(component, metric)));