Skip to content

Commit

Permalink
SONAR-6589 move MeasureRepository out of Component
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Jun 1, 2015
1 parent bcefe62 commit ad043b4
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 112 deletions.
Expand Up @@ -22,7 +22,6 @@
import java.util.List;
import org.sonar.server.computation.context.ComputationContext;
import org.sonar.server.computation.event.EventRepository;
import org.sonar.server.computation.measure.MeasureRepository;
import org.sonar.server.computation.step.PopulateComponentsUuidAndKeyStep;

public interface Component {
Expand Down Expand Up @@ -68,9 +67,4 @@ public boolean isDeeperThan(Type otherType) {
*/
EventRepository getEventRepository();

/**
* the measure repository for the current component
*/
MeasureRepository getMeasureRepository();

}
Expand Up @@ -19,26 +19,16 @@
*/
package org.sonar.server.computation.component;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.api.measures.Metric;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.core.measure.db.MeasureDto;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.computation.ComputationContext;
import org.sonar.server.computation.batch.BatchReportReader;
import org.sonar.server.computation.event.Event;
import org.sonar.server.computation.event.EventRepository;
import org.sonar.server.computation.measure.MeasureRepository;
import org.sonar.server.db.DbClient;

import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.ImmutableList.copyOf;
Expand All @@ -50,18 +40,15 @@ public class ComponentImpl implements Component {
private final Type type;
private final BatchReport.Component component;
private final List<Component> children;
private final BatchReportReader reportReader;
private final EventRepository eventRepository = new SetEventRepository();

// Mutable values
private String key;
private String uuid;

public ComponentImpl(ComputationContext context, BatchReport.Component component,
BatchReportReader reportReader, @Nullable Iterable<Component> children) {
public ComponentImpl(ComputationContext context, BatchReport.Component component, @Nullable Iterable<Component> children) {
this.context = context;
this.component = component;
this.reportReader = reportReader;
this.type = convertType(component.getType());
this.children = children == null ? Collections.<Component>emptyList() : copyOf(filter(children, notNull()));
}
Expand Down Expand Up @@ -130,36 +117,6 @@ public EventRepository getEventRepository() {
return eventRepository;
}

@Override
public MeasureRepository getMeasureRepository() {
return new MeasureRepository() {
@Override
public Optional<MeasureDto> findPrevious(Metric<?> metric) {
DbClient dbClient = context.getDbClient();
try (DbSession dbSession = dbClient.openSession(false)) {
return Optional.fromNullable(
// TODO replace component.getKey() by ${link #getKey} as component.getKey() is only for project/module and does not take into
// account usage of the branch
dbClient.measureDao().findByComponentKeyAndMetricKey(dbSession, component.getKey(), metric.getKey())
);
}
}

@Override
public Optional<BatchReport.Measure> findCurrent(final Metric<?> metric) {
return Optional.fromNullable(Iterables.find(
reportReader.readComponentMeasures(component.getRef()),
new Predicate<BatchReport.Measure>() {
@Override
public boolean apply(@Nonnull BatchReport.Measure input) {
return input.getMetricKey().equals(metric.getKey());
}
}
));
}
};
}

private static class SetEventRepository implements EventRepository {
private final Set<Event> events = new HashSet<>();

Expand All @@ -173,4 +130,5 @@ public Iterable<Event> getEvents() {
return events;
}
}

}
Expand Up @@ -66,7 +66,7 @@ public Component build(ComputationContext context) {
private Component buildComponentRoot(ComputationContext computationContext, BatchReportReader reportReader) {
int rootComponentRef = reportReader.readMetadata().getRootComponentRef();
BatchReport.Component component = reportReader.readComponent(rootComponentRef);
return new ComponentImpl(computationContext, component, reportReader, buildComponent(computationContext, rootComponentRef));
return new ComponentImpl(computationContext, component, buildComponent(computationContext, rootComponentRef));
}

private Iterable<Component> buildComponent(final ComputationContext computationContext, int componentRef) {
Expand All @@ -77,7 +77,7 @@ private Iterable<Component> buildComponent(final ComputationContext computationC
@Override
public Component apply(@Nonnull Integer componentRef) {
BatchReport.Component component = reportReader.readComponent(componentRef);
return new ComponentImpl(computationContext, component, reportReader, buildComponent(computationContext, componentRef));
return new ComponentImpl(computationContext, component, buildComponent(computationContext, componentRef));
}
}
);
Expand Down
Expand Up @@ -46,6 +46,7 @@
import org.sonar.server.computation.issue.ScmAccountCacheLoader;
import org.sonar.server.computation.issue.SourceLinesCache;
import org.sonar.server.computation.language.PlatformLanguageRepository;
import org.sonar.server.computation.measure.MeasureRepositoryImpl;
import org.sonar.server.computation.measure.MetricCache;
import org.sonar.server.computation.step.ComputationStep;
import org.sonar.server.computation.step.ComputationSteps;
Expand Down Expand Up @@ -113,6 +114,7 @@ private static List componentClasses() {

// repositories
PlatformLanguageRepository.class,
MeasureRepositoryImpl.class,
ProjectSettingsRepository.class,

// component caches
Expand Down
Expand Up @@ -24,11 +24,12 @@

import com.google.common.base.Optional;
import org.sonar.core.measure.db.MeasureDto;
import org.sonar.server.computation.component.Component;

public interface MeasureRepository {
// FIXME should not expose MeasureDto from DAO layer
Optional<MeasureDto> findPrevious(Metric<?> metric);
Optional<MeasureDto> findPrevious(Component component, Metric<?> metric);

// FIXME should not expose Measure from BatchReport
Optional<BatchReport.Measure> findCurrent(Metric<?> metric);
Optional<BatchReport.Measure> findCurrent(Component component, Metric<?> metric);
}
@@ -0,0 +1,66 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.
*/
package org.sonar.server.computation.measure;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import javax.annotation.Nonnull;
import org.sonar.api.measures.Metric;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.core.measure.db.MeasureDto;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.computation.batch.BatchReportReader;
import org.sonar.server.computation.component.Component;
import org.sonar.server.db.DbClient;

public class MeasureRepositoryImpl implements MeasureRepository {
private final DbClient dbClient;
private final BatchReportReader reportReader;

public MeasureRepositoryImpl(DbClient dbClient, BatchReportReader reportReader) {
this.dbClient = dbClient;
this.reportReader = reportReader;
}

@Override
public Optional<MeasureDto> findPrevious(Component component, Metric<?> metric) {
try (DbSession dbSession = dbClient.openSession(false)) {
return Optional.fromNullable(
// TODO replace component.getKey() by ${link #getKey} as component.getKey() is only for project/module and does not take into
// account usage of the branch
dbClient.measureDao().findByComponentKeyAndMetricKey(dbSession, component.getKey(), metric.getKey())
);
}
}

@Override
public Optional<BatchReport.Measure> findCurrent(Component component, final Metric<?> metric) {
return Optional.fromNullable(Iterables.find(
reportReader.readComponentMeasures(component.getRef()),
new Predicate<BatchReport.Measure>() {
@Override
public boolean apply(@Nonnull BatchReport.Measure input) {
return input.getMetricKey().equals(metric.getKey());
}
}
));
}
}
Expand Up @@ -32,6 +32,7 @@
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
import org.sonar.server.computation.event.Event;
import org.sonar.server.computation.measure.MeasureRepository;
import org.sonar.server.computation.qualityprofile.QPMeasureData;
import org.sonar.server.computation.qualityprofile.QualityProfile;

Expand All @@ -43,6 +44,11 @@
import static org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor.Order.POST_ORDER;

public class QualityProfileEventsStep implements ComputationStep {
private final MeasureRepository measureRepository;

public QualityProfileEventsStep(MeasureRepository measureRepository) {
this.measureRepository = measureRepository;
}

@Override
public void execute(ComputationContext context) {
Expand All @@ -55,15 +61,15 @@ public void visitProject(Component tree) {
}

private void executeForProject(Component projectComponent) {
Optional<MeasureDto> previousMeasure = projectComponent.getMeasureRepository().findPrevious(CoreMetrics.QUALITY_PROFILES);
Optional<MeasureDto> previousMeasure = measureRepository.findPrevious(projectComponent, CoreMetrics.QUALITY_PROFILES);
if (!previousMeasure.isPresent()) {
// first analysis -> do not generate events
return;
}

// Load current profiles
Map<String, QualityProfile> previousProfiles = QPMeasureData.fromJson(previousMeasure.get().getData()).getProfilesByKey();
Optional<BatchReport.Measure> currentMeasure = projectComponent.getMeasureRepository().findCurrent(CoreMetrics.QUALITY_PROFILES);
Optional<BatchReport.Measure> currentMeasure = measureRepository.findCurrent(projectComponent, CoreMetrics.QUALITY_PROFILES);
if (!currentMeasure.isPresent()) {
throw new IllegalStateException("Missing measure " + CoreMetrics.QUALITY_PROFILES + " for component " + projectComponent.getRef());
}
Expand Down
Expand Up @@ -20,16 +20,13 @@
package org.sonar.server.computation.component;

import com.google.common.collect.ImmutableList;
import org.sonar.server.computation.context.ComputationContext;
import org.sonar.server.computation.event.EventRepository;
import org.sonar.server.computation.measure.MeasureRepository;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.server.computation.context.ComputationContext;
import org.sonar.server.computation.event.EventRepository;

public class DumbComponent implements Component {
public static final Component DUMB_PROJECT = new DumbComponent(Type.PROJECT, 1, "PROJECT_KEY", "PROJECT_UUID");
Expand Down Expand Up @@ -95,8 +92,4 @@ public EventRepository getEventRepository() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_ERROR);
}

@Override
public MeasureRepository getMeasureRepository() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_ERROR);
}
}

0 comments on commit ad043b4

Please sign in to comment.