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));
}
*/
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.
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
*/
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 {
/**
*/
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>
*/
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;
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());
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;
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);
--- /dev/null
+/*
+ * 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;
*/
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;
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
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) {
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)));